# @pixcai/make-api

> Make a promise-based API use RESTful style

Latest version **0.0.5** (published 2020-01-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install @pixcai/make-api
pnpm add @pixcai/make-api
yarn add @pixcai/make-api
bun add @pixcai/make-api
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.5 |
| Published | 2020-01-06 |
| First published | 2019-03-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 127.1 KB |
| Known vulnerabilities | 0 (+24 in 1 direct dependencies) |
| Install scripts | no |
| Author | pixcai |
| Maintainers | pixcai |
| Keywords | api, axios |

## Links

- npm: https://www.npmjs.com/package/@pixcai/make-api
- npm.io page: https://npm.io/package/@pixcai/make-api

## Dependencies (1)

- [axios](https://npm.io/package/axios.md) ^0.18.0

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.0.5 (latest) — 2020-01-06
- 0.0.4 — 2019-04-30
- 0.0.3 — 2019-03-20
- 0.0.2 — 2019-03-19
- 0.0.1 — 2019-03-15

## README

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:
```js
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`:
```js
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:
```js
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:
```js
import API, { POST } from '@pixcai/make-api';

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

const user = {
  register: POST('/user/register'),
};
```
```js
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

---
_Source: https://npm.io/package/@pixcai/make-api · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
