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

kefetchup

Package Overview
Dependencies
Maintainers
3
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kefetchup - npm Package Compare versions

Comparing version 1.0.7 to 1.1.0

45

dist/kefetchup.es.js

@@ -74,14 +74,9 @@ class ResponseError extends Error {

* Creates an instance of GenericAPIClient.
* @param {string} [baseURL=''] a base url to prepend to all request urls except for the ones with root urls
* @param {RequestInit} [baseClientConfig={}] a default config for requests
* @param {string} [$baseURL=''] a base url to prepend to all request urls except for the ones with root urls
* @param {RequestInit} [$baseClientConfig={}] a default config for requests
*/
constructor(baseURL = '', baseClientConfig = {}) {
this.baseURL = baseURL;
this.baseClientConfig = baseClientConfig;
this.fetchHandler = window.fetch ? window.fetch.bind(window) : defaultFetch;
this.get = this.alias('get');
this.put = this.alias('put');
this.post = this.alias('post');
this.patch = this.alias('patch');
this.delete = this.alias('delete');
constructor($baseURL = '', $baseClientConfig = {}) {
this.$baseURL = $baseURL;
this.$baseClientConfig = $baseClientConfig;
this.$fetchHandler = window.fetch ? window.fetch.bind(window) : defaultFetch;
}

@@ -93,8 +88,8 @@ /**

*/
request(url, fetchConfig, overrideDefaultConfig = false) {
$request(url, fetchConfig, overrideDefaultConfig = false) {
if (!url.match(/^(\w+:)?\/\//)) {
url = this.baseURL ? new URL(url, this.baseURL).href : url;
url = this.$baseURL ? new URL(url, this.$baseURL).href : url;
}
return this.requestFactory(url, overrideDefaultConfig ?
fetchConfig : Object.assign({}, this.baseClientConfig, fetchConfig, { headers: Object.assign({}, (this.baseClientConfig.headers || {}), (fetchConfig.headers || {})) }), this.fetchHandler);
return this.$requestFactory(url, overrideDefaultConfig ?
fetchConfig : Object.assign({}, this.$baseClientConfig, fetchConfig, { headers: Object.assign({}, (this.$baseClientConfig.headers || {}), (fetchConfig.headers || {})) }), this.$fetchHandler);
}

@@ -111,3 +106,3 @@ /**

*/
responseHandler(response) {
$responseHandler(response) {
if (response.ok) {

@@ -129,3 +124,3 @@ return response;

*/
errorHandler(e) {
$errorHandler(e) {
if (e instanceof ResponseError) {

@@ -149,6 +144,6 @@ throw e;

*/
requestFactory(url, config, requestFunction) {
$requestFactory(url, config, requestFunction) {
return requestFunction(url, config)
.then(r => this.responseHandler(r))
.catch(e => this.errorHandler(e));
.then(r => this.$responseHandler(r))
.catch(e => this.$errorHandler(e));
}

@@ -165,7 +160,7 @@ /**

*/
alias(method) {
return function (url, fetchConfig = this.baseClientConfig, overrideDefaultConfig) {
$alias(method) {
return function (url, fetchConfig = this.$baseClientConfig, overrideDefaultConfig) {
fetchConfig = fetchConfig;
fetchConfig.method = method ? method.toUpperCase() : (fetchConfig.method || 'GET').toUpperCase();
return this.request(url, fetchConfig, overrideDefaultConfig);
return this.$request(url, fetchConfig, overrideDefaultConfig);
};

@@ -217,3 +212,3 @@ }

*/
responseHandler(resp) {
$responseHandler(resp) {
return resp.json();

@@ -229,3 +224,3 @@ }

*/
responseHandler(resp) {
$responseHandler(resp) {
return resp.text();

@@ -232,0 +227,0 @@ }

@@ -117,16 +117,11 @@ 'use strict';

* Creates an instance of GenericAPIClient.
* @param {string} [baseURL=''] a base url to prepend to all request urls except for the ones with root urls
* @param {RequestInit} [baseClientConfig={}] a default config for requests
* @param {string} [$baseURL=''] a base url to prepend to all request urls except for the ones with root urls
* @param {RequestInit} [$baseClientConfig={}] a default config for requests
*/
function GenericAPIClient(baseURL, baseClientConfig) {
if (baseURL === void 0) { baseURL = ''; }
if (baseClientConfig === void 0) { baseClientConfig = {}; }
this.baseURL = baseURL;
this.baseClientConfig = baseClientConfig;
this.fetchHandler = window.fetch ? window.fetch.bind(window) : defaultFetch;
this.get = this.alias('get');
this.put = this.alias('put');
this.post = this.alias('post');
this.patch = this.alias('patch');
this.delete = this.alias('delete');
function GenericAPIClient($baseURL, $baseClientConfig) {
if ($baseURL === void 0) { $baseURL = ''; }
if ($baseClientConfig === void 0) { $baseClientConfig = {}; }
this.$baseURL = $baseURL;
this.$baseClientConfig = $baseClientConfig;
this.$fetchHandler = window.fetch ? window.fetch.bind(window) : defaultFetch;
}

@@ -138,9 +133,9 @@ /**

*/
GenericAPIClient.prototype.request = function (url, fetchConfig, overrideDefaultConfig) {
GenericAPIClient.prototype.$request = function (url, fetchConfig, overrideDefaultConfig) {
if (overrideDefaultConfig === void 0) { overrideDefaultConfig = false; }
if (!url.match(/^(\w+:)?\/\//)) {
url = this.baseURL ? new URL(url, this.baseURL).href : url;
url = this.$baseURL ? new URL(url, this.$baseURL).href : url;
}
return this.requestFactory(url, overrideDefaultConfig ?
fetchConfig : __assign({}, this.baseClientConfig, fetchConfig, { headers: __assign({}, (this.baseClientConfig.headers || {}), (fetchConfig.headers || {})) }), this.fetchHandler);
return this.$requestFactory(url, overrideDefaultConfig ?
fetchConfig : __assign({}, this.$baseClientConfig, fetchConfig, { headers: __assign({}, (this.$baseClientConfig.headers || {}), (fetchConfig.headers || {})) }), this.$fetchHandler);
};

@@ -157,3 +152,3 @@ /**

*/
GenericAPIClient.prototype.responseHandler = function (response) {
GenericAPIClient.prototype.$responseHandler = function (response) {
if (response.ok) {

@@ -175,3 +170,3 @@ return response;

*/
GenericAPIClient.prototype.errorHandler = function (e) {
GenericAPIClient.prototype.$errorHandler = function (e) {
if (e instanceof ResponseError) {

@@ -195,7 +190,7 @@ throw e;

*/
GenericAPIClient.prototype.requestFactory = function (url, config, requestFunction) {
GenericAPIClient.prototype.$requestFactory = function (url, config, requestFunction) {
var _this = this;
return requestFunction(url, config)
.then(function (r) { return _this.responseHandler(r); })
.catch(function (e) { return _this.errorHandler(e); });
.then(function (r) { return _this.$responseHandler(r); })
.catch(function (e) { return _this.$errorHandler(e); });
};

@@ -212,8 +207,8 @@ /**

*/
GenericAPIClient.prototype.alias = function (method) {
GenericAPIClient.prototype.$alias = function (method) {
return function (url, fetchConfig, overrideDefaultConfig) {
if (fetchConfig === void 0) { fetchConfig = this.baseClientConfig; }
if (fetchConfig === void 0) { fetchConfig = this.$baseClientConfig; }
fetchConfig = fetchConfig;
fetchConfig.method = method ? method.toUpperCase() : (fetchConfig.method || 'GET').toUpperCase();
return this.request(url, fetchConfig, overrideDefaultConfig);
return this.$request(url, fetchConfig, overrideDefaultConfig);
};

@@ -271,3 +266,3 @@ };

*/
JsonAPIClient.prototype.responseHandler = function (resp) {
JsonAPIClient.prototype.$responseHandler = function (resp) {
return resp.json();

@@ -288,3 +283,3 @@ };

*/
TextAPIClient.prototype.responseHandler = function (resp) {
TextAPIClient.prototype.$responseHandler = function (resp) {
return resp.text();

@@ -291,0 +286,0 @@ };

@@ -116,16 +116,11 @@ var kefetchup = (function (exports) {

* Creates an instance of GenericAPIClient.
* @param {string} [baseURL=''] a base url to prepend to all request urls except for the ones with root urls
* @param {RequestInit} [baseClientConfig={}] a default config for requests
* @param {string} [$baseURL=''] a base url to prepend to all request urls except for the ones with root urls
* @param {RequestInit} [$baseClientConfig={}] a default config for requests
*/
function GenericAPIClient(baseURL, baseClientConfig) {
if (baseURL === void 0) { baseURL = ''; }
if (baseClientConfig === void 0) { baseClientConfig = {}; }
this.baseURL = baseURL;
this.baseClientConfig = baseClientConfig;
this.fetchHandler = window.fetch ? window.fetch.bind(window) : defaultFetch;
this.get = this.alias('get');
this.put = this.alias('put');
this.post = this.alias('post');
this.patch = this.alias('patch');
this.delete = this.alias('delete');
function GenericAPIClient($baseURL, $baseClientConfig) {
if ($baseURL === void 0) { $baseURL = ''; }
if ($baseClientConfig === void 0) { $baseClientConfig = {}; }
this.$baseURL = $baseURL;
this.$baseClientConfig = $baseClientConfig;
this.$fetchHandler = window.fetch ? window.fetch.bind(window) : defaultFetch;
}

@@ -137,9 +132,9 @@ /**

*/
GenericAPIClient.prototype.request = function (url, fetchConfig, overrideDefaultConfig) {
GenericAPIClient.prototype.$request = function (url, fetchConfig, overrideDefaultConfig) {
if (overrideDefaultConfig === void 0) { overrideDefaultConfig = false; }
if (!url.match(/^(\w+:)?\/\//)) {
url = this.baseURL ? new URL(url, this.baseURL).href : url;
url = this.$baseURL ? new URL(url, this.$baseURL).href : url;
}
return this.requestFactory(url, overrideDefaultConfig ?
fetchConfig : __assign({}, this.baseClientConfig, fetchConfig, { headers: __assign({}, (this.baseClientConfig.headers || {}), (fetchConfig.headers || {})) }), this.fetchHandler);
return this.$requestFactory(url, overrideDefaultConfig ?
fetchConfig : __assign({}, this.$baseClientConfig, fetchConfig, { headers: __assign({}, (this.$baseClientConfig.headers || {}), (fetchConfig.headers || {})) }), this.$fetchHandler);
};

@@ -156,3 +151,3 @@ /**

*/
GenericAPIClient.prototype.responseHandler = function (response) {
GenericAPIClient.prototype.$responseHandler = function (response) {
if (response.ok) {

@@ -174,3 +169,3 @@ return response;

*/
GenericAPIClient.prototype.errorHandler = function (e) {
GenericAPIClient.prototype.$errorHandler = function (e) {
if (e instanceof ResponseError) {

@@ -194,7 +189,7 @@ throw e;

*/
GenericAPIClient.prototype.requestFactory = function (url, config, requestFunction) {
GenericAPIClient.prototype.$requestFactory = function (url, config, requestFunction) {
var _this = this;
return requestFunction(url, config)
.then(function (r) { return _this.responseHandler(r); })
.catch(function (e) { return _this.errorHandler(e); });
.then(function (r) { return _this.$responseHandler(r); })
.catch(function (e) { return _this.$errorHandler(e); });
};

@@ -211,8 +206,8 @@ /**

*/
GenericAPIClient.prototype.alias = function (method) {
GenericAPIClient.prototype.$alias = function (method) {
return function (url, fetchConfig, overrideDefaultConfig) {
if (fetchConfig === void 0) { fetchConfig = this.baseClientConfig; }
if (fetchConfig === void 0) { fetchConfig = this.$baseClientConfig; }
fetchConfig = fetchConfig;
fetchConfig.method = method ? method.toUpperCase() : (fetchConfig.method || 'GET').toUpperCase();
return this.request(url, fetchConfig, overrideDefaultConfig);
return this.$request(url, fetchConfig, overrideDefaultConfig);
};

@@ -270,3 +265,3 @@ };

*/
JsonAPIClient.prototype.responseHandler = function (resp) {
JsonAPIClient.prototype.$responseHandler = function (resp) {
return resp.json();

@@ -287,3 +282,3 @@ };

*/
TextAPIClient.prototype.responseHandler = function (resp) {
TextAPIClient.prototype.$responseHandler = function (resp) {
return resp.text();

@@ -290,0 +285,0 @@ };

@@ -119,16 +119,11 @@ (function (global, factory) {

* Creates an instance of GenericAPIClient.
* @param {string} [baseURL=''] a base url to prepend to all request urls except for the ones with root urls
* @param {RequestInit} [baseClientConfig={}] a default config for requests
* @param {string} [$baseURL=''] a base url to prepend to all request urls except for the ones with root urls
* @param {RequestInit} [$baseClientConfig={}] a default config for requests
*/
function GenericAPIClient(baseURL, baseClientConfig) {
if (baseURL === void 0) { baseURL = ''; }
if (baseClientConfig === void 0) { baseClientConfig = {}; }
this.baseURL = baseURL;
this.baseClientConfig = baseClientConfig;
this.fetchHandler = window.fetch ? window.fetch.bind(window) : defaultFetch;
this.get = this.alias('get');
this.put = this.alias('put');
this.post = this.alias('post');
this.patch = this.alias('patch');
this.delete = this.alias('delete');
function GenericAPIClient($baseURL, $baseClientConfig) {
if ($baseURL === void 0) { $baseURL = ''; }
if ($baseClientConfig === void 0) { $baseClientConfig = {}; }
this.$baseURL = $baseURL;
this.$baseClientConfig = $baseClientConfig;
this.$fetchHandler = window.fetch ? window.fetch.bind(window) : defaultFetch;
}

@@ -140,9 +135,9 @@ /**

*/
GenericAPIClient.prototype.request = function (url, fetchConfig, overrideDefaultConfig) {
GenericAPIClient.prototype.$request = function (url, fetchConfig, overrideDefaultConfig) {
if (overrideDefaultConfig === void 0) { overrideDefaultConfig = false; }
if (!url.match(/^(\w+:)?\/\//)) {
url = this.baseURL ? new URL(url, this.baseURL).href : url;
url = this.$baseURL ? new URL(url, this.$baseURL).href : url;
}
return this.requestFactory(url, overrideDefaultConfig ?
fetchConfig : __assign({}, this.baseClientConfig, fetchConfig, { headers: __assign({}, (this.baseClientConfig.headers || {}), (fetchConfig.headers || {})) }), this.fetchHandler);
return this.$requestFactory(url, overrideDefaultConfig ?
fetchConfig : __assign({}, this.$baseClientConfig, fetchConfig, { headers: __assign({}, (this.$baseClientConfig.headers || {}), (fetchConfig.headers || {})) }), this.$fetchHandler);
};

@@ -159,3 +154,3 @@ /**

*/
GenericAPIClient.prototype.responseHandler = function (response) {
GenericAPIClient.prototype.$responseHandler = function (response) {
if (response.ok) {

@@ -177,3 +172,3 @@ return response;

*/
GenericAPIClient.prototype.errorHandler = function (e) {
GenericAPIClient.prototype.$errorHandler = function (e) {
if (e instanceof ResponseError) {

@@ -197,7 +192,7 @@ throw e;

*/
GenericAPIClient.prototype.requestFactory = function (url, config, requestFunction) {
GenericAPIClient.prototype.$requestFactory = function (url, config, requestFunction) {
var _this = this;
return requestFunction(url, config)
.then(function (r) { return _this.responseHandler(r); })
.catch(function (e) { return _this.errorHandler(e); });
.then(function (r) { return _this.$responseHandler(r); })
.catch(function (e) { return _this.$errorHandler(e); });
};

@@ -214,8 +209,8 @@ /**

*/
GenericAPIClient.prototype.alias = function (method) {
GenericAPIClient.prototype.$alias = function (method) {
return function (url, fetchConfig, overrideDefaultConfig) {
if (fetchConfig === void 0) { fetchConfig = this.baseClientConfig; }
if (fetchConfig === void 0) { fetchConfig = this.$baseClientConfig; }
fetchConfig = fetchConfig;
fetchConfig.method = method ? method.toUpperCase() : (fetchConfig.method || 'GET').toUpperCase();
return this.request(url, fetchConfig, overrideDefaultConfig);
return this.$request(url, fetchConfig, overrideDefaultConfig);
};

@@ -273,3 +268,3 @@ };

*/
JsonAPIClient.prototype.responseHandler = function (resp) {
JsonAPIClient.prototype.$responseHandler = function (resp) {
return resp.json();

@@ -290,3 +285,3 @@ };

*/
TextAPIClient.prototype.responseHandler = function (resp) {
TextAPIClient.prototype.$responseHandler = function (resp) {
return resp.text();

@@ -293,0 +288,0 @@ };

@@ -25,16 +25,11 @@ "use strict";

* Creates an instance of GenericAPIClient.
* @param {string} [baseURL=''] a base url to prepend to all request urls except for the ones with root urls
* @param {RequestInit} [baseClientConfig={}] a default config for requests
* @param {string} [$baseURL=''] a base url to prepend to all request urls except for the ones with root urls
* @param {RequestInit} [$baseClientConfig={}] a default config for requests
*/
function GenericAPIClient(baseURL, baseClientConfig) {
if (baseURL === void 0) { baseURL = ''; }
if (baseClientConfig === void 0) { baseClientConfig = {}; }
this.baseURL = baseURL;
this.baseClientConfig = baseClientConfig;
this.fetchHandler = window.fetch ? window.fetch.bind(window) : defaultFetch_1.defaultFetch;
this.get = this.alias('get');
this.put = this.alias('put');
this.post = this.alias('post');
this.patch = this.alias('patch');
this.delete = this.alias('delete');
function GenericAPIClient($baseURL, $baseClientConfig) {
if ($baseURL === void 0) { $baseURL = ''; }
if ($baseClientConfig === void 0) { $baseClientConfig = {}; }
this.$baseURL = $baseURL;
this.$baseClientConfig = $baseClientConfig;
this.$fetchHandler = window.fetch ? window.fetch.bind(window) : defaultFetch_1.defaultFetch;
}

@@ -46,9 +41,9 @@ /**

*/
GenericAPIClient.prototype.request = function (url, fetchConfig, overrideDefaultConfig) {
GenericAPIClient.prototype.$request = function (url, fetchConfig, overrideDefaultConfig) {
if (overrideDefaultConfig === void 0) { overrideDefaultConfig = false; }
if (!url.match(/^(\w+:)?\/\//)) {
url = this.baseURL ? new URL(url, this.baseURL).href : url;
url = this.$baseURL ? new URL(url, this.$baseURL).href : url;
}
return this.requestFactory(url, overrideDefaultConfig ?
fetchConfig : __assign({}, this.baseClientConfig, fetchConfig, { headers: __assign({}, (this.baseClientConfig.headers || {}), (fetchConfig.headers || {})) }), this.fetchHandler);
return this.$requestFactory(url, overrideDefaultConfig ?
fetchConfig : __assign({}, this.$baseClientConfig, fetchConfig, { headers: __assign({}, (this.$baseClientConfig.headers || {}), (fetchConfig.headers || {})) }), this.$fetchHandler);
};

@@ -65,3 +60,3 @@ /**

*/
GenericAPIClient.prototype.responseHandler = function (response) {
GenericAPIClient.prototype.$responseHandler = function (response) {
if (response.ok) {

@@ -83,3 +78,3 @@ return response;

*/
GenericAPIClient.prototype.errorHandler = function (e) {
GenericAPIClient.prototype.$errorHandler = function (e) {
if (e instanceof errors_1.ResponseError) {

@@ -103,7 +98,7 @@ throw e;

*/
GenericAPIClient.prototype.requestFactory = function (url, config, requestFunction) {
GenericAPIClient.prototype.$requestFactory = function (url, config, requestFunction) {
var _this = this;
return requestFunction(url, config)
.then(function (r) { return _this.responseHandler(r); })
.catch(function (e) { return _this.errorHandler(e); });
.then(function (r) { return _this.$responseHandler(r); })
.catch(function (e) { return _this.$errorHandler(e); });
};

@@ -120,8 +115,8 @@ /**

*/
GenericAPIClient.prototype.alias = function (method) {
GenericAPIClient.prototype.$alias = function (method) {
return function (url, fetchConfig, overrideDefaultConfig) {
if (fetchConfig === void 0) { fetchConfig = this.baseClientConfig; }
if (fetchConfig === void 0) { fetchConfig = this.$baseClientConfig; }
fetchConfig = fetchConfig;
fetchConfig.method = method ? method.toUpperCase() : (fetchConfig.method || 'GET').toUpperCase();
return this.request(url, fetchConfig, overrideDefaultConfig);
return this.$request(url, fetchConfig, overrideDefaultConfig);
};

@@ -128,0 +123,0 @@ };

@@ -34,3 +34,3 @@ "use strict";

*/
JsonAPIClient.prototype.responseHandler = function (resp) {
JsonAPIClient.prototype.$responseHandler = function (resp) {
return resp.json();

@@ -52,3 +52,3 @@ };

*/
TextAPIClient.prototype.responseHandler = function (resp) {
TextAPIClient.prototype.$responseHandler = function (resp) {
return resp.text();

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

@@ -8,11 +8,11 @@ /**

export declare class GenericAPIClient {
readonly baseURL: string;
readonly baseClientConfig: RequestInit;
fetchHandler: any;
readonly $baseURL: string;
readonly $baseClientConfig: RequestInit;
$fetchHandler: any;
/**
* Creates an instance of GenericAPIClient.
* @param {string} [baseURL=''] a base url to prepend to all request urls except for the ones with root urls
* @param {RequestInit} [baseClientConfig={}] a default config for requests
* @param {string} [$baseURL=''] a base url to prepend to all request urls except for the ones with root urls
* @param {RequestInit} [$baseClientConfig={}] a default config for requests
*/
constructor(baseURL?: string, baseClientConfig?: RequestInit);
constructor($baseURL?: string, $baseClientConfig?: RequestInit);
/**

@@ -23,3 +23,3 @@ * Makes requests using request factory and resolves config merge conflicts.

*/
private request;
private $request;
/**

@@ -35,3 +35,3 @@ * Processes the response before allowing to return its value from request function.

*/
protected responseHandler(response: Response): any;
protected $responseHandler(response: Response): any;
/**

@@ -46,3 +46,3 @@ * Processes the request error before allowing to throw it upstack.

*/
protected errorHandler(e: any): any;
protected $errorHandler(e: any): any;
/**

@@ -58,3 +58,3 @@ * A general request factory function.

*/
protected requestFactory(url: string, config: RequestInit, requestFunction: (url: string, config?: RequestInit) => Promise<Response>): Promise<any>;
protected $requestFactory(url: string, config: RequestInit, requestFunction: (url: string, config?: RequestInit) => Promise<Response>): Promise<any>;
/**

@@ -70,8 +70,3 @@ * Request method alias factory.

*/
protected alias(method: string): (this: GenericAPIClient, url: string, fetchConfig?: RequestInit, overrideDefaultConfig?: boolean | undefined) => Promise<any>;
readonly get: (this: GenericAPIClient, url: string, fetchConfig?: RequestInit, overrideDefaultConfig?: boolean | undefined) => Promise<any>;
readonly put: (this: GenericAPIClient, url: string, fetchConfig?: RequestInit, overrideDefaultConfig?: boolean | undefined) => Promise<any>;
readonly post: (this: GenericAPIClient, url: string, fetchConfig?: RequestInit, overrideDefaultConfig?: boolean | undefined) => Promise<any>;
readonly patch: (this: GenericAPIClient, url: string, fetchConfig?: RequestInit, overrideDefaultConfig?: boolean | undefined) => Promise<any>;
readonly delete: (this: GenericAPIClient, url: string, fetchConfig?: RequestInit, overrideDefaultConfig?: boolean | undefined) => Promise<any>;
protected $alias(method: string): (this: GenericAPIClient, url: string, fetchConfig?: RequestInit, overrideDefaultConfig?: boolean | undefined) => Promise<any>;
/**

@@ -78,0 +73,0 @@ * Retrieves response status string in a readable format from a status number

@@ -12,3 +12,3 @@ import { GenericAPIClient } from './genericClient';

*/
responseHandler(resp: Response): Promise<unknown>;
$responseHandler(resp: Response): Promise<unknown>;
}

@@ -22,3 +22,3 @@ /**

*/
responseHandler(resp: Response): Promise<string>;
$responseHandler(resp: Response): Promise<string>;
}
{
"name": "kefetchup",
"version": "1.0.7",
"version": "1.1.0",
"description": "Simple fetch client API to spice up your application",

@@ -5,0 +5,0 @@ "keywords": [],

@@ -11,12 +11,12 @@ import { defaultFetch } from './defaultFetch';

export class GenericAPIClient {
public fetchHandler = window.fetch ? window.fetch.bind(window) : defaultFetch;
public $fetchHandler = window.fetch ? window.fetch.bind(window) : defaultFetch;
/**
* Creates an instance of GenericAPIClient.
* @param {string} [baseURL=''] a base url to prepend to all request urls except for the ones with root urls
* @param {RequestInit} [baseClientConfig={}] a default config for requests
* @param {string} [$baseURL=''] a base url to prepend to all request urls except for the ones with root urls
* @param {RequestInit} [$baseClientConfig={}] a default config for requests
*/
constructor(
public readonly baseURL: string = '',
public readonly baseClientConfig: RequestInit = {}
public readonly $baseURL: string = '',
public readonly $baseClientConfig: RequestInit = {}
) {}

@@ -29,3 +29,3 @@

*/
private request(
private $request(
url: string,

@@ -36,6 +36,6 @@ fetchConfig: RequestInit,

if (!url.match(/^(\w+:)?\/\//)) {
url = this.baseURL ? new URL(url, this.baseURL).href : url;
url = this.$baseURL ? new URL(url, this.$baseURL).href : url;
}
return this.requestFactory(
return this.$requestFactory(
url,

@@ -45,9 +45,9 @@ overrideDefaultConfig ?

{
...this.baseClientConfig,
...this.$baseClientConfig,
...fetchConfig,
headers: {
...(this.baseClientConfig.headers || {}), ...(fetchConfig.headers || {})
...(this.$baseClientConfig.headers || {}), ...(fetchConfig.headers || {})
}
},
this.fetchHandler
this.$fetchHandler
);

@@ -66,3 +66,3 @@ }

*/
protected responseHandler(response: Response): any {
protected $responseHandler(response: Response): any {
if (response.ok) {

@@ -84,3 +84,3 @@ return response;

*/
protected errorHandler(e): any {
protected $errorHandler(e): any {
if (e instanceof ResponseError) {

@@ -104,3 +104,3 @@ throw e;

*/
protected requestFactory(
protected $requestFactory(
url: string,

@@ -111,4 +111,4 @@ config: RequestInit,

return requestFunction(url, config)
.then(r => this.responseHandler(r))
.catch(e => this.errorHandler(e));
.then(r => this.$responseHandler(r))
.catch(e => this.$errorHandler(e));
}

@@ -126,20 +126,14 @@

*/
protected alias(method: string) {
protected $alias(method: string) {
return function (this: GenericAPIClient,
url: string,
fetchConfig: RequestInit = this.baseClientConfig,
fetchConfig: RequestInit = this.$baseClientConfig,
overrideDefaultConfig?: boolean
): ReturnType<typeof this['request']> {
): ReturnType<typeof this['$request']> {
fetchConfig = fetchConfig;
fetchConfig.method = method ? method.toUpperCase() : (fetchConfig.method || 'GET').toUpperCase();
return this.request(url, fetchConfig, overrideDefaultConfig);
return this.$request(url, fetchConfig, overrideDefaultConfig);
}
}
public readonly get = this.alias('get');
public readonly put = this.alias('put');
public readonly post = this.alias('post');
public readonly patch = this.alias('patch');
public readonly delete = this.alias('delete');
/**

@@ -146,0 +140,0 @@ * Retrieves response status string in a readable format from a status number

@@ -14,3 +14,3 @@ import { GenericAPIClient } from './genericClient';

*/
responseHandler(resp: Response): Promise<unknown> {
$responseHandler(resp: Response): Promise<unknown> {
return resp.json();

@@ -27,5 +27,5 @@ }

*/
responseHandler(resp: Response) {
$responseHandler(resp: Response) {
return resp.text();
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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