https://github.com/single9/api-wrapper
Call your RESTful APIs like a function.
https://github.com/single9/api-wrapper
api http npm npm-package restful-api wrapper
Last synced: about 2 months ago
JSON representation
Call your RESTful APIs like a function.
- Host: GitHub
- URL: https://github.com/single9/api-wrapper
- Owner: single9
- License: mit
- Created: 2021-10-21T09:02:56.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-15T02:10:09.000Z (7 months ago)
- Last Synced: 2024-10-16T09:06:25.259Z (7 months ago)
- Topics: api, http, npm, npm-package, restful-api, wrapper
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@single9/api-wrapper
- Size: 298 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
API Wrapper
===========[](https://codecov.io/gh/single9/api-wrapper)
Define and Call your restful APIs like a function.
This package is based on [`@single9/api-tester`](https://www.npmjs.com/package/@single9/api-tester) but use [Axios](https://www.npmjs.com/package/axios) instead of [Request](https://www.npmjs.com/package/request).
Installation
============npm i @single9/api-wrapper
Usage
=====```js
const ApiWrapper = require('@single9/api-wrapper');
```Create
------```js
const api = new ApiWrapper([
{
name: '', // only allow certain words and digits
path: '', // e.g. /api/posts
method: '', // e.g. post or POST
},
], {
configureAxios(axios){
// The axios you can add interceptors or global functions.
},
baseUrl: '', // e.g. https://jsonplaceholder.typicode.com
// Default: http://localhost:3000
headers: {
// The headers you want to send. e.g. 'authorization': 'Bearer SAdoweasd...',
},
auth: { // authorization
username: 'username',
password: 'password',
}
})
```### Factory `baseUrl`
You can use factory function to dynamically set the base URL. This is useful if your host domain is
a SRV record.**Example**
```js
const api = new ApiWrapper([
{
name: '', // only allow certain words and digits
path: '', // e.g. /api/posts
method: '', // e.g. post or POST
},
], {
baseUrl: async () => resolveSRV(process.env.API_HOST),
});
```Use
---api.(params)
- **api**: Your `ApiWrapper` instance.
- **api_name**: The name of the API that you set before.
- **params**: Compatible with [axios request config](https://axios-http.com/docs/req_config)
- queryString
- pathParams### Params
#### params.queryStringUsed for query string. e.g. /users?limit=100
```js
api.test({
queryString: {
key: value
}
})
``````js
api.test({
queryString: [
{
name: string,
value: string | number,
}
]
})
```#### params.pathParams
Used for path parameters. e.g. /user/:id
```js
api.test({
pathParams: {
key: value
}
})
``````js
api.test({
pathParams: [
{
name: string,
value: string | number,
}
]
})
```Example
-------```js
const ApiWrapper = require('@single9/api-wrapper');// Create your API schema
const schema = [
{
name: 'newPost', // this is your api function name
path: '/posts',
method: 'post',
},
{
name: 'getTodo',
path: '/todos/:todoId', // path parameter
method: 'get',
},
];const api = new ApiWrapper(schema, {
configureAxios(item){
item.interceptors.request.use(
(request) => { console.log('url: %s , req: %o', request.url); return request; },
)
item.interceptors.response.use(
(response) => { console.log('url: %s , res: %o', response.url, response.data); return response; },
)
},
baseUrl: 'https://jsonplaceholder.typicode.com',
});async function start() {
try {
const post = await api.newPost({
// Post Body
data: {
title: 'foo!!!!!!',
body: 'bar!!',
userId: 1
},
});console.log(post.data);
const get = await api.getTodo({
pathParams: {
todoId: 2, // replace `:todoId` with value 2.
},
});console.log(get.data);
} catch (err) {
console.error(err);
}
}start();
```