Skip to content

Commit fd31ad0

Browse files
committed
⚡ Shrink size of module 1.18 KB → 916 B
1 parent b906abe commit fd31ad0

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> Super light-weight wrapper around `fetch`
44
5-
- [x] Only 1.18 KB minified & gziped
5+
- [x] Only 916 B when minified & gziped
66
- [x] Only native API (polyfills required)
77
- [x] TypeScript support
88
- [x] Instance with custom defaults

src/index.ts

+22-24
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,24 @@ const CONTENT_TYPES: Record<ContentTypes, string> = {
4040
blob: '*/*',
4141
}
4242

43-
class ResponseError extends Error {
44-
response: Response
45-
constructor(response: Response) {
46-
super(response.statusText)
47-
this.name = 'ResponseError'
48-
this.response = response
49-
}
43+
function ResponseError(response: Response) {
44+
return Object.assign(new Error(response.statusText), {
45+
name: 'ResponseError',
46+
response: response,
47+
})
5048
}
5149

52-
class TimeoutError extends Error {
53-
constructor() {
54-
super('Request timed out')
55-
this.name = 'TimeoutError'
56-
}
50+
function TimeoutError() {
51+
return Object.assign(new Error('Request timed out'), {
52+
name: 'TimeoutError',
53+
})
5754
}
5855

59-
const mergeOptions = (left: Options = {}, right: Options = {}) => ({
60-
...left,
61-
...right,
62-
headers: { ...left.headers, ...right.headers },
63-
})
56+
const merge = (a?: unknown, b?: unknown, c?: unknown, d?: unknown) =>
57+
Object.assign({}, a, b, c, d)
58+
59+
const mergeOptions = (left: Options = {}, right: Options = {}): Options =>
60+
merge(left, right, { headers: merge(left.headers, right.headers) })
6461

6562
function isAborted(error: Error) {
6663
return error.name === 'AbortError'
@@ -82,7 +79,7 @@ const DEFAULT_OPTIONS: Options = {
8279
return response
8380
}
8481

85-
throw new ResponseError(response)
82+
throw ResponseError(response)
8683
},
8784
onSuccess(response) {
8885
return response
@@ -93,6 +90,8 @@ const DEFAULT_OPTIONS: Options = {
9390
}
9491

9592
function request(baseResource: string, baseInit: Options): Request {
93+
const options = mergeOptions(DEFAULT_OPTIONS, baseInit)
94+
9695
const {
9796
json,
9897
params,
@@ -103,14 +102,13 @@ function request(baseResource: string, baseInit: Options): Request {
103102
onResponse,
104103
onSuccess,
105104
onFailure,
106-
...options
107-
} = mergeOptions(DEFAULT_OPTIONS, baseInit)
105+
} = options
108106

109107
const query = params == null ? '' : '?' + serialize(params)
110108
const resource = prefixUrl + baseResource + query
111109

112110
const headers = new Headers(options.headers)
113-
const init: RequestInit = { ...options, headers }
111+
const init: RequestInit = merge(options, { headers })
114112

115113
if (json != null) {
116114
init.body = JSON.stringify(json)
@@ -129,7 +127,7 @@ function request(baseResource: string, baseInit: Options): Request {
129127
const controller = new AbortController()
130128

131129
timerID = setTimeout(() => {
132-
reject(new TimeoutError())
130+
reject(TimeoutError())
133131
controller.abort()
134132
}, timeout)
135133

@@ -142,7 +140,7 @@ function request(baseResource: string, baseInit: Options): Request {
142140

143141
init.signal = controller.signal
144142
} else {
145-
timerID = setTimeout(() => reject(new TimeoutError()), timeout)
143+
timerID = setTimeout(() => reject(TimeoutError()), timeout)
146144
}
147145
}
148146

@@ -177,7 +175,7 @@ function create(baseOptions?: Options) {
177175
const createMethod = (method: RequestMethods) => (
178176
resource: string,
179177
options?: Options
180-
) => request(resource, mergeOptions(baseOptions, { method, ...options }))
178+
) => request(resource, mergeOptions(baseOptions, merge({ method }, options)))
181179

182180
const intance = {
183181
create,

0 commit comments

Comments
 (0)