fast-typescript-to-jsonschema
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -6,6 +6,6 @@ import type { AnyOption } from './types'; | ||
constructor(); | ||
getJsonSchema(file: string, type: string, entry?: { | ||
getJsonSchema(file: string, type?: string, entry?: { | ||
keySet: Set<string>; | ||
refKeyTime: Record<string, number>; | ||
}): AnyOption; | ||
}, isLast?: boolean): AnyOption; | ||
genJsonschema(fileJson: AnyOption, typeJson: AnyOption, entry?: { | ||
@@ -12,0 +12,0 @@ keySet: Set<string>; |
@@ -13,6 +13,10 @@ "use strict"; | ||
} | ||
getJsonSchema(file, type, entry) { | ||
getJsonSchema(file, type, entry, isLast) { | ||
if (!entry) { | ||
entry = { keySet: new Set(), refKeyTime: {} }; | ||
} | ||
if (file && !type) { | ||
type = file; | ||
file = 'index.ts'; | ||
} | ||
if (!file || !type) | ||
@@ -29,3 +33,6 @@ return { type: 'object' }; | ||
if (_.get(this.jsonData, [refFile])) { | ||
typeJson = this.getJsonSchema(refFile, typeJson.from ? type.replace(/.+?(?=\.)|.+/, typeJson.from) : type, entry); | ||
typeJson = this.getJsonSchema(refFile, typeJson.from ? type.replace(/.+?(?=\.)|.+/, typeJson.from) : type, entry, true); | ||
if (!isLast && typeJson.typeParams) { | ||
delete typeJson.typeParams; | ||
} | ||
return typeJson; | ||
@@ -49,2 +56,5 @@ } | ||
} | ||
if (!isLast && typeJson.typeParams) { | ||
delete typeJson.typeParams; | ||
} | ||
return typeJson; | ||
@@ -127,3 +137,3 @@ } | ||
const refFile = realRef.$ref.replace(/#\/definitions|#/, ''); | ||
const realSchema = this.getJsonSchema(refFile, result.$realRef); | ||
const realSchema = this.getJsonSchema(refFile, result.$realRef, undefined, true); | ||
delete realRef.$ref; | ||
@@ -202,6 +212,6 @@ delete realRef.from; | ||
const handleExtends = (item) => { | ||
const newTypeJson = this.getJsonSchema(file, item.extends, entry); | ||
const newTypeJson = this.getJsonSchema(file, item.extends, entry, true); | ||
if (newTypeJson) { | ||
const result = newTypeJson.$ref | ||
? this.getJsonSchema(newTypeJson.$ref.replace(/#/g, ''), item.extends, entry) | ||
? this.getJsonSchema(newTypeJson.$ref.replace(/#/g, ''), item.extends, entry, true) | ||
: this.genJsonschema(fileJson, newTypeJson, entry); | ||
@@ -259,3 +269,3 @@ if (typeof result === 'object') { | ||
const $dependRefKey = $refJson.$ref.replace('#', ''); | ||
const result = this.getJsonSchema($dependRefKey, $refJson.from ? $refKey.replace(/.+?(?=\.)|.+/, $refJson.from) : $refKey, entry); | ||
const result = this.getJsonSchema($dependRefKey, $refJson.from ? $refKey.replace(/.+?(?=\.)|.+/, $refJson.from) : $refKey, entry, true); | ||
handle && handleCommonRef(result, $refKey); | ||
@@ -536,2 +546,5 @@ return result; | ||
allOfAnyOfHandle(typeJson); | ||
if (typeJson.typeParams && Object.keys(typeJson.typeParams).length) { | ||
handleGenericDefaultType(typeJson.properties, typeJson.typeParams); | ||
} | ||
if (typeJson.type === 'array') { | ||
@@ -538,0 +551,0 @@ commonArrayHandle(typeJson); |
@@ -31,2 +31,3 @@ import type { AnyOption, TSTypeAnnotationConfig, TypeAnnotationConfig } from './types'; | ||
genJsonDataFormFile(file: string, ast?: AnyOption | null, filterFiles?: string[]): AnyOption; | ||
genJsonDataFromCode(code: string): AnyOption; | ||
getDescription(node: AnyOption): AnyOption[]; | ||
@@ -33,0 +34,0 @@ getSimpleDescription(node: AnyOption): string; |
@@ -389,2 +389,6 @@ "use strict"; | ||
} | ||
genJsonDataFromCode(code) { | ||
const ast_ = utils_1.genAstFromCode(code); | ||
return this.genJsonDataFormFile('index.ts', ast_); | ||
} | ||
getDescription(node) { | ||
@@ -391,0 +395,0 @@ const { leadingComments = [] } = node || {}; |
import type { AnyOption } from './types'; | ||
export declare function genAst(file: string): AnyOption | null; | ||
export declare function genAstFromCode(code: string): AnyOption | null; | ||
export declare const randomString: (len?: number | undefined) => string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.randomString = exports.genAst = void 0; | ||
exports.randomString = exports.genAstFromCode = exports.genAst = void 0; | ||
const tslib_1 = require("tslib"); | ||
const fs = tslib_1.__importStar(require("fs")); | ||
const parser_1 = require("@babel/parser"); | ||
const parseConfig = { | ||
sourceType: 'unambiguous', | ||
allowImportExportEverywhere: true, | ||
allowAwaitOutsideFunction: true, | ||
allowReturnOutsideFunction: true, | ||
allowSuperOutsideMethod: true, | ||
allowUndeclaredExports: true, | ||
createParenthesizedExpressions: true, | ||
errorRecovery: true, | ||
plugins: ['typescript', 'decorators-legacy'], | ||
}; | ||
function genAst(file) { | ||
try { | ||
const parseConfig = { | ||
sourceType: 'unambiguous', | ||
allowImportExportEverywhere: true, | ||
allowAwaitOutsideFunction: true, | ||
allowReturnOutsideFunction: true, | ||
allowSuperOutsideMethod: true, | ||
allowUndeclaredExports: true, | ||
createParenthesizedExpressions: true, | ||
errorRecovery: true, | ||
plugins: ['typescript', 'decorators-legacy'], | ||
}; | ||
const fileContent = fs.readFileSync(file).toString(); | ||
@@ -29,2 +29,12 @@ const ast = parser_1.parse(fileContent, parseConfig); | ||
exports.genAst = genAst; | ||
function genAstFromCode(code) { | ||
try { | ||
const ast = parser_1.parse(code, parseConfig); | ||
return ast; | ||
} | ||
catch (err) { | ||
return null; | ||
} | ||
} | ||
exports.genAstFromCode = genAstFromCode; | ||
const randomString = (len) => { | ||
@@ -31,0 +41,0 @@ const length = len || 32; |
{ | ||
"name": "fast-typescript-to-jsonschema", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "fast-typescript-to-jsonschema generates JSON Schema files from your Typescript sources.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
# fast-typescript-to-jsonschema | ||
[![npm version](https://img.shields.io/npm/v/fast-typescript-to-jsonschema.svg)](https://www.npmjs.com/package/fast-typescript-to-jsonschema) | ||
[![npm version](https://img.shields.io/npm/v/fast-typescript-to-jsonschema.svg)](https://www.npmjs.com/package/fast-typescript-to-jsonschema) | ||
![Test](https://github.com/yunke-yunfly/fast-typescript-to-jsonschema/workflows/Test/badge.svg) | ||
@@ -36,2 +36,4 @@ [![codecov](https://codecov.io/gh/yunke-yunfly/fast-typescript-to-jsonschema/branch/master/graph/badge.svg)](https://app.codecov.io/gh/yunke-yunfly/fast-typescript-to-jsonschema) | ||
3.1. gen jsonschema from file. | ||
```js | ||
@@ -57,2 +59,27 @@ const { default: genTypeSchema } = require('fast-typescript-to-jsonschema'); | ||
3.2. gen jsonschema from code. | ||
```js | ||
const { default: genTypeSchema } = require('fast-typescript-to-jsonschema'); | ||
const code = ` | ||
interface ITest { | ||
attr1: string; | ||
attr2: number; | ||
attr3?: boolean; | ||
} | ||
` | ||
// generate data | ||
genTypeSchema.genJsonDataFromCode(code); | ||
// get all jsonschema data of current file | ||
const json = genTypeSchema.genJsonData(); | ||
// get jsonschema of specific type | ||
const jsonSchema = genTypeSchema.getJsonSchema('ITest'); | ||
// result | ||
console.log(jsonSchema); | ||
``` | ||
4. execute script | ||
@@ -166,3 +193,3 @@ | ||
- [1.1 Basic Extending Types](docs/extends.en-US.md#11-basic-extending-types) | ||
- [1.2 Multiple Extending Types ](docs/extends.en-US.md#12-multiple-extending-types) | ||
- [1.2 Multiple Extending Types](docs/extends.en-US.md#12-multiple-extending-types) | ||
- [Enums](docs/enum.en-US.md#enums) | ||
@@ -169,0 +196,0 @@ - [1.1 Numeric Enums](docs/enum.en-US.md#11-numeric-enums) |
# fast-typescript-to-jsonschema | ||
[![npm version](https://img.shields.io/npm/v/fast-typescript-to-jsonschema.svg)](https://www.npmjs.com/package/fast-typescript-to-jsonschema) | ||
[![npm version](https://img.shields.io/npm/v/fast-typescript-to-jsonschema.svg)](https://www.npmjs.com/package/fast-typescript-to-jsonschema) | ||
![Test](https://github.com/yunke-yunfly/fast-typescript-to-jsonschema/workflows/Test/badge.svg) | ||
@@ -34,4 +34,6 @@ [![codecov](https://codecov.io/gh/yunke-yunfly/fast-typescript-to-jsonschema/branch/master/graph/badge.svg)](https://app.codecov.io/gh/yunke-yunfly/fast-typescript-to-jsonschema) | ||
3.创建`test.js`文件,内容如下: | ||
3. 创建`test.js`文件,内容如下: | ||
3.1 通过文件生成 jsonschema | ||
```js | ||
@@ -57,2 +59,27 @@ const { default: genTypeSchema } = require('fast-typescript-to-jsonschema'); | ||
3.2 通过 code 生成 jsonschema | ||
```js | ||
const { default: genTypeSchema } = require('fast-typescript-to-jsonschema'); | ||
const code = ` | ||
interface ITest { | ||
attr1: string; | ||
attr2: number; | ||
attr3?: boolean; | ||
} | ||
` | ||
// generate data | ||
genTypeSchema.genJsonDataFromCode(code); | ||
// get all jsonschema data of current file | ||
const json = genTypeSchema.genJsonData(); | ||
// get jsonschema of specific type | ||
const jsonSchema = genTypeSchema.getJsonSchema('ITest'); | ||
// result | ||
console.log(jsonSchema); | ||
``` | ||
4.执行脚本 | ||
@@ -89,3 +116,3 @@ | ||
- example 案例地址: | ||
https://github.com/yunke-yunfly/fast-typescript-to-jsonschema/tree/master/example | ||
<https://github.com/yunke-yunfly/fast-typescript-to-jsonschema/tree/master/example> | ||
@@ -147,3 +174,2 @@ ## 注释 | ||
> 更多支持的类型解析[请看](docs/index.md),目录如下: | ||
@@ -150,0 +176,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
138797
20
1508
237