Skip to content

Commit 431d551

Browse files
committed
feat: 为特殊格式增加别名
1 parent 0d4ccec commit 431d551

File tree

3 files changed

+86
-4
lines changed

3 files changed

+86
-4
lines changed

src/base-openapi-client.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
import type { OpenapiClientAdapter, Methods } from './lib/adapter';
22
import { utils } from './utils';
33

4+
export namespace string {
5+
export type BigInt = string;
6+
/**
7+
* 2020-02-12T07:20:50.52Z
8+
*/
9+
export type DateTime = string;
10+
/**
11+
* 2020-02-12
12+
*/
13+
export type Date = string;
14+
/**
15+
* 07:20:50.52Z
16+
*/
17+
export type Time = string;
18+
/**
19+
* example@gmail.com
20+
*/
21+
export type Email = string;
22+
/**
23+
* https://example.com/foo/bar
24+
*/
25+
export type Uri = string;
26+
/**
27+
* 127.0.0.1
28+
*/
29+
export type IPv4 = string;
30+
/**
31+
* 2001:0DB8:0000:0023:0008:0800:200C:417A
32+
*/
33+
export type IPv6 = string;
34+
}
35+
436
export namespace BaseOpenapiClient {
537
export interface UserInputOpts<T extends object = object> {
638
headers?: Record<string, unknown>;

src/lib/parse-schema.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,31 @@ export const parseSchema = (
2222
case 'number':
2323
if (parsed.format === 'int64') {
2424
// bigint 在传输过程中会转为字符串
25-
return `(string${nullable})`;
25+
return `(string.BigInt${nullable})`;
2626
}
2727
return `(number${nullable})`;
2828
case 'string':
29-
if (parsed.format === 'binary') return `(Blob${nullable})`;
30-
return `(string${nullable})`;
29+
switch (parsed.format) {
30+
case 'binary':
31+
return `(Blob${nullable})`;
32+
case 'date':
33+
return `(string.Date${nullable})`;
34+
case 'date-time':
35+
return `(string.DateTime${nullable})`;
36+
case 'time':
37+
return `(string.Time${nullable})`;
38+
case 'email':
39+
return `(string.Email${nullable})`;
40+
case 'uri':
41+
return `(string.Uri${nullable})`;
42+
case 'ipv4':
43+
return `(string.IPv4${nullable})`;
44+
case 'ipv6':
45+
return `(string.IPv6${nullable})`;
46+
default:
47+
return `(string${nullable})`;
48+
}
49+
3150
case 'array':
3251
return `((${parseSchema(docs, parsed.items)})[]${nullable})`;
3352
case 'object': {

test/lib/parse-schema.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ describe('常规', () => {
1515
expect(type).toMatchInlineSnapshot(`"(string)"`);
1616
});
1717

18+
test('邮箱', () => {
19+
const type = parseSchema(docs, { type: 'string', format: 'email' });
20+
expect(type).toMatchInlineSnapshot(`"(string.Email)"`);
21+
});
22+
23+
test('超链接', () => {
24+
const type = parseSchema(docs, { type: 'string', format: 'uri' });
25+
expect(type).toMatchInlineSnapshot(`"(string.Uri)"`);
26+
});
27+
1828
test('布尔', () => {
1929
const type = parseSchema(docs, { type: 'boolean' });
2030
expect(type).toMatchInlineSnapshot(`"(boolean)"`);
@@ -85,7 +95,28 @@ describe('常规', () => {
8595

8696
test('bigint转为字符串', () => {
8797
const type = parseSchema(docs, { type: 'integer', format: 'int64' });
88-
expect(type).toMatchInlineSnapshot(`"(string)"`);
98+
expect(type).toMatchInlineSnapshot(`"(string.BigInt)"`);
99+
});
100+
101+
test('时间', () => {
102+
expect(parseSchema(docs, { type: 'string', format: 'date' })).toMatchInlineSnapshot(
103+
`"(string.Date)"`,
104+
);
105+
expect(
106+
parseSchema(docs, { type: 'string', format: 'date-time' }),
107+
).toMatchInlineSnapshot(`"(string.DateTime)"`);
108+
expect(parseSchema(docs, { type: 'string', format: 'time' })).toMatchInlineSnapshot(
109+
`"(string.Time)"`,
110+
);
111+
});
112+
113+
test('ip', () => {
114+
expect(parseSchema(docs, { type: 'string', format: 'ipv4' })).toMatchInlineSnapshot(
115+
`"(string.IPv4)"`,
116+
);
117+
expect(parseSchema(docs, { type: 'string', format: 'ipv6' })).toMatchInlineSnapshot(
118+
`"(string.IPv6)"`,
119+
);
89120
});
90121

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

0 commit comments

Comments
 (0)