๐
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import fetch from '@apicase/adapter-fetch' | |
| import { ApiService } from '@apicase/core' | |
| import { ApiTree, restWrapped } from '@apicase/services' | |
| const Root = new ApiService(fetch, { url: '/api' }) | |
| const api = new ApiTree(Root, [ | |
| /* Equal with previous example */ | |
| restWrapped('posts'), | |
| /* Pick only specified routes */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { ServicesTree } from '@apicase/services' | |
| const api = new ServicesTree([ | |
| { url: '/api', on: { done: console.log } } | |
| ]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import fetch from '@apicase/adapter-fetch' | |
| import { ApiTree } from '@apicase/services' | |
| import { ApiService } from '@apicase/core' | |
| const Root = new ApiService(fetch, { url: '/api' }) | |
| const api = new ApiTree(Root, [ | |
| { url: 'posts', children: [ | |
| { name: 'postsGetAll', url: '', method: 'GET' }, | |
| { name: 'postsCreate', url: '', method: 'POST' }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* I haven't so much time - do or go away */ | |
| const fileUpload = doRequest({ | |
| url: '/very/slow', | |
| options: { timeout: 5000 } | |
| }) | |
| /* Time's up - you are dead! */ | |
| fileUpload.on('cancel', () => { | |
| console.log('Too slow!') | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const search = doRequest({ | |
| url: '/search', | |
| options: { immediate: false, debounce: 2000 } | |
| }) | |
| /* Spamming requests while typing */ | |
| search.start({ query: { text: 'H' } }) | |
| search.start({ query: { text: 'He' } }) | |
| search.start({ query: { text: 'Hel' } }) | |
| search.start({ query: { text: 'Hell' } }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { ApiQueue } from '@apicase/core' | |
| /* Create queue */ | |
| const queue = new ApiQueue() | |
| /* Push requests */ | |
| queue.push(doRequest, { url: '/posts' }) | |
| queue.push(Service.doRequest, { body }) | |
| /* Global queue events */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const req = doRequest({ url: '/api/posts' }) | |
| req.on('cancel', console.log) | |
| req.cancel() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* API base */ | |
| const ApiRoot = new ApiService(fetch, { | |
| url: '/api' | |
| }) | |
| /* Auth service */ | |
| const AuthService = ApiRoot.extend({ | |
| url: 'auth', | |
| method: 'POST' | |
| }).on('done', res => localStorage.setItem('token', res.body.token)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* API base */ | |
| const ApiRoot = new ApiService(fetch, { | |
| url: '/api' | |
| }) | |
| /* Auth service */ | |
| const AuthService = ApiRoot.extend({ | |
| url: 'auth', | |
| method: 'POST' | |
| }).on('done', res => localStorage.setItem('token', res.body.token)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const ApiRoot = ApiRoot.extend({ | |
| url: '/api', | |
| hooks: { | |
| /* Inject our token and go to the next step */ | |
| before ({ payload, meta, next }) { | |
| if (!meta.requiresAuth) return next(payload) | |
| const token = localStorage.getItem('token') | |
| next(withToken(token, payload)) | |
| } | |
| } |