isomorphic-api-client
Simple, minimalistic, isomorphic generic client to make requests to an api
Installation
npm install --save isomorphic-api-client
Usage
First configure the api communication, you need do it once
// our API url is: https://myhost.com:3000/api/v2
make request to the api:
const skirtl = username: 'squirtlesquirtle' email: 'fake@badmail.moc' const squirtle = email: 'squirtle@pokemon.wl' let apiClient = ; let user = await apiClientlet theNew = await apiClienttheNew = await apiClientawait apiClient
or you can do it more easy:
const Users = '/users' let user = await Users // GET https://myhost.com:3000/api/v2/users/1let theNew = await Users // POST https://myhost.com:3000/api/v2/userstheNew = await apiClient // UPDATE https://myhost.com:3000/api/v2/users/2await apiClient // DELETE https://myhost.com:3000/api/v2/users/2
And you can do it with Auth:
const Users = '/users'let apiClient = try let user = await Users // GET https://myhost.com:3000/api/v2/users/1/secret and FAIL! catch error if error instanceof ClientError && errorcode == 401 // do it log in let token = await apiClient // now get the token apiClient //set the token let user = await Users // GET https://myhost.com:3000/api/v2/users/1/secret and.. EUREKA!!
ToDo:
- Tests and readme for Auth token requests
- Create most common Error Clases (NotFound, Unauthorized, InternalServerError, ...)
- Methods for special cases, like catch Auth token of headers and not of body
- Methods to simplify isomorphic Auth
License
MIT Licensed. Copyright (c) Futurecommerce 2016.