Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): better handle content types #195

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cuddly-beans-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": minor
---

feat(fetch): detect form data repsonses properly
5 changes: 5 additions & 0 deletions .changeset/metal-boxes-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

client(angular/fetch/xhr): detect all application/json or +json as JSON
5 changes: 5 additions & 0 deletions .changeset/ninety-rice-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": minor
---

feat(fetch): add application/octet-stream, application/pdf, and application/zip as binary response types
2 changes: 1 addition & 1 deletion src/templates/core/angular/getRequestBody.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const getRequestBody = (options: ApiRequestOptions): unknown => {
if (options.body) {
if (options.mediaType?.includes('/json')) {
if (options.mediaType?.includes('application/json') || options.mediaType?.includes('+json')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not make this just endsWith('json')?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may not always end with json, as there may be charset information in the header. JSON with always be application/json or something/something+json (but may be appended by "; charset=utf8")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about includes json then? Why not drop + and /?

return JSON.stringify(options.body)
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
return options.body;
Expand Down
2 changes: 1 addition & 1 deletion src/templates/core/fetch/getRequestBody.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const getRequestBody = (options: ApiRequestOptions): unknown => {
if (options.body !== undefined) {
if (options.mediaType?.includes('/json')) {
if (options.mediaType?.includes('application/json') || options.mediaType?.includes('+json')) {
return JSON.stringify(options.body)
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
return options.body;
Expand Down
13 changes: 6 additions & 7 deletions src/templates/core/fetch/getResponseBody.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ export const getResponseBody = async (response: Response): Promise<unknown> => {
try {
const contentType = response.headers.get('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json'];
const binaryTypes = ['audio/', 'image/', 'video/'];
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
if (isJSON) {
const binaryTypes = ['application/octet-stream', 'application/pdf', 'application/zip', 'audio/', 'image/', 'video/'];
if (contentType.includes('application/json') || contentType.includes('+json')) {
return await response.json();
} else if (isBinary) {
} else if (binaryTypes.some(type => contentType.includes(type))) {
return await response.blob();
} else {
} else if (contentType.includes('multipart/form-data')) {
return await response.formData();
} else if (contentType.includes('text/')) {
return await response.text();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/templates/core/xhr/getRequestBody.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const getRequestBody = (options: ApiRequestOptions): unknown => {
if (options.body !== undefined) {
if (options.mediaType?.includes('/json')) {
if (options.mediaType?.includes('application/json') || options.mediaType?.includes('+json')) {
return JSON.stringify(options.body)
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
return options.body;
Expand Down
4 changes: 1 addition & 3 deletions src/templates/core/xhr/getResponseBody.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ export const getResponseBody = (xhr: XMLHttpRequest): unknown => {
try {
const contentType = xhr.getResponseHeader('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json'];
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
if (isJSON) {
if (contentType.includes('application/json') || contentType.includes('+json')) {
return JSON.parse(xhr.responseText);
} else {
return xhr.responseText;
Expand Down
22 changes: 14 additions & 8 deletions test/__snapshots__/v2/core/request.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptio

export const getRequestBody = (options: ApiRequestOptions): unknown => {
if (options.body !== undefined) {
if (options.mediaType?.includes('/json')) {
if (options.mediaType?.includes('application/json') || options.mediaType?.includes('+json')) {
return JSON.stringify(options.body);
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
return options.body;
Expand Down Expand Up @@ -213,15 +213,21 @@ export const getResponseBody = async (response: Response): Promise<unknown> => {
try {
const contentType = response.headers.get('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json'];
const binaryTypes = ['audio/', 'image/', 'video/'];
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
if (isJSON) {
const binaryTypes = [
'application/octet-stream',
'application/pdf',
'application/zip',
'audio/',
'image/',
'video/',
];
if (contentType.includes('application/json') || contentType.includes('+json')) {
return await response.json();
} else if (isBinary) {
} else if (binaryTypes.some(type => contentType.includes(type))) {
return await response.blob();
} else {
} else if (contentType.includes('multipart/form-data')) {
return await response.formData();
} else if (contentType.includes('text/')) {
return await response.text();
}
}
Expand Down
22 changes: 14 additions & 8 deletions test/__snapshots__/v3/core/request.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptio

export const getRequestBody = (options: ApiRequestOptions): unknown => {
if (options.body !== undefined) {
if (options.mediaType?.includes('/json')) {
if (options.mediaType?.includes('application/json') || options.mediaType?.includes('+json')) {
return JSON.stringify(options.body);
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
return options.body;
Expand Down Expand Up @@ -213,15 +213,21 @@ export const getResponseBody = async (response: Response): Promise<unknown> => {
try {
const contentType = response.headers.get('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json'];
const binaryTypes = ['audio/', 'image/', 'video/'];
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
if (isJSON) {
const binaryTypes = [
'application/octet-stream',
'application/pdf',
'application/zip',
'audio/',
'image/',
'video/',
];
if (contentType.includes('application/json') || contentType.includes('+json')) {
return await response.json();
} else if (isBinary) {
} else if (binaryTypes.some(type => contentType.includes(type))) {
return await response.blob();
} else {
} else if (contentType.includes('multipart/form-data')) {
return await response.formData();
} else if (contentType.includes('text/')) {
return await response.text();
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/v3_angular/core/request.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const getHeaders = (config: OpenAPIConfig, options: ApiRequestOptions): O

export const getRequestBody = (options: ApiRequestOptions): unknown => {
if (options.body) {
if (options.mediaType?.includes('/json')) {
if (options.mediaType?.includes('application/json') || options.mediaType?.includes('+json')) {
return JSON.stringify(options.body);
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
return options.body;
Expand Down
22 changes: 14 additions & 8 deletions test/__snapshots__/v3_client/core/request.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptio

export const getRequestBody = (options: ApiRequestOptions): unknown => {
if (options.body !== undefined) {
if (options.mediaType?.includes('/json')) {
if (options.mediaType?.includes('application/json') || options.mediaType?.includes('+json')) {
return JSON.stringify(options.body);
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
return options.body;
Expand Down Expand Up @@ -213,15 +213,21 @@ export const getResponseBody = async (response: Response): Promise<unknown> => {
try {
const contentType = response.headers.get('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json'];
const binaryTypes = ['audio/', 'image/', 'video/'];
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
if (isJSON) {
const binaryTypes = [
'application/octet-stream',
'application/pdf',
'application/zip',
'audio/',
'image/',
'video/',
];
if (contentType.includes('application/json') || contentType.includes('+json')) {
return await response.json();
} else if (isBinary) {
} else if (binaryTypes.some(type => contentType.includes(type))) {
return await response.blob();
} else {
} else if (contentType.includes('multipart/form-data')) {
return await response.formData();
} else if (contentType.includes('text/')) {
return await response.text();
}
}
Expand Down
22 changes: 14 additions & 8 deletions test/__snapshots__/v3_enums_typescript/core/request.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptio

export const getRequestBody = (options: ApiRequestOptions): unknown => {
if (options.body !== undefined) {
if (options.mediaType?.includes('/json')) {
if (options.mediaType?.includes('application/json') || options.mediaType?.includes('+json')) {
return JSON.stringify(options.body);
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
return options.body;
Expand Down Expand Up @@ -213,15 +213,21 @@ export const getResponseBody = async (response: Response): Promise<unknown> => {
try {
const contentType = response.headers.get('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json'];
const binaryTypes = ['audio/', 'image/', 'video/'];
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
if (isJSON) {
const binaryTypes = [
'application/octet-stream',
'application/pdf',
'application/zip',
'audio/',
'image/',
'video/',
];
if (contentType.includes('application/json') || contentType.includes('+json')) {
return await response.json();
} else if (isBinary) {
} else if (binaryTypes.some(type => contentType.includes(type))) {
return await response.blob();
} else {
} else if (contentType.includes('multipart/form-data')) {
return await response.formData();
} else if (contentType.includes('text/')) {
return await response.text();
}
}
Expand Down
22 changes: 14 additions & 8 deletions test/__snapshots__/v3_experimental/core/request.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptio

export const getRequestBody = (options: ApiRequestOptions): unknown => {
if (options.body !== undefined) {
if (options.mediaType?.includes('/json')) {
if (options.mediaType?.includes('application/json') || options.mediaType?.includes('+json')) {
return JSON.stringify(options.body);
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
return options.body;
Expand Down Expand Up @@ -213,15 +213,21 @@ export const getResponseBody = async (response: Response): Promise<unknown> => {
try {
const contentType = response.headers.get('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json'];
const binaryTypes = ['audio/', 'image/', 'video/'];
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
if (isJSON) {
const binaryTypes = [
'application/octet-stream',
'application/pdf',
'application/zip',
'audio/',
'image/',
'video/',
];
if (contentType.includes('application/json') || contentType.includes('+json')) {
return await response.json();
} else if (isBinary) {
} else if (binaryTypes.some(type => contentType.includes(type))) {
return await response.blob();
} else {
} else if (contentType.includes('multipart/form-data')) {
return await response.formData();
} else if (contentType.includes('text/')) {
return await response.text();
}
}
Expand Down
22 changes: 14 additions & 8 deletions test/__snapshots__/v3_node/core/request.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptio

export const getRequestBody = (options: ApiRequestOptions): unknown => {
if (options.body !== undefined) {
if (options.mediaType?.includes('/json')) {
if (options.mediaType?.includes('application/json') || options.mediaType?.includes('+json')) {
return JSON.stringify(options.body);
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
return options.body;
Expand Down Expand Up @@ -212,15 +212,21 @@ export const getResponseBody = async (response: Response): Promise<unknown> => {
try {
const contentType = response.headers.get('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json'];
const binaryTypes = ['audio/', 'image/', 'video/'];
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
if (isJSON) {
const binaryTypes = [
'application/octet-stream',
'application/pdf',
'application/zip',
'audio/',
'image/',
'video/',
];
if (contentType.includes('application/json') || contentType.includes('+json')) {
return await response.json();
} else if (isBinary) {
} else if (binaryTypes.some(type => contentType.includes(type))) {
return await response.blob();
} else {
} else if (contentType.includes('multipart/form-data')) {
return await response.formData();
} else if (contentType.includes('text/')) {
return await response.text();
}
}
Expand Down
22 changes: 14 additions & 8 deletions test/__snapshots__/v3_options/core/request.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptio

export const getRequestBody = (options: ApiRequestOptions): unknown => {
if (options.body !== undefined) {
if (options.mediaType?.includes('/json')) {
if (options.mediaType?.includes('application/json') || options.mediaType?.includes('+json')) {
return JSON.stringify(options.body);
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
return options.body;
Expand Down Expand Up @@ -213,15 +213,21 @@ export const getResponseBody = async (response: Response): Promise<unknown> => {
try {
const contentType = response.headers.get('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json'];
const binaryTypes = ['audio/', 'image/', 'video/'];
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
const isBinary = binaryTypes.some(type => contentType.toLowerCase().startsWith(type));
if (isJSON) {
const binaryTypes = [
'application/octet-stream',
'application/pdf',
'application/zip',
'audio/',
'image/',
'video/',
];
if (contentType.includes('application/json') || contentType.includes('+json')) {
return await response.json();
} else if (isBinary) {
} else if (binaryTypes.some(type => contentType.includes(type))) {
return await response.blob();
} else {
} else if (contentType.includes('multipart/form-data')) {
return await response.formData();
} else if (contentType.includes('text/')) {
return await response.text();
}
}
Expand Down
6 changes: 2 additions & 4 deletions test/__snapshots__/v3_xhr/core/request.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptio

export const getRequestBody = (options: ApiRequestOptions): unknown => {
if (options.body !== undefined) {
if (options.mediaType?.includes('/json')) {
if (options.mediaType?.includes('application/json') || options.mediaType?.includes('+json')) {
return JSON.stringify(options.body);
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
return options.body;
Expand Down Expand Up @@ -218,9 +218,7 @@ export const getResponseBody = (xhr: XMLHttpRequest): unknown => {
try {
const contentType = xhr.getResponseHeader('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json'];
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
if (isJSON) {
if (contentType.includes('application/json') || contentType.includes('+json')) {
return JSON.parse(xhr.responseText);
} else {
return xhr.responseText;
Expand Down