Skip to content

Commit

Permalink
feat: 为时间和大整数的类型增加别名
Browse files Browse the repository at this point in the history
  • Loading branch information
geekact committed Oct 20, 2024
1 parent 0d4ccec commit a17fd67
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/base-openapi-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import type { OpenapiClientAdapter, Methods } from './lib/adapter';
import { utils } from './utils';

export type BigIntString = string;
/**
* 2020-02-12T07:20:50.52Z
*/
export type DateTimeString = string;
/**
* 2020-02-12
*/
export type DateString = string;

export namespace BaseOpenapiClient {
export interface UserInputOpts<T extends object = object> {
headers?: Record<string, unknown>;
Expand Down
4 changes: 3 additions & 1 deletion src/lib/parse-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ export const parseSchema = (
case 'number':
if (parsed.format === 'int64') {
// bigint 在传输过程中会转为字符串
return `(string${nullable})`;
return `(BigIntString${nullable})`;
}
return `(number${nullable})`;
case 'string':
if (parsed.format === 'binary') return `(Blob${nullable})`;
if (parsed.format === 'date') return `(DateString${nullable})`;
if (parsed.format === 'date-time') return `(DateTimeString${nullable})`;
return `(string${nullable})`;
case 'array':
return `((${parseSchema(docs, parsed.items)})[]${nullable})`;
Expand Down
12 changes: 11 additions & 1 deletion test/lib/parse-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,17 @@ describe('常规', () => {

test('bigint转为字符串', () => {
const type = parseSchema(docs, { type: 'integer', format: 'int64' });
expect(type).toMatchInlineSnapshot(`"(string)"`);
expect(type).toMatchInlineSnapshot(`"(BigIntString)"`);
});

test('时间', () => {
expect(parseSchema(docs, { type: 'string', format: 'date' })).toMatchInlineSnapshot(
`"(DateString)"`,
);

expect(
parseSchema(docs, { type: 'string', format: 'date-time' }),
).toMatchInlineSnapshot(`"(DateTimeString)"`);
});

test('动态对象属性', () => {
Expand Down

0 comments on commit a17fd67

Please sign in to comment.