๐
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, next }) { | |
const token = localStorage.getItem('token') | |
next(withToken(token, payload)) | |
} | |
} | |
}) |
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 { ApiService } from '@apicase/core' | |
import fetch from '@apicase/adapter-fetch' | |
import R from 'ramda' | |
/* I pretend I know Ramda */ | |
const withToken = R.assoc(['headers', 'token']) | |
const ApiRoot = new ApiService(fetch, { url: '/api' }) | |
.on('error', logError) |
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 { ApiService } from '@apicase/core' | |
import fetch from '@apicase/adapter-fetch' | |
/* Api root with global logger and API base */ | |
const ApiRoot = new ApiService(fetch, { url: '/api' }) | |
.on('error', logError) | |
/* Inherit options and event listeners from ApiRoot */ | |
const CreatePost = ApiRoot.extend({ | |
url: 'posts', |
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 { ApiService } from '@apicase/core' | |
import fetch from '@apicase/adapter-fetch' | |
/* Create a service */ | |
const EditPost = new ApiService(fetch, { | |
url: '/api/posts/:id', | |
method: 'POST', | |
headers: { token: localStorage.getItem('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
doRequest({ | |
url: '/api/posts/:id', | |
method: 'POST', | |
params: { id: 1 }, | |
headers: { token: localStorage.getItem('token') }, | |
body: { title: 'Hello, Apicase!' } | |
}) |
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 { success, result } = await doRequest({ url: '/api/posts' }) |
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
doRequest({ url: '/api/posts' }) | |
.on('done', res => console.log('Success')) | |
.on('fail', res => console.log('Failed but it is OK')) | |
.on('error', err => console.log('JS Error', err)) | |
.on('cancel', (_) => console.log('Request was cancelled')) | |
.on('progress', (p) => console.log('XHR progress', p.loaded / p.total)) |
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
doRequest({ url: '/api/posts' }) | |
.on('done', res => console.log(res)) | |
.on('fail', res => console.warn(res)) |
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 { apicase } from '@apicase/core' | |
import fetch from '@apicase/adapter-fetch' | |
const doRequest = apicase(fetch) |
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 asyncPipe = (...fns) => arg => { | |
const res = fns[0](arg) | |
const slice = fns.slice(1) | |
const next = slice.length ? asyncPipe(...slice) : identity | |
const isPromise = typeof res === 'object' && 'then' in res | |
return isPromise ? res.then(next) : next(res) | |
} | |
const add = a => b => a + b |