Skip to content

Commit

Permalink
perf: Decrease size of final bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
MattCCC committed Sep 30, 2024
1 parent 70aea50 commit 22eee06
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 12 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@
"size-limit": [
{
"path": "dist/browser/index.mjs",
"limit": "10 KB"
"limit": "5 KB"
},
{
"path": "dist/browser/index.global.js",
"limit": "10 KB"
"limit": "5 KB"
},
{
"path": "dist/node/index.js",
"limit": "10 KB"
"limit": "5 KB"
}
],
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/cache-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { hash } from './hash';
import { fetchf } from './index';
import type { FetcherConfig } from './types/request-handler';
import type { CacheEntry } from './types/cache-manager';
import { GET, OBJECT, UNDEFINED } from './const';
import { GET, OBJECT, UNDEFINED } from './constants';
import { shallowSerialize, sortObject } from './utils';

const cache = new Map<string, CacheEntry<any>>();
Expand Down
4 changes: 3 additions & 1 deletion src/const.ts → src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const APPLICATION_JSON = 'application/json';
export const APPLICATION_CONTENT_TYPE = 'application/';

export const APPLICATION_JSON = APPLICATION_CONTENT_TYPE + 'json';
export const CONTENT_TYPE = 'Content-Type';

export const UNDEFINED = 'undefined';
Expand Down
2 changes: 1 addition & 1 deletion src/queue-manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ABORT_ERROR, TIMEOUT_ERROR } from './const';
import { ABORT_ERROR, TIMEOUT_ERROR } from './constants';
import type { RequestConfig } from './types';
import type { QueueItem, RequestsQueue } from './types/queue-manager';

Expand Down
2 changes: 1 addition & 1 deletion src/request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
OBJECT,
STRING,
UNDEFINED,
} from './const';
} from './constants';
import { parseResponseData } from './response-parser';
import { generateCacheKey, getCache, setCache } from './cache-manager';

Expand Down
14 changes: 11 additions & 3 deletions src/response-parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { APPLICATION_JSON, CONTENT_TYPE } from './const';
import {
APPLICATION_CONTENT_TYPE,
APPLICATION_JSON,
CONTENT_TYPE,
} from './constants';
import type { DefaultResponse, FetchResponse } from './types/request-handler';

/**
Expand Down Expand Up @@ -30,9 +34,13 @@ export async function parseResponseData<ResponseData = DefaultResponse>(
data = await response.json(); // Parse JSON response
} else if (contentType.includes('multipart/form-data')) {
data = await response.formData(); // Parse as FormData
} else if (contentType.includes('application/octet-stream')) {
} else if (
contentType.includes(APPLICATION_CONTENT_TYPE + 'octet-stream')
) {
data = await response.blob(); // Parse as blob
} else if (contentType.includes('application/x-www-form-urlencoded')) {
} else if (
contentType.includes(APPLICATION_CONTENT_TYPE + 'x-www-form-urlencoded')
) {
data = await response.formData(); // Handle URL-encoded forms
} else if (contentType.includes('text/')) {
data = await response.text(); // Parse as text
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { OBJECT, STRING, UNDEFINED } from './const';
import { OBJECT, STRING, UNDEFINED } from './constants';
import type {
DefaultUrlParams,
HeadersObject,
Expand Down
2 changes: 1 addition & 1 deletion test/request-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
RequestHandlerReturnType,
} from '../src/types/request-handler';
import { fetchf } from '../src';
import { ABORT_ERROR } from '../src/const';
import { ABORT_ERROR } from '../src/constants';
import { ResponseErr } from '../src/response-error';

jest.mock('../src/utils', () => {
Expand Down

0 comments on commit 22eee06

Please sign in to comment.