Comparing version 1.0.5 to 1.0.6
@@ -134,5 +134,5 @@ class ResponseException extends Error { | ||
requestFactory(url, config, requestFunction) { | ||
return new Promise((resolve, _) => requestFunction(url, config) | ||
.then(r => resolve(this.responseHandler(r))) | ||
.catch(this.errorHandler)); | ||
return requestFunction(url, config) | ||
.then(r => this.responseHandler(r)) | ||
.catch(e => this.errorHandler(e)); | ||
} | ||
@@ -139,0 +139,0 @@ /** |
@@ -178,5 +178,5 @@ 'use strict'; | ||
var _this = this; | ||
return new Promise(function (resolve, _) { return requestFunction(url, config) | ||
.then(function (r) { return resolve(_this.responseHandler(r)); }) | ||
.catch(_this.errorHandler); }); | ||
return requestFunction(url, config) | ||
.then(function (r) { return _this.responseHandler(r); }) | ||
.catch(function (e) { return _this.errorHandler(e); }); | ||
}; | ||
@@ -183,0 +183,0 @@ /** |
@@ -177,5 +177,5 @@ var kefetchup = (function (exports) { | ||
var _this = this; | ||
return new Promise(function (resolve, _) { return requestFunction(url, config) | ||
.then(function (r) { return resolve(_this.responseHandler(r)); }) | ||
.catch(_this.errorHandler); }); | ||
return requestFunction(url, config) | ||
.then(function (r) { return _this.responseHandler(r); }) | ||
.catch(function (e) { return _this.errorHandler(e); }); | ||
}; | ||
@@ -182,0 +182,0 @@ /** |
@@ -180,5 +180,5 @@ (function (global, factory) { | ||
var _this = this; | ||
return new Promise(function (resolve, _) { return requestFunction(url, config) | ||
.then(function (r) { return resolve(_this.responseHandler(r)); }) | ||
.catch(_this.errorHandler); }); | ||
return requestFunction(url, config) | ||
.then(function (r) { return _this.responseHandler(r); }) | ||
.catch(function (e) { return _this.errorHandler(e); }); | ||
}; | ||
@@ -185,0 +185,0 @@ /** |
@@ -98,5 +98,5 @@ "use strict"; | ||
var _this = this; | ||
return new Promise(function (resolve, _) { return requestFunction(url, config) | ||
.then(function (r) { return resolve(_this.responseHandler(r)); }) | ||
.catch(_this.errorHandler); }); | ||
return requestFunction(url, config) | ||
.then(function (r) { return _this.responseHandler(r); }) | ||
.catch(function (e) { return _this.errorHandler(e); }); | ||
}; | ||
@@ -103,0 +103,0 @@ /** |
{ | ||
"name": "kefetchup", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Simple fetch client API to spice up your application", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
@@ -12,3 +12,3 @@ # KeFetchUp! | ||
It's just a small and very extendable fetch client made for our company's purposes. | ||
It's just a small and very extendable [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) client made for our company's purposes. | ||
@@ -26,3 +26,3 @@ Kefetchup aims to help you move your API calls into a higher generic abstraction by providing necessary tools for it. | ||
GenericAPIClient, | ||
// A simple class that extends GenericAPIClient. | ||
@@ -51,3 +51,3 @@ // The only difference is that it returns straight parsed JSON object, instead of a fetch response. | ||
```js | ||
import { GenericAPIClient } from 'kefetchup' | ||
import { GenericAPIClient, ResponseException, ResponseErrors, withQuery } from 'kefetchup' | ||
@@ -62,4 +62,11 @@ class MyApiClient extends GenericAPIClient { | ||
*/ | ||
responseHandler(resp) { | ||
return resp.json(); | ||
async responseHandler(response) { | ||
const resp = super.responseHandler(response); | ||
// Let's say we want to throw errors for 400+ statuses too | ||
if (resp.status >= 400) { | ||
throw new ResponseException(ResponseErrors[resp.status], resp.status, resp); | ||
} | ||
return await resp.json(); | ||
} | ||
@@ -86,8 +93,21 @@ | ||
// In class' body we can write custom method handlers for our API calls | ||
getImportantThingsList() { | ||
// Send a GET request to 'https://my-api-server.com/api/important-things?importance=high&amount=5' | ||
return this.get(withQuery('/important-things', { | ||
importance: 'high', | ||
amount: 5 | ||
})); | ||
async getImportantThingsList() { | ||
try { | ||
// Send a GET request to 'https://my-api-server.com/api/important-things?importance=high&amount=5' | ||
return await this.get(withQuery('/important-things', { | ||
importance: 'high', | ||
amount: 5 | ||
})); | ||
} catch (e) { | ||
// e instanceof ResponseException === true | ||
// Here you can handle method-specific errors | ||
if (e.status === 401) { | ||
console.error('Token is incorrect for', e.data); | ||
return []; | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} | ||
@@ -104,3 +124,5 @@ } | ||
// do things with your important things... | ||
}).catch(e => { | ||
// and catch your errors properly... | ||
}); | ||
``` |
@@ -101,6 +101,5 @@ import { defaultFetch } from './defaultFetch'; | ||
): Promise<any> { | ||
return new Promise<Response>((resolve, _) => requestFunction(url, config) | ||
.then(r => resolve(this.responseHandler(r))) | ||
.catch(this.errorHandler) | ||
); | ||
return requestFunction(url, config) | ||
.then(r => this.responseHandler(r)) | ||
.catch(e => this.errorHandler(e)); | ||
} | ||
@@ -107,0 +106,0 @@ |
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
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
142476
123
1756