Skip to content

Commit 347ddfa

Browse files
committed
lint
1 parent fddbc20 commit 347ddfa

File tree

15 files changed

+91
-133
lines changed

15 files changed

+91
-133
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
],
2929
"quotes": ["error", "single", { "avoidEscape": true, "allowTemplateLiterals": true }],
3030
"quote-props": ["error", "as-needed"],
31+
"semi": ["error", "always"],
3132
"simple-import-sort/imports": 1,
3233
"simple-import-sort/exports": 1,
3334
"unused-imports/no-unused-imports": 1,

packages/schema-sdk/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ npm install schema-sdk
2727

2828
## Usage
2929

30-
Below are examples demonstrating how to use schema-sdk for generating TypeScript clients and handling OpenAPI specifications:
30+
Below are examples demonstrating how to use `schema-sdk` for generating TypeScript clients and handling OpenAPI specifications:
3131

3232
### Generating OpenAPI Client
3333

3434
```ts
3535
import schema from 'path-to-your/swagger.json';
36-
import { generateOpenApiClient, getDefaultSchemaTSOptions } from 'schema-sdk';
36+
import { generateOpenApiClient, getDefaultSchemaSDKOptions } from 'schema-sdk';
3737
import { writeFileSync } from 'fs';
3838

39-
const options = getDefaultSchemaTSOptions({
39+
const options = getDefaultSchemaSDKOptions({
4040
exclude: [
4141
'*.v1beta1.*',
4242
'*.v2beta1.*',

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ it('swagger', () => {
1616
'io.k8s.api.events.v1.Event',
1717
'io.k8s.api.flowcontrol*'
1818
]
19-
})
19+
});
2020
const code = generateOpenApiClient({
2121
...options,
2222
// version: 'v1',
@@ -64,7 +64,7 @@ it('merged', () => {
6464
namingStrategy: {
6565
useLastSegment: true
6666
}
67-
})
67+
});
6868
const code = generateOpenApiClient({
6969
...options,
7070
version: 'v1',
@@ -75,6 +75,6 @@ it('merged', () => {
7575
});
7676

7777
it('openapi', () => {
78-
const data = Object.keys(schema.definitions)
78+
const data = Object.keys(schema.definitions);
7979
writeFileSync(__dirname + '/../../../__fixtures__/output/swagger-definitions.json', JSON.stringify(data, null, 2));
8080
});

packages/schema-sdk/__tests__/template.literal.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import generate from '@babel/generator';
2-
import { getDefaultSchemaTSOptions } from 'schema-typescript';
32

4-
import { createPathTemplateLiteral } from '../src';
3+
import { createPathTemplateLiteral, getDefaultSchemaSDKOptions } from '../src';
54

6-
const options = getDefaultSchemaTSOptions();
5+
const options = getDefaultSchemaSDKOptions();
76
export const renderTemplateTag = (str: string) => {
87
return generate(createPathTemplateLiteral({
98
...options,
109
mergedParams: true
11-
}, str)).code
12-
}
10+
}, str)).code;
11+
};
1312
it('/osmosis/{gamm}/v1beta1/estimate/swap_exact_amount_in', () => {
1413
expect(renderTemplateTag('/osmosis/{gamm}/v1beta1/estimate/swap_exact_amount_in'))
1514
.toEqual('`/osmosis/${params.gamm}/v1beta1/estimate/swap_exact_amount_in`');

packages/schema-sdk/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"dependencies": {
3232
"@babel/generator": "^7.24.4",
3333
"@babel/types": "^7.24.0",
34-
"schema-typescript": "^0.5.0"
34+
"schema-typescript": "^0.5.0",
35+
"deepmerge": "^4.3.1"
3536
},
3637
"keywords": ["jsonschema", "schema", "typescript", "swagger", "openapi"]
3738
}

packages/schema-sdk/src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import { getDefaultSchemaTSOptions } from 'schema-typescript';
2-
31
export * from './openapi';
42
export * from './openapi.types';
5-
export * from './utils';
6-
7-
export { getDefaultSchemaTSOptions };
3+
export * from './types';
4+
export * from './utils';

packages/schema-sdk/src/openapi.ts

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
11
import generate from '@babel/generator';
22
import * as t from '@babel/types';
3-
import { SchemaTSOptions } from 'schema-typescript';
43
import { generateTypeScriptTypes } from 'schema-typescript';
54
import { getTypeNameSafe, shouldInclude, toCamelCase, toPascalCase } from 'schema-typescript';
65

76
import { OpenAPIPathItem, OpenAPISpec, Operation, Parameter, Response } from './openapi.types';
7+
import { OpenAPIOptions } from './types';
88
import { createPathTemplateLiteral } from './utils';
99

10-
export interface OpenAPIOptions extends SchemaTSOptions {
11-
version?: 'v1' | 'v1beta1' | 'v2beta1' | 'v2beta2';
12-
mergedParams?: boolean;
13-
paths?: {
14-
// Include/Exclude types
15-
include?: string[];
16-
exclude?: string[];
17-
18-
includeTags?: string[];
19-
excludeTags?: string[];
20-
21-
includeRequests?: string[];
22-
excludeRequests?: string[];
23-
}
24-
}
25-
2610
/**
2711
includes: {
2812
requests: ['patch', 'head', 'options', 'get', 'delete'],
@@ -86,11 +70,11 @@ const shouldIncludeOperation = (
8670
const shouldIncludeByRequest = shouldInclude(method, {
8771
include: options.paths?.includeRequests ?? [],
8872
exclude: options.paths?.excludeRequests ?? []
89-
})
73+
});
9074

9175
if (!shouldIncludeByRequest) return false;
9276
return true;
93-
}
77+
};
9478

9579
export const getApiTypeNameSafe = (options: OpenAPIOptions, str: string): string => {
9680
return getTypeNameSafe(options.namingStrategy, str);
@@ -135,14 +119,14 @@ export const getResponseType = (options: OpenAPIOptions, prop: Response) => {
135119
// resolve $ref
136120
if (prop.schema) {
137121
if (!prop.schema.$ref) {
138-
throw new Error('no property set on open api parameter schema!')
122+
throw new Error('no property set on open api parameter schema!');
139123
}
140124
const ref = prop.schema.$ref.split('/');
141125
const definitionName = ref.pop();
142126
return t.tsTypeReference(t.identifier(getApiTypeNameSafe(options, definitionName)));
143127
}
144128
return t.tsAnyKeyword();
145-
}
129+
};
146130

147131
export const getParameterType = (options: OpenAPIOptions, prop: Parameter) => {
148132
if (prop.type) {
@@ -168,14 +152,14 @@ export const getParameterType = (options: OpenAPIOptions, prop: Parameter) => {
168152
// resolve $ref
169153
if (prop.schema) {
170154
if (!prop.schema.$ref) {
171-
throw new Error('no property set on open api parameter schema!')
155+
throw new Error('no property set on open api parameter schema!');
172156
}
173157
const ref = prop.schema.$ref.split('/');
174158
const definitionName = ref.pop();
175159
return t.tsTypeReference(t.identifier(getApiTypeNameSafe(options, definitionName)));
176160
}
177161
return t.tsAnyKeyword();
178-
}
162+
};
179163

180164
interface ParameterInterfaces {
181165
query: Parameter[];
@@ -203,8 +187,8 @@ const initParams = (): ParameterInterfaces => {
203187
path: [],
204188
formData: [],
205189
body: []
206-
}
207-
}
190+
};
191+
};
208192

209193

210194
export function generateOpenApiParams(options: OpenAPIOptions, path: string, pathItem: OpenAPIPathItem): t.TSInterfaceDeclaration[] {
@@ -253,7 +237,7 @@ export function generateOpenApiParams(options: OpenAPIOptions, path: string, pat
253237
p.optional = true;
254238
}
255239
inner.push(p);
256-
})
240+
});
257241

258242
if (!options.mergedParams) {
259243
if (paramType === 'body') {
@@ -272,7 +256,7 @@ export function generateOpenApiParams(options: OpenAPIOptions, path: string, pat
272256
if (inner.length) {
273257
props.push(
274258
p
275-
)
259+
);
276260
}
277261
}
278262
} else {
@@ -312,15 +296,15 @@ export function getOpenApiParams(options: OpenAPIOptions, path: string, pathItem
312296
pathItem.parameters = pathItem.parameters ?? [];
313297
const pathParms = pathItem.parameters?.filter(param => param.in === 'path') ?? [];
314298
if (pathParms.length !== pathInfo.params.length) {
315-
const parameters = pathItem.parameters?.filter(param => param.in !== 'path') ?? []
299+
const parameters = pathItem.parameters?.filter(param => param.in !== 'path') ?? [];
316300
pathInfo.params.forEach(name => {
317301
const found = pathParms.find(param => param.name === name);
318302
parameters.push(found ? found : {
319303
name,
320304
type: 'string',
321305
required: true,
322306
in: 'path'
323-
})
307+
});
324308
});
325309
pathItem.parameters = parameters;
326310
}
@@ -358,7 +342,7 @@ export function getOpenApiParams(options: OpenAPIOptions, path: string, pathItem
358342
if (operation.parameters) {
359343
// Categorize parameters by 'in' field
360344
operation.parameters.forEach(param => {
361-
opParamMethod[param.in].push(param)
345+
opParamMethod[param.in].push(param);
362346
});
363347
}
364348

@@ -373,13 +357,13 @@ export function generateOpenApiTypes(options: OpenAPIOptions, schema: OpenAPISpe
373357
Object.entries(schema.paths).forEach(([path, pathItem]) => {
374358
interfaces.push(...generateOpenApiParams(options, path, pathItem));
375359
});
376-
return interfaces.map(i => t.exportNamedDeclaration(i))
360+
return interfaces.map(i => t.exportNamedDeclaration(i));
377361
}
378362

379363
const getOperationMethodName = (operation: Operation, method: string, path: string) => {
380364
const methodName = operation.operationId || toCamelCase(method + path.replace(/\W/g, '_'));
381365
return methodName;
382-
}
366+
};
383367

384368
export function generateMethods(options: OpenAPIOptions, schema: OpenAPISpec): t.ClassMethod[] {
385369
const methods: t.ClassMethod[] = [];
@@ -398,7 +382,7 @@ export function generateMethods(options: OpenAPIOptions, schema: OpenAPISpec): t
398382

399383
const typeName = toPascalCase(getOperationMethodName(operation, method, path)) + 'Request';
400384
const id = t.identifier('params');
401-
id.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier(typeName)))
385+
id.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier(typeName)));
402386
const params = [id];
403387

404388
const returnType = getOperationReturnType(options, operation, method);
@@ -491,7 +475,7 @@ export function generateOpenApiClient(options: OpenAPIOptions, schema: OpenAPISp
491475
true
492476
),
493477
...generateMethods(options, schema)
494-
]
478+
];
495479

496480
const classBody = t.classBody([
497481
t.classMethod(

packages/schema-sdk/src/template.literal.test.ts

Lines changed: 0 additions & 61 deletions
This file was deleted.

packages/schema-sdk/src/types.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import deepmerge from 'deepmerge';
2+
import type { DeepPartial } from 'schema-typescript';
3+
import { defaultSchemaTSOptions, SchemaTSOptions } from 'schema-typescript';
4+
5+
export interface OpenAPIOptions extends SchemaTSOptions {
6+
version?: 'v1' | 'v1beta1' | 'v2beta1' | 'v2beta2';
7+
mergedParams?: boolean;
8+
paths?: {
9+
// Include/Exclude types
10+
include?: string[];
11+
exclude?: string[];
12+
13+
includeTags?: string[];
14+
excludeTags?: string[];
15+
16+
includeRequests?: string[];
17+
excludeRequests?: string[];
18+
}
19+
}
20+
21+
export const defaultSchemaSDKOptions: OpenAPIOptions = {
22+
...defaultSchemaTSOptions,
23+
mergedParams: false,
24+
paths: {
25+
include: [],
26+
exclude: [],
27+
includeTags: [],
28+
excludeTags: [],
29+
includeRequests: [],
30+
excludeRequests: []
31+
}
32+
};
33+
34+
export const getDefaultSchemaSDKOptions = (options?: DeepPartial<SchemaTSOptions>): SchemaTSOptions => {
35+
return deepmerge(defaultSchemaSDKOptions, options ?? {}) as SchemaTSOptions;
36+
};
37+

packages/schema-typescript/__tests__/additional-props.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ it('additional', () => {
1919
},
2020
required: ['firstName', 'lastName'],
2121
additionalProperties: true
22-
} as any)).toMatchSnapshot()
23-
})
22+
} as any)).toMatchSnapshot();
23+
});
2424

2525
it('additional', () => {
2626
expect(generateTypeScript({
@@ -43,5 +43,5 @@ it('additional', () => {
4343
required: ['newProp'],
4444
additionalProperties: true
4545
}
46-
} as any)).toMatchSnapshot()
47-
})
46+
} as any)).toMatchSnapshot();
47+
});

packages/schema-typescript/__tests__/const.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ it('const', () => {
1616
},
1717
required: ['firstName', 'age'],
1818
additionalProperties: true
19-
} as any)).toMatchSnapshot()
20-
})
19+
} as any)).toMatchSnapshot();
20+
});

0 commit comments

Comments
 (0)