Skip to content

Commit

Permalink
fix: Removed some strings to have few bytes less
Browse files Browse the repository at this point in the history
  • Loading branch information
red-game-dev committed Oct 27, 2024
1 parent 6780700 commit 60ed7c1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ export const TIMEOUT_ERROR = 'TimeoutError';
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';
10 changes: 7 additions & 3 deletions src/request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ import {
CANCELLED_ERROR,
CHARSET_UTF_8,
CONTENT_TYPE,
DELETE,
GET,
HEAD,
OBJECT,
PUT,
STRING,
UNDEFINED,
} from './constants';
Expand Down Expand Up @@ -178,7 +180,9 @@ export function createRequestHandler(
method: string,
body?: unknown,
): void => {
if (!body && ['PUT', 'DELETE'].includes(method)) return;
if (!body && [PUT, DELETE].includes(method)) {
return;
}

const contentTypeValue = APPLICATION_JSON + ';' + CHARSET_UTF_8;

Expand All @@ -187,7 +191,7 @@ export function createRequestHandler(
headers.set(CONTENT_TYPE, contentTypeValue);
}
} else if (
typeof headers === 'object' &&
typeof headers === OBJECT &&
!Array.isArray(headers) &&
!headers[CONTENT_TYPE]
) {
Expand Down Expand Up @@ -270,8 +274,8 @@ export function createRequestHandler(
credentials,
body,
method,
url: baseURL + urlPath,
headers,
url: baseURL + urlPath,
};
};

Expand Down
29 changes: 19 additions & 10 deletions test/request-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ import type {
RequestHandlerReturnType,
} from '../src/types/request-handler';
import { fetchf } from '../src';
import { ABORT_ERROR, APPLICATION_JSON, CHARSET_UTF_8 } from '../src/constants';
import {
ABORT_ERROR,
APPLICATION_JSON,
CHARSET_UTF_8,
CONTENT_TYPE,
DELETE,
GET,
POST,
PUT,
} from '../src/constants';
import { ResponseErr } from '../src/response-error';

jest.mock('../src/utils', () => {
Expand Down Expand Up @@ -286,12 +295,12 @@ 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: 'POST', body: undefined, expectContentType: true },
{ method: 'GET', body: undefined, 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 },
])(
'$method request with body: $body',
({ method, body, expectContentType }) => {
Expand All @@ -303,18 +312,18 @@ describe('Request Handler', () => {
const result = requestHandler.buildConfig(apiUrl, { method, body });
if (expectContentType) {
expect(result.headers).toHaveProperty(
'Content-Type',
CONTENT_TYPE,
contentTypeValue,
);
} else {
expect(result.headers).not.toHaveProperty('Content-Type');
expect(result.headers).not.toHaveProperty(CONTENT_TYPE);
}
},
);
},
);

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 60ed7c1

Please sign in to comment.