Skip to content

Commit

Permalink
chore: Removed DELETE, PUT constants for saving couple of bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
red-game-dev committed Nov 15, 2024
1 parent 60ed7c1 commit e4eb311
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
30 changes: 15 additions & 15 deletions src/request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ import {
CANCELLED_ERROR,
CHARSET_UTF_8,
CONTENT_TYPE,
DELETE,
GET,
HEAD,
OBJECT,
PUT,
STRING,
UNDEFINED,
} from './constants';
Expand Down Expand Up @@ -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;
};

/**
Expand Down
12 changes: 5 additions & 7 deletions test/request-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 },
])(
Expand All @@ -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`, () => {
Expand Down

0 comments on commit e4eb311

Please sign in to comment.