Skip to content

Commit

Permalink
types
Browse files Browse the repository at this point in the history
  • Loading branch information
pyramation committed May 4, 2024
1 parent db0ecff commit b172937
Show file tree
Hide file tree
Showing 22 changed files with 129,649 additions and 2,711 deletions.
65,036 changes: 65,036 additions & 0 deletions __fixtures__/openapi/paths-only.json

Large diffs are not rendered by default.

10,845 changes: 10,845 additions & 0 deletions __fixtures__/output/swagger-client.merged.ts

Large diffs are not rendered by default.

10,306 changes: 10,306 additions & 0 deletions __fixtures__/output/swagger-client.ts

Large diffs are not rendered by default.

507 changes: 507 additions & 0 deletions __fixtures__/output/swagger-definitions.json

Large diffs are not rendered by default.

3,739 changes: 3,739 additions & 0 deletions __fixtures__/output/swagger.includes.ts

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4,414 changes: 4,414 additions & 0 deletions __fixtures__/output/swagger.overrides.last.ts

Large diffs are not rendered by default.

2,660 changes: 1,330 additions & 1,330 deletions __fixtures__/output/swagger.ts

Large diffs are not rendered by default.

21,156 changes: 21,155 additions & 1 deletion __tests__/__snapshots__/openapi.generate.test.ts.snap

Large diffs are not rendered by default.

5,550 changes: 5,548 additions & 2 deletions __tests__/__snapshots__/openapi.overrides.test.ts.snap

Large diffs are not rendered by default.

6,408 changes: 5,075 additions & 1,333 deletions __tests__/__snapshots__/openapi.test.ts.snap

Large diffs are not rendered by default.

40 changes: 39 additions & 1 deletion __tests__/openapi.generate.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
import { writeFileSync } from 'fs';

import schema from '../__fixtures__/openapi/swagger.json';
import { getDefaultSchemaTSOptions } from '../src';
import { generateOpenApiClient } from '../src/openapi';

it('swagger', () => {
const code = generateOpenApiClient(schema as any);
const options = getDefaultSchemaTSOptions({
// include: [
// '*.v1.*'
// ],
includeMethodComments: true,
namingStrategy: {
useLastSegment: true
}
})
const code = generateOpenApiClient({
...options,
version: 'v1',
mergedParams: false
}, schema as any);
expect(code).toMatchSnapshot();
writeFileSync(__dirname + '/../__fixtures__/output/swagger-client.ts', code);
});

it('merged', () => {
const options = getDefaultSchemaTSOptions({
// include: [
// '*.v1.*'
// ],
includeMethodComments: true,
namingStrategy: {
useLastSegment: true
}
})
const code = generateOpenApiClient({
...options,
version: 'v1',
mergedParams: true
}, schema as any);
expect(code).toMatchSnapshot();
writeFileSync(__dirname + '/../__fixtures__/output/swagger-client.merged.ts', code);
});

it('openapi', () => {
const data = Object.keys(schema.definitions)
writeFileSync(__dirname + '/../__fixtures__/output/swagger-definitions.json', JSON.stringify(data, null, 2));
});
22 changes: 19 additions & 3 deletions __tests__/openapi.overrides.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,26 @@ const overrides = {
}
};

it('swagger', () => {
it('useLastSegment false', () => {
const code = generateTypeScript(myschema as any, {
overrides
overrides,
includeTypeComments: true,
namingStrategy: {
useLastSegment: false
}
});
expect(code).toMatchSnapshot();
writeFileSync(__dirname + '/../__fixtures__/output/swagger.overrides.ts', code);
writeFileSync(__dirname + '/../__fixtures__/output/swagger.overrides.all.ts', code);
});

it('useLastSegment true', () => {
const code = generateTypeScript(myschema as any, {
overrides,
includeTypeComments: true,
namingStrategy: {
useLastSegment: true
}
});
expect(code).toMatchSnapshot();
writeFileSync(__dirname + '/../__fixtures__/output/swagger.overrides.last.ts', code);
});
22 changes: 22 additions & 0 deletions __tests__/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,25 @@ it('swagger', () => {
expect(code).toMatchSnapshot();
writeFileSync(__dirname + '/../__fixtures__/output/swagger.ts', code);
});

it('naming', () => {
const code = generateTypeScript(myschema as any, {
exclude: [
'*.v1beta1.*',
'*.v1beta2.*',
'*.v2beta2.*'
],
includeTypeComments: true,
namingStrategy: {
renameMap: {
'io.k8s.api.events.v1.Event': 'EventsEvent',
'io.k8s.api.events.v1.EventList': 'EventsEventList',
'io.k8s.api.events.v1.EventSeries': 'EventsEventSeries',
'io.k8s.api.discovery.v1.EndpointPort': 'DiscoveryEndpoint',
'io.k8s.kube-aggregator.pkg.apis.apiregistration.v1.ServiceReference': 'ApiServiceReference'
}
}
});
expect(code).toMatchSnapshot();
writeFileSync(__dirname + '/../__fixtures__/output/swagger.includes.ts', code);
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@babel/core": "^7.24.4",
"@babel/generator": "^7.24.4",
"@babel/types": "^7.24.0",
"deepmerge": "^4.3.1"
"deepmerge": "^4.3.1",
"minimatch": "^9.0.4"
}
}
36 changes: 32 additions & 4 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import deepmerge from 'deepmerge';

import type { JSONSchema } from "./types";
import type { DeepPartial, JSONSchema } from "./types";

export interface SchemaDefinitionOverrides {
[key: string]: JSONSchema
}

export interface SchemaNamingStrategy {
useLastSegment?: boolean;
prefixStrip?: string | RegExp;
renameMap?: { [originalName: string]: string };
}
export interface SchemaTSOptions {
useSingleQuotes: boolean;
camelCase?: boolean; // defaults to false
camelCaseFn?: (str: string) => string; // optional function to convert keys to camelCase
strictTypeSafety: boolean; // true uses { [k: string]: unknown; }, false uses any
overrides?: SchemaDefinitionOverrides;

includeTypeComments?: boolean;
includePropertyComments?: boolean;
includeMethodComments?: boolean;

namingStrategy?: SchemaNamingStrategy;

// Include/Exclude types
include?: string[];
exclude?: string[];
}

export interface SchemaTSContextI {
Expand All @@ -33,7 +49,7 @@ export class SchemaTSContext implements SchemaTSContextI {
schema: JSONSchema,
parents: JSONSchema[] = []
) {
this.options = deepmerge(defaultOptions, options ?? {});
this.options = getDefaultSchemaTSOptions(options);
this.schema = schema;
this.root = root;
this.parents = parents;
Expand All @@ -48,9 +64,21 @@ export class SchemaTSContext implements SchemaTSContextI {
}
}

export const defaultOptions: SchemaTSOptions = {
export const defaultSchemaTSOptions: SchemaTSOptions = {
useSingleQuotes: true,
camelCase: false,
camelCaseFn: null,
strictTypeSafety: true
strictTypeSafety: true,
exclude: [],
include: [],
includePropertyComments: false,
includeMethodComments: false,
includeTypeComments: false,
namingStrategy: {
useLastSegment: true
}
};

export const getDefaultSchemaTSOptions = (options?: DeepPartial<SchemaTSOptions>): SchemaTSOptions => {
return deepmerge(defaultSchemaTSOptions, options ?? {}) as SchemaTSOptions;
};
Loading

0 comments on commit b172937

Please sign in to comment.