0.2.2 • Published 5 years ago

create-api-client v0.2.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Create API Client

Use the popular path syntax /enpoint/:id to create api client

npm version Build Status Greenkeeper badge Coverage Status

Install

npm install create-api-client

Usage

import { createClient } from 'create-api-client';

// create client
const getEndpoint = createClient("GET /api/endpoint/:id");

// request a fetch
getEndpoint({id: 1})

How it works

createClient use path-to-regex to parse your path, find every segment of path which started with : and create an api client.

const postComment = createClient("POST /api/post/:postId/comment/:commentId");

The created api client would take the key which is path params (postId, commentId) from passed in object to build url to request to, and pass the rest to the fetch client.

If you prefix path with HTTP methods, the created api client would also pass it. So the following call

postComment({postId: 10, commentId: 100, body: {message: 1000}})

is same as

fetch("/api/post/10/comment/100", {method: "POST", body: {message: 1000}})

License

MIT