Skip to content

Commit

Permalink
feat: 支持了 format=binary
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed Aug 1, 2024
1 parent e62cd53 commit f6235df
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/printer/Schemata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export class Schemata {

switch (type) {
case 'string': {
const { enum: enumValues = [] } = schema;
const { enum: enumValues = [], format } = schema;
const isBlob = format === 'binary';
return {
comments,
type:
Expand All @@ -97,7 +98,9 @@ export class Schemata {
enumValues.map((e) => (isString(e) ? JSON.stringify(e) : this.named.getRefType(e.$ref) || 'unknown')),
'|',
)
: type,
: isBlob
? 'Blob'
: 'string',
required: Boolean(schema.required),
};
}
Expand Down
2 changes: 0 additions & 2 deletions src/printer/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ export const INTERNAL_NAMES = [
'AnyOf',
'AnyObject',
'AnyArray',
'URL',
'resolveURL',
// config
AXIOS_IMPORT_NAME,
AXIOS_QUEST_CONFIG_TYPE_NAME,
Expand Down
127 changes: 127 additions & 0 deletions test/printer/upload.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { Printer } from '../../src/printer';

test('upload root', () => {
const printer = new Printer({
openapi: '3.1.0',
info: {
title: 'test',
version: '1.0.0',
},
paths: {
'/upload': {
post: {
tags: ['upload'],
summary: 'upload',
description: 'upload',
operationId: 'upload',
requestBody: {
content: {
'multipart/form-data': {
schema: {
type: 'string',
format: 'binary',
description: 'A file',
},
},
},
},
},
},
},
});

const output = printer.print({
hideComponents: true,
hideInfo: true,
hideFooters: true,
hideHeaders: true,
hideImports: true,
hideHelpers: true,
});

expect(output).toMatchInlineSnapshot(`
"/**
* @description upload
* @summary upload
* @param data A file
* @param [config] request config
*/
export async function upload(data:Blob,config?:AxiosRequestConfig): AxiosPromise<unknown> {
return axios({
method: "post",
url: \`/upload\`,
data: data,
...config
});
}"
`);
});

test('upload single', () => {
const printer = new Printer({
openapi: '3.1.0',
info: {
title: 'test',
version: '1.0.0',
},
paths: {
'/upload': {
post: {
tags: ['upload'],
summary: 'upload',
description: 'upload',
operationId: 'upload',
requestBody: {
content: {
'multipart/form-data': {
schema: {
type: 'object',
properties: {
file: {
type: 'string',
format: 'binary',
description: 'A file',
required: true,
},
},
},
},
},
},
},
},
},
});

const output = printer.print({
hideComponents: true,
hideInfo: true,
hideFooters: true,
hideHeaders: true,
hideImports: true,
hideHelpers: true,
});

expect(output).toMatchInlineSnapshot(`
"/**
* @description upload
* @summary upload
* @param data request param
* @param [config] request config
*/
export async function upload(data:{
/**
* @description A file
* @format binary
*/
"file":Blob;
},config?:AxiosRequestConfig): AxiosPromise<unknown> {
return axios({
method: "post",
url: \`/upload\`,
data: data,
...config
});
}"
`);
});

0 comments on commit f6235df

Please sign in to comment.