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 1 commit
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
Next Next commit
feat: migration of the codebase to TypeScript (#56)
Co-authored-by: Maciej Urbańczyk <[email protected]>
  • Loading branch information
aeworxet and magicmatatjahu committed Jan 1, 1970
commit 9e2292051707caaefd501b5d73c665422564d0c5
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
75 changes: 39 additions & 36 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,19 +31,19 @@
**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
Expand All @@ -62,23 +65,34 @@ 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.</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 bundle = require('@asyncapi/bundler');
const fs = require('fs');
const path = requrie('path');
const path = require('path');

const document = await bundle(fs.readFileSync(
path.resolve('./asyncapi.yaml', 'utf-8')
Expand All @@ -89,24 +103,13 @@ console.log(document.yml());
<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 +127,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> |

22 changes: 10 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
<!-- tocstop -->

## Overview
An official library that lets you bundle/merge your specification files into one. AsyncAPI bundler can help you if -
An official library that lets you bundle/merge your specification files into one. AsyncAPI bundler can help you if:

<details>
<summary>your specification file is divided into different smaller files and is using json `$ref` to reference components </summary>
<summary>your specification file is divided into different smaller files and is using JSON `$ref` property to reference components </summary>

```yaml

Expand Down Expand Up @@ -160,7 +160,7 @@ npm install @asyncapi/bundler

## Usage

AsyncAPI-bundler could be easily used within your javascript projects as a Nodejs module.
AsyncAPI bundler can be easily used within your JavaScript project as a Node.js module:

```js
const bundle = require('@asyncapi/bundler');
Expand All @@ -175,14 +175,14 @@ const document = await bundle(
}
);

console.log(document.json()); // the complete bundled asyncapi document.
console.log(document.json()); // the complete bundled AsyncAPI document
```

### Resolving external references into components
You can resolve external references by moving them to Messages object, under `compoents/messages`.
You can resolve external references by moving them to Messages Object, under `components/messages`.

<details>
<summary>For Example</summary>
<summary>For example</summary>

```yml
# asyncapi.yaml
Expand Down Expand Up @@ -261,9 +261,7 @@ console.log(document.json());

| 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 to resovle external references to components. |


| files | <code>Array.&lt;string&gt; | Array of stringified AsyncAPI documents in YAML format, that are to be bundled. |
| [options] | <code>Object</code> | |
| [options.base] | <code>string</code> \| <code>object</code> | Base object whose properties will be retained. |
| [options.referenceIntoComponents] | <code>boolean<code> | Pass `true` to resolve external references to components. |
11 changes: 0 additions & 11 deletions example/bundle.js

This file was deleted.

11 changes: 11 additions & 0 deletions example/bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as bundle from '@asyncapi/bundler';
import * as fs from 'fs';

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

main().catch(e => console.error(e));
3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@asyncapi/bundler": "file:../"
"@asyncapi/bundler": "^0.2.2",
"@types/node": "^18.7.23"
}
}
7 changes: 7 additions & 0 deletions example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"types": [
"node"
]
}
}
Loading