Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Add save deny list to base REST resource #355

Merged
merged 2 commits into from
Apr 7, 2022
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

### Added

- Allow REST resources to configure a deny list of attributes to be excluded when saving [#355](https://github.com/Shopify/shopify-node-api/pull/355)

## [3.0.0] - 2022-04-04

### Added
Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/base-rest-resource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,26 @@ describe('Base REST resource', () => {
}).toMatchMadeHttpRequest();
});

it('ignores unsaveable attribute', async () => {
const expectedRequestBody = {fake_resource: {attribute: 'attribute'}};
const responseBody = {fake_resource: {id: 1, attribute: 'attribute'}};
fetchMock.mockResponseOnce(JSON.stringify(responseBody));

const resource = new FakeResource({session});
resource.attribute = 'attribute';
resource.unsaveable_attribute = 'unsaveable_attribute';
await resource.save();

expect(resource.id).toBeUndefined();
expect({
method: 'POST',
domain,
path: `${prefix}/fake_resources.json`,
headers,
data: JSON.stringify(expectedRequestBody),
}).toMatchMadeHttpRequest();
});

it('deletes existing resource', async () => {
fetchMock.mockResponseOnce(JSON.stringify({}));

Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/fake-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export default class FakeResource extends Base {
protected static NAME = 'fake_resource';
protected static PLURAL_NAME = 'fake_resources';

protected static READ_ONLY_ATTRIBUTES: string[] = ['unsaveable_attribute'];

protected static HAS_ONE = {
has_one_attribute: FakeResource,
};
Expand Down
8 changes: 6 additions & 2 deletions src/base-rest-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Base {
protected static PLURAL_NAME = '';
protected static PRIMARY_KEY = 'id';
protected static CUSTOM_PREFIX: string | null = null;
protected static READ_ONLY_ATTRIBUTES: string[] = [];

protected static HAS_ONE: {[attribute: string]: typeof Base} = {};
protected static HAS_MANY: {[attribute: string]: typeof Base} = {};
Expand Down Expand Up @@ -288,10 +289,13 @@ class Base {
}

public serialize(): Body {
const {HAS_MANY, HAS_ONE} = this.resource();
const {HAS_MANY, HAS_ONE, READ_ONLY_ATTRIBUTES} = this.resource();

return Object.entries(this).reduce((acc: Body, [attribute, value]) => {
if (['session'].includes(attribute)) {
if (
['session'].includes(attribute) ||
READ_ONLY_ATTRIBUTES.includes(attribute)
) {
return acc;
}

Expand Down