Skip to content

Commit

Permalink
upgrade to esm
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbarbour committed Oct 14, 2023
1 parent 2526c3a commit 0601a93
Show file tree
Hide file tree
Showing 7 changed files with 3,336 additions and 7,253 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: actions/[email protected]

- name: Set Version
run: npm --no-git-tag-version version 1.1.${{ github.run_number }}
run: npm --no-git-tag-version version 2.0.${{ github.run_number }}

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
Expand Down
10,551 changes: 3,314 additions & 7,237 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "@hexlabs/schema-api-ts",
"type": "module",
"description": "Generate AWS Stacks, APIs and Types from OpenAPI",
"license": "Apache-2.0",
"version": "1.0.0",
Expand All @@ -19,7 +20,7 @@
"build": "tsc --project tsconfig.json",
"test": "jest --ci --runInBand --coverage --reporters=default --reporters=jest-junit --passWithNoTests",
"lint": "eslint **/*.ts",
"start": "ts-node src/cli.ts generate -h $(pwd)/test/schema-example.ts v=1.0.0"
"start": "ts-node --project tsconfig.json src/cli.ts generate -h $(pwd)/test/schema-example.ts v=1.0.0"
},
"eslintConfig": {
"extends": [
Expand Down Expand Up @@ -85,20 +86,21 @@
"@hexlabs/http-api-ts": "^1.1.33",
"@types/aws-lambda": "^8.10.40",
"@types/jest": "^24.9.1",
"@types/node": "^12.12.14",
"@types/node": "^18.18.5",
"@typescript-eslint/eslint-plugin": "^4.24.0",
"@typescript-eslint/parser": "^4.24.0",
"eslint": "^6.8.0",
"eslint-plugin-jest": "^23.1.1",
"husky": "^3.1.0",
"jest": "^26.6.3",
"jest": "^29.7.0",
"jest-junit": "^10.0.0",
"ts-jest": "^26.5.6",
"typescript": "^4.2.4"
"ts-jest": "^29.1.1",
"typescript": "^4.7.4"
},
"dependencies": {
"chalk": "^2.4.2",
"commander": "^6.2.1",
"json-schema-to-typescript": "^10.1.4"
"json-schema-to-typescript": "^10.1.4",
"ts-node": "^10.9.1"
}
}
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as fs from 'fs';
import {OAS} from "./oas";
import chalk from 'chalk';
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('ts-node').register({typeCheck: false});
(await import('ts-node')).register({typeCheck: false});
import { Command } from 'commander';
import {generateSdkFrom} from "./sdk-mapper";
import {generateMockFrom} from "./mock-mapper";
Expand All @@ -24,7 +24,7 @@ export async function types(oas: OAS): Promise<string> {
async function generateFromSchema(schemaLocation: string, command: any) {
const {hydra, aws} = command;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const schema: OAS = schemaLocation.endsWith('.ts') ? require(schemaLocation).default : require(schemaLocation);
const schema: OAS = schemaLocation.endsWith('.ts') ? (await import(schemaLocation)).default : require(schemaLocation);
const pathFinder = PathFinder.from(schema, hydra, aws);
const args = process.argv;
const version = args.find(it => it.startsWith('v='))?.substring(2);
Expand Down
2 changes: 1 addition & 1 deletion src/mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ import {bind, Handler, HttpMethod, HttpError, router, Request} from '@hexlabs/ht
${this.aws ? "import {APIGatewayProxyEvent, APIGatewayProxyResult} from 'aws-lambda';" : ""}
${this.hydra ? "import {ResourceApiDefinition, CollectionApiDefinition, ScopedOperation} from '@hexlabs/lambda-api-ts';" : ""}
// eslint-disable-next-line @typescript-eslint/no-var-requires
const schema = require('./schema.json');
import schema from './schema.json';
import * as Model from "./model";
import {Validator} from '@hexlabs/schema-api-ts';
Expand Down
8 changes: 4 additions & 4 deletions src/schema-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type FilteredKeys<T> = { [P in keyof T]: T[P] extends never ? never : P }[keyof
type Stripped<T> = { [Q in FilteredKeys<T>]: T[Q] };

type Combine<A,B> = A extends undefined ? ( B extends undefined ? never : B) : ( B extends undefined ? A : A & B)
type ObjectSchema<A extends boolean | JSONSchema | undefined = false, R extends Record<string, JSONSchema>| undefined = undefined, O extends JSONSchema | undefined= undefined> = Stripped<{type: 'object', title: string, additionalProperties: A extends undefined ? never : A, required: R extends undefined ? never: UnionToTuple<keyof R>, properties: Combine<R,O> }>;
type ObjectSchema<A extends boolean | JSONSchema | undefined = false, R extends Record<string, JSONSchema>| undefined = undefined, O extends Record<string, JSONSchema> | undefined= undefined> = Stripped<{type: 'object', title: string, additionalProperties: A extends undefined ? never : A, required: R extends undefined ? never: UnionToTuple<keyof R>, properties: Combine<R,O> }>;
type ArraySchema<R, A extends boolean | JSONSchema | undefined = undefined> = Stripped<{type: 'array', title: string, additionalItems: A extends undefined ? never : A, items: R }>;

export class SchemaBuilder<T extends {components:{schemas: any}}> {
Expand All @@ -64,7 +64,7 @@ export class SchemaBuilder<T extends {components:{schemas: any}}> {
return this.schemaParent
}

object<R extends Record<string, JSONSchema> | undefined = undefined,O = undefined, A extends boolean | JSONSchema | undefined = false, P extends JSONSchema | undefined = undefined>
object<R extends Record<string, JSONSchema> | undefined = undefined, O extends Record<string, JSONSchema> | undefined = undefined, A extends boolean | JSONSchema | undefined = false, P extends JSONSchema | undefined = undefined>
(required: R = undefined as unknown as R, optional: O = undefined as unknown as O, additionalProperties: A = false as unknown as A, title = undefined, parts: P = undefined as unknown as P): ObjectSchema<A, R, O> {
const requireds = Object.keys(required ?? {});
return {
Expand Down Expand Up @@ -122,7 +122,7 @@ export class SchemaBuilder<T extends {components:{schemas: any}}> {
null(): {type: 'null'} { return {type: 'null'} }

reference<K extends keyof T['components']['schemas']>(key: K): { '$ref': K extends string ? `#/components/schemas/${K}` : string } {
return { '$ref': `#/components/schemas/${key}` } as any;
return { '$ref': `#/components/schemas/${key as string}` } as any;
}

hydraOperation(): ObjectSchema<undefined, {statusCodes: ArraySchema<{type: 'string'}>, method: {type: 'string'}}, {expects: {type: 'string'}, returns: {type: 'string'}}> {
Expand Down Expand Up @@ -270,7 +270,7 @@ export class OpenApiSpecificationBuilder<S extends {components: {schemas?: any,
}

componentReference<K extends keyof S['components'], T extends keyof S['components'][K]>(componentKey: K, key: T): { '$ref': K extends string ? T extends string ? `#/components/${K}/${T}` : string : string } {
return {'$ref': `#/components/${componentKey}/${key}`} as any;
return {'$ref': `#/components/${componentKey as string}/${key as string}`} as any;
}

addComponent<K extends keyof OASComponents, B extends (builder: this) => OASComponents[K]>(location: K, itemBuilder: B): B extends (builder: any) => infer R ? OpenApiSpecificationBuilder<S & { components: { [k in K]: R } }> : never {
Expand Down
8 changes: 6 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
"strictFunctionTypes": true,
"allowJs": true,
"moduleResolution": "node",
"module": "commonjs",
"target": "ES2019",
"module": "ES2022",
"target": "ES2022",
"pretty": true,
"inlineSourceMap": false,
"inlineSources": false,
"esModuleInterop": true
},
"ts-node": {
"esm": true,
"experimentalSpecifierResolution": "node"
},
"include": [
"src/**/*",
]
Expand Down

0 comments on commit 0601a93

Please sign in to comment.