Skip to content

Commit d805f9c

Browse files
committed
add multipart/form-data content resolver
1 parent fbc4cfc commit d805f9c

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

src/core/form-data.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { Operation } from "./openapi";
2+
3+
export function hasFormData(operation: Operation) {
4+
if (!operation.requestBody) return false;
5+
return "multipart/form-data" in operation.requestBody.content;
6+
}

src/core/openapi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type ResponseContent = {
3838
schema: SchemaDefinition,
3939
};
4040

41-
type ContentType = "application/json" | "text/plain";
41+
type ContentType = "application/json" | "text/plain" | "multipart/form-data";
4242

4343
export type Content = Record<ContentType, ResponseContent>;
4444

src/core/resolvers/content.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { Content } from "~/core/openapi";
22

33
export default function getContentSchema(content: Content) {
4-
return content["application/json"].schema;
4+
if ("multipart/form-data" in content) {
5+
return content["multipart/form-data"].schema;
6+
}
7+
return (content["application/json"] as Content["application/json"]).schema;
58
}

src/core/resolvers/operation-param.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { hasFormData } from "~/core/form-data";
12
import type { Operation, OperationParameter } from "~/core/openapi";
23
import { resolveSchema } from "~/core/resolvers/schema-definition";
34
import getContentSchema from "./content";
@@ -12,6 +13,11 @@ function resolveRequestBody(body: Exclude<Operation["requestBody"], undefined>,
1213
return `requestBody${body.required ? "" : "?"}: ${resolveSchema(getContentSchema(body.content))}`;
1314
}
1415

16+
function resolveFormData(body: Exclude<Operation["requestBody"], undefined>, typescript: boolean) {
17+
if (!typescript) return "formBody";
18+
return `formBody${body.required ? "" : "?"}: ${resolveSchema(getContentSchema(body.content))}`;
19+
}
20+
1521
function sortRequiredParamsFirst(paramA: OperationParameter, paramB: OperationParameter) {
1622
if (paramA.required === paramB.required) return 0;
1723
return paramA.required ? -1 : 1;
@@ -25,6 +31,13 @@ export function resolveOperationParams(operation: Operation, typescript: boolean
2531
const collection = [
2632
...resolvedParams,
2733
];
28-
if (operation.requestBody) collection.push(resolveRequestBody(operation.requestBody, typescript));
34+
if (operation.requestBody) {
35+
operation.requestBody.content;
36+
if (hasFormData(operation)) {
37+
collection.push(resolveFormData(operation.requestBody, typescript));
38+
} else {
39+
collection.push(resolveRequestBody(operation.requestBody, typescript));
40+
}
41+
}
2942
return collection;
3043
}

0 commit comments

Comments
 (0)