๐
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
| 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/: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
| 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
| 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' | |
| 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
| 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
| 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)) | |
| } | |
| } |
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)) |