Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@apicase/core

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@apicase/core - npm Package Compare versions

Comparing version 0.7.0-beta.7 to 0.7.0-beta.8

41

es/apicase.js

@@ -87,3 +87,4 @@ import _default3 from 'nanoutils/lib/mapObjIndexed';

const on = evt => cb => {
res.on(evt, cb);
bus.on(evt, cb);
return res;
};

@@ -99,8 +100,20 @@ const res = {

},
on: (evt, cb) => {
bus.on(evt, cb);
on: (evt, cb) => on(evt)(cb),
onDone: on('done'),
onFail: on('fail'),
off: (evt, cb) => {
const idx = bus.events.indexOf(cb);
if (idx !== -1) {
bus.events[name].splice(idx, 1);
}
return res;
},
onDone: on('done'),
onFail: on('fail'),
start: () => {
res.promise = composedHooks.before(req.payload);
res.promise.catch(function (err) {
bus.emit('error', err);
return Promise.reject(err);
});
return res;
},
cancel: () => {

@@ -218,11 +231,17 @@ return Promise.resolve().then(() => cancelCallback()).then(() => {

// Start queue and add to res
res.promise = composedHooks.before(req.payload);
res.promise.catch(function (err) {
bus.emit('error', err);
return Promise.reject(err);
});
res.promise = Promise.resolve(null);
res.then = cb => res.promise.then(cb);
res.catch = cb => res.promise.catch(cb);
if (req.options.immediate === true || req.options.immediate === undefined) {
res.start();
}
if (req.options.timer) {
setTimeout(res.start, req.options.timer);
}
if (req.options.immediate !== false) {
res.start();
}
return res;

@@ -229,0 +248,0 @@ };

@@ -5,2 +5,5 @@ export default {

meta: {},
options: {
immediate: true
},
hooks: {

@@ -7,0 +10,0 @@ before: [],

@@ -17,2 +17,4 @@ import _default5 from 'nanoutils/lib/map';

options: adapter => _default((from, to) => to),
payload: adapter => adapter && adapter.merge ? adapter.merge : _default2

@@ -19,0 +21,0 @@ };

@@ -52,2 +52,3 @@ import _default5 from 'nanoutils/lib/omit';

hooks: opts.hooks,
options: opts.options,
payload: _default5(['adapter', 'meta', 'hooks'], opts)

@@ -54,0 +55,0 @@ });

@@ -1,23 +0,66 @@

import { apiCall } from './call';
import NanoEvents from 'nanoevents';
import { mergeOptions } from './merge';
/*
TODO: Rework this
Should have .on callback too
*/
export function ApiQueue() {}
/**
* Create a queue of requests
* @param {Object} [options={ immediate: true }] Object with options
* @param {boolean} options.immediate options.immediate Start queue automatically
*/
export function ApiQueue(opts) {
opts = Object.assign({ immediate: true }, opts);
this.queue = [];
const bus = new NanoEvents();
// export function apiQueue(optsArray, prev) {
// if (!optsArray.length) return Promise.resolve(prev)
// var opt =
// typeof optsArray[0] === 'function' ? optsArray[0](prev) : optsArray[0]
// if (process.env.NODE_ENV !== 'production') {
// if (typeof opt !== 'object') {
// throw new TypeError(
// '[apicase.apiQueue] Expected options to be an Object but some of them are not'
// )
// }
// }
// return apiCall(opt).then(function(res) {
// return apiQueue(optsArray.slice(1), res)
// })
// }
this.push = function (cb, opts) {
const request = cb(mergeOptions(null, [opts, { options: { immediate: false, timer: null } }]));
this.queue.push(request);
request.on('done', (res, state) => {
if (this.queue.indexOf(request) === this.queue.length - 1) {
bus.emit('done', res, state);
}
});
request.on('fail', (res, state) => {
bus.emit('fail', res, state);
});
request.on('finish', () => this.remove(request));
request.on('cancel', () => this.remove(request));
if (this.queue.length === 1) {
if (opts.immediate) {
this.queue[0].start();
}
} else {
const preLast = this.queue[this.queue.length - 2];
preLast.on('done', () => {
const idx = this.queue.indexOf(preLast) + 1;
if (this.queue[idx]) {
this.queue[idx].start();
}
});
}
return request;
};
this.remove = function (request) {
this.queue.splice(this.queue.indexOf(request), 1);
if (!this.queue.length) {
bus.emit('finish');
}
};
this.on = function (evt, cb) {
bus.on(evt, cb);
return this;
};
this.off = function (evt, cb) {
const idx = bus.events.indexOf(cb);
if (idx !== -1) {
bus.events[name].splice(idx, 1);
}
return this;
};
}

@@ -72,3 +72,3 @@ import _default from 'nanoutils/lib/equals';

before: ({ payload, next }) => {
this.queue[this.queue.length - 1].on('finish', () => next(payload));
this.queue[this.queue.length - 1].on('finish', () => next(payload)).on('cancel', () => next(payload));
}

@@ -75,0 +75,0 @@ }

{
"name": "@apicase/core",
"version": "0.7.0-beta.7",
"version": "0.7.0-beta.8",
"description": "Core library to make API calls with any adapter",

@@ -13,3 +13,3 @@ "main": "es/index.js",

"ghooks": {
"pre-commit": "npm run lint"
"pre-commit": "npm run check"
}

@@ -21,3 +21,4 @@ },

"test": "jest",
"build": "node build.js && npm run test && npm run size",
"check": "npm run lint && npm run test && npm run size",
"build": "node build.js && npm run check",
"prepublish": "npm run build"

@@ -24,0 +25,0 @@ },

@@ -46,3 +46,3 @@ import { apicase } from '../lib/apicase'

expect(typeof reject).toBe('function')
resolve(payload)
setTimeout(resolve, 100, payload)
})

@@ -59,3 +59,3 @@

expect(result).toEqual({ a: 1, b: 2 })
resolve(payload)
setTimeout(resolve, 100, payload)
})

@@ -275,10 +275,13 @@

it('emits done event on adapter done', async doneCb => {
const done = jest.fn(result => {
const done1 = jest.fn(result => {
expect(result).toEqual({ a: 1 })
})
const done2 = jest.fn(result => {
expect(result).toEqual({ a: 1 })
})
await apicase(resolveAdapter)({ a: 1 }).on('done', done)
expect(done).toBeCalled()
await apicase(rejectAdapter)({ a: 1 }).on('done', done)
expect(done).toHaveBeenCalledTimes(1)
await apicase(resolveAdapter)({ a: 1 }).on('done', done1)
expect(done1).toBeCalled()
await apicase(rejectAdapter)({ a: 1 }).on('done', done2)
expect(done2).not.toBeCalled()
doneCb()

@@ -288,10 +291,13 @@ })

it('emits fail event on adapter fail', async done => {
const fail = jest.fn(result => {
const fail1 = jest.fn(result => {
expect(result).toEqual({ a: 1 })
})
const fail2 = jest.fn(result => {
expect(result).toEqual({ a: 1 })
})
await apicase(rejectAdapter)({ a: 1 }).on('fail', fail)
expect(fail).toBeCalled()
await apicase(resolveAdapter)({ a: 1 }).on('fail', fail)
expect(fail).toHaveBeenCalledTimes(1)
await apicase(rejectAdapter)({ a: 1 }).on('fail', fail1)
expect(fail1).toBeCalled()
await apicase(resolveAdapter)({ a: 1 }).on('fail', fail2)
expect(fail2).not.toBeCalled()
done()

@@ -298,0 +304,0 @@ })

@@ -9,2 +9,5 @@ import { mergeOptions } from '../lib/merge'

meta: {},
options: {
immediate: true
},
hooks: {

@@ -11,0 +14,0 @@ before: [],

@@ -65,2 +65,5 @@ import { normalizeOptions } from '../lib/normalize'

meta: {},
options: {
immediate: true
},
hooks: {

@@ -67,0 +70,0 @@ before: [opts.hooks.before],

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc