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 #161

Merged
merged 6 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,19 +2,37 @@

A schema parser for RAML data types.

> **ATTENTION: This package is not browser-compatible.**
> **Note**
> Version >= `3.0.0` of package is only supported by `@asyncapi/parser` version >= `2.0.0`.

> **Warning**
> This package is not browser-compatible.

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

<!-- toc -->

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

<!-- tocstop -->

## Installation

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

## Usage

```js
const parser = require('asyncapi-parser')
const ramlDtParser = require('@asyncapi/raml-dt-schema-parser')
```ts
import { Parser } from '@asyncapi/parser';
import { RamlDTSchemaParser } from '@asyncapi/raml-dt-schema-parser';

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

const asyncapiWithRAML = `
asyncapi: 2.0.0
Expand All @@ -34,18 +52,49 @@ channels:
type: string
examples:
anExample: Jack Johnson
`
`;

parser.registerSchemaParser(ramlDtParser)
const { document } = await parser.parse(asyncapiWithRAML);
```

await parser.parse(asyncapiWithRAML)
```js
const { Parser } = require('@asyncapi/parser');
const { RamlDTSchemaParser } = require('@asyncapi/raml-dt-schema-parser');

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

const asyncapiWithRAML = `
asyncapi: 2.0.0
info:
title: Example with RAML
version: 0.1.0
channels:
example:
publish:
message:
schemaFormat: 'application/raml+yaml;version=1.0'
payload: # The following is a RAML data type
type: object
properties:
title: string
author:
type: string
examples:
anExample: Jack Johnson
`;

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

It also supports referencing remote RAML data types:

```js
const parser = require('asyncapi-parser')
const ramlDtParser = require('@asyncapi/raml-dt-schema-parser')
import { Parser } from '@asyncapi/parser';
import { RamlDTSchemaParser } from '@asyncapi/raml-dt-schema-parser';

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

const asyncapiWithRAML = `
asyncapi: 2.0.0
Expand All @@ -59,9 +108,7 @@ channels:
schemaFormat: 'application/raml+yaml;version=1.0'
payload:
$ref: 'yourserver.com/data-types/library.raml#/Book'
`

parser.registerSchemaParser(ramlDtParser)
`;

await parser.parse(asyncapiWithRAML)
const { document } = await parser.parse(asyncapiWithRAML);
```
35 changes: 0 additions & 35 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;