Skip to content

Commit

Permalink
Merge pull request #2 from CodingItWrong/create-options
Browse files Browse the repository at this point in the history
Create options
  • Loading branch information
CodingItWrong authored Sep 19, 2021
2 parents 6e7fdc2 + 0469bf5 commit a4dde10
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
12 changes: 9 additions & 3 deletions src/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,17 @@ class Resource {
return this.api.get(url).then(extractData).catch(extractErrorResponse);
}

create(partialRecord) {
const record = Object.assign({}, partialRecord, {type: this.name});
create({attributes, relationships, options}) {
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)
.post(`${this.name}?${getOptionsQuery(options)}`, requestData)
.then(extractData)
.catch(extractErrorResponse);
}
Expand Down
27 changes: 22 additions & 5 deletions test/Resource.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,23 +245,40 @@ describe('Resource', () => {
});

describe('create', () => {
it('can create a record', () => {
const partialRecord = {attributes: {key: 'value'}};
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(partialRecord);
const result = resource.create({attributes, relationships});

expect(api.post).toHaveBeenCalledWith('widgets', {
expect(api.post).toHaveBeenCalledWith('widgets?', {
data: {
...partialRecord,
type: 'widgets',
attributes,
relationships,
},
});
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});
Expand Down

0 comments on commit a4dde10

Please sign in to comment.