Use the popular path syntax
/enpoint/:id
to create api client
npm install create-api-client --save
import { createClient } from 'create-api-client';
// create client
const getEndpoint = createClient("GET /api/endpoint/:id");
// request a fetch
getEndpoint({id: 1})
createClient
use path-to-regex to parse your path, find every segment which started with :
as parameters and create an api client.
const getComment = createClient("/api/post/:postId/comment/:commentId");
The created api client would take the key which is path params (postId
, commentId
) from passed in params to build URL and call fetch client(defaults to global variable fetch
) as the first argument.
getComment({postId: 10, commentId: 100})
// same as
// fetch("/api/post/10/comment/100")
If you provide query
in params, the query string would generate from it and append to url.
getComment({postId: 10, commentId: 100, query: {latest: 10}})
// same as
// fetch("/api/post/10/comment/100?latest=10")
You can change HTTP method by prefix path with HTTP method name.
const getComment = createClient("POST /api/post/:postId");
The created api client would pass it as method
in second argument.
postComment({postId: 10})
// same as
// fetch("/api/post/10", { method: 'POST'})
Finally, all other params you passed to the API client would pass to fetch client as it is. Which you can put header, body, ...etc.
postComment({
postId: 10,
headers = {
Accept: "application/json"
},
body: {
message: 1000
}
}
)
// same as
// fetch("/api/post/10", {
// method: "POST",
// headers: {
// Accept: "application/json"
// },
// body: {
// message: 1000
// }
// })
createClient(path, config)
- (Function): Returns created API client.
path
: path to use path-to-regex to parse and compileconfig
(Object) : Config object to customize api clientconfig.fetchClient
(function): The function to issue request. Defaults to global variablefetch
.
createClient.config(config)
config
(Object) : Config object to customize api client
- (Function): Returns a new createClient preseted with provided config.
createClient.map(pathMapping)
pathMapping
(Object) : An mapping of method name and path. e.g.{ getUser: 'GET /user', removeUser: 'DELETE /user/:id' }
- (Object): Returns a createClient mapping Object. e.g.
{ getUser: [createClient Function], removeUser: [createClient Function] }
MIT