axios-template-literal
v1.0.2
Published
Ufff, sorry, too many sugar for axios.
Downloads
2
Maintainers
Readme
axios-template-literal
Install
npm install axios-template-literal
yarn add axios-template-literal
Usage
const Axios = require("axios");
const createAxiosTemplateLiteral = require("axios-template-literal");
/**
* Create you default axios instance
*/
const instance = Axios.create({
baseURL: "https://gorest.co.in/public-api/",
timeout: 5000,
headers: { "X-Custom-Header": "foobar" },
});
/**
* Pass axios instance to axiosTemplateLiteral function
*/
const axios = createAxiosTemplateLiteral(instance);
/**
* Make your request!
*/
axios`
GET /users
${JSON.stringify({ page: 5 })}
`.then((res) => console.log(res));
/**
* Or you can do it like that, predefining the request method
*/
axios.get`
/users
{ "page": 5 }
`.then((res) => console.log(res));
/**
* For the typescript support you can pass return type of request promise
*/
type YouCustomType = string;
axios<YouCustomType>`
GET /users
${JSON.stringify({ page: 5 })}
`.then((res) => console.log(res));
axios.get<YouCustomType>`
GET /users
${JSON.stringify({ page: 5 })}
`.then((res) => console.log(res));
Acknowledgement
This library was inspired by http-template-literal by @pfrazee.
License
Licensed under the MIT License. Check the LICENSE file for details.