@apicase/core
Advanced tools
Comparing version 0.10.1 to 0.11.0
@@ -182,2 +182,7 @@ var _default3 = require('nanoutils/cjs/mapObjIndexed'); | ||
const setPayload = diff => { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (typeof diff !== 'object' || Array.isArray(diff)) { | ||
throw new Error(`[Apicase] Payload should be an object, ${typeof diff} given`); | ||
} | ||
} | ||
const prev = res.payload; | ||
@@ -193,9 +198,7 @@ const next = _default(prev, diff); | ||
const setResult = diff => { | ||
const setResult = next => { | ||
const prev = res.result; | ||
const next = _default(prev, diff); | ||
bus.emit('change:result', { | ||
prev: prev, | ||
next: next, | ||
diff: diff | ||
next: next | ||
}); | ||
@@ -202,0 +205,0 @@ res.result = next; |
@@ -179,2 +179,7 @@ import _default3 from 'nanoutils/lib/mapObjIndexed'; | ||
const setPayload = diff => { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (typeof diff !== 'object' || Array.isArray(diff)) { | ||
throw new Error(`[Apicase] Payload should be an object, ${typeof diff} given`); | ||
} | ||
} | ||
const prev = res.payload; | ||
@@ -190,9 +195,7 @@ const next = _default(prev, diff); | ||
const setResult = diff => { | ||
const setResult = next => { | ||
const prev = res.result; | ||
const next = _default(prev, diff); | ||
bus.emit('change:result', { | ||
prev: prev, | ||
next: next, | ||
diff: diff | ||
next: next | ||
}); | ||
@@ -199,0 +202,0 @@ res.result = next; |
{ | ||
"name": "@apicase/core", | ||
"version": "0.10.1", | ||
"version": "0.11.0", | ||
"description": "Core library to make API calls with any adapter", | ||
@@ -5,0 +5,0 @@ "main": "cjs/index.js", |
@@ -1,11 +0,11 @@ | ||
import { apicase } from '../lib/apicase' | ||
import { pick } from 'nanoutils' | ||
import { apicase } from "../lib/apicase" | ||
import { pick } from "nanoutils" | ||
const pickState = pick([ | ||
'success', | ||
'pending', | ||
'started', | ||
'cancelled', | ||
'payload', | ||
'result' | ||
"success", | ||
"pending", | ||
"started", | ||
"cancelled", | ||
"payload", | ||
"result" | ||
]) | ||
@@ -21,4 +21,4 @@ | ||
describe('Calls', () => { | ||
it('calls adapter callback() with payload', done => { | ||
describe("Calls", () => { | ||
it("calls adapter callback() with payload", done => { | ||
const callback = jest.fn(({ payload, resolve }) => { | ||
@@ -29,3 +29,3 @@ expect(payload).toEqual({ a: 1 }) | ||
apicase({ callback })({ a: 1 }).on('done', res => { | ||
apicase({ callback })({ a: 1 }).on("done", res => { | ||
expect(callback).toBeCalled() | ||
@@ -36,3 +36,3 @@ done() | ||
it('returns thenable object with state, cancel() and on() methods', async done => { | ||
it("returns thenable object with state, cancel() and on() methods", async done => { | ||
const res = apicase(resolveAdapter)({ a: 1 }) | ||
@@ -52,13 +52,13 @@ expect(pickState(res)).toEqual({ | ||
describe('Adapters', () => { | ||
it('has payload, result resolve and reject callbacks', done => { | ||
describe("Adapters", () => { | ||
it("has payload, result resolve and reject callbacks", done => { | ||
const callback = jest.fn(({ payload, result, resolve, reject }) => { | ||
expect(payload).toEqual({ a: 1 }) | ||
expect(result).toEqual({}) | ||
expect(typeof resolve).toBe('function') | ||
expect(typeof reject).toBe('function') | ||
expect(typeof resolve).toBe("function") | ||
expect(typeof reject).toBe("function") | ||
setTimeout(resolve, 100, payload) | ||
}) | ||
apicase({ callback })({ a: 1 }).on('done', res => { | ||
apicase({ callback })({ a: 1 }).on("done", res => { | ||
done() | ||
@@ -68,3 +68,3 @@ }) | ||
it('creates state if createState() callback provided', done => { | ||
it("creates state if createState() callback provided", done => { | ||
const createState = () => ({ a: 1, b: 2 }) | ||
@@ -76,3 +76,3 @@ const callback = jest.fn(({ payload, result, resolve }) => { | ||
apicase({ callback, createState })({ a: 2 }).on('done', res => { | ||
apicase({ callback, createState })({ a: 2 }).on("done", res => { | ||
done() | ||
@@ -82,3 +82,3 @@ }) | ||
it('has setResult() callback to merge new state with old (like resolve)', done => { | ||
it("has setResult() callback to set new state", done => { | ||
const createState = () => ({ a: 1, b: 2 }) | ||
@@ -90,4 +90,4 @@ const callback = jest.fn(({ payload, result, resolve, setResult }) => { | ||
apicase({ callback, createState })({ a: 2 }).on('done', res => { | ||
expect(res).toEqual({ a: 2, b: 3 }) | ||
apicase({ callback, createState })({ a: 2 }).on("done", res => { | ||
expect(res).toEqual({ a: 2 }) | ||
done() | ||
@@ -97,3 +97,3 @@ }) | ||
it('converts payload before callback if convert() provided', done => { | ||
it("converts payload before callback if convert() provided", done => { | ||
const convert = payload => ({ ...payload, a: 2 }) | ||
@@ -105,3 +105,3 @@ const callback = jest.fn(({ payload, result, resolve, setResult }) => { | ||
apicase({ callback, convert })({ b: 3 }).on('done', res => { | ||
apicase({ callback, convert })({ b: 3 }).on("done", res => { | ||
done() | ||
@@ -111,3 +111,3 @@ }) | ||
it('can be cancelled if used setCancelCallback() and emits cencel event', done => { | ||
it("can be cancelled if used setCancelCallback() and emits cencel event", done => { | ||
const cancelCallback = jest.fn(timer => () => clearTimeout(timer)) | ||
@@ -129,4 +129,4 @@ const callback = jest.fn( | ||
const call = apicase({ callback })({ b: 3 }) | ||
.on('done', resolve) | ||
.on('cancel', cancel) | ||
.on("done", resolve) | ||
.on("cancel", cancel) | ||
@@ -136,5 +136,5 @@ call.cancel() | ||
it('resolves request on resolve() call and rejects on reject()', done => { | ||
apicase(resolveAdapter)({ a: 1 }).on('done', res => { | ||
apicase(rejectAdapter)(res).on('fail', res => { | ||
it("resolves request on resolve() call and rejects on reject()", done => { | ||
apicase(resolveAdapter)({ a: 1 }).on("done", res => { | ||
apicase(rejectAdapter)(res).on("fail", res => { | ||
expect(res).toEqual({ a: 1 }) | ||
@@ -147,5 +147,5 @@ done() | ||
describe('Hooks', () => { | ||
describe('Before hooks', () => { | ||
it('is called before request', doneCb => { | ||
describe("Hooks", () => { | ||
describe("Before hooks", () => { | ||
it("is called before request", doneCb => { | ||
const before = jest.fn(({ payload, next }) => { | ||
@@ -160,3 +160,3 @@ expect(callback).not.toBeCalled() | ||
apicase({ callback })({ a: 1, hooks: { before } }).on('done', res => { | ||
apicase({ callback })({ a: 1, hooks: { before } }).on("done", res => { | ||
doneCb() | ||
@@ -166,3 +166,3 @@ }) | ||
it('updates payload before adapter call', done => { | ||
it("updates payload before adapter call", done => { | ||
const before = jest.fn(({ payload, next }) => | ||
@@ -179,3 +179,3 @@ next({ | ||
const payload = { a: 1, b: 1, hooks: { before } } | ||
apicase({ callback })(payload).on('done', () => { | ||
apicase({ callback })(payload).on("done", () => { | ||
done() | ||
@@ -185,3 +185,3 @@ }) | ||
it('resolves/rejects request without adapter call', done => { | ||
it("resolves/rejects request without adapter call", done => { | ||
const success = jest.fn(({ payload, done }) => done(payload)) | ||
@@ -193,5 +193,5 @@ const fail = jest.fn(({ payload, fail }) => fail(payload)) | ||
doReq({ a: 1, hooks: { before: success } }).on('done', res => { | ||
doReq({ a: 1, hooks: { before: success } }).on("done", res => { | ||
expect(res).toEqual({ a: 1 }) | ||
doReq({ a: 2, hooks: { before: fail } }).on('fail', res => { | ||
doReq({ a: 2, hooks: { before: fail } }).on("fail", res => { | ||
expect(res).toEqual({ a: 2 }) | ||
@@ -205,7 +205,7 @@ expect(callback).not.toBeCalled() | ||
describe('Done hooks', () => { | ||
it('is called on request done', doneCb => { | ||
describe("Done hooks", () => { | ||
it("is called on request done", doneCb => { | ||
const done = jest.fn(({ payload, next }) => next({ a: 2 })) | ||
apicase(resolveAdapter)({ a: 1, hooks: { done } }).on('done', res => { | ||
apicase(resolveAdapter)({ a: 1, hooks: { done } }).on("done", res => { | ||
expect(res).toEqual({ a: 2 }) | ||
@@ -217,6 +217,6 @@ expect(done).toBeCalled() | ||
it('is not called on request fail', doneCb => { | ||
it("is not called on request fail", doneCb => { | ||
const done = jest.fn(({ payload, next }) => next({ a: 2 })) | ||
apicase(rejectAdapter)({ a: 1, hooks: { done } }).on('fail', res => { | ||
apicase(rejectAdapter)({ a: 1, hooks: { done } }).on("fail", res => { | ||
expect(res).toEqual({ a: 1 }) | ||
@@ -228,3 +228,3 @@ expect(done).not.toBeCalled() | ||
it('can fail call', doneCb => { | ||
it("can fail call", doneCb => { | ||
const done = jest.fn(({ payload, fail }) => fail({ a: 2 })) | ||
@@ -234,3 +234,3 @@ const fail = jest.fn(({ payload, next }) => next({ a: 3 })) | ||
apicase(resolveAdapter)(payload).on('fail', res => { | ||
apicase(resolveAdapter)(payload).on("fail", res => { | ||
expect(res).toEqual({ a: 3 }) | ||
@@ -244,7 +244,7 @@ expect(done).toBeCalled() | ||
describe('Fail hooks', () => { | ||
it('is called on request reject', doneCb => { | ||
describe("Fail hooks", () => { | ||
it("is called on request reject", doneCb => { | ||
const fail = jest.fn(({ payload, next }) => next({ a: 2 })) | ||
apicase(rejectAdapter)({ a: 1, hooks: { fail } }).on('fail', res => { | ||
apicase(rejectAdapter)({ a: 1, hooks: { fail } }).on("fail", res => { | ||
expect(res).toEqual({ a: 2 }) | ||
@@ -256,6 +256,6 @@ expect(fail).toBeCalled() | ||
it('is not called on request done', done => { | ||
it("is not called on request done", done => { | ||
const fail = jest.fn(({ payload, next }) => next({ a: 2 })) | ||
apicase(resolveAdapter)({ a: 1, hooks: { fail } }).on('done', res => { | ||
apicase(resolveAdapter)({ a: 1, hooks: { fail } }).on("done", res => { | ||
expect(res).toEqual({ a: 1 }) | ||
@@ -267,3 +267,3 @@ expect(fail).not.toBeCalled() | ||
it('can done call', doneCb => { | ||
it("can done call", doneCb => { | ||
const fail = jest.fn(({ payload, done }) => done({ a: 2 })) | ||
@@ -273,3 +273,3 @@ const done = jest.fn(({ payload, next }) => next({ a: 3 })) | ||
apicase(rejectAdapter)(payload).on('done', res => { | ||
apicase(rejectAdapter)(payload).on("done", res => { | ||
expect(res).toEqual({ a: 3 }) | ||
@@ -284,9 +284,9 @@ expect(done).toBeCalled() | ||
describe('Events', () => { | ||
it('emits finish event on request finish', async done => { | ||
describe("Events", () => { | ||
it("emits finish event on request finish", async done => { | ||
const finishA = jest.fn(payload => expect(payload).toEqual({ a: 1 })) | ||
const finishB = jest.fn(payload => expect(payload).toEqual({ a: 1 })) | ||
await apicase(resolveAdapter)({ a: 1 }).on('finish', finishA) | ||
await apicase(rejectAdapter)({ a: 1 }).on('finish', finishB) | ||
await apicase(resolveAdapter)({ a: 1 }).on("finish", finishA) | ||
await apicase(rejectAdapter)({ a: 1 }).on("finish", finishB) | ||
expect(finishA).toBeCalled() | ||
@@ -297,3 +297,3 @@ expect(finishB).toBeCalled() | ||
it('emits cancel event on request cancel', async done => { | ||
it("emits cancel event on request cancel", async done => { | ||
const cancel = jest.fn(() => {}) | ||
@@ -305,3 +305,3 @@ const callback = ({ setCancelCallback, payload, resolve }) => { | ||
const a = apicase({ callback })({ a: 1 }).on('cancel', cancel) | ||
const a = apicase({ callback })({ a: 1 }).on("cancel", cancel) | ||
await a.cancel() | ||
@@ -312,3 +312,3 @@ expect(cancel).toBeCalled() | ||
it('emits done event on adapter done', async doneCb => { | ||
it("emits done event on adapter done", async doneCb => { | ||
const done1 = jest.fn(result => { | ||
@@ -321,5 +321,5 @@ expect(result).toEqual({ a: 1 }) | ||
await apicase(resolveAdapter)({ a: 1 }).on('done', done1) | ||
await apicase(resolveAdapter)({ a: 1 }).on("done", done1) | ||
expect(done1).toBeCalled() | ||
await apicase(rejectAdapter)({ a: 1 }).on('done', done2) | ||
await apicase(rejectAdapter)({ a: 1 }).on("done", done2) | ||
expect(done2).not.toBeCalled() | ||
@@ -329,3 +329,3 @@ doneCb() | ||
it('emits fail event on adapter fail', async done => { | ||
it("emits fail event on adapter fail", async done => { | ||
const fail1 = jest.fn(result => { | ||
@@ -338,5 +338,5 @@ expect(result).toEqual({ a: 1 }) | ||
await apicase(rejectAdapter)({ a: 1 }).on('fail', fail1) | ||
await apicase(rejectAdapter)({ a: 1 }).on("fail", fail1) | ||
expect(fail1).toBeCalled() | ||
await apicase(resolveAdapter)({ a: 1 }).on('fail', fail2) | ||
await apicase(resolveAdapter)({ a: 1 }).on("fail", fail2) | ||
expect(fail2).not.toBeCalled() | ||
@@ -346,19 +346,19 @@ done() | ||
it('emits change:state event on state change', () => {}) | ||
it("emits change:state event on state change", () => {}) | ||
it('emits change:payload event on payload change', () => {}) | ||
it("emits change:payload event on payload change", () => {}) | ||
it('emits change:result event on result change', () => {}) | ||
it("emits change:result event on result change", () => {}) | ||
}) | ||
describe('State', () => { | ||
describe('success', () => {}) | ||
describe("State", () => { | ||
describe("success", () => {}) | ||
describe('pending', () => {}) | ||
describe("pending", () => {}) | ||
describe('started', () => {}) | ||
describe("started", () => {}) | ||
describe('payload', () => {}) | ||
describe("payload", () => {}) | ||
describe('result', () => {}) | ||
describe("result", () => {}) | ||
}) |
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
455444
13132
9