Skip to content

Commit 55775a4

Browse files
committed
cleanup
1 parent b8355b2 commit 55775a4

File tree

4 files changed

+42
-34
lines changed

4 files changed

+42
-34
lines changed

packages/schema-sdk/__tests__/openapi.generate.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { writeFileSync } from 'fs';
2-
import { getDefaultSchemaTSOptions } from 'schema-typescript';
32

43
import schema from '../../../__fixtures__/openapi/swagger.json';
54
import { generateOpenApiClient } from '../src/openapi';
5+
import { getDefaultSchemaSDKOptions } from '../src/types';
66

77
it('swagger', () => {
8-
const options = getDefaultSchemaTSOptions({
9-
// include: [
10-
// '*.v1.*'
11-
// ],
8+
const options = getDefaultSchemaSDKOptions({
9+
includeSwaggerUrl: true,
1210
exclude: [
1311
'*.v1beta1.*',
1412
'*.v2beta1.*',
@@ -56,10 +54,11 @@ it('swagger', () => {
5654
});
5755

5856
it('merged', () => {
59-
const options = getDefaultSchemaTSOptions({
57+
const options = getDefaultSchemaSDKOptions({
6058
// include: [
6159
// '*.v1.*'
6260
// ],
61+
includeSwaggerUrl: true,
6362
includeMethodComments: true,
6463
namingStrategy: {
6564
useLastSegment: true

packages/schema-sdk/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"dependencies": {
3232
"@babel/generator": "^7.24.4",
3333
"@babel/types": "^7.24.0",
34+
"@interweb-utils/casing": "^0.2.0",
3435
"schema-typescript": "^0.5.0",
3536
"deepmerge": "^4.3.1"
3637
},

packages/schema-sdk/src/openapi.ts

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import generate from '@babel/generator';
22
import * as t from '@babel/types';
3+
import { toCamelCase, toPascalCase } from '@interweb-utils/casing';
34
import { generateTypeScriptTypes } from 'schema-typescript';
4-
import { getTypeNameSafe, shouldInclude, toCamelCase, toPascalCase } from 'schema-typescript';
5+
import { getTypeNameSafe, shouldInclude } from 'schema-typescript';
56

67
import { OpenAPIPathItem, OpenAPISpec, Operation, Parameter, Response } from './openapi.types';
78
import { OpenAPIOptions } from './types';
@@ -449,34 +450,39 @@ export function generateMethods(options: OpenAPIOptions, schema: OpenAPISpec): t
449450
}
450451

451452

452-
export function generateOpenApiClient(options: OpenAPIOptions, schema: OpenAPISpec): string {
453-
const methods = [
454-
t.classMethod(
455-
'method',
456-
t.identifier('getSwaggerJSON'),
457-
[],
458-
t.blockStatement([
459-
t.variableDeclaration('const', [
460-
t.variableDeclarator(
461-
t.identifier('path'),
462-
t.stringLiteral('/openapi/v2') // Change to '/swagger.json' if needed
463-
)
464-
]),
465-
t.returnStatement(
466-
t.callExpression(
467-
t.memberExpression(t.thisExpression(), t.identifier('get')),
468-
[t.identifier('path')]
469-
)
453+
export const getSwaggerJSONMethod = (): t.ClassMethod => {
454+
return t.classMethod(
455+
'method',
456+
t.identifier('getSwaggerJSON'),
457+
[],
458+
t.blockStatement([
459+
t.variableDeclaration('const', [
460+
t.variableDeclarator(
461+
t.identifier('path'),
462+
t.stringLiteral('/openapi/v2')
470463
)
471464
]),
472-
false,
473-
false,
474-
false,
475-
true
476-
),
477-
...generateMethods(options, schema)
478-
];
465+
t.returnStatement(
466+
t.callExpression(
467+
t.memberExpression(t.thisExpression(), t.identifier('get')),
468+
[t.identifier('path')]
469+
)
470+
)
471+
]),
472+
false,
473+
false,
474+
false,
475+
true
476+
);
477+
};
479478

479+
export function generateOpenApiClient(options: OpenAPIOptions, schema: OpenAPISpec): string {
480+
const methods = [];
481+
if (options.includeSwaggerUrl) {
482+
methods.push(getSwaggerJSONMethod());
483+
}
484+
methods.push(...generateMethods(options, schema));
485+
480486
const classBody = t.classBody([
481487
t.classMethod(
482488
'constructor',

packages/schema-sdk/src/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { defaultSchemaTSOptions, SchemaTSOptions } from 'schema-typescript';
55
export interface OpenAPIOptions extends SchemaTSOptions {
66
version?: 'v1' | 'v1beta1' | 'v2beta1' | 'v2beta2';
77
mergedParams?: boolean;
8+
includeSwaggerUrl?: boolean;
89
paths?: {
910
// Include/Exclude types
1011
include?: string[];
@@ -21,6 +22,7 @@ export interface OpenAPIOptions extends SchemaTSOptions {
2122
export const defaultSchemaSDKOptions: OpenAPIOptions = {
2223
...defaultSchemaTSOptions,
2324
mergedParams: false,
25+
includeSwaggerUrl: false,
2426
paths: {
2527
include: [],
2628
exclude: [],
@@ -31,7 +33,7 @@ export const defaultSchemaSDKOptions: OpenAPIOptions = {
3133
}
3234
};
3335

34-
export const getDefaultSchemaSDKOptions = (options?: DeepPartial<SchemaTSOptions>): SchemaTSOptions => {
35-
return deepmerge(defaultSchemaSDKOptions, options ?? {}) as SchemaTSOptions;
36+
export const getDefaultSchemaSDKOptions = (options?: DeepPartial<OpenAPIOptions>): OpenAPIOptions => {
37+
return deepmerge(defaultSchemaSDKOptions, options ?? {}) as OpenAPIOptions;
3638
};
3739

0 commit comments

Comments
 (0)