Permalink
Browse files

feat(rest): add findOne, updateOne,.. methods to allow for path/id?fi…

…lter=some
  • Loading branch information...
doktordirk committed Aug 17, 2016
1 parent 320b798 commit b6beffb2fea3a5b7a4ec0540d04db386374b2026
Showing with 397 additions and 159 deletions.
  1. +15 −10 README.md
  2. +15 −8 doc/Quick start.md
  3. +19 −5 doc/api_rest.md
  4. +69 −4 src/rest.js
  5. +279 −132 test/rest.spec.js
@@ -135,17 +135,22 @@ export class MyClass {

## Quick Rest api overview

All methods will, when the body is passed as an object, stringify the body if the `Content-Type` is set to `application/json` or convert the body to querystring format for all other set `Content-Type`s.
All methods will, when the body is passed as an object, stringify the body if the `Content-Type` is set to `application/json` or convert the body to querystring format for all other set `Content-Type`s. If an id is supplied, the next parameter must be criteria (can be undefined).

````js
endpoint
.client // the httpClient instance
.endpoint // name of the endpoint
.find(resource, criteria, options) // GET
.post(resource, body, options) { // POST
.update(resource, criteria, body, options) // PUT
.patch(resource, criteria, body, options) // PATCH
.destroy(resource, 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) // 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
```
@@ -102,13 +102,20 @@ You probably don't only want to retrieve data but send some also. The Rest clien

````js
endpoint
.find(resource, criteria, options) // GET
.post(resource, body, options) // POST
.update(resource, criteria, body, options) // PUT
.patch(resource, criteria, body, options) // PATCH
.destroy(resource, 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) // 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
```
The [Rest api](api_rest.md) has more information about those. Here is just another quick example:
@@ -119,7 +126,7 @@ import {Rest} from 'aurelia-api';
@inject(Rest)
export class MyViewModel {
constructor (restClient) {
restClient.update('product', 17, {price: 4000})
restClient.update('product', 17, null, {price: 4000})
.then(console.log)
.catch(console.error);
}
@@ -20,13 +20,19 @@ import {Rest} from 'aurelia-api';
| ------ | ---------------------------------------------------- |
| string | The endpoint for which the Rest client is registered |

### .defaults

| Type | Description |
| ------ | ---------------------------------------------------- |
| {} | The fetch client defaults to use for all request |

----------

## Methods

All methods will:

* maintain trailing slashes of the resource parameter
* maintain trailing slashes of the resource parameter
* stringify the body, if it is an object and the `Content-Type` is set to `application/json` (the default).
* convert the body to querystring format, if the body is an object and the `Content-Type` is set to any other value.
* leave the body unchanged, if the `Content-Type` is not set or when the body is not an object.
@@ -73,6 +79,7 @@ export class MyViewModel {
----------

### .find(resource, criteria[, options])
### .findOne(resource, id, criteria[, options])

Find one or multiple resources. (GET request)

@@ -81,7 +88,8 @@ Find one or multiple resources. (GET request)
| Parameter | Type | Description |
| --------- | -------------- | ---------------------------------------------- |
| resource | string | The name of the resource you want. |
| criteria | object/integer | A specific ID, or object of supported filters. |
| id | string/number | A specific ID. |
| criteria | object | Object of supported filters. |
| options | object | Additional options for the fetch |

#### Returns
@@ -156,6 +164,7 @@ export class MyViewModel {
----------

### .update(resource, criteria, body[, options])
### .updateOne(resource, id, criteria, body[, options])

Send a PUT request to supplied `resource`.

@@ -164,7 +173,8 @@ Send a PUT request to supplied `resource`.
| Parameter | Type | Description |
| --------- | -------------- | ---------------------------------------------- |
| resource | string | The name of the resource you wish to update. |
| criteria | object/integer | A specific ID, or object of supported filters. |
| id | string/number | A specific ID. |
| criteria | object | Object of supported filters. |
| body | object | The new values for the records. |
| options | object | Additional options for the fetch |

@@ -192,6 +202,7 @@ export class MyViewModel {
----------

### .patch(resource, criteria, body[, options])
### .patchOne(resource, id, criteria, body[, options])

Send a PATCH request to supplied `resource`.

@@ -200,7 +211,8 @@ Send a PATCH request to supplied `resource`.
| Parameter | Type | Description |
| --------- | -------------- | ---------------------------------------------- |
| resource | string | The name of the resource you wish to update. |
| criteria | object/integer | A specific ID, or object of supported filters. |
| id | string/number | A specific ID. |
| criteria | object | Object of supported filters. |
| body | object | The new values for the records. |
| options | object | Additional options for the fetch |

@@ -228,6 +240,7 @@ export class MyViewModel {
----------

### .destroy(resource, criteria[, options])
### .destroyOne(resource, id, criteria[, options])

Delete one or multiple resources. (DELETE request)

@@ -236,7 +249,8 @@ Delete one or multiple resources. (DELETE request)
| Parameter | Type | Description |
| --------- | -------------- | ---------------------------------------------- |
| resource | string | The name of the resource you wish to use. |
| criteria | object/integer | A specific ID, or object of supported filters. |
| id | string/number | A specific ID. |
| criteria | object | Object of supported filters. |
| options | object | Additional options for the fetch |

#### Returns
@@ -1,4 +1,4 @@
import {buildQueryString} from 'aurelia-path';
import {buildQueryString, join} from 'aurelia-path';
import {HttpClient} from 'aurelia-fetch-client';
import extend from 'extend';

@@ -69,10 +69,24 @@ export class Rest {
*
* @return {Promise<any>|Promise<Error>} Server response as Object
*/
find(resource: string, criteria?: {}, options?: {}): Promise<any|Error> {
find(resource: string, criteria?: {}|string|Number, options?: {}): Promise<any|Error> {
return this.request('GET', getRequestPath(resource, criteria), undefined, options);
}

/**
* Find a resource.
*
* @param {string} resource Resource to find in
* @param {string|Number} id String / number for id to be added to the path.
* @param {{}} criteria Object for where clause
* @param {{}} [options] Extra fetch options.
*
* @return {Promise<any>|Promise<Error>} Server response as Object
*/
findOne(resource: string, id: string|Number, criteria?: {}, options?: {}): Promise<any|Error> {
return this.request('GET', getRequestPath(resource, id, criteria), undefined, options);
}

/**
* Create a new instance for resource.
*
@@ -100,6 +114,21 @@ export class Rest {
return this.request('PUT', getRequestPath(resource, criteria), body, options);
}

/**
* Update a resource.
*
* @param {string} resource Resource to update
* @param {string|Number} id String / number for id to be added to the path.
* @param {{}} criteria Object for where clause
* @param {object} body New data for provided criteria.
* @param {{}} [options] Extra fetch options.
*
* @return {Promise<any>|Promise<Error>} Server response as Object
*/
updateOne(resource: string, id: string|number, criteria?: {}, body?: {}, options?: {}): Promise<any|Error> {
return this.request('PUT', getRequestPath(resource, id, criteria), body, options);
}

/**
* Patch a resource.
*
@@ -114,6 +143,21 @@ export class Rest {
return this.request('PATCH', getRequestPath(resource, criteria), body, options);
}

/**
* Patch a resource.
*
* @param {string} resource Resource to patch
* @param {string|Number} id String / number for id to be added to the path.
* @param {{}} criteria Object for where clause
* @param {object} body Data to patch for provided criteria.
* @param {{}} [options] Extra fetch options.
*
* @return {Promise<any>|Promise<Error>} Server response as Object
*/
patchOne(resource: string, id: string|Number, criteria?: {}, body?: {}, options?: {}): Promise<any|Error> {
return this.request('PATCH', getRequestPath(resource, id, criteria), body, options);
}

/**
* Delete a resource.
*
@@ -127,6 +171,20 @@ export class Rest {
return this.request('DELETE', getRequestPath(resource, criteria), undefined, options);
}

/**
* Delete a resource.
*
* @param {string} resource The resource to delete
* @param {string|Number} id String / number for id to be added to the path.
* @param {{}} criteria Object for where clause
* @param {{}} [options] Extra fetch options.
*
* @return {Promise<any>|Promise<Error>} Server response as Object
*/
destroyOne(resource: string, id: string|Number, criteria?: {}, options?: {}): Promise<any|Error> {
return this.request('DELETE', getRequestPath(resource, id, criteria), undefined, options);
}

/**
* Create a new instance for resource.
*
@@ -141,11 +199,18 @@ export class Rest {
}
}

function getRequestPath(resource, criteria) {
function getRequestPath(resource, idOrCriteria, criteria) {
let hasSlash = resource.slice(-1) === '/';

if (typeof idOrCriteria === 'string' || typeof idOrCriteria === 'number') {
resource = `${join(resource, idOrCriteria)}${hasSlash ? '/' : ''}`;
} else {
criteria = idOrCriteria;
}

if (typeof criteria === 'object' && criteria !== null) {
resource += `?${buildQueryString(criteria)}`;
} else if (criteria) {
let hasSlash = resource.slice(-1) === '/';
resource += `${hasSlash ? '' : '/'}${criteria}${hasSlash ? '/' : ''}`;
}

Oops, something went wrong.

0 comments on commit b6beffb

Please sign in to comment.