Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pyramation committed May 25, 2024
1 parent b8355b2 commit 55775a4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 34 deletions.
11 changes: 5 additions & 6 deletions packages/schema-sdk/__tests__/openapi.generate.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { writeFileSync } from 'fs';
import { getDefaultSchemaTSOptions } from 'schema-typescript';

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

it('swagger', () => {
const options = getDefaultSchemaTSOptions({
// include: [
// '*.v1.*'
// ],
const options = getDefaultSchemaSDKOptions({
includeSwaggerUrl: true,
exclude: [
'*.v1beta1.*',
'*.v2beta1.*',
Expand Down Expand Up @@ -56,10 +54,11 @@ it('swagger', () => {
});

it('merged', () => {
const options = getDefaultSchemaTSOptions({
const options = getDefaultSchemaSDKOptions({
// include: [
// '*.v1.*'
// ],
includeSwaggerUrl: true,
includeMethodComments: true,
namingStrategy: {
useLastSegment: true
Expand Down
1 change: 1 addition & 0 deletions packages/schema-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"@babel/generator": "^7.24.4",
"@babel/types": "^7.24.0",
"@interweb-utils/casing": "^0.2.0",
"schema-typescript": "^0.5.0",
"deepmerge": "^4.3.1"
},
Expand Down
58 changes: 32 additions & 26 deletions packages/schema-sdk/src/openapi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import generate from '@babel/generator';
import * as t from '@babel/types';
import { toCamelCase, toPascalCase } from '@interweb-utils/casing';
import { generateTypeScriptTypes } from 'schema-typescript';
import { getTypeNameSafe, shouldInclude, toCamelCase, toPascalCase } from 'schema-typescript';
import { getTypeNameSafe, shouldInclude } from 'schema-typescript';

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


export function generateOpenApiClient(options: OpenAPIOptions, schema: OpenAPISpec): string {
const methods = [
t.classMethod(
'method',
t.identifier('getSwaggerJSON'),
[],
t.blockStatement([
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('path'),
t.stringLiteral('/openapi/v2') // Change to '/swagger.json' if needed
)
]),
t.returnStatement(
t.callExpression(
t.memberExpression(t.thisExpression(), t.identifier('get')),
[t.identifier('path')]
)
export const getSwaggerJSONMethod = (): t.ClassMethod => {
return t.classMethod(
'method',
t.identifier('getSwaggerJSON'),
[],
t.blockStatement([
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('path'),
t.stringLiteral('/openapi/v2')
)
]),
false,
false,
false,
true
),
...generateMethods(options, schema)
];
t.returnStatement(
t.callExpression(
t.memberExpression(t.thisExpression(), t.identifier('get')),
[t.identifier('path')]
)
)
]),
false,
false,
false,
true
);
};

export function generateOpenApiClient(options: OpenAPIOptions, schema: OpenAPISpec): string {
const methods = [];
if (options.includeSwaggerUrl) {
methods.push(getSwaggerJSONMethod());
}
methods.push(...generateMethods(options, schema));

const classBody = t.classBody([
t.classMethod(
'constructor',
Expand Down
6 changes: 4 additions & 2 deletions packages/schema-sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { defaultSchemaTSOptions, SchemaTSOptions } from 'schema-typescript';
export interface OpenAPIOptions extends SchemaTSOptions {
version?: 'v1' | 'v1beta1' | 'v2beta1' | 'v2beta2';
mergedParams?: boolean;
includeSwaggerUrl?: boolean;
paths?: {
// Include/Exclude types
include?: string[];
Expand All @@ -21,6 +22,7 @@ export interface OpenAPIOptions extends SchemaTSOptions {
export const defaultSchemaSDKOptions: OpenAPIOptions = {
...defaultSchemaTSOptions,
mergedParams: false,
includeSwaggerUrl: false,
paths: {
include: [],
exclude: [],
Expand All @@ -31,7 +33,7 @@ export const defaultSchemaSDKOptions: OpenAPIOptions = {
}
};

export const getDefaultSchemaSDKOptions = (options?: DeepPartial<SchemaTSOptions>): SchemaTSOptions => {
return deepmerge(defaultSchemaSDKOptions, options ?? {}) as SchemaTSOptions;
export const getDefaultSchemaSDKOptions = (options?: DeepPartial<OpenAPIOptions>): OpenAPIOptions => {
return deepmerge(defaultSchemaSDKOptions, options ?? {}) as OpenAPIOptions;
};

0 comments on commit 55775a4

Please sign in to comment.