make-request-factory

1.0.1 • Public • Published

makeRequestFactory

import * as makeRequestFactory from "@jiasuyunkeji-fe/make-request-factory";

const makeRequest = makeRequestFactory({
    prefix: 'https://httpbin.org',
    debug: true, //  debug mode, will print request and response data
    mocks: { // api mocks
        'get /api/message': { message: 'hello' },
        'post /api/model/:id': (req, res) => {
            res.send({ id: req.params.id });
        }
    },
    preFetch: async options => withAuthorization(options)
});
const response = await makeRequest('post', '/anything/:id')({
    headers: { 'X-Key': 'abc' },
    params: { id: 32 }, // path params, /anything/:id => /anything/32
    query: { pageSize: 10 }, // query params,/anything/32 => /anything/32?pageSize=10
    body: { k: 'v' }, // body data

    mode: 'cors', // no-cors, cors, *same-origin
    cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
    credentials: 'same-origin', // include, *same-origin, omit
    headers: {
        'Content-Type': 'application/json',
        // 'Content-Type': 'application/x-www-form-urlencoded',
    },
    redirect: 'follow', // manual, *follow, error
    referrer: 'no-referrer', // no-referrer, *client

    timeout: 10000, // timeout in ms
    responseType: 'json', // *json, string, formData, blob, arrayBuffer
    responseAll: 'false', // returns not only body, but also status, headers
});

Readme

Keywords

none

Package Sidebar

Install

npm i make-request-factory

Weekly Downloads

6

Version

1.0.1

License

ISC

Unpacked Size

14 kB

Total Files

6

Last publish

Collaborators

  • cqzs