0.0.5 • Published 4 years ago

@pixcai/make-api v0.0.5

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

make-api

Make a promise-based API use RESTful style

Installing

Using npm:

npm install @pixcai/make-api -S

Using yarn:

yarn add @pixcai/make-api

Example

Basic usage:

import { GET, POST } from '@pixcai/make-api';

const user = {
  register: POST('http://localhost:3000/user/register'),
  query: GET('http://localhost:3000/user/:id'),
};

user.register({
  data: {
    username: 'admin',
    password: '123456',
  },
}).then(({ data }) => console.log(data));

user.query({}, { id: 1 }).then(({ data }) => console.log(data));

make-api default uses axios for request. We can configure axios options by API.request:

import API, { POST } from '@pixcai/make-api';

API.request.defaults.baseURL = 'http://localhost:3000';

const user = {
  register: POST('/user/register'),
};

user.register({
  data: {
    username: 'admin',
    password: '123456',
  },
}).then(({ data }) => console.log(data));

And we can use different config for different API:

import API from '@pixcai/make-api';

const userAPI = new API({ baseURL: 'http://localhost:3000' });
const user = {
  register: userAPI.POST('/user/register'),
};

const adminAPI = new API({ baseURL: 'http://localhost:8001' });
const admin = {
  register: adminAPI.POST('/register'),
};

Or custom request function:

import API, { POST } from '@pixcai/make-api';

API.request = ({ baseURL, url, ...config }) => window.fetch(baseURL + url, config);

const user = {
  register: POST('/user/register'),
};
import API from '@pixcai/make-api';

const userAPI = new API({
  baseURL: 'http://localhost:3000',
}, {
  request: ({ baseURL, url, ...config }) => window.fetch(baseURL + url, config),
});

API

DELETE(url)

GET(url)

HEAD(url)

OPTIONS(url)

PATCH(url)

POST(url)

PUT(url)

API.request(config)

new API(defaultConfig, factory = API)

Make an instance that have all the above methods and uses defaultConfig and factory.

  • defaultConfig: request config.

  • factory: An object that must have field request.

License

MIT

0.0.5

4 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago