Skip to content

Instantly share code, notes, and snippets.

View Kelin2025's full-sized avatar
๐Ÿ‘‹
Let him cook

Anton Kosykh Kelin2025

๐Ÿ‘‹
Let him cook
View GitHub Profile
@Kelin2025
Kelin2025 / rest-tree.js
Created March 17, 2018 19:32
Apicase rest tree
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 */
@Kelin2025
Kelin2025 / tree-events.js
Created March 17, 2018 19:30
Apicase events in services tree
import { ServicesTree } from '@apicase/services'
const api = new ServicesTree([
{ url: '/api', on: { done: console.log } }
])
@Kelin2025
Kelin2025 / tree.js
Created March 17, 2018 19:29
Services tree
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' },
@Kelin2025
Kelin2025 / timeout.js
Created March 17, 2018 19:25
Apicase timeout
/* 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!')
})
@Kelin2025
Kelin2025 / debounce.js
Created March 17, 2018 19:24
Apicase debounce
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' } })
@Kelin2025
Kelin2025 / queue.js
Created March 17, 2018 19:21
Apicase queues
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 */
@Kelin2025
Kelin2025 / cancel.js
Created March 17, 2018 19:16
Apicase cancellation
const req = doRequest({ url: '/api/posts' })
req.on('cancel', console.log)
req.cancel()
@Kelin2025
Kelin2025 / refresh-token-once.js
Last active March 17, 2018 19:15
Refresh token once
/* 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))
@Kelin2025
Kelin2025 / refresh-token.js
Created March 17, 2018 19:14
Apicase and refresh token
/* 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))
@Kelin2025
Kelin2025 / meta.js
Created March 17, 2018 19:09
Apicase meta
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))
}
}