lafetch
Very light modular replacement for libs like axios, based on the fetch API.
- Binary data support and full tests are coming soon. *
USAGE
// It's a full toolkit with types. Importing only Fetch is fine. // Default values.const api = // Automatically turns response into an object. // Otherwise, returns raw response from fetch API: // do `await response.json() to get it. json?: true // Common base url. E.g. your api domain. base?: '/' // Object with headers. headers?: {} // List of async middlewares executed from last to first // procesing queries. middleware?: in?: // ({query, response}) => Promise<{query, response}> out?: // (query: Query) => Promise<Query> // fetch adapter. Defaults to the window fetch. // In node can work with https://www.npmjs.com/package/node-fetch // But there many of config fields are not implemented, i.e. credentials. adapter?: fetch credentials?: 'omit' | 'same-origin' | 'include' // fetch API credentials field. throwCodes?: /5../ // HTTP status codes to throw. Defaults to /\n/ (no throws at all). handleArrays?: '[]' // querystring: arr[]=1&arr[]=2&... vs arr=1,2,...
Query is of type
Real example:
// addHeaders helper is curried.const addDevHeaders = async query const api = base: 'https://api.example.com/' middleware: out: // to a server. addDevHeaders in: // from a server. // You can handle error status codes, for instance. async query response // Argument is a Query.const data = await api const response = await api