Permalink
Browse files

feat(rest): add responseOutput to expose response object from fetch call

  • Loading branch information...
Kukks committed Jun 7, 2017
1 parent bcc24c3 commit 8989df46c64c2e6f14a18961cfd253274442ca57
Showing with 160 additions and 51 deletions.
  1. +14 −14 doc/Quick start.md
  2. +14 −14 doc/README.md
  3. +36 −21 src/rest.js
  4. +96 −2 test/rest.spec.js
@@ -103,20 +103,20 @@ You probably don't only want to retrieve data but send some also. The Rest clien

````js
endpoint
.client // the httpClient instance
.endpoint // name of the endpoint
.default // The fetch client defaults
.find(resource, criteria, options) // GET
.findOne(resource, id, criteria, options) // GET
.post(resource, body, options) { // POST
.update(resource, criteria, body, options) // PUT
.updateOne(resource, id, criteria, body, options) // PUT
.patch(resource, criteria, body, options) // PATCH
.patchOne(resource, id, criteria, body, options) // PATCH
.destroy(resource, criteria, options) // DELETE
.destroyOne(resource, id, criteria, options) // DELETE
.create(resource, body, options) // POST
.request(method, path, body, options) // method
.client // the httpClient instance
.endpoint // name of the endpoint
.default // The fetch client defaults
.find(resource, criteria, options, responseOutput) // GET
.findOne(resource, id, criteria, options, responseOutput) // GET
.post(resource, body, options, responseOutput) { // POST
.update(resource, criteria, body, options, responseOutput) // PUT
.updateOne(resource, id, criteria, body, options, responseOutput) // PUT
.patch(resource, criteria, body, options, responseOutput) // PATCH
.patchOne(resource, id, criteria, body, options, responseOutput) // PATCH
.destroy(resource, criteria, options, responseOutput) // DELETE
.destroyOne(resource, id, criteria, options, responseOutput) // DELETE
.create(resource, body, options, responseOutput) // POST
.request(method, path, body, options, responseOutput) // method
```
The [Rest api](api_rest.md) has more information about those. Here is just another quick example:
@@ -137,20 +137,20 @@ All methods return on success a Promise with the server response parsed to an ob

```js
endpoint
.client // the httpClient instance
.endpoint // name of the endpoint
.default // The fetch client defaults
.find(resource, idOrCriteria, options) // GET
.findOne(resource, id, criteria, options) // GET
.post(resource, body, options) // POST
.update(resource, idOrCriteria, body, options) // PUT
.updateOne(resource, id, criteria, body, options) // PUT
.patch(resource, idOrCriteria, body, options) // PATCH
.patchOne(resource, id, criteria, body, options) // PATCH
.destroy(resource, idOrCriteria, options) // DELETE
.destroyOne(resource, id, criteria, options) // DELETE
.create(resource, body, options) // POST
.request(method, path, body, options) // method
.client // the httpClient instance
.endpoint // name of the endpoint
.default // The fetch client defaults
.find(resource, criteria, options, responseOutput) // GET
.findOne(resource, id, criteria, options, responseOutput) // GET
.post(resource, body, options, responseOutput) { // POST
.update(resource, criteria, body, options, responseOutput) // PUT
.updateOne(resource, id, criteria, body, options, responseOutput) // PUT
.patch(resource, criteria, body, options, responseOutput) // PATCH
.patchOne(resource, id, criteria, body, options, responseOutput) // PATCH
.destroy(resource, criteria, options, responseOutput) // DELETE
.destroyOne(resource, id, criteria, options, responseOutput) // DELETE
.create(resource, body, options, responseOutput) // POST
.request(method, path, body, options, responseOutput) // method
```
## Note
@@ -64,10 +64,11 @@ export class Rest {
* @param {string} path Path to the resource
* @param {{}} [body] The body to send if applicable
* @param {{}} [options] Fetch request options overwrites
* @param {{ response: Response}} [responseOutput] reference output for Response object
*
* @return {Promise<*>|Promise<Error>} Server response as Object
*/
request(method: string, path: string, body?: {}, options?: {}): Promise<any|Error> {
request(method: string, path: string, body?: {}, options?: {}, responseOutput?: { response: Response}): Promise<any|Error> {
let requestOptions = extend(true, {headers: {}}, this.defaults, options || {}, {method, body});
let contentType = requestOptions.headers['Content-Type'] || requestOptions.headers['content-type'];

@@ -79,6 +80,10 @@ export class Rest {

return this.client.fetch(path, requestOptions).then((response: Response) => {
if (response.status >= 200 && response.status < 400) {
if (responseOutput) {
responseOutput.response = response;
}

return response.json().catch(() => null);
}

@@ -92,11 +97,12 @@ export class Rest {
* @param {string} resource Resource to find in
* @param {string|number|{}} idOrCriteria Object for where clause, string / number for id.
* @param {{}} [options] Extra request options.
* @param {{ response: Response}} [responseOutput] reference output for Response object
*
* @return {Promise<*>|Promise<Error>} Server response as Object
*/
find(resource: string, idOrCriteria?: string|number|{}, options?: {}): Promise<any|Error> {
return this.request('GET', getRequestPath(resource, this.useTraditionalUriTemplates, idOrCriteria), undefined, options);
find(resource: string, idOrCriteria?: string|number|{}, options?: {}, responseOutput?: { response: Response}): Promise<any|Error> {
return this.request('GET', getRequestPath(resource, this.useTraditionalUriTemplates, idOrCriteria), undefined, options, responseOutput);
}

/**
@@ -106,11 +112,12 @@ export class Rest {
* @param {string|number} id String / number for id to be added to the path.
* @param {{}} [criteria] Object for where clause
* @param {{}} [options] Extra request options.
* @param {{ response: Response}} [responseOutput] reference output for Response object
*
* @return {Promise<*>|Promise<Error>} Server response as Object
*/
findOne(resource: string, id: string|number, criteria?: {}, options?: {}): Promise<any|Error> {
return this.request('GET', getRequestPath(resource, this.useTraditionalUriTemplates, id, criteria), undefined, options);
findOne(resource: string, id: string|number, criteria?: {}, options?: {}, responseOutput?: { response: Response}): Promise<any|Error> {
return this.request('GET', getRequestPath(resource, this.useTraditionalUriTemplates, id, criteria), undefined, options, responseOutput);
}

/**
@@ -119,11 +126,12 @@ export class Rest {
* @param {string} resource Resource to create
* @param {{}} [body] The data to post (as Object)
* @param {{}} [options] Extra request options.
* @param {{ response: Response}} [responseOutput] reference output for Response object
*
* @return {Promise<*>|Promise<Error>} Server response as Object
*/
post(resource: string, body?: {}, options?: {}): Promise<any|Error> {
return this.request('POST', resource, body, options);
post(resource: string, body?: {}, options?: {}, responseOutput?: { response: Response}): Promise<any|Error> {
return this.request('POST', resource, body, options, responseOutput);
}

/**
@@ -133,11 +141,12 @@ export class Rest {
* @param {string|number|{}} idOrCriteria Object for where clause, string / number for id.
* @param {{}} [body] New data for provided idOrCriteria.
* @param {{}} [options] Extra request options.
* @param {{ response: Response}} [responseOutput] reference output for Response object
*
* @return {Promise<*>|Promise<Error>} Server response as Object
*/
update(resource: string, idOrCriteria?: string|number|{}, body?: {}, options?: {}): Promise<any|Error> {
return this.request('PUT', getRequestPath(resource, this.useTraditionalUriTemplates, idOrCriteria), body, options);
update(resource: string, idOrCriteria?: string|number|{}, body?: {}, options?: {}, responseOutput?: { response: Response}): Promise<any|Error> {
return this.request('PUT', getRequestPath(resource, this.useTraditionalUriTemplates, idOrCriteria), body, options, responseOutput);
}

/**
@@ -148,11 +157,12 @@ export class Rest {
* @param {{}} [criteria] Object for where clause
* @param {{}} [body] New data for provided criteria.
* @param {{}} [options] Extra request options.
* @param {{ response: Response}} [responseOutput] reference output for Response object
*
* @return {Promise<*>|Promise<Error>} Server response as Object
*/
updateOne(resource: string, id: string|number, criteria?: {}, body?: {}, options?: {}): Promise<any|Error> {
return this.request('PUT', getRequestPath(resource, this.useTraditionalUriTemplates, id, criteria), body, options);
updateOne(resource: string, id: string|number, criteria?: {}, body?: {}, options?: {}, responseOutput?: { response: Response}): Promise<any|Error> {
return this.request('PUT', getRequestPath(resource, this.useTraditionalUriTemplates, id, criteria), body, options, responseOutput);
}

/**
@@ -162,11 +172,12 @@ export class Rest {
* @param {string|number|{}} [idOrCriteria] Object for where clause, string / number for id.
* @param {{}} [body] Data to patch for provided idOrCriteria.
* @param {{}} [options] Extra request options.
* @param {{ response: Response}} [responseOutput] reference output for Response object
*
* @return {Promise<*>|Promise<Error>} Server response as Object
*/
patch(resource: string, idOrCriteria?: string|number|{}, body?: {}, options?: {}): Promise<any|Error> {
return this.request('PATCH', getRequestPath(resource, this.useTraditionalUriTemplates, idOrCriteria), body, options);
patch(resource: string, idOrCriteria?: string|number|{}, body?: {}, options?: {}, responseOutput?: { response: Response}): Promise<any|Error> {
return this.request('PATCH', getRequestPath(resource, this.useTraditionalUriTemplates, idOrCriteria), body, options, responseOutput);
}

/**
@@ -177,11 +188,12 @@ export class Rest {
* @param {{}} [criteria] Object for where clause
* @param {{}} [body] Data to patch for provided criteria.
* @param {{}} [options] Extra request options.
* @param {{ response: Response}} [responseOutput] reference output for Response object
*
* @return {Promise<*>|Promise<Error>} Server response as Object
*/
patchOne(resource: string, id: string|number, criteria?: {}, body?: {}, options?: {}): Promise<any|Error> {
return this.request('PATCH', getRequestPath(resource, this.useTraditionalUriTemplates, id, criteria), body, options);
patchOne(resource: string, id: string|number, criteria?: {}, body?: {}, options?: {}, responseOutput?: { response: Response}): Promise<any|Error> {
return this.request('PATCH', getRequestPath(resource, this.useTraditionalUriTemplates, id, criteria), body, options, responseOutput);
}

/**
@@ -190,11 +202,12 @@ export class Rest {
* @param {string} resource The resource to delete
* @param {string|number|{}} [idOrCriteria] Object for where clause, string / number for id.
* @param {{}} [options] Extra request options.
* @param {{ response: Response}} [responseOutput] reference output for Response object
*
* @return {Promise<*>|Promise<Error>} Server response as Object
*/
destroy(resource: string, idOrCriteria?: string|number|{}, options?: {}): Promise<any|Error> {
return this.request('DELETE', getRequestPath(resource, this.useTraditionalUriTemplates, idOrCriteria), undefined, options);
destroy(resource: string, idOrCriteria?: string|number|{}, options?: {}, responseOutput?: { response: Response}): Promise<any|Error> {
return this.request('DELETE', getRequestPath(resource, this.useTraditionalUriTemplates, idOrCriteria), undefined, options, responseOutput);
}

/**
@@ -204,11 +217,12 @@ export class Rest {
* @param {string|number} id String / number for id to be added to the path.
* @param {{}} [criteria] Object for where clause
* @param {{}} [options] Extra request options.
* @param {{ response: Response}} [responseOutput] reference output for Response object
*
* @return {Promise<*>|Promise<Error>} Server response as Object
*/
destroyOne(resource: string, id: string|number, criteria?: {}, options?: {}): Promise<any|Error> {
return this.request('DELETE', getRequestPath(resource, this.useTraditionalUriTemplates, id, criteria), undefined, options);
destroyOne(resource: string, id: string|number, criteria?: {}, options?: {}, responseOutput?: { response: Response}): Promise<any|Error> {
return this.request('DELETE', getRequestPath(resource, this.useTraditionalUriTemplates, id, criteria), undefined, options, responseOutput);
}

/**
@@ -217,11 +231,12 @@ export class Rest {
* @param {string} resource The resource to create
* @param {{}} [body] The data to post (as Object)
* @param {{}} [options] Extra request options.
* @param {{ response: Response}} [responseOutput] reference output for Response object
*
* @return {Promise<*>} Server response as Object
*/
create(resource: string, body?: {}, options?: {}): Promise<any|Error> {
return this.post(resource, body, options);
create(resource: string, body?: {}, options?: {}, responseOutput?: { response: Response}): Promise<any|Error> {
return this.post(resource, body, options, responseOutput);
}
}

Oops, something went wrong.

0 comments on commit 8989df4

Please sign in to comment.