From 9d00a157267af7f5f4f81315bbf03239f55c3e7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=23=E4=BA=91=E6=B7=A1=E7=84=B6?= Date: Sat, 15 Apr 2023 16:10:17 +0800 Subject: [PATCH] =?UTF-8?q?feat(reader):=20=E6=94=AF=E6=8C=81=20Blob=20?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E8=A7=A3=E8=AF=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/readers/BaseReader.ts | 4 +-- src/readers/PathsReader.ts | 24 ++++++++++---- src/readers/types.ts | 2 +- test/readers/DocumentReader.test.ts | 11 ++++++- test/readers/PathsReader.test.ts | 50 +++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 10 deletions(-) diff --git a/src/readers/BaseReader.ts b/src/readers/BaseReader.ts index d2b9b1a..831a7d8 100644 --- a/src/readers/BaseReader.ts +++ b/src/readers/BaseReader.ts @@ -1,5 +1,5 @@ import { OpenAPIV3 } from 'openapi-types'; -import { INTERNAL_TYPE_NAMES } from '../const'; +import { INTERNAL_TYPE_NAMES, JSON_MIME } from '../const'; import { Named } from './Named'; import { ReaderOptions, StrictReaderOptions, TypeAlias, TypeItem } from './types'; @@ -8,7 +8,7 @@ export class BaseReader { static defaults: StrictReaderOptions = { okCode: 200, - okMediaType: 'application/json', + okMediaType: JSON_MIME, requestPathTypeName: 'ReqPath', requestQueryTypeName: 'ReqParams', requestBodyTypeName: 'ReqData', diff --git a/src/readers/PathsReader.ts b/src/readers/PathsReader.ts index eb8035a..10395e5 100644 --- a/src/readers/PathsReader.ts +++ b/src/readers/PathsReader.ts @@ -1,7 +1,8 @@ import { OpenAPIV3 } from 'openapi-types'; +import { BLOB_MIME, JSON_MIME } from '../const'; import { ComponentsReader } from './ComponentsReader'; import { methods } from './const'; -import { TypeList, TypeOperation, TypeOperations, TypeOrigin } from './types'; +import { TypeItem, TypeList, TypeOperation, TypeOperations, TypeOrigin } from './types'; export class PathsReader extends ComponentsReader { readingUrl = ''; @@ -117,15 +118,26 @@ export class PathsReader extends ComponentsReader { }); } - readOperationRequest(name: string, body: OpenAPIV3.OperationObject['requestBody']) { + readOperationRequest(name: string, body: OpenAPIV3.OperationObject['requestBody']): TypeItem | undefined { if (!body) return; if (this.isReference(body)) return; const { content } = body; - const okMedia = content[this.options.okMediaType]; - if (!okMedia) return; - - return this.readOperationMedia(name, okMedia); + const jsonReq = content[JSON_MIME]; + const blobReq = content[BLOB_MIME]; + + if (jsonReq) return this.readOperationMedia(name, jsonReq); + if (blobReq) + return { + kind: 'alias', + name, + root: false, + target: 'Blob', + origin: 'Blob', + props: [], + required: true, + ref: '', + }; } protected readOperationResponse(name: string, responses: NonNullable) { diff --git a/src/readers/types.ts b/src/readers/types.ts index cb82256..bcd6678 100644 --- a/src/readers/types.ts +++ b/src/readers/types.ts @@ -42,7 +42,7 @@ export interface ReaderOptions { okCode?: number; /** - * ok 的媒体类型 + * ok 的响应类型 * @default ["application/json"] */ okMediaType?: string; diff --git a/test/readers/DocumentReader.test.ts b/test/readers/DocumentReader.test.ts index a3ad900..d74eac5 100644 --- a/test/readers/DocumentReader.test.ts +++ b/test/readers/DocumentReader.test.ts @@ -849,7 +849,16 @@ test('DocumentReader', () => { "method": "post", "name": "uploadFile", "request": { - "body": undefined, + "body": { + "kind": "alias", + "name": "UploadFileReqData", + "origin": "Blob", + "props": [], + "ref": "", + "required": true, + "root": false, + "target": "Blob", + }, "path": { "children": [ { diff --git a/test/readers/PathsReader.test.ts b/test/readers/PathsReader.test.ts index 05a7742..a748a84 100644 --- a/test/readers/PathsReader.test.ts +++ b/test/readers/PathsReader.test.ts @@ -253,6 +253,56 @@ test('req body', () => { ]); }); +test('req file', () => { + const reader = new PathsReader({ + info: { + title: 'test', + version: '1.0.0', + }, + openapi: '3.0.0', + paths: { + '/pet': { + get: { + operationId: 'findPet', + requestBody: { + content: { + 'application/octet-stream': { + schema: { + type: 'string', + format: 'binary', + }, + }, + }, + }, + responses: {}, + }, + }, + }, + }); + + const t = reader.readPaths(); + expect(t).toEqual([ + { + name: 'findPet', + method: HttpMethods.GET, + url: '/pet', + request: { + body: { + kind: 'alias', + name: 'FindPetReqData', + origin: 'Blob', + props: [], + ref: '', + required: true, + root: false, + target: 'Blob', + }, + }, + response: {}, + }, + ]); +}); + test('req query + path', () => { const reader = new PathsReader({ info: {