| 1234567891011121314151617181920212223242526272829 |
- const wrap = (fetch, options) => {
- const fn = async (url, body) => {
- const headers = {
- 'Accepts': 'application/json'
- }
- if (body) headers['Content-Type'] = 'application/json'
- const result = await fetch(url, {
- ...options,
- headers: {
- ...headers,
- ...(options && options.headers)
- },
- body: body && JSON.stringify(body)
- })
- return await result.json()
- }
- Object.defineProperties(fn, {
- get: { get: () => wrap(fetch, { ...options, method: 'GET' }) },
- post: { get: () => wrap(fetch, { ...options, method: 'POST' }) },
- put: { get: () => wrap(fetch, { ...options, method: 'PUT' }) },
- patch: { get: () => wrap(fetch, { ...options, method: 'PATCH' }) },
- delete: { get: () => wrap(fetch, { ...options, method: 'DELETE' }) }
- })
- return fn
- }
- const api = wrap(fetch)
- module.exports = api
|