๐
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 { Container, ApiAbort } from 'apicase' | |
// For example, we have user info and validation schema for data | |
import user from './user' | |
import schema from './schema' | |
// For example we have route that create a new product | |
// And we have to sent title of product | |
const services = { |
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 { Container } from 'apicase' | |
export default new Container({ | |
services: { | |
hello: { | |
url: '/hello', | |
watchers: [ | |
(ctx, key, value) => { | |
switch (key) { | |
case 'calls': |
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 apis from './container.js' | |
// Note that it always resolves | |
let res = await apis.go(serviceName, queryData, params) | |
// You can handle result as before | |
// But you often do not have to do it | |
// because of next features I'll talk about | |
if (res.ok) { | |
console.log('Success') |
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 { Container } from 'apicase' | |
export default new Container({ | |
services: { | |
posts: { | |
url: '/posts', | |
// You can use it for shortcuts | |
// or more interesting computed properties | |
mixins: { | |
success () { |
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 { Container, ApiAbort } from 'apicase' | |
// I separated it for better understanding | |
import services from './services' | |
// Pass authorization token to every service | |
const 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
/** | |
* Array.find | |
*/ | |
if (!Array.prototype.find) { | |
Array.prototype.find = function(predicate) { | |
'use strict'; | |
if (this === null) { | |
throw new TypeError('Array.prototype.find called on null or undefined'); | |
} | |
if (typeof predicate !== 'function') { |
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' | |
const headers = { 'Content-Type': 'multipart/form-data' } | |
const catalog = { | |
url: 'catalog', | |
services: [ | |
{ name: 'category', url: 'categories', method: 'get' }, | |
{ name: 'platform', url: 'platforms', method: 'get' }, | |
{ name: 'apps', url: 'apps', method: 'get' }, |
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 |
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
doRequest({ url: '/api/posts' }) | |
.on('done', res => console.log(res)) | |
.on('fail', res => console.warn(res)) |