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

@argab/request-service

Package Overview
Dependencies
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@argab/request-service - npm Package Compare versions

Comparing version 3.2.0 to 3.2.1

66

dist/Request.js

@@ -8,4 +8,6 @@ "use strict";

});
exports.AbstractRequest = exports.Request = exports.RequestService = exports.REQUEST_RESOLVE_METHODS = void 0;
exports.AbstractRequest = exports.Request = exports.RequestService = void 0;
var _classPrivateFieldGet2 = _interopRequireDefault(require("@babel/runtime/helpers/classPrivateFieldGet"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

@@ -45,2 +47,4 @@

function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
var AbstractRequest = /*#__PURE__*/function () {

@@ -110,2 +114,4 @@ function AbstractRequest() {

var _proxy = /*#__PURE__*/new WeakSet();
var RequestService = /*#__PURE__*/function (_AbstractRequest) {

@@ -131,2 +137,5 @@ (0, _inherits2["default"])(RequestService, _AbstractRequest);

_this = _super.call(this);
_proxy.add((0, _assertThisInitialized2["default"])(_this));
_this._getRepo = getRepo instanceof Function ? getRepo : function () {

@@ -143,4 +152,2 @@ return null;

_this._retry = (0, _helpers.isPrototype)(_RequestRetry.RequestRetry, requestRetry) ? requestRetry : _RequestRetry.RequestRetry;
config instanceof Object && Object.assign(_this._config, config);
extend instanceof Object && Object.assign(_this._extend, extend);
_this._factory = new _this._factory({

@@ -150,2 +157,4 @@ request: request,

});
config instanceof Object && Object.assign(_this._config, config);
extend instanceof Object && Object.assign(_this._extend, extend);

@@ -155,11 +164,5 @@ var _extend = _this["extends"]().middleware;

_extend instanceof Object && Object.keys(_extend).forEach(function (key) {
_this._middleware.prototype[key] = _extend[key];
return _this._middleware.prototype[key] = _extend[key];
});
return (0, _possibleConstructorReturn2["default"])(_this, (0, _helpers.proxy)((0, _assertThisInitialized2["default"])(_this), null, function (state, method, args) {
if (false === ['repo', 'stub'].includes(method) && state[method] instanceof Function) {
return state[method](args[0]);
}
return (0, _helpers.applyCall)(new state._middleware(state), method, args);
}));
return (0, _possibleConstructorReturn2["default"])(_this, _classPrivateMethodGet((0, _assertThisInitialized2["default"])(_this), _proxy, _proxy2).call((0, _assertThisInitialized2["default"])(_this)));
}

@@ -202,5 +205,17 @@

exports.RequestService = RequestService;
var REQUEST_RESOLVE_METHODS = ['then', 'catch', 'finally', 'success', 'error'];
exports.REQUEST_RESOLVE_METHODS = REQUEST_RESOLVE_METHODS;
function _proxy2() {
return (0, _helpers.proxy)(this, null, function (state, method, args) {
if (false === ['repo', 'stub'].includes(method) && state[method] instanceof Function) {
return state[method](args[0]);
}
return (0, _helpers.applyCall)(new state._middleware(state), method, args);
});
}
var _methods = /*#__PURE__*/new WeakMap();
var _resolveMethods = /*#__PURE__*/new WeakMap();
var Request = /*#__PURE__*/function () {

@@ -211,5 +226,15 @@ function Request(data) {

(0, _defineProperty2["default"])(this, "chain", []);
(0, _defineProperty2["default"])(this, "_methods", ['then', 'catch', 'finally', 'success', 'error', 'retry', 'retryOnCatch', 'retryChain', 'retryMaxCount', 'retryTimeout']);
(0, _defineProperty2["default"])(this, "_fetch", void 0);
(0, _defineProperty2["default"])(this, "_resolve", void 0);
_methods.set(this, {
writable: true,
value: ['then', 'catch', 'finally', 'success', 'error', 'retry', 'retryOnCatch', 'retryChain', 'retryMaxCount', 'retryTimeout']
});
_resolveMethods.set(this, {
writable: true,
value: ['then', 'catch', 'finally', 'success', 'error']
});
this.data = data;

@@ -223,2 +248,15 @@ }

(0, _createClass2["default"])(Request, [{
key: "methods",
get: function get() {
return (0, _classPrivateFieldGet2["default"])(this, _methods).slice();
},
set: function set(name) {
return (0, _classPrivateFieldGet2["default"])(this, _methods).push(name);
}
}, {
key: "resolveMethods",
get: function get() {
return (0, _classPrivateFieldGet2["default"])(this, _resolveMethods).slice();
}
}, {
key: "then",

@@ -225,0 +263,0 @@ value: function then(callback) {}

@@ -60,4 +60,3 @@ "use strict";

extend instanceof Object && Object.keys(extend).forEach(function (key) {
request._methods.push(key);
request.methods = key;
request[key] = extend[key];

@@ -70,2 +69,32 @@ });

}, {
key: "createOrAssign",
value: function createOrAssign(request, data, config) {
data || (data = {});
config || (config = {});
if (request && request instanceof _Request.Request) {
request.chain = [];
request.data = (0, _helpers.mergeDeep)(request.data, data);
Object.assign(request.data, {
method: data.method,
uri: data.uri,
params: data.params,
repo: null,
repoPath: null,
repoMethod: null,
statusCode: 0,
dataError: null,
result: null
});
return request;
}
return this.create({
method: data.method,
uri: data.uri,
params: data.params,
config: config
});
}
}, {
key: "getClient",

@@ -72,0 +101,0 @@ value: function getClient(data) {

@@ -239,10 +239,5 @@ "use strict";

});
request._methods.forEach(function (method) {
request.methods.forEach(function (method) {
return request[method] instanceof Function && (request._fetch[method] = function (arg) {
if (_Request.REQUEST_RESOLVE_METHODS.includes(method)) {
arg instanceof Function || (arg = function arg() {
return undefined;
});
} else request[method](arg);
request.resolveMethods.includes(method) ? arg instanceof Function || (arg = function arg() {}) : request[method](arg);

@@ -261,3 +256,2 @@ _this3._resolve.push({

});
return request._fetch;

@@ -264,0 +258,0 @@ }

@@ -16,2 +16,4 @@ "use strict";

var _classPrivateFieldGet2 = _interopRequireDefault(require("@babel/runtime/helpers/classPrivateFieldGet"));
var _helpers = require("./helpers");

@@ -27,113 +29,49 @@

var RequestMiddleware = /*#__PURE__*/function () {
function RequestMiddleware(service) {
var request = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
(0, _classCallCheck2["default"])(this, RequestMiddleware);
(0, _defineProperty2["default"])(this, "_staged", {});
(0, _defineProperty2["default"])(this, "_chain", []);
(0, _defineProperty2["default"])(this, "_service", void 0);
(0, _defineProperty2["default"])(this, "_request", void 0);
(0, _defineProperty2["default"])(this, "_repo", null);
(0, _defineProperty2["default"])(this, "_repoPath", null);
(0, _defineProperty2["default"])(this, "_repoMethod", null);
(0, _defineProperty2["default"])(this, "_runRepo", false);
if (!(service instanceof _Request.RequestService)) throw 'The RequestMiddleware`s "service" is not an instance of "RequestService".';
if (request && !(request instanceof _Request.Request)) throw 'The RequestMiddleware`s "request" is not an instance of "Request".';
this._service = service;
this._request = request;
function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
var _proxy = function _proxy(state) {
return (0, _helpers.proxy)(state, null, function (state, method, args) {
var _state$_request;
var _repo = /*#__PURE__*/new WeakMap();
if (state._runRepo) {
state._runRepo = false;
var _chain = /*#__PURE__*/new WeakMap();
state._chain.push({
method: method,
args: args
});
var _proxy = /*#__PURE__*/new WeakSet();
state._repoMethod = method;
return (0, _helpers.applyCall)(state._repo, method, args);
}
var _runRepo = /*#__PURE__*/new WeakSet();
if (['repo', 'stub'].includes(method)) {
state._chain.push({
method: method,
args: args
});
var _runManager = /*#__PURE__*/new WeakSet();
state._repo = (0, _helpers.applyCall)(state._service, method, args);
state._repo instanceof _Interfaces.RequestRepository && (state._repo.client = _proxy(state));
state._repoPath = args[0];
state._runRepo = true;
return _proxy(state);
}
var RequestMiddleware = /*#__PURE__*/function () {
function RequestMiddleware(service) {
var request = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
(0, _classCallCheck2["default"])(this, RequestMiddleware);
var client = state._staged.client || ((_state$_request = state._request) === null || _state$_request === void 0 ? void 0 : _state$_request.data.client) || state._service._config.client;
_runManager.add(this);
if (state._service._factory.getClientPrototype({
client: client
}).prototype[method] instanceof Function) {
var uri = args[0];
var params = args[1];
_runRepo.add(this);
if (state._request) {
state._request.chain = [];
state._request.data = (0, _helpers.mergeDeep)(state._request.data, state._staged);
Object.assign(state._request.data, {
method: method,
uri: uri,
params: params,
repo: null,
repoPath: null,
repoMethod: null,
statusCode: 0,
dataError: null,
result: null
});
} else {
state._request = state._service._factory.create({
method: method,
uri: uri,
params: params,
config: (0, _helpers.mergeDeep)(_objectSpread({}, state._service._config), state._staged)
});
}
_proxy.add(this);
Object.assign(state._request.data, {
repo: state._repo,
repoPath: state._repoPath,
repoMethod: state._repoMethod
});
var manager = new state._service._manager({
request: state._request,
service: state._service
});
(0, _defineProperty2["default"])(this, "_staged", {});
(0, _defineProperty2["default"])(this, "_service", void 0);
(0, _defineProperty2["default"])(this, "_request", void 0);
state._chain.push({
method: method,
args: args
});
_repo.set(this, {
writable: true,
value: {
instance: null,
path: null,
method: null,
run: false
}
});
state._request.chain = state._chain;
state._request._fetch || manager.save();
manager.send();
return manager.fetch();
}
_chain.set(this, {
writable: true,
value: []
});
if (state[method] instanceof Function) {
state._chain.push({
method: method,
args: args
});
(0, _helpers.applyCall)(state, method, args);
return _proxy(state);
}
});
};
return _proxy(this);
if (!(service instanceof _Request.RequestService)) throw 'The RequestMiddleware`s "service" is not an instance of "RequestService".';
if (request && !(request instanceof _Request.Request)) throw 'The RequestMiddleware`s "request" is not an instance of "Request".';
this._service = service;
this._request = request;
return _classPrivateMethodGet(this, _proxy, _proxy2).call(this);
}

@@ -150,2 +88,79 @@

exports.RequestMiddleware = RequestMiddleware;
exports.RequestMiddleware = RequestMiddleware;
function _proxy2() {
var _this = this;
return (0, _helpers.proxy)(this, null, function (state, method, args) {
var _state$_request;
var runRepo = _classPrivateMethodGet(state, _runRepo, _runRepo2).call(state, method, args);
if (runRepo) return runRepo;
var client = state._staged.client || ((_state$_request = state._request) === null || _state$_request === void 0 ? void 0 : _state$_request.data.client) || state._service._config.client;
if (state._service._factory.getClientPrototype({
client: client
}).prototype[method] instanceof Function) {
return _classPrivateMethodGet(_this, _runManager, _runManager2).call(_this, method, args);
}
if (state[method] instanceof Function) {
(0, _classPrivateFieldGet2["default"])(state, _chain).push({
method: method,
args: args
});
(0, _helpers.applyCall)(state, method, args);
return _classPrivateMethodGet(state, _proxy, _proxy2).call(state);
}
});
}
function _runRepo2(method, args) {
if ((0, _classPrivateFieldGet2["default"])(this, _repo).run) {
(0, _classPrivateFieldGet2["default"])(this, _repo).run = false;
(0, _classPrivateFieldGet2["default"])(this, _repo).method = method;
(0, _classPrivateFieldGet2["default"])(this, _chain).push({
method: method,
args: args
});
return (0, _helpers.applyCall)((0, _classPrivateFieldGet2["default"])(this, _repo).instance, method, args);
}
if (['repo', 'stub'].includes(method)) {
(0, _classPrivateFieldGet2["default"])(this, _chain).push({
method: method,
args: args
});
(0, _classPrivateFieldGet2["default"])(this, _repo).instance = (0, _helpers.applyCall)(this._service, method, args);
(0, _classPrivateFieldGet2["default"])(this, _repo).instance instanceof _Interfaces.RequestRepository && ((0, _classPrivateFieldGet2["default"])(this, _repo).instance.client = _classPrivateMethodGet(this, _proxy, _proxy2).call(this));
(0, _classPrivateFieldGet2["default"])(this, _repo).path = args[0];
(0, _classPrivateFieldGet2["default"])(this, _repo).run = true;
return _classPrivateMethodGet(this, _proxy, _proxy2).call(this);
}
}
function _runManager2(method, args) {
this._request = this._service._factory.createOrAssign(this._request, _objectSpread(_objectSpread({}, this._staged), {}, {
uri: args[0],
params: args[1],
method: method
}), (0, _helpers.mergeDeep)(_objectSpread({}, this._service._config), this._staged));
Object.assign(this._request.data, {
repo: (0, _classPrivateFieldGet2["default"])(this, _repo).instance,
repoPath: (0, _classPrivateFieldGet2["default"])(this, _repo).path,
repoMethod: (0, _classPrivateFieldGet2["default"])(this, _repo).method
});
var manager = new this._service._manager({
request: this._request,
service: this._service
});
(0, _classPrivateFieldGet2["default"])(this, _chain).push({
method: method,
args: args
});
this._request.chain = (0, _classPrivateFieldGet2["default"])(this, _chain);
this._request._fetch || manager.save();
manager.send();
return manager.fetch();
}
{
"name": "@argab/request-service",
"version": "3.2.0",
"version": "3.2.1",
"description": "Provides a fully customizable Library for handling API using Request repositories and Stubs.",

@@ -5,0 +5,0 @@ "repository": {

@@ -70,3 +70,5 @@ import {RequestFactory} from "./RequestFactory"

constructor({config, getRepo, getStub, useStubs, extend, factory, manager, middleware, request, requestRetry}) {
super()
this._getRepo = getRepo instanceof Function ? getRepo : () => null

@@ -79,2 +81,3 @@ this._getStub = getStub instanceof Function ? getStub : () => null

this._retry = isPrototype(RequestRetry, requestRetry) ? requestRetry : RequestRetry
this._factory = new this._factory({request, service: this})

@@ -84,15 +87,6 @@ config instanceof Object && Object.assign(this._config, config)

this._factory = new this._factory({request, service: this})
const _extend = this.extends().middleware
_extend instanceof Object && Object.keys(_extend).forEach(key => {
this._middleware.prototype[key] = _extend[key]
})
_extend instanceof Object && Object.keys(_extend).forEach(key => this._middleware.prototype[key] = _extend[key])
return proxy(this, null, (state, method, args) => {
if (false === ['repo', 'stub'].includes(method) && state[method] instanceof Function) {
return state[method](args[0])
}
return applyCall(new state._middleware(state), method, args)
})
return this.#proxy()
}

@@ -123,6 +117,13 @@

}
#proxy() {
return proxy(this, null, (state, method, args) => {
if (false === ['repo', 'stub'].includes(method) && state[method] instanceof Function) {
return state[method](args[0])
}
return applyCall(new state._middleware(state), method, args)
})
}
}
export const REQUEST_RESOLVE_METHODS = ['then', 'catch', 'finally', 'success', 'error']
class Request {

@@ -133,6 +134,20 @@

_methods = ['then', 'catch', 'finally', 'success', 'error', 'retry', 'retryOnCatch', 'retryChain', 'retryMaxCount', 'retryTimeout']
_fetch
_resolve
#methods = ['then', 'catch', 'finally', 'success', 'error', 'retry', 'retryOnCatch', 'retryChain', 'retryMaxCount', 'retryTimeout']
#resolveMethods = ['then', 'catch', 'finally', 'success', 'error']
get methods() {
return this.#methods.slice()
}
set methods(name) {
return this.#methods.push(name)
}
get resolveMethods() {
return this.#resolveMethods.slice()
}
constructor(data) {

@@ -139,0 +154,0 @@ this.data = data

import {RequestClient, RequestHandler, RequestLoader} from "./Interfaces"
import {AbstractRequest, Request} from "./Request"
import {isPrototype} from "./helpers"
import {isPrototype, mergeDeep} from "./helpers"

@@ -26,3 +26,3 @@ class RequestFactory {

extend instanceof Object && Object.keys(extend).forEach(key => {
request._methods.push(key)
request.methods = key
request[key] = extend[key]

@@ -35,2 +35,36 @@ })

createOrAssign(request, data, config) {
data ||= {}
config ||= {}
if (request && request instanceof Request) {
request.chain = []
request.data = mergeDeep(request.data, data)
Object.assign(request.data, {
method: data.method,
uri: data.uri,
params: data.params,
repo: null,
repoPath: null,
repoMethod: null,
statusCode: 0,
dataError: null,
result: null,
})
return request
}
return this.create({
method: data.method,
uri: data.uri,
params: data.params,
config
})
}
getClient(data) {

@@ -50,3 +84,5 @@ const client = this.getClientPrototype(data)

let handlers = data.handler || this._handler
Array.isArray(handlers) || (handlers = [handlers])
handlers.forEach(handler => {

@@ -53,0 +89,0 @@ if (isPrototype(RequestHandler, handler)) {

import {RequestService, Request} from "./Request"
import {REQUEST_RESOLVE_METHODS} from "./Request"

@@ -14,2 +13,3 @@ class RequestManager {

constructor({request, service}) {
if (!(request instanceof Request)) throw 'The RequestManager`s "request" is not an instance of "Request".'

@@ -81,13 +81,15 @@ if (!(service instanceof RequestService)) throw 'The RequestManager`s "service" is not an instance of "RequestService".'

fetch() {
const request = this._request
if (request._fetch instanceof Promise) return request._fetch
request._fetch = new Promise(resolve => request._resolve = () => resolve(request.data.result))
request._methods.forEach(method => request[method] instanceof Function && (request._fetch[method] = (arg) => {
if (REQUEST_RESOLVE_METHODS.includes(method)) {
arg instanceof Function || (arg = () => undefined)
} else request[method](arg)
request.methods.forEach(method => request[method] instanceof Function && (request._fetch[method] = (arg) => {
request.resolveMethods.includes(method) ? (arg instanceof Function || (arg = () => {})) : request[method](arg)
this._resolve.push({method, arg})
request.chain.push({method, args: [arg]})

@@ -94,0 +96,0 @@

@@ -8,12 +8,14 @@ import {mergeDeep, proxy, applyCall} from "./helpers"

_staged = {}
_chain = []
_service
_request
_repo = null
_repoPath = null
_repoMethod = null
_runRepo = false
#repo = {
instance: null,
path: null,
method: null,
run: false,
}
#chain = []
constructor(service, request = null) {

@@ -27,82 +29,81 @@

const _proxy = (state) => proxy(state, null, (state, method, args) => {
return this.#proxy()
}
if (state._runRepo) {
state._runRepo = false
state._chain.push({method, args})
state._repoMethod = method
return applyCall(state._repo, method, args)
}
config(data) {
data instanceof Object && (this._staged = mergeDeep(this._staged, data))
}
if (['repo', 'stub'].includes(method)) {
state._chain.push({method, args})
state._repo = applyCall(state._service, method, args)
state._repo instanceof RequestRepository && (state._repo.client = _proxy(state))
state._repoPath = args[0]
state._runRepo = true
return _proxy(state)
}
#proxy() {
const client = state._staged.client || state._request?.data.client || state._service._config.client
return proxy(this, null, (state, method, args) => {
if (state._service._factory.getClientPrototype({client}).prototype[method] instanceof Function) {
const runRepo = state.#runRepo(method, args)
const uri = args[0]
const params = args[1]
if (runRepo) return runRepo
if (state._request) {
state._request.chain = []
state._request.data = mergeDeep(state._request.data, state._staged)
Object.assign(state._request.data, {
method,
uri,
params,
repo: null,
repoPath: null,
repoMethod: null,
statusCode: 0,
dataError: null,
result: null,
})
} else {
state._request = state._service._factory.create({
method,
uri,
params,
config: mergeDeep({...state._service._config}, state._staged)
})
}
const client = state._staged.client || state._request?.data.client || state._service._config.client
Object.assign(state._request.data, {
repo: state._repo,
repoPath: state._repoPath,
repoMethod: state._repoMethod
})
if (state._service._factory.getClientPrototype({client}).prototype[method] instanceof Function) {
const manager = new state._service._manager({
request: state._request,
service: state._service
})
return this.#runManager(method, args)
}
state._chain.push({method, args})
state._request.chain = state._chain
state._request._fetch || manager.save()
if (state[method] instanceof Function) {
manager.send()
return manager.fetch()
state.#chain.push({method, args})
}
applyCall(state, method, args)
if (state[method] instanceof Function) {
state._chain.push({method, args})
applyCall(state, method, args)
return _proxy(state)
return state.#proxy()
}
})
}
return _proxy(this)
#runRepo(method, args) {
if (this.#repo.run) {
this.#repo.run = false
this.#repo.method = method
this.#chain.push({method, args})
return applyCall(this.#repo.instance, method, args)
}
if (['repo', 'stub'].includes(method)) {
this.#chain.push({method, args})
this.#repo.instance = applyCall(this._service, method, args)
this.#repo.instance instanceof RequestRepository && (this.#repo.instance.client = this.#proxy())
this.#repo.path = args[0]
this.#repo.run = true
return this.#proxy()
}
}
config(data) {
data instanceof Object && (this._staged = mergeDeep(this._staged, data))
#runManager(method, args) {
this._request = this._service._factory.createOrAssign(this._request, {
...this._staged,
uri: args[0],
params: args[1],
method
}, mergeDeep({...this._service._config}, this._staged))
Object.assign(this._request.data, {
repo: this.#repo.instance,
repoPath: this.#repo.path,
repoMethod: this.#repo.method
})
const manager = new this._service._manager({
request: this._request,
service: this._service
})
this.#chain.push({method, args})
this._request.chain = this.#chain
this._request._fetch || manager.save()
manager.send()
return manager.fetch()
}

@@ -109,0 +110,0 @@ }

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