From 4246fdd0fcb145c9f340607ba8624c0b34e0228c Mon Sep 17 00:00:00 2001 From: geekact Date: Sun, 20 Oct 2024 16:58:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=BA=E7=89=B9=E6=AE=8A=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=A2=9E=E5=8A=A0=E5=88=AB=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/base-openapi-client.ts | 24 ++++++++++++++++++++++++ src/lib/parse-schema.ts | 21 ++++++++++++++++++--- test/lib/parse-schema.test.ts | 24 +++++++++++++++++++++++- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/base-openapi-client.ts b/src/base-openapi-client.ts index d70fbae..5ca030d 100644 --- a/src/base-openapi-client.ts +++ b/src/base-openapi-client.ts @@ -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; + /** + * example@gmail.com + */ + export type Email = string; + /** + * https://example.com/foo/bar + */ + export type Uri = string; +} + export namespace BaseOpenapiClient { export interface UserInputOpts { headers?: Record; diff --git a/src/lib/parse-schema.ts b/src/lib/parse-schema.ts index 17d7e84..8d3fa67 100644 --- a/src/lib/parse-schema.ts +++ b/src/lib/parse-schema.ts @@ -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': { diff --git a/test/lib/parse-schema.test.ts b/test/lib/parse-schema.test.ts index a40faa9..f04b34f 100644 --- a/test/lib/parse-schema.test.ts +++ b/test/lib/parse-schema.test.ts @@ -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)"`); @@ -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('动态对象属性', () => {