Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migration of the codebase to TypeScript (#56) #66

Merged
merged 5 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
public
docs
lib
build
example
125 changes: 125 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
env:
es6: true
jest/globals: true
node: true

parser: "@typescript-eslint/parser"

plugins:
- "@typescript-eslint"
- security
- sonarjs
- jest

extends:
- eslint:recommended
- plugin:@typescript-eslint/eslint-recommended
- plugin:@typescript-eslint/recommended
- plugin:security/recommended
- plugin:sonarjs/recommended
- plugin:jest/recommended

parserOptions:
ecmaVersion: 2018

rules:
# Ignore Rules
strict: 0
no-underscore-dangle: 0
no-mixed-requires: 0
no-process-exit: 0
no-warning-comments: 0
curly: 0
no-multi-spaces: 0
no-alert: 0
consistent-return: 0
consistent-this: [0, self]
func-style: 0
max-nested-callbacks: 0
camelcase: 0

# Warnings
no-debugger: 1
no-empty: 1
no-invalid-regexp: 1
no-unused-expressions: 1
no-native-reassign: 1
no-fallthrough: 1
sonarjs/cognitive-complexity: 1

# Errors
eqeqeq: 2
no-undef: 2
no-dupe-keys: 2
no-empty-character-class: 2
no-self-compare: 2
valid-typeof: 2
no-unused-vars: [2, { "args": "none" }]
handle-callback-err: 2
no-shadow-restricted-names: 2
no-new-require: 2
no-mixed-spaces-and-tabs: 2
block-scoped-var: 2
no-else-return: 2
no-throw-literal: 2
no-void: 2
radix: 2
wrap-iife: [2, outside]
no-shadow: 0
no-path-concat: 2
valid-jsdoc: [0, {requireReturn: false, requireParamDescription: false, requireReturnDescription: false}]

# stylistic errors
no-spaced-func: 2
semi-spacing: 2
quotes: [2, 'single']
key-spacing: [2, { beforeColon: false, afterColon: true }]
indent: [2, 2]
no-lonely-if: 2
no-floating-decimal: 2
brace-style: [2, 1tbs, { allowSingleLine: true }]
comma-style: [2, last]
no-multiple-empty-lines: [2, {max: 1}]
no-nested-ternary: 2
operator-assignment: [2, always]
padded-blocks: [2, never]
quote-props: [2, as-needed]
keyword-spacing: [2, {'before': true, 'after': true, 'overrides': {}}]
space-before-blocks: [2, always]
array-bracket-spacing: [2, never]
computed-property-spacing: [2, never]
space-in-parens: [2, never]
space-unary-ops: [2, {words: true, nonwords: false}]
wrap-regex: 2
linebreak-style: 0
semi: [2, always]
arrow-spacing: [2, {before: true, after: true}]
no-class-assign: 2
no-const-assign: 2
no-dupe-class-members: 2
no-this-before-super: 2
no-var: 2
object-shorthand: [2, always]
prefer-arrow-callback: 2
prefer-const: 2
prefer-spread: 2
prefer-template: 2

# TypeScript
"@typescript-eslint/no-empty-interface": "off"
# disable JS rule
no-use-before-define: "off"
"@typescript-eslint/no-use-before-define": ["error"]
"@typescript-eslint/no-empty-function": "off"
"@typescript-eslint/ban-ts-comment": "off"
"@typescript-eslint/no-explicit-any": "off"
"@typescript-eslint/explicit-module-boundary-types": "off"
"@typescript-eslint/no-this-alias": "off"

overrides:
- files: "tests/**/*.spec.ts"
rules:
prefer-arrow-callback: 0
sonarjs/no-duplicate-string: 0
security/detect-object-injection: 0
security/detect-non-literal-fs-filename: 0
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
/lib
95 changes: 51 additions & 44 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@
<dd></dd>
</dl>

## Members

<dl>
<dt><a href="#validate">validate</a> ⇒ <code>Array.&lt;Object&gt;</code></dt>
<dd></dd>
</dl>

## Functions

<dl>
<dt><a href="#bundle">bundle(files, options)</a> ⇒ <code><a href="#Document">Document</a></code></dt>
<dt><a href="#bundle">bundle(files, [options])</a> ⇒ <code><a href="#Document">Document</a></code></dt>
<dd></dd>
<dt><a href="#parse">parse(JSONSchema)</a></dt>
<dd><p>resolves external references and updates $refs</p>
</dd>
<dt><a href="#isExternalReference">isExternalReference(ref)</a> ⇒ <code>boolean</code></dt>
<dd><p>This function checks for external reference.</p>
</dd>
<dd><p>Checks if <code>ref</code> is an external reference.</p></dd>
<dt><a href="#resolveExternalRefs">resolveExternalRefs(parsedJSON, $refs)</a> ⇒ <code>ExternalComponents</code></dt>
<dd></dd>
<dt><a href="#resolve">resolve(asyncapiDocuments, options)</a> ⇒ <code>Array.&lt;Object&gt;</code></dt>
<dd></dd>
<dt><a href="#parse">parse(JSONSchema)</a></dt>
<dd><p>Resolves external references and updates $refs.</p></dd>
</dl>

<a name="Document"></a>
Expand All @@ -28,27 +31,27 @@
**Kind**: global class

* [Document](#Document)
* [new Document(parsedJSONList, [base])](#new_Document_new)
* [new Document(parsedJSONList, base)](#new_Document_new)
* [.json()](#Document+json) ⇒ <code>Object</code>
* [.yml()](#Document+yml) ⇒ <code>string</code>
* [.string()](#Document+string) ⇒ <code>string</code>

<a name="new_Document_new"></a>

### new Document(parsedJSONList, [base])
### new Document(parsedJSONList, base)

| Param | Type |
| --- | --- |
| parsedJSONList | <code>Array.&lt;Object&gt;</code> |
| [base] | <code>Object</code> |
| base | <code>Object</code> |

**Example**
```js
const document = new Document(parsedJSONList, base);

console.log(document.json()); // get json object
console.log(document.yml()); // get yaml string
console.log(document.string()); // get json string
console.log(document.json()); // get JSON object
console.log(document.yml()); // get YAML string
console.log(document.string()); // get JSON string
```
<a name="Document+json"></a>

Expand All @@ -62,51 +65,55 @@ console.log(document.string()); // get json string

### document.string() ⇒ <code>string</code>
**Kind**: instance method of [<code>Document</code>](#Document)
<a name="validate"></a>

## validate ⇒ <code>Array.&lt;Object&gt;</code>
**Kind**: global variable

| Param | Type |
| --- | --- |
| asyncapiDocuments | <code>Object</code> |
| options | <code>Object</code> |
| options.referenceIntoComponents | <code>boolean</code> |

<a name="bundle"></a>

## bundle(files, options) ⇒ [<code>Document</code>](#Document)
## bundle(files, [options]) ⇒ [<code>Document</code>](#Document)
**Kind**: global function

| Param | Type | Description |
| --- | --- | --- |
| files | <code>Array.&lt;string&gt;</code> \| <code>Array.&lt;Object&gt;</code> | files that are to be bundled |
| options | <code>Object</code> | |
| options.base | <code>string</code> \| <code>object</code> | base object whose prperties will be retained. |
| options.referenceIntoComponents | <code>boolean</code> | pass true value to resolve references into component |
| files | <code>Array.&lt;string&gt;</code> | <p>Array of stringified AsyncAPI documents in YAML format, that are to be bundled (or array of filepaths, resolved and passed via <code>Array.map()</code> and <code>fs.readFileSync</code>, which is the same, see <code>README.md</code>).</p> |
| [options] | <code>Object</code> | |
| [options.base] | <code>string</code> \| <code>object</code> | <p>Base object whose properties will be retained.</p> |
| [options.referenceIntoComponents] | <code>boolean</code> | <p>Pass <code>true</code> to resolve external references to components.</p> |

**Example**
```js
const bundle = requrie('@asyncapi/bundler');
const fs = require('fs');
const path = requrie('path');
import { readFileSync, writeFileSync } from 'fs';
import bundle from '@asyncapi/bundler';

async function main() {
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
referenceIntoComponents: true,
});

const document = await bundle(fs.readFileSync(
path.resolve('./asyncapi.yaml', 'utf-8')
));
console.log(document.yml()); // the complete bundled AsyncAPI document
writeFileSync('asyncapi.yaml', document.yml()); // the complete bundled AsyncAPI document
}

console.log(document.yml());
main().catch(e => console.error(e));
```
<a name="bundle..resolvedJsons"></a>

### bundle~resolvedJsons
Bundle all external references for each files.
<p>Bundle all external references for each file.</p>

**Kind**: inner constant of [<code>bundle</code>](#bundle)
<a name="parse"></a>

## parse(JSONSchema)
resolves external references and updates $refs

**Kind**: global function

| Param | Type |
| --- | --- |
| JSONSchema | <code>Array.&lt;Object&gt;</code> |

<a name="isExternalReference"></a>

## isExternalReference(ref) ⇒ <code>boolean</code>
This function checks for external reference.
<p>Checks if <code>ref</code> is an external reference.</p>

**Kind**: global function

Expand All @@ -124,14 +131,14 @@ This function checks for external reference.
| parsedJSON | <code>Array.&lt;Object&gt;</code> |
| $refs | <code>$RefParser</code> |

<a name="resolve"></a>
<a name="parse"></a>

## parse(JSONSchema)
<p>Resolves external references and updates $refs.</p>

## resolve(asyncapiDocuments, options) ⇒ <code>Array.&lt;Object&gt;</code>
**Kind**: global function

| Param | Type |
| --- | --- |
| asyncapiDocuments | <code>Object</code> |
| options | <code>Object</code> |
| options.referenceIntoComponents | <code>boolean</code> |
| JSONSchema | <code>Array.&lt;Object&gt;</code> |

Loading