piral-fetch
Advanced tools
Comparing version 0.9.0-pre.746 to 0.9.0-pre.751
@@ -9,9 +9,33 @@ "use strict"; | ||
function createFetchApi(config = {}) { | ||
return () => ({ | ||
fetch(path, options = {}) { | ||
return fetch_1.httpFetch(config, path, options); | ||
}, | ||
}); | ||
return context => { | ||
return { | ||
fetch(path, options = {}) { | ||
const originalHeaders = options.headers || {}; | ||
const headerPromises = []; | ||
context.emit('before-fetch', { | ||
headers: originalHeaders, | ||
method: options.method || 'GET', | ||
target: path, | ||
setHeaders(headers) { | ||
if (headers) { | ||
headerPromises.push(headers); | ||
} | ||
}, | ||
}); | ||
return Promise.all(headerPromises) | ||
.then(newHeaders => { | ||
const headers = newHeaders.reduce((obj, header) => { | ||
if (typeof header === 'object' && header) { | ||
return Object.assign({}, obj, header); | ||
} | ||
return obj; | ||
}, originalHeaders); | ||
return Object.assign({}, options, { headers }); | ||
}) | ||
.then(options => fetch_1.httpFetch(config, path, options)); | ||
}, | ||
}; | ||
}; | ||
} | ||
exports.createFetchApi = createFetchApi; | ||
//# sourceMappingURL=create.js.map |
{ | ||
"name": "piral-fetch", | ||
"version": "0.9.0-pre.746", | ||
"version": "0.9.0-pre.751", | ||
"description": "Extensions for standardizing fetch in Piral.", | ||
@@ -39,3 +39,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"piral-core": "^0.9.0-pre.746" | ||
"piral-core": "^0.9.0-pre.751" | ||
}, | ||
@@ -47,3 +47,3 @@ "peerDependencies": { | ||
}, | ||
"gitHead": "d79bf9b30d1f80473bbd6cb8225cbb1156ecb7d7" | ||
"gitHead": "461d07d5b5d1f7a3d9512b933f065ea100762889" | ||
} |
@@ -7,5 +7,6 @@ import 'url-polyfill'; | ||
it('works with default options against a JSON API', async () => { | ||
const context = { emit: jest.fn() } as any; | ||
const { fetch } = createFetchApi({ | ||
base: 'https://jsonplaceholder.typicode.com', | ||
})(undefined) as any; | ||
})(context) as any; | ||
const result = await fetch('users').then(m => m.body); | ||
@@ -18,5 +19,6 @@ expect(Array.isArray(result)).toBeTruthy(); | ||
it('interprets the result as text if explicitly used despite JSON API', async () => { | ||
const context = { emit: jest.fn() } as any; | ||
const { fetch } = createFetchApi({ | ||
base: 'https://jsonplaceholder.typicode.com', | ||
})(undefined) as any; | ||
})(context) as any; | ||
const result = await fetch('users', { result: 'text' }).then(m => m.body); | ||
@@ -28,5 +30,6 @@ expect(typeof result).toBe('string'); | ||
it('has the correct response code', async () => { | ||
const context = { emit: jest.fn() } as any; | ||
const { fetch } = createFetchApi({ | ||
base: 'https://jsonplaceholder.typicode.com', | ||
})(undefined) as any; | ||
})(context) as any; | ||
const result = await fetch('users').then(m => m.code); | ||
@@ -37,5 +40,6 @@ expect(result).toBe(200); | ||
it('works with default options against a non-JSON API', async () => { | ||
const context = { emit: jest.fn() } as any; | ||
const { fetch } = createFetchApi({ | ||
base: 'https://cdn.animenewsnetwork.com/encyclopedia/', | ||
})(undefined) as any; | ||
})(context) as any; | ||
const result = await fetch('api.xml?anime=4658').then(m => m.body); | ||
@@ -42,0 +46,0 @@ expect(result.substr(0, 5)).toBe(`<ann>`); |
@@ -11,7 +11,40 @@ import { Extend } from 'piral-core'; | ||
export function createFetchApi(config: FetchConfig = {}): Extend<PiletFetchApi> { | ||
return () => ({ | ||
fetch<T>(path, options = {}) { | ||
return httpFetch<T>(config, path, options); | ||
}, | ||
}); | ||
return context => { | ||
return { | ||
fetch<T>(path, options: any = {}) { | ||
const originalHeaders = options.headers || {}; | ||
const headerPromises: Array<Promise<any>> = []; | ||
context.emit('before-fetch', { | ||
headers: originalHeaders, | ||
method: options.method || 'GET', | ||
target: path, | ||
setHeaders(headers: Promise<any> | any) { | ||
if (headers) { | ||
headerPromises.push(headers); | ||
} | ||
}, | ||
}); | ||
return Promise.all(headerPromises) | ||
.then(newHeaders => { | ||
const headers = newHeaders.reduce((obj, header) => { | ||
if (typeof header === 'object' && header) { | ||
return { | ||
...obj, | ||
...header, | ||
}; | ||
} | ||
return obj; | ||
}, originalHeaders); | ||
return { | ||
...options, | ||
headers, | ||
}; | ||
}) | ||
.then(options => httpFetch<T>(config, path, options)); | ||
}, | ||
}; | ||
}; | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18869
372