项目作者: MihaiBalint

项目描述 :
Client for REST APIs (javascript)
高级语言: JavaScript
项目地址: git://github.com/MihaiBalint/crest-js.git
创建时间: 2019-01-09T05:20:34Z
项目社区:https://github.com/MihaiBalint/crest-js

开源协议:MIT License

下载


crest-js

Crest JS is a small and friendly Client for REST APIs. Unlike most HTTP clients
out there, Crest does not accept URLs as strings. URLs are instead constructed with
Javascript Proxies and an opinionated convention for method names. This makes your
code more readable as you no longer have to understand the complex string shuffling
commonly associated with building URLs.

Inspired by this medium article

Check out the awesome examples below

  1. const { crest } = require('crest-js');
  2. // Let's play with a contrived API for a database of companies and their staff
  3. const api = crest({ baseUrl: 'https://api.example.com' })
  4. .authorizationBasic('your-secret-here');

Let’s try a simple HTTP request

  1. api
  2. .getCompaniesBranches('11335577')
  3. // translates to: GET /companies/11335577/branches
  4. .then((branches) => {
  5. console.log(', '.join(branches.map((branch) => branch.location)));
  6. });

Now let’s update the location for a company branch

  1. api
  2. .putCompaniesBranches('11335577', '2468', {json: {location: '186 1st Avenue, NY'} })
  3. // translates to: PUT /companies/11335577/branches/2468
  4. // payload: { "location": "186 1st Avenue, NY" }

And finally let’s send a reminder to all staff accounts from a company branch.

  1. const the_message = 'Remember to announce your time off before EOB today.';
  2. api
  3. .getCompaniesStaff('11335577', {branch_id: '2468'})
  4. // translates to: GET /companies/11335577/staff?branch_id=2468
  5. .then((staff) => {
  6. return Promise.all(staff.map((member) => {
  7. return api.postCompaniesStaffMessages('11335577', member.id, {json: {message: the_message}});
  8. // translates to: POST /companies/11335577/staff/{id}/messages
  9. // payload: { "message": "Remember to announce..." }
  10. }))
  11. .then(() => {
  12. console.log('Message sent.');
  13. });
  14. });

Alternatively we could do something with the github API using async/await

  1. const github = crest({ baseUrl: 'https://api.github.com' })
  2. .authorizationBasic('your-secret-here');
  3. const orgs = await github.getUsersOrgs('MihaiBalint');
  4. // translates to: GET /users/MihaiBalint/orgs
  5. const repos = await github.getUsersRepos('MihaiBalint');
  6. // translates to: GET /users/MihaiBalint/repos
  7. // POST /authorizations
  8. const auth = await github.postAuthorizations({ json: { 'scopes': ['public_repo'] } });
  9. // translates to: POST /authorizations
  10. // payload: { "scopes": ["public_repo"] }

So what actually happened there? Crest converts camel-case method names to URLs
and interpolates method arguments to obtain the url path. Query parameters and
request payloads are added using dict arguments.

Custom request headers

When you need to send some proproetary headers with every request, you could do the following:

  1. const github = crest({ baseUrl: 'https://proprietary-api.example.com' })
  2. .setCustomHeaders({ 'X-Custom-Header': 'your-proprietary-value' });
  3. const bits = await github.getBits('proprietary');
  4. // translates to: GET /bits/proprietary with http headers:
  5. // X-Custom-Header: your-proprietary-value

Installation

node:

  1. $ npm install crest-js

Running node tests

Install dependencies:

  1. $ npm install
  2. $ npm test

License

MIT