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!: rewrite to TS and adjust to the v2 ParserJS #142

Merged
merged 14 commits into from
Apr 28, 2023
9 changes: 9 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules
*.tgz
.vscode
.DS_Store
/docs
/coverage
/lib
/esm
/cjs
43 changes: 35 additions & 8 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
parser: "@typescript-eslint/parser"

env:
node: true
es6: true
mocha: true
jest: true

plugins:
- "@typescript-eslint"
- sonarjs
- mocha
- security
- github

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

parserOptions:
ecmaVersion: 2018
Expand All @@ -21,6 +28,7 @@ rules:
no-mixed-requires: 0
no-process-exit: 0
no-warning-comments: 0
no-use-before-define: 0
curly: 0
no-multi-spaces: 0
no-alert: 0
Expand All @@ -29,6 +37,10 @@ rules:
func-style: 0
max-nested-callbacks: 0
camelcase: 0
no-dupe-class-members: 0
security/detect-object-injection: 0
sonarjs/no-small-switch: 0
sonarjs/no-nested-template-literals: 0

# Warnings
no-debugger: 1
Expand All @@ -46,7 +58,6 @@ rules:
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
Expand All @@ -58,7 +69,6 @@ rules:
radix: 2
wrap-iife: [2, outside]
no-shadow: 0
no-use-before-define: [2, nofunc]
no-path-concat: 2
valid-jsdoc: [0, {requireReturn: false, requireParamDescription: false, requireReturnDescription: false}]

Expand Down Expand Up @@ -89,7 +99,6 @@ rules:
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]
Expand All @@ -98,8 +107,26 @@ rules:
prefer-spread: 2
prefer-template: 2

# TypeScript
"@typescript-eslint/no-empty-interface": "off"
"@typescript-eslint/no-use-before-define": ["off"]
"@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"
"@typescript-eslint/no-unnecessary-type-constraint": "off"
"@typescript-eslint/ban-types": "off"

overrides:
- files: "test/**"
- files:
- "test/**"
- "*.spec.ts"
- "*.test.ts"
rules:
prefer-arrow-callback: 0
sonarjs/no-duplicate-string: 0
sonarjs/no-duplicate-string: 0
security/detect-object-injection: 0
security/detect-non-literal-fs-filename: 0
"@typescript-eslint/no-non-null-assertion": 0
"@typescript-eslint/no-unused-vars": 0
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
node_modules
node_modules
*.tgz
.vscode
.DS_Store
/docs
/coverage
/lib
/esm
/cjs
10 changes: 10 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store
*.tgz
*.swp
.github
.all-contributorsrc
.editorconfig
assets/logo.png
vscode
coverage
node_modules
75 changes: 61 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@

An AsyncAPI schema parser for OpenAPI 3.0.x and Swagger 2.x schemas.

> **Note**
> Version >= `3.0.0` of package is only supported by `@asyncapi/parser` version >= `2.0.0`.

<!-- toc is generated with GitHub Actions do not remove toc markers -->

<!-- toc -->

- [Installation](#installation)
- [Usage](#usage)

<!-- tocstop -->

## Installation

```
```bash
npm install @asyncapi/openapi-schema-parser
// OR
yarn add @asyncapi/openapi-schema-parser
```

## Usage

```js
const parser = require('asyncapi-parser')
const openapiSchemaParser = require('@asyncapi/openapi-schema-parser')
```ts
import { Parser } from '@asyncapi/parser';
import { OpenAPISchemaParser } from '@asyncapi/openapi-schema-parser';

const parser = new Parser();
parser.registerSchemaParser(OpenAPISchemaParser());

const asyncapiWithOpenAPI = `
asyncapi: 2.0.0
Expand All @@ -33,18 +50,50 @@ channels:
author:
type: string
example: Jack Johnson
`
`;

const { document } = await parser.parse(asyncapiWithOpenAPI);
```

```js
const { Parser } = require('@asyncapi/parser');
const { OpenAPISchemaParser } = require('@asyncapi/openapi-schema-parser');

parser.registerSchemaParser(openapiSchemaParser);
const parser = new Parser();
parser.registerSchemaParser(OpenAPISchemaParser());

await parser.parse(asyncapiWithOpenAPI)
const asyncapiWithOpenAPI = `
asyncapi: 2.0.0
info:
title: Example with OpenAPI
version: 0.1.0
channels:
example:
publish:
message:
schemaFormat: 'application/vnd.oai.openapi;version=3.0.0'
payload: # The following is an OpenAPI schema
type: object
properties:
title:
type: string
nullable: true
author:
type: string
example: Jack Johnson
`;

const { document } = await parser.parse(asyncapiWithOpenAPI);
```

It also supports referencing remote OpenAPI schemas:

```js
const parser = require('asyncapi-parser')
const openapiSchemaParser = require('@asyncapi/openapi-schema-parser')
```ts
import { Parser } from '@asyncapi/parser';
import { OpenAPISchemaParser } from '@asyncapi/openapi-schema-parser';

const parser = new Parser();
parser.registerSchemaParser(OpenAPISchemaParser());

const asyncapiWithOpenAPI = `
asyncapi: 2.0.0
Expand All @@ -58,9 +107,7 @@ channels:
schemaFormat: 'application/vnd.oai.openapi;version=3.0.0'
payload:
$ref: 'yourserver.com/schemas#/Book'
`

parser.registerSchemaParser(openapiSchemaParser)
`;

await parser.parse(asyncapiWithOpenAPI)
const { document } = await parser.parse(asyncapiWithOpenAPI);
```
71 changes: 0 additions & 71 deletions index.js

This file was deleted.

31 changes: 31 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { Config } from '@jest/types';

const config: Config.InitialOptions = {
coverageReporters: [
'text'
],
transform: {
'^.+\\.(t|j)sx?$': '@swc/jest',
},
// The root of your source code, typically /src
// `<rootDir>` is a token Jest substitutes
roots: ['<rootDir>'],
moduleNameMapper: {
'^nimma/legacy$': '<rootDir>/node_modules/nimma/dist/legacy/cjs/index.js',
'^nimma/(.*)': '<rootDir>/node_modules/nimma/dist/cjs/$1',
'^@stoplight/spectral-ruleset-bundler/(.*)$': '<rootDir>/node_modules/@stoplight/spectral-ruleset-bundler/dist/$1'
},

// Test spec file resolution pattern
// Matches parent folder `__tests__` and filename
// should contain `test` or `spec`.
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
// Module file extensions for importing
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
testTimeout: 10000,
collectCoverageFrom: [
'src/**'
],
};

export default config;
Loading