@apicase/core
Advanced tools
Comparing version 0.9.0 to 0.10.0
@@ -11,5 +11,9 @@ var _default3 = require('nanoutils/cjs/mapObjIndexed'); | ||
const compose = (hooks, createData) => (payload, changePayload) => new Promise(resolve => { | ||
function CHANGE_CALLBACK(cb) { | ||
this.callback = cb; | ||
} | ||
const compose = (hooks, createData) => (payload, changePayload) => new Promise((resolve, reject) => { | ||
if (!hooks.length) return resolve(payload); | ||
const data = createData(payload); | ||
const data = createData(payload, cb => reject(new CHANGE_CALLBACK(cb))); | ||
const next = payload => { | ||
@@ -97,7 +101,12 @@ changePayload(payload); | ||
bus.emit('start', getState()); | ||
res.promise = composedHooks.before(payload); | ||
res.promise.catch(function (err) { | ||
bus.emit('error', err); | ||
return Promise.reject(err); | ||
res.promise = composedHooks.before(payload).catch(err => { | ||
if (err instanceof CHANGE_CALLBACK) { | ||
return err.callback(); | ||
} else { | ||
bus.emit('error', err); | ||
return Promise.reject(err); | ||
} | ||
}); | ||
res.then = cb => res.promise.then(cb); | ||
res.catch = cb => res.promise.catch(cb); | ||
}; | ||
@@ -222,7 +231,7 @@ const getState = () => pickState(res); | ||
update: setPayload, | ||
payload: payload => ({ | ||
payload: (payload, changeFlow) => ({ | ||
meta: req.meta, | ||
payload: payload, | ||
done: composedHooks.done, | ||
fail: composedHooks.fail | ||
done: res => changeFlow(() => composedHooks.done(res)), | ||
fail: res => changeFlow(() => composedHooks.fail(res)) | ||
}), | ||
@@ -234,8 +243,8 @@ finalCb: doRequest | ||
update: setResult, | ||
payload: result => ({ | ||
payload: (result, changeFlow) => ({ | ||
meta: req.meta, | ||
retry: doRequest, | ||
retry: res => changeFlow(() => doRequest(res)), | ||
payload: res.payload, | ||
result: result, | ||
fail: composedHooks.fail | ||
fail: res => changeFlow(() => composedHooks.fail(res)) | ||
}), | ||
@@ -252,3 +261,3 @@ finalCb: result => { | ||
update: setResult, | ||
payload: result => ({ | ||
payload: (result, changeFlow) => ({ | ||
meta: req.meta, | ||
@@ -258,6 +267,7 @@ retry: doRequest, | ||
result: result, | ||
done: composedHooks.done | ||
done: res => changeFlow(() => composedHooks.done(res)) | ||
}), | ||
finalCb: result => { | ||
setState({ pending: false }); | ||
setResult(result); | ||
bus.emit('fail', result, getState()); | ||
@@ -267,9 +277,15 @@ bus.emit('finish', result, getState()); | ||
} | ||
} | ||
// Object with callbacks that start hooks queue | ||
};const composedHooks = _default3((params, k) => payload => { | ||
// Object with callbacks that start hooks queue | ||
} }; | ||
const composedHooks = _default3((params, k) => payload => { | ||
setState(params.state); | ||
if (res.cancelled) return res; | ||
return compose(req.hooks[k], params.payload)(payload, params.update).then(params.finalCb); | ||
return compose(req.hooks[k], params.payload)(payload, params.update).then(params.finalCb).catch(result => { | ||
if (result instanceof CHANGE_CALLBACK) { | ||
return result.callback(); | ||
} else { | ||
return Promise.reject(result); | ||
} | ||
}); | ||
}, hooks); | ||
@@ -276,0 +292,0 @@ |
@@ -8,5 +8,9 @@ import _default3 from 'nanoutils/lib/mapObjIndexed'; | ||
const compose = (hooks, createData) => (payload, changePayload) => new Promise(resolve => { | ||
function CHANGE_CALLBACK(cb) { | ||
this.callback = cb; | ||
} | ||
const compose = (hooks, createData) => (payload, changePayload) => new Promise((resolve, reject) => { | ||
if (!hooks.length) return resolve(payload); | ||
const data = createData(payload); | ||
const data = createData(payload, cb => reject(new CHANGE_CALLBACK(cb))); | ||
const next = payload => { | ||
@@ -94,7 +98,12 @@ changePayload(payload); | ||
bus.emit('start', getState()); | ||
res.promise = composedHooks.before(payload); | ||
res.promise.catch(function (err) { | ||
bus.emit('error', err); | ||
return Promise.reject(err); | ||
res.promise = composedHooks.before(payload).catch(err => { | ||
if (err instanceof CHANGE_CALLBACK) { | ||
return err.callback(); | ||
} else { | ||
bus.emit('error', err); | ||
return Promise.reject(err); | ||
} | ||
}); | ||
res.then = cb => res.promise.then(cb); | ||
res.catch = cb => res.promise.catch(cb); | ||
}; | ||
@@ -219,7 +228,7 @@ const getState = () => pickState(res); | ||
update: setPayload, | ||
payload: payload => ({ | ||
payload: (payload, changeFlow) => ({ | ||
meta: req.meta, | ||
payload: payload, | ||
done: composedHooks.done, | ||
fail: composedHooks.fail | ||
done: res => changeFlow(() => composedHooks.done(res)), | ||
fail: res => changeFlow(() => composedHooks.fail(res)) | ||
}), | ||
@@ -231,8 +240,8 @@ finalCb: doRequest | ||
update: setResult, | ||
payload: result => ({ | ||
payload: (result, changeFlow) => ({ | ||
meta: req.meta, | ||
retry: doRequest, | ||
retry: res => changeFlow(() => doRequest(res)), | ||
payload: res.payload, | ||
result: result, | ||
fail: composedHooks.fail | ||
fail: res => changeFlow(() => composedHooks.fail(res)) | ||
}), | ||
@@ -249,3 +258,3 @@ finalCb: result => { | ||
update: setResult, | ||
payload: result => ({ | ||
payload: (result, changeFlow) => ({ | ||
meta: req.meta, | ||
@@ -255,6 +264,7 @@ retry: doRequest, | ||
result: result, | ||
done: composedHooks.done | ||
done: res => changeFlow(() => composedHooks.done(res)) | ||
}), | ||
finalCb: result => { | ||
setState({ pending: false }); | ||
setResult(result); | ||
bus.emit('fail', result, getState()); | ||
@@ -264,9 +274,15 @@ bus.emit('finish', result, getState()); | ||
} | ||
} | ||
// Object with callbacks that start hooks queue | ||
};const composedHooks = _default3((params, k) => payload => { | ||
// Object with callbacks that start hooks queue | ||
} }; | ||
const composedHooks = _default3((params, k) => payload => { | ||
setState(params.state); | ||
if (res.cancelled) return res; | ||
return compose(req.hooks[k], params.payload)(payload, params.update).then(params.finalCb); | ||
return compose(req.hooks[k], params.payload)(payload, params.update).then(params.finalCb).catch(result => { | ||
if (result instanceof CHANGE_CALLBACK) { | ||
return result.callback(); | ||
} else { | ||
return Promise.reject(result); | ||
} | ||
}); | ||
}, hooks); | ||
@@ -273,0 +289,0 @@ |
{ | ||
"name": "@apicase/core", | ||
"version": "0.9.0", | ||
"version": "0.10.0", | ||
"description": "Core library to make API calls with any adapter", | ||
@@ -5,0 +5,0 @@ "main": "cjs/index.js", |
@@ -35,3 +35,3 @@ # apicase-core | ||
url: '/api/posts/:id', | ||
method: 'POST' | ||
method: 'POST', | ||
params: { id: 1 }, | ||
@@ -38,0 +38,0 @@ body: { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
455148
13124