From 371578471fd654b0bf10e7f026763fc0b2d7a5ee Mon Sep 17 00:00:00 2001 From: Josh Justice Date: Sun, 19 Sep 2021 14:18:01 -0400 Subject: [PATCH 1/4] Refactor create to prepare for options argument --- src/Resource.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Resource.js b/src/Resource.js index 113846c..ba45ace 100644 --- a/src/Resource.js +++ b/src/Resource.js @@ -74,8 +74,14 @@ class Resource { return this.api.get(url).then(extractData).catch(extractErrorResponse); } - create(partialRecord) { - const record = Object.assign({}, partialRecord, {type: this.name}); + create({attributes, relationships}) { + const record = {type: this.name}; + if (attributes) { + record.attributes = attributes; + } + if (relationships) { + record.relationships = relationships; + } const requestData = {data: record}; return this.api .post(`${this.name}`, requestData) From 28dbdfa54087c1f8b77d85e2ff5c09acf0f18351 Mon Sep 17 00:00:00 2001 From: Josh Justice Date: Sun, 19 Sep 2021 14:19:06 -0400 Subject: [PATCH 2/4] Refactor test to match separate arguments --- test/Resource.spec.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/Resource.spec.js b/test/Resource.spec.js index d0a8786..d977e89 100644 --- a/test/Resource.spec.js +++ b/test/Resource.spec.js @@ -246,17 +246,19 @@ describe('Resource', () => { describe('create', () => { it('can create a record', () => { - const partialRecord = {attributes: {key: 'value'}}; + const attributes = {key: 'value'}; + const relationships = {key2: 'value2'}; const responseBody = {data: record}; api.post.mockResolvedValue({data: responseBody}); - const result = resource.create(partialRecord); + const result = resource.create({attributes, relationships}); expect(api.post).toHaveBeenCalledWith('widgets', { data: { - ...partialRecord, type: 'widgets', + attributes, + relationships, }, }); return expect(result).resolves.toEqual(responseBody); From 3e3dd5f283728320d5f0adc3c76527acd01de69d Mon Sep 17 00:00:00 2001 From: Josh Justice Date: Sun, 19 Sep 2021 14:20:15 -0400 Subject: [PATCH 3/4] Pass options for create --- src/Resource.js | 4 ++-- test/Resource.spec.js | 23 +++++++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Resource.js b/src/Resource.js index ba45ace..5367039 100644 --- a/src/Resource.js +++ b/src/Resource.js @@ -74,7 +74,7 @@ class Resource { return this.api.get(url).then(extractData).catch(extractErrorResponse); } - create({attributes, relationships}) { + create({attributes, relationships, options}) { const record = {type: this.name}; if (attributes) { record.attributes = attributes; @@ -84,7 +84,7 @@ class Resource { } const requestData = {data: record}; return this.api - .post(`${this.name}`, requestData) + .post(`${this.name}?${getOptionsQuery(options)}`, requestData) .then(extractData) .catch(extractErrorResponse); } diff --git a/test/Resource.spec.js b/test/Resource.spec.js index d977e89..6784123 100644 --- a/test/Resource.spec.js +++ b/test/Resource.spec.js @@ -245,16 +245,16 @@ describe('Resource', () => { }); describe('create', () => { - it('can create a record', () => { - const attributes = {key: 'value'}; - const relationships = {key2: 'value2'}; + const attributes = {key: 'value'}; + const relationships = {key2: 'value2'}; + it('can create a record', () => { const responseBody = {data: record}; api.post.mockResolvedValue({data: responseBody}); const result = resource.create({attributes, relationships}); - expect(api.post).toHaveBeenCalledWith('widgets', { + expect(api.post).toHaveBeenCalledWith('widgets?', { data: { type: 'widgets', attributes, @@ -264,6 +264,21 @@ describe('Resource', () => { return expect(result).resolves.toEqual(responseBody); }); + it('passes options', () => { + const responseBody = {data: record}; + api.post.mockResolvedValue({data: responseBody}); + + resource.create({ + attributes, + relationships, + options: optionsWithInclude, + }); + + expect(api.post).toHaveBeenCalledWith('widgets?include=comments', { + data: {type: 'widgets', attributes, relationships}, + }); + }); + it('rejects with the response upon error', () => { const errorResponse = {dummy: 'data'}; api.post.mockRejectedValue({response: errorResponse}); From 0469bf52f3cf728b85dbaa3830e399940a933d5a Mon Sep 17 00:00:00 2001 From: Josh Justice Date: Sun, 19 Sep 2021 14:21:08 -0400 Subject: [PATCH 4/4] Update readme to indicate all methods except delete take options --- README.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 6e37f62..4bc6c1b 100644 --- a/README.md +++ b/README.md @@ -116,20 +116,6 @@ resource .then(response => console.log(response.data)); ``` -#### Options - -All read methods take an optional `options` property, consisting of an object of additional options to pass. Each key/value pair in the object is translated into a query string parameter key/value pair: - -```js -resource.all({ - options: { - include: 'comments', - }, -}); - -// requests to widgets?include=comments -``` - ### Writing #### create @@ -170,6 +156,20 @@ Deletes the passed-in record. Only the `id` property is used, so you can pass ei widgetResource.delete({ id: 42 }); ``` +### Options + +All methods that return records (so, all but `delete()`) take an optional `options` property, consisting of an object of additional options to pass. Each key/value pair in the object is translated into a query string parameter key/value pair: + +```js +resource.all({ + options: { + include: 'comments', + }, +}); + +// requests to widgets?include=comments +``` + ## License Apache-2.0