Skip to content

Create options #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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