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

Commit

Permalink
Add save deny list to base REST resource
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomarg committed Apr 6, 2022
1 parent 71fa5ba commit d1a579a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
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 [#](https://github.com/Shopify/shopify-node-api/pull/)

## [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
4 changes: 4 additions & 0 deletions src/__tests__/fake-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export default class FakeResource extends Base {
protected static NAME = 'fake_resource';
protected static PLURAL_NAME = 'fake_resources';

protected static ATTRIBUTE_SAVE_DENY_LIST: 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 ATTRIBUTE_SAVE_DENY_LIST: 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, ATTRIBUTE_SAVE_DENY_LIST} = this.resource();

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

Expand Down

0 comments on commit d1a579a

Please sign in to comment.