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 4246fdd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/base-openapi-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import type { OpenapiClientAdapter, Methods } from './lib/adapter';
import { utils } from './utils';

export namespace StringOf {
export type BigInt = string;
/**
* 2020-02-12T07:20:50.52Z
*/
export type DateTime = string;
/**
* 2020-02-12
*/
export type Date = string;
/**
* 07:20:50.52Z
*/
export type Time = string;
/**
* [email protected]
*/
export type Email = string;
/**
* https://example.com/foo/bar
*/
export type Uri = string;
}

export namespace BaseOpenapiClient {
export interface UserInputOpts<T extends object = object> {
headers?: Record<string, unknown>;
Expand Down
21 changes: 18 additions & 3 deletions src/lib/parse-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,27 @@ export const parseSchema = (
case 'number':
if (parsed.format === 'int64') {
// bigint 在传输过程中会转为字符串
return `(string${nullable})`;
return `(StringOf.BigInt${nullable})`;
}
return `(number${nullable})`;
case 'string':
if (parsed.format === 'binary') return `(Blob${nullable})`;
return `(string${nullable})`;
switch (parsed.format) {
case 'binary':
return `(Blob${nullable})`;
case 'date':
return `(StringOf.Date${nullable})`;
case 'date-time':
return `(StringOf.DateTime${nullable})`;
case 'time':
return `(StringOf.Time${nullable})`;
case 'email':
return `(StringOf.Email${nullable})`;
case 'uri':
return `(StringOf.Uri${nullable})`;
default:
return `(string${nullable})`;
}

case 'array':
return `((${parseSchema(docs, parsed.items)})[]${nullable})`;
case 'object': {
Expand Down
24 changes: 23 additions & 1 deletion test/lib/parse-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ describe('常规', () => {
expect(type).toMatchInlineSnapshot(`"(string)"`);
});

test('邮箱', () => {
const type = parseSchema(docs, { type: 'string', format: 'email' });
expect(type).toMatchInlineSnapshot(`"(StringOf.Email)"`);
});

test('超链接', () => {
const type = parseSchema(docs, { type: 'string', format: 'uri' });
expect(type).toMatchInlineSnapshot(`"(StringOf.Uri)"`);
});

test('布尔', () => {
const type = parseSchema(docs, { type: 'boolean' });
expect(type).toMatchInlineSnapshot(`"(boolean)"`);
Expand Down Expand Up @@ -85,7 +95,19 @@ describe('常规', () => {

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

test('时间', () => {
expect(parseSchema(docs, { type: 'string', format: 'date' })).toMatchInlineSnapshot(
`"(StringOf.Date)"`,
);
expect(
parseSchema(docs, { type: 'string', format: 'date-time' }),
).toMatchInlineSnapshot(`"(StringOf.DateTime)"`);
expect(parseSchema(docs, { type: 'string', format: 'time' })).toMatchInlineSnapshot(
`"(StringOf.Time)"`,
);
});

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

0 comments on commit 4246fdd

Please sign in to comment.