@apicase/core
Advanced tools
Comparing version 0.7.0-beta.6 to 0.7.0-beta.7
@@ -86,2 +86,5 @@ import _default3 from 'nanoutils/lib/mapObjIndexed'; | ||
const bus = new EventBus(); | ||
const on = evt => cb => { | ||
res.on(evt, cb); | ||
}; | ||
const res = { | ||
@@ -100,2 +103,4 @@ state: { | ||
}, | ||
onDone: on('done'), | ||
onFail: on('fail'), | ||
cancel: () => { | ||
@@ -147,5 +152,5 @@ return Promise.resolve().then(() => cancelCallback()).then(() => { | ||
return new Promise(resolve => { | ||
const resolveCb = result => setResult(result) || composedHooks.resolve(res.state.result).then(() => resolve(res.state)); | ||
const doneCb = result => setResult(result) || composedHooks.done(res.state.result).then(() => resolve(res.state)); | ||
const rejectCb = result => setResult(result) || composedHooks.reject(res.state.result).then(() => resolve(res.state)); | ||
const failCb = result => setResult(result) || composedHooks.fail(res.state.result).then(() => resolve(res.state)); | ||
@@ -156,4 +161,4 @@ adapter.callback({ | ||
setResult: setResult, | ||
resolve: resolveCb, | ||
reject: rejectCb, | ||
resolve: doneCb, | ||
reject: failCb, | ||
emit: bus.emit, | ||
@@ -174,8 +179,8 @@ setCancelCallback: cb => { | ||
payload: payload, | ||
resolve: composedHooks.resolve, | ||
reject: composedHooks.reject | ||
done: composedHooks.done, | ||
fail: composedHooks.fail | ||
}), | ||
finalCb: doRequest | ||
}, | ||
resolve: { | ||
done: { | ||
state: { success: true }, | ||
@@ -186,11 +191,11 @@ update: setResult, | ||
result: result, | ||
reject: composedHooks.reject | ||
fail: composedHooks.fail | ||
}), | ||
finalCb: result => { | ||
setState({ pending: false }); | ||
bus.emit('resolve', result, res.state); | ||
bus.emit('done', result, res.state); | ||
bus.emit('finish', result, res.state); | ||
} | ||
}, | ||
reject: { | ||
fail: { | ||
state: { success: false }, | ||
@@ -201,7 +206,7 @@ update: setResult, | ||
result: result, | ||
resolve: composedHooks.resolve | ||
done: composedHooks.done | ||
}), | ||
finalCb: result => { | ||
setState({ pending: false }); | ||
bus.emit('reject', result, res.state); | ||
bus.emit('fail', result, res.state); | ||
bus.emit('finish', result, res.state); | ||
@@ -208,0 +213,0 @@ } |
@@ -7,5 +7,5 @@ export default { | ||
before: [], | ||
resolve: [], | ||
reject: [] | ||
done: [], | ||
fail: [] | ||
} | ||
}; |
@@ -51,3 +51,3 @@ import _default from 'nanoutils/lib/equals'; | ||
const service = new ApiService(adapter, this._opts.concat(newOpts)); | ||
service._listeners = this._listeners; | ||
// service._listeners = this._listeners | ||
return service; | ||
@@ -54,0 +54,0 @@ }; |
{ | ||
"name": "@apicase/core", | ||
"version": "0.7.0-beta.6", | ||
"version": "0.7.0-beta.7", | ||
"description": "Core library to make API calls with any adapter", | ||
@@ -26,3 +26,3 @@ "main": "es/index.js", | ||
"path": "es/apicase.js", | ||
"limit": "1.5 KB" | ||
"limit": "2 KB" | ||
}, | ||
@@ -29,0 +29,0 @@ { |
@@ -18,3 +18,3 @@ import { apicase } from '../lib/apicase' | ||
apicase({ callback })({ a: 1 }).on('resolve', res => { | ||
apicase({ callback })({ a: 1 }).on('done', res => { | ||
expect(callback).toBeCalled() | ||
@@ -50,3 +50,3 @@ done() | ||
apicase({ callback })({ a: 1 }).on('resolve', res => { | ||
apicase({ callback })({ a: 1 }).on('done', res => { | ||
done() | ||
@@ -63,3 +63,3 @@ }) | ||
apicase({ callback, createState })({ a: 2 }).on('resolve', res => { | ||
apicase({ callback, createState })({ a: 2 }).on('done', res => { | ||
done() | ||
@@ -76,3 +76,3 @@ }) | ||
apicase({ callback, createState })({ a: 2 }).on('resolve', res => { | ||
apicase({ callback, createState })({ a: 2 }).on('done', res => { | ||
expect(res).toEqual({ a: 2, b: 3 }) | ||
@@ -90,3 +90,3 @@ done() | ||
apicase({ callback, convert })({ b: 3 }).on('resolve', res => { | ||
apicase({ callback, convert })({ b: 3 }).on('done', res => { | ||
done() | ||
@@ -113,3 +113,3 @@ }) | ||
const call = apicase({ callback })({ b: 3 }) | ||
.on('resolve', resolve) | ||
.on('done', resolve) | ||
.on('cancel', cancel) | ||
@@ -121,4 +121,4 @@ | ||
it('resolves request on resolve() call and rejects on reject()', done => { | ||
apicase(resolveAdapter)({ a: 1 }).on('resolve', res => { | ||
apicase(rejectAdapter)(res).on('reject', res => { | ||
apicase(resolveAdapter)({ a: 1 }).on('done', res => { | ||
apicase(rejectAdapter)(res).on('fail', res => { | ||
expect(res).toEqual({ a: 1 }) | ||
@@ -133,3 +133,3 @@ done() | ||
describe('Before hooks', () => { | ||
it('is called before request', done => { | ||
it('is called before request', doneCb => { | ||
const before = jest.fn(({ payload, next }) => { | ||
@@ -144,4 +144,4 @@ expect(callback).not.toBeCalled() | ||
apicase({ callback })({ a: 1, hooks: { before } }).on('resolve', res => { | ||
done() | ||
apicase({ callback })({ a: 1, hooks: { before } }).on('done', res => { | ||
doneCb() | ||
}) | ||
@@ -162,3 +162,3 @@ }) | ||
const payload = { a: 1, b: 1, hooks: { before } } | ||
apicase({ callback })(payload).on('resolve', () => { | ||
apicase({ callback })(payload).on('done', () => { | ||
done() | ||
@@ -169,4 +169,4 @@ }) | ||
it('resolves/rejects request without adapter call', done => { | ||
const success = jest.fn(({ payload, resolve }) => resolve(payload)) | ||
const fail = jest.fn(({ payload, reject }) => reject(payload)) | ||
const success = jest.fn(({ payload, done }) => done(payload)) | ||
const fail = jest.fn(({ payload, fail }) => fail(payload)) | ||
const callback = jest.fn(({ payload, resolve }) => resolve(payload)) | ||
@@ -176,5 +176,5 @@ | ||
doReq({ a: 1, hooks: { before: success } }).on('resolve', res => { | ||
doReq({ a: 1, hooks: { before: success } }).on('done', res => { | ||
expect(res).toEqual({ a: 1 }) | ||
doReq({ a: 2, hooks: { before: fail } }).on('reject', res => { | ||
doReq({ a: 2, hooks: { before: fail } }).on('fail', res => { | ||
expect(res).toEqual({ a: 2 }) | ||
@@ -188,36 +188,33 @@ expect(callback).not.toBeCalled() | ||
describe('Resolve hooks', () => { | ||
it('is called on request resolve', done => { | ||
const resolve = jest.fn(({ payload, next }) => next({ a: 2 })) | ||
describe('Done hooks', () => { | ||
it('is called on request done', doneCb => { | ||
const done = jest.fn(({ payload, next }) => next({ a: 2 })) | ||
apicase(resolveAdapter)({ a: 1, hooks: { resolve } }).on( | ||
'resolve', | ||
res => { | ||
expect(res).toEqual({ a: 2 }) | ||
expect(resolve).toBeCalled() | ||
done() | ||
} | ||
) | ||
apicase(resolveAdapter)({ a: 1, hooks: { done } }).on('done', res => { | ||
expect(res).toEqual({ a: 2 }) | ||
expect(done).toBeCalled() | ||
doneCb() | ||
}) | ||
}) | ||
it('is not called on request reject', done => { | ||
const resolve = jest.fn(({ payload, next }) => next({ a: 2 })) | ||
it('is not called on request fail', doneCb => { | ||
const done = jest.fn(({ payload, next }) => next({ a: 2 })) | ||
apicase(rejectAdapter)({ a: 1, hooks: { resolve } }).on('reject', res => { | ||
apicase(rejectAdapter)({ a: 1, hooks: { done } }).on('fail', res => { | ||
expect(res).toEqual({ a: 1 }) | ||
expect(resolve).not.toBeCalled() | ||
done() | ||
expect(done).not.toBeCalled() | ||
doneCb() | ||
}) | ||
}) | ||
it('can reject call', done => { | ||
const resolve = jest.fn(({ payload, reject }) => reject({ a: 2 })) | ||
const reject = jest.fn(({ payload, next }) => next({ a: 3 })) | ||
const payload = { a: 1, hooks: { resolve, reject } } | ||
it('can fail call', doneCb => { | ||
const done = jest.fn(({ payload, fail }) => fail({ a: 2 })) | ||
const fail = jest.fn(({ payload, next }) => next({ a: 3 })) | ||
const payload = { a: 1, hooks: { done, fail } } | ||
apicase(resolveAdapter)(payload).on('reject', res => { | ||
apicase(resolveAdapter)(payload).on('fail', res => { | ||
expect(res).toEqual({ a: 3 }) | ||
expect(resolve).toBeCalled() | ||
expect(reject).toBeCalled() | ||
done() | ||
expect(done).toBeCalled() | ||
expect(fail).toBeCalled() | ||
doneCb() | ||
}) | ||
@@ -227,36 +224,33 @@ }) | ||
describe('Reject hooks', () => { | ||
it('is called on request reject', done => { | ||
const reject = jest.fn(({ payload, next }) => next({ a: 2 })) | ||
describe('Fail hooks', () => { | ||
it('is called on request reject', doneCb => { | ||
const fail = jest.fn(({ payload, next }) => next({ a: 2 })) | ||
apicase(rejectAdapter)({ a: 1, hooks: { reject } }).on('reject', res => { | ||
apicase(rejectAdapter)({ a: 1, hooks: { fail } }).on('fail', res => { | ||
expect(res).toEqual({ a: 2 }) | ||
expect(reject).toBeCalled() | ||
done() | ||
expect(fail).toBeCalled() | ||
doneCb() | ||
}) | ||
}) | ||
it('is not called on request resolve', done => { | ||
const reject = jest.fn(({ payload, next }) => next({ a: 2 })) | ||
it('is not called on request done', done => { | ||
const fail = jest.fn(({ payload, next }) => next({ a: 2 })) | ||
apicase(resolveAdapter)({ a: 1, hooks: { reject } }).on( | ||
'resolve', | ||
res => { | ||
expect(res).toEqual({ a: 1 }) | ||
expect(reject).not.toBeCalled() | ||
done() | ||
} | ||
) | ||
apicase(resolveAdapter)({ a: 1, hooks: { fail } }).on('done', res => { | ||
expect(res).toEqual({ a: 1 }) | ||
expect(fail).not.toBeCalled() | ||
done() | ||
}) | ||
}) | ||
it('can resolve call', done => { | ||
const reject = jest.fn(({ payload, resolve }) => resolve({ a: 2 })) | ||
const resolve = jest.fn(({ payload, next }) => next({ a: 3 })) | ||
const payload = { a: 1, hooks: { resolve, reject } } | ||
it('can done call', doneCb => { | ||
const fail = jest.fn(({ payload, done }) => done({ a: 2 })) | ||
const done = jest.fn(({ payload, next }) => next({ a: 3 })) | ||
const payload = { a: 1, hooks: { done, fail } } | ||
apicase(rejectAdapter)(payload).on('resolve', res => { | ||
apicase(rejectAdapter)(payload).on('done', res => { | ||
expect(res).toEqual({ a: 3 }) | ||
expect(resolve).toBeCalled() | ||
expect(reject).toBeCalled() | ||
done() | ||
expect(done).toBeCalled() | ||
expect(fail).toBeCalled() | ||
doneCb() | ||
}) | ||
@@ -292,23 +286,23 @@ }) | ||
it('emits resolve event on adapter resolve', async done => { | ||
const resolve = jest.fn(result => { | ||
it('emits done event on adapter done', async doneCb => { | ||
const done = jest.fn(result => { | ||
expect(result).toEqual({ a: 1 }) | ||
}) | ||
await apicase(resolveAdapter)({ a: 1 }).on('resolve', resolve) | ||
expect(resolve).toBeCalled() | ||
await apicase(rejectAdapter)({ a: 1 }).on('resolve', resolve) | ||
expect(resolve).toHaveBeenCalledTimes(1) | ||
done() | ||
await apicase(resolveAdapter)({ a: 1 }).on('done', done) | ||
expect(done).toBeCalled() | ||
await apicase(rejectAdapter)({ a: 1 }).on('done', done) | ||
expect(done).toHaveBeenCalledTimes(1) | ||
doneCb() | ||
}) | ||
it('emits reject event on adapter resolve', async done => { | ||
const reject = jest.fn(result => { | ||
it('emits fail event on adapter fail', async done => { | ||
const fail = jest.fn(result => { | ||
expect(result).toEqual({ a: 1 }) | ||
}) | ||
await apicase(rejectAdapter)({ a: 1 }).on('reject', reject) | ||
expect(reject).toBeCalled() | ||
await apicase(resolveAdapter)({ a: 1 }).on('reject', reject) | ||
expect(reject).toHaveBeenCalledTimes(1) | ||
await apicase(rejectAdapter)({ a: 1 }).on('fail', fail) | ||
expect(fail).toBeCalled() | ||
await apicase(resolveAdapter)({ a: 1 }).on('fail', fail) | ||
expect(fail).toHaveBeenCalledTimes(1) | ||
done() | ||
@@ -315,0 +309,0 @@ }) |
@@ -11,4 +11,4 @@ import { mergeOptions } from '../lib/merge' | ||
before: [], | ||
resolve: [], | ||
reject: [] | ||
done: [], | ||
fail: [] | ||
} | ||
@@ -22,3 +22,3 @@ }) | ||
hooks: { | ||
resolve: [() => {}] | ||
done: [() => {}] | ||
} | ||
@@ -28,3 +28,3 @@ } | ||
hooks: { | ||
resolve: [() => {}] | ||
done: [() => {}] | ||
} | ||
@@ -34,7 +34,4 @@ } | ||
expect(merged).toHaveProperty('hooks') | ||
expect(merged.hooks).toHaveProperty('resolve') | ||
expect(merged.hooks.resolve).toEqual([ | ||
p1.hooks.resolve[0], | ||
p2.hooks.resolve[0] | ||
]) | ||
expect(merged.hooks).toHaveProperty('done') | ||
expect(merged.hooks.done).toEqual([p1.hooks.done[0], p2.hooks.done[0]]) | ||
}) | ||
@@ -46,3 +43,3 @@ | ||
before: () => {}, | ||
resolve: [() => {}] | ||
done: [() => {}] | ||
} | ||
@@ -52,4 +49,4 @@ } | ||
hooks: { | ||
resolve: [() => {}], | ||
reject: () => {} | ||
done: [() => {}], | ||
fail: () => {} | ||
} | ||
@@ -59,4 +56,4 @@ } | ||
before: [p1.hooks.before], | ||
resolve: [p1.hooks.resolve[0], p2.hooks.resolve[0]], | ||
reject: [p2.hooks.reject] | ||
done: [p1.hooks.done[0], p2.hooks.done[0]], | ||
fail: [p2.hooks.fail] | ||
}) | ||
@@ -63,0 +60,0 @@ }) |
@@ -8,3 +8,3 @@ import { normalizeOptions } from '../lib/normalize' | ||
before() {}, | ||
resolve: [() => {}, () => {}] | ||
done: [() => {}, () => {}] | ||
} | ||
@@ -14,3 +14,3 @@ } | ||
expect(h.before).toEqual([opts.hooks.before]) | ||
expect(h.resolve).toEqual([opts.hooks.resolve[0], opts.hooks.resolve[1]]) | ||
expect(h.done).toEqual([opts.hooks.done[0], opts.hooks.done[1]]) | ||
}) | ||
@@ -22,4 +22,4 @@ | ||
before: [], | ||
resolve: [], | ||
reject: [] | ||
done: [], | ||
fail: [] | ||
}) | ||
@@ -70,4 +70,4 @@ }) | ||
before: [opts.hooks.before], | ||
resolve: [], | ||
reject: [] | ||
done: [], | ||
fail: [] | ||
} | ||
@@ -74,0 +74,0 @@ }) |
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
30608
866