Skip to content

Instantly share code, notes, and snippets.

@dimensi
Created May 30, 2018 19:54
Show Gist options
  • Select an option

  • Save dimensi/87499ab59c55b9dad8d5bc871a70d3f3 to your computer and use it in GitHub Desktop.

Select an option

Save dimensi/87499ab59c55b9dad8d5bc871a70d3f3 to your computer and use it in GitHub Desktop.
Apipie class example
const checkAuth = (options) => (context) => {
if (context.meta.auth) {
context.headers.append('token', 'my super token')
}
}
class BaseApi extends Apipie {
constructor() {
this.baseURL = 'http://localhost'
this.beforeHooks = [checkAuth()];
this.afterHooks = [];
}
}
class Items extends BaseApi {
constructor() {
this.prefix = '/items';
this.afterHooks.push(anotherHooks)
this.meta = {
globalMeta: true,
}
}
getAll() {
return this.fetch('/all', {
meta: {
auth: true
}
})
}
getByID(id) {
return this.fetch('/:id', {
params: {
id,
},
meta: {
auth: true
}
})
}
getByFilter(filter) {
return this.fetch('/', {
query: filter,
})
}
}
const items = new Items();
items.getByID(id)
return items;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment