Skip to content

Instantly share code, notes, and snippets.

@Kelin2025
Created March 17, 2018 19:06
Show Gist options
  • Save Kelin2025/523ac2102968f05b52e127e3eaa8a808 to your computer and use it in GitHub Desktop.
Save Kelin2025/523ac2102968f05b52e127e3eaa8a808 to your computer and use it in GitHub Desktop.
Apicase hooks
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)
const CreatePost = ApiRoot.extend({
url: 'posts',
method: 'POST',
hooks: {
/* Inject our token and go to the next step */
before ({ payload, next }) {
const token = localStorage.getItem('token')
next(withToken(token, payload))
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment