Skip to content

Commit

Permalink
Remove unnecessary method validation on the server http client
Browse files Browse the repository at this point in the history
  • Loading branch information
d-gubert committed Oct 3, 2023
1 parent 3fd0bc4 commit 5d3d4d9
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions packages/server-fetch/src/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
import type { ExtendedFetchOptions, FetchOptions, OriginalFetchOptions } from './types';

function isPostOrPutOrDeleteWithBody(options?: ExtendedFetchOptions): boolean {
// No method === 'get'
if (!options?.method) {
return false;
}
const { method, body } = options;
const lowerMethod = method?.toLowerCase();
return ['post', 'put', 'delete'].includes(lowerMethod) && body != null;
}

const jsonParser = (options: ExtendedFetchOptions) => {
if (!options) {
return {};
}

if (isPostOrPutOrDeleteWithBody(options)) {
try {
if (options && typeof options.body === 'object' && !Buffer.isBuffer(options.body)) {
options.body = JSON.stringify(options.body);
options.headers = {
'Content-Type': 'application/json',
...options.headers,
};
}
} catch (e) {
// Body is not JSON, do nothing
try {
if (typeof options.body === 'object' && !Buffer.isBuffer(options.body)) {
options.body = JSON.stringify(options.body);
options.headers = {
...options.headers,
'Content-Type': 'application/json', // force content type to be json
};
}
} catch (e) {
// Body is not JSON, do nothing
}

return options as FetchOptions;
Expand Down

0 comments on commit 5d3d4d9

Please sign in to comment.