Created
May 30, 2018 19:54
-
-
Save dimensi/87499ab59c55b9dad8d5bc871a70d3f3 to your computer and use it in GitHub Desktop.
Apipie class example
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 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