Skip to content

Commit 3a15e0c

Browse files
authored
feat: added file upload and download support (#84)
* infer fetcher response type and support for blob * fix: return type
1 parent a12f12b commit 3a15e0c

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

plugins/typescript/src/core/findCompatibleMediaType.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export const findCompatibleMediaType = (
1818
if (
1919
contentType.startsWith("*/*") ||
2020
contentType.startsWith("application/json") ||
21-
contentType.startsWith("application/octet-stream")
21+
contentType.startsWith("application/octet-stream") ||
22+
contentType.startsWith("multipart/form-data")
2223
) {
2324
return requestBodyOrResponseObject.content[contentType];
2425
}

plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ export const getType = (
165165
schema.nullable
166166
);
167167
case "string":
168+
if (schema.format === "binary") {
169+
return f.createTypeReferenceNode("Blob");
170+
}
168171
return withNullable(
169172
f.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
170173
schema.nullable

plugins/typescript/src/templates/fetcher.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ export async function ${camel(prefix)}Fetch<
9898
throw error;
9999
}
100100
101-
return await response.json();
101+
if (response.headers.get('content-type').includes('json')) {
102+
return await response.json();
103+
} else {
104+
// if it is not a json response, asume it is a blob and cast it to TData
105+
return (await response.blob()) as unknown as TData;
106+
}
102107
} catch (e) {
103108
throw {
104109
status: "unknown" as const,

0 commit comments

Comments
 (0)