From 98c563a23d610fa052819038844cb7a12a0623a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustaf=20R=C3=A4ntil=C3=A4?= Date: Sat, 11 Sep 2021 11:37:30 +0200 Subject: [PATCH] feat(core): fixed openapi schema ref Also small TypeScript 4.4 support fix fix #11 --- lib/batch-convert.ts | 8 ++-- lib/file.ts | 4 +- .../__snapshots__/ts-to-openapi.test.ts.snap | 48 +++++++++++++++++++ lib/tests/ts-to-openapi.test.ts | 36 ++++++++++++++ package.json | 38 +++++++-------- 5 files changed, 109 insertions(+), 25 deletions(-) create mode 100644 lib/tests/__snapshots__/ts-to-openapi.test.ts.snap create mode 100644 lib/tests/ts-to-openapi.test.ts diff --git a/lib/batch-convert.ts b/lib/batch-convert.ts index 404e765..e7cc45a 100644 --- a/lib/batch-convert.ts +++ b/lib/batch-convert.ts @@ -1,7 +1,7 @@ import * as path from 'path' import { map } from "already" -import { hex, gray } from "chalk" +import * as chalk from "chalk" import { Converter, Target } from "./converter" import { glob, reRootFiles, prettyFile } from './file' @@ -112,10 +112,10 @@ export async function batchConvert( const prefixText = '[typeconv]'; const prefix = allInputTypes === 0 - ? gray( prefixText ) + ? chalk.gray( prefixText ) : notConverted - ? hex( '#D2D200' )( prefixText ) - : hex( '#00D21F' )( prefixText ); + ? chalk.hex( '#D2D200' )( prefixText ) + : chalk.hex( '#00D21F' )( prefixText ); console.error( `${prefix} ${prettyFile( rel, root )} -> ` + diff --git a/lib/file.ts b/lib/file.ts index 6c14c9f..aa58529 100644 --- a/lib/file.ts +++ b/lib/file.ts @@ -51,9 +51,9 @@ export async function writeFile( filename: string, data: string ) { await tryWrite( ); } - catch ( err ) + catch ( err: any ) { - if ( err.code === 'ENOENT' ) + if ( err?.code === 'ENOENT' ) { await fsPromises.mkdir( path.dirname( filename ), diff --git a/lib/tests/__snapshots__/ts-to-openapi.test.ts.snap b/lib/tests/__snapshots__/ts-to-openapi.test.ts.snap new file mode 100644 index 0000000..2215f35 --- /dev/null +++ b/lib/tests/__snapshots__/ts-to-openapi.test.ts.snap @@ -0,0 +1,48 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`ts-to-openapi typescript to openapi 1`] = ` +"openapi: 3.0.0 +info: + title: My API + version: v1 +paths: {} +$comment: >- + Generated by core-types-json-schema + (https://github.com/grantila/core-types-json-schema) on behalf of typeconv + (https://github.com/grantila/typeconv) +components: + schemas: + Foo: + properties: + a: + type: string + b: + nullable: true + c: + type: number + d: + type: boolean + e: + $ref: '#/components/schemas/Thing' + required: + - a + - b + - c + - d + - e + additionalProperties: false + type: object + Thing: + properties: + x: + const: 6 + type: number + 'y': + type: string + required: + - x + - 'y' + additionalProperties: false + type: object +" +`; diff --git a/lib/tests/ts-to-openapi.test.ts b/lib/tests/ts-to-openapi.test.ts new file mode 100644 index 0000000..251199f --- /dev/null +++ b/lib/tests/ts-to-openapi.test.ts @@ -0,0 +1,36 @@ +import { makeConverter, getTypeScriptReader, getOpenApiWriter } from "../" + + +describe( "ts-to-openapi", ( ) => +{ + it( "typescript to openapi", async ( ) => + { + const input = ` + type Thing = { + x: 6 + y: string + } + + export type Foo = { + a: string + b: null + c: number + d: boolean + e: Thing + } + `; + + const { convert } = makeConverter( + getTypeScriptReader( ), + getOpenApiWriter( { + format: 'yaml', + title: 'My API', + version: 'v1' + } ) + ); + + const { data } = await convert( { data: input } ); + + expect( data ).toMatchSnapshot( ); + }); +} ); diff --git a/package.json b/package.json index 6b21ecf..2214857 100644 --- a/package.json +++ b/package.json @@ -36,31 +36,31 @@ "graphql" ], "devDependencies": { - "@types/babel__code-frame": "^7.0.2", - "@types/jest": "^26.0.22", - "@types/js-yaml": "^4.0.0", - "@types/node": "^14.14.37", + "@types/babel__code-frame": "^7.0.3", + "@types/jest": "^27.0.1", + "@types/js-yaml": "^4.0.3", + "@types/node": "^16.9.1", "cz-conventional-changelog": "^3.1.0", - "execa": "^5.0.0", - "jest": "^26.6.3", + "execa": "^5.1.1", + "jest": "^27.1.1", "rimraf": "^3.0.1", "tempy": "^1.0.1", - "ts-jest": "^26.5.4", - "ts-node": "^9.1.1", - "typescript": "^4.2.4" + "ts-jest": "^27.0.5", + "ts-node": "^10.2.1", + "typescript": "^4.4.3" }, "dependencies": { - "@babel/code-frame": "^7.12.13", - "already": "^1.13.2", - "chalk": "^4.1.0", - "core-types": "^1.8.1", - "core-types-graphql": "^1.2.0", - "core-types-json-schema": "^1.5.0", - "core-types-suretype": "^1.1.2", - "core-types-ts": "^1.2.2", + "@babel/code-frame": "^7.14.5", + "already": "^2.1.0", + "chalk": "^4.1.2", + "core-types": "^1.8.2", + "core-types-graphql": "^1.3.0", + "core-types-json-schema": "^1.5.1", + "core-types-suretype": "^1.2.0", + "core-types-ts": "^1.3.0", "globby": "^11.0.3", - "js-yaml": "^4.0.0", - "oppa": "^0.3.3", + "js-yaml": "^4.1.0", + "oppa": "^0.4.0", "terminal-link": "^2.1.1" }, "config": {