Skip to content

Commit

Permalink
feat(core): fixed openapi schema ref
Browse files Browse the repository at this point in the history
Also small TypeScript 4.4 support fix

fix #11
  • Loading branch information
grantila committed Sep 11, 2021
1 parent 5444771 commit 98c563a
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 25 deletions.
8 changes: 4 additions & 4 deletions lib/batch-convert.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 )} -> ` +
Expand Down
4 changes: 2 additions & 2 deletions lib/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
Expand Down
48 changes: 48 additions & 0 deletions lib/tests/__snapshots__/ts-to-openapi.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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
"
`;
36 changes: 36 additions & 0 deletions lib/tests/ts-to-openapi.test.ts
Original file line number Diff line number Diff line change
@@ -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( );
});
} );
38 changes: 19 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 98c563a

Please sign in to comment.