Skip to content

Commit a4dde10

Browse files
Merge pull request #2 from CodingItWrong/create-options
Create options
2 parents 6e7fdc2 + 0469bf5 commit a4dde10

File tree

3 files changed

+45
-22
lines changed

3 files changed

+45
-22
lines changed

README.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,6 @@ resource
116116
.then(response => console.log(response.data));
117117
```
118118

119-
#### Options
120-
121-
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:
122-
123-
```js
124-
resource.all({
125-
options: {
126-
include: 'comments',
127-
},
128-
});
129-
130-
// requests to widgets?include=comments
131-
```
132-
133119
### Writing
134120

135121
#### create
@@ -170,6 +156,20 @@ Deletes the passed-in record. Only the `id` property is used, so you can pass ei
170156
widgetResource.delete({ id: 42 });
171157
```
172158

159+
### Options
160+
161+
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:
162+
163+
```js
164+
resource.all({
165+
options: {
166+
include: 'comments',
167+
},
168+
});
169+
170+
// requests to widgets?include=comments
171+
```
172+
173173
## License
174174

175175
Apache-2.0

src/Resource.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,17 @@ class Resource {
7474
return this.api.get(url).then(extractData).catch(extractErrorResponse);
7575
}
7676

77-
create(partialRecord) {
78-
const record = Object.assign({}, partialRecord, {type: this.name});
77+
create({attributes, relationships, options}) {
78+
const record = {type: this.name};
79+
if (attributes) {
80+
record.attributes = attributes;
81+
}
82+
if (relationships) {
83+
record.relationships = relationships;
84+
}
7985
const requestData = {data: record};
8086
return this.api
81-
.post(`${this.name}`, requestData)
87+
.post(`${this.name}?${getOptionsQuery(options)}`, requestData)
8288
.then(extractData)
8389
.catch(extractErrorResponse);
8490
}

test/Resource.spec.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -245,23 +245,40 @@ describe('Resource', () => {
245245
});
246246

247247
describe('create', () => {
248-
it('can create a record', () => {
249-
const partialRecord = {attributes: {key: 'value'}};
248+
const attributes = {key: 'value'};
249+
const relationships = {key2: 'value2'};
250250

251+
it('can create a record', () => {
251252
const responseBody = {data: record};
252253
api.post.mockResolvedValue({data: responseBody});
253254

254-
const result = resource.create(partialRecord);
255+
const result = resource.create({attributes, relationships});
255256

256-
expect(api.post).toHaveBeenCalledWith('widgets', {
257+
expect(api.post).toHaveBeenCalledWith('widgets?', {
257258
data: {
258-
...partialRecord,
259259
type: 'widgets',
260+
attributes,
261+
relationships,
260262
},
261263
});
262264
return expect(result).resolves.toEqual(responseBody);
263265
});
264266

267+
it('passes options', () => {
268+
const responseBody = {data: record};
269+
api.post.mockResolvedValue({data: responseBody});
270+
271+
resource.create({
272+
attributes,
273+
relationships,
274+
options: optionsWithInclude,
275+
});
276+
277+
expect(api.post).toHaveBeenCalledWith('widgets?include=comments', {
278+
data: {type: 'widgets', attributes, relationships},
279+
});
280+
});
281+
265282
it('rejects with the response upon error', () => {
266283
const errorResponse = {dummy: 'data'};
267284
api.post.mockRejectedValue({response: errorResponse});

0 commit comments

Comments
 (0)