diff --git a/src/constants.ts b/src/constants.ts index dd0a0be..aca50ce 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -14,6 +14,5 @@ export const CANCELLED_ERROR = 'CanceledError'; export const GET = 'GET'; export const POST = 'POST'; -export const PUT = 'PUT'; export const DELETE = 'DELETE'; export const HEAD = 'HEAD'; diff --git a/src/request-handler.ts b/src/request-handler.ts index 4e67b41..59f5c53 100644 --- a/src/request-handler.ts +++ b/src/request-handler.ts @@ -40,11 +40,9 @@ import { CANCELLED_ERROR, CHARSET_UTF_8, CONTENT_TYPE, - DELETE, GET, HEAD, OBJECT, - PUT, STRING, UNDEFINED, } from './constants'; @@ -180,23 +178,25 @@ export function createRequestHandler( method: string, body?: unknown, ): void => { - if (!body && [PUT, DELETE].includes(method)) { + if (!body && ['PUT', 'DELETE'].includes(method)) { return; - } - - const contentTypeValue = APPLICATION_JSON + ';' + CHARSET_UTF_8; + } else { + const contentTypeValue = APPLICATION_JSON + ';' + CHARSET_UTF_8; - if (headers instanceof Headers) { - if (!headers.has(CONTENT_TYPE)) { - headers.set(CONTENT_TYPE, contentTypeValue); + if (headers instanceof Headers) { + if (!headers.has(CONTENT_TYPE)) { + headers.set(CONTENT_TYPE, contentTypeValue); + } + } else if ( + typeof headers === OBJECT && + !Array.isArray(headers) && + !headers[CONTENT_TYPE] + ) { + headers[CONTENT_TYPE] = contentTypeValue; } - } else if ( - typeof headers === OBJECT && - !Array.isArray(headers) && - !headers[CONTENT_TYPE] - ) { - headers[CONTENT_TYPE] = contentTypeValue; } + + return; }; /** diff --git a/test/request-handler.spec.ts b/test/request-handler.spec.ts index d1c18c0..787b74a 100644 --- a/test/request-handler.spec.ts +++ b/test/request-handler.spec.ts @@ -13,10 +13,8 @@ import { APPLICATION_JSON, CHARSET_UTF_8, CONTENT_TYPE, - DELETE, GET, POST, - PUT, } from '../src/constants'; import { ResponseErr } from '../src/response-error'; @@ -295,10 +293,10 @@ describe('Request Handler', () => { }); describe.each([ - { method: DELETE, body: undefined, expectContentType: false }, - { method: PUT, body: undefined, expectContentType: false }, - { method: DELETE, body: { foo: 'bar' }, expectContentType: true }, - { method: PUT, body: { foo: 'bar' }, expectContentType: true }, + { method: 'DELETE', body: undefined, expectContentType: false }, + { method: 'PUT', body: undefined, expectContentType: false }, + { method: 'DELETE', body: { foo: 'bar' }, expectContentType: true }, + { method: 'PUT', body: { foo: 'bar' }, expectContentType: true }, { method: POST, body: undefined, expectContentType: true }, { method: GET, body: undefined, expectContentType: true }, ])( @@ -323,7 +321,7 @@ describe('Request Handler', () => { }, ); - describe.each([DELETE, PUT])( + describe.each(['DELETE', 'PUT'])( '%s method with custom Content-Type', (method) => { it(`should keep custom Content-Type for ${method} method`, () => {