api-map
v1.0.3
Published
A tool for mapping your REST endpoints to any JS client
Downloads
2
Readme
API Map
By: Robby Emmert
- Define your API endpoints once, then call them with a one-liner from anywhere.
- Implement global API settings such as headers and base url.
- Switch API clients with one line of code.
Quick Start
Install ApiMap to your project with
npm install --save api-map
Set up your global API settings, where
resolver
is a function that returns a promise, given a url, method, data, and network settings (read more about resolvers here or see a list of pre-build resolvers available on NPM here).
import { Api } from 'api-map';
import resolver from './custom-resolver';
// OR
import resolver from 'api-map-fetch-resolver' // or a pre-built resolver of your choice
var api = new Api({
baseUrl: '/api',
defaultHeaders: {
contentType: 'application/json'
}
}, resolver);
- Define your endpoints
var postList = api.map({
url: '/posts',
method: 'GET'
});
-or-
var singlePost = api.map({
url: params => `/posts/${params.id}`
// Method is 'GET' by default on all endpoint definitions.
});
- Query the api in DRY fashion:
postList.request()
.then(res => console.log('result', res));
-or-
singlePost.request({ id: 123 })
.then(res => console.log('result', res));