@apicase/core
Advanced tools
Comparing version 0.7.0-beta.13 to 0.7.0-beta.14
@@ -128,3 +128,3 @@ import _default4 from 'nanoutils/lib/mapObjIndexed'; | ||
const payload = arguments.length ? arguments[0] : req.payload; | ||
const timeout = req.options.debounce || req.options.timer; | ||
const timeout = req.options.debounce || req.options.delay; | ||
res.payload = adapter.convert ? adapter.convert(payload) : payload; | ||
@@ -141,2 +141,8 @@ res.result = adapter.createState ? adapter.createState() : {}; | ||
if (req.options.timeout) { | ||
setTimeout(() => { | ||
if (res.pending) res.cancel(); | ||
}, req.options.timeout); | ||
} | ||
return res; | ||
@@ -143,0 +149,0 @@ }, |
{ | ||
"name": "@apicase/core", | ||
"version": "0.7.0-beta.13", | ||
"version": "0.7.0-beta.14", | ||
"description": "Core library to make API calls with any adapter", | ||
@@ -5,0 +5,0 @@ "main": "es/index.js", |
@@ -20,6 +20,83 @@ # apicase-core | ||
## Documentation | ||
### Full docs | ||
[**Read on gitbook**](kelin2025.gitbooks.io/apicase/content/) | ||
### Basic request | ||
Wrap adapter into `apicase` method and use it like it's Axios | ||
```javascript | ||
import { apicase } from '@apicase/core' | ||
improt fetch from '@apicase/adapter-fetch' | ||
const doRequest = apicase(fetch) | ||
const { success, result } = await doRequest({ | ||
url: '/api/posts/:id', | ||
method: 'POST' | ||
params: { id: 1 }, | ||
body: { | ||
title: 'Hello', | ||
text: 'This is Apicase' | ||
}, | ||
headers: { | ||
token: localStorage.getItem('token') | ||
} | ||
}) | ||
if (success) { | ||
console.log('Yay!', result) | ||
} else { | ||
console.log('Hey...', result) | ||
} | ||
``` | ||
### Events-based requests handling | ||
Following _"Business logic failures are not exceptions"_ principle, | ||
Apicase separates error handling from request fails: | ||
```javascript | ||
doRequest({ url: '/api/posts' }) | ||
.on('done', res => { console.log('Done', res) }) | ||
.on('fail', res => { console.log('Fail', res) }) | ||
.on('error', err => { console.error(err) }) | ||
``` | ||
### Apicase services | ||
Move your API logic outside the main application code | ||
```javascript | ||
import { ApiService } from '@apicase/core' | ||
import fetch from '@apicase/adapter-fetch' | ||
const ApiRoot = new ApiService(fetch, { url: '/api' }) | ||
.on('done', logSucccess) | ||
.on('fail', logFailure) | ||
const AuthService = ApiRoot | ||
.extend({ url: 'auth' }) | ||
.on('done', res => { | ||
localStorage.setItem('token', res.body.token) | ||
}) | ||
AuthService.doRequest({ | ||
body: { login: 'Apicase', password: '*****' } | ||
}) | ||
``` | ||
### Request queues | ||
Keep correct order of requests using queues | ||
```javascript | ||
import { ApiQueue } from '@apicase/core' | ||
const queue = new ApiQueue() | ||
queue.push(SendMessage.doRequest, { body: { message: 'that stuff' } }) | ||
queue.push(SendMessage.doRequest, { body: { message: 'really' } }) | ||
queue.push(SendMessage.doRequest, { body: { message: 'works' } }) | ||
``` | ||
## TODO | ||
* [ ] Complete `adapter-fetch` and `adapter-xhr` | ||
* [ ] Complete `apiQueue` and `apiAll` helpers | ||
* [x] Complete `adapter-fetch` and `adapter-xhr` | ||
* [x] Complete `ApiQueue` | ||
* [ ] Improve debugging | ||
* [ ] Rewrite tests for actual version | ||
@@ -26,0 +103,0 @@ * [ ] Rewrite `apicase-services` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
368433
20
10709
112
1