From 11e097dfa314619ed1f133ef7f5415295d51afa0 Mon Sep 17 00:00:00 2001 From: Paulo Margarido <64600052+paulomarg@users.noreply.github.com> Date: Wed, 6 Apr 2022 11:35:20 -0400 Subject: [PATCH 1/2] Add save deny list to base REST resource --- CHANGELOG.md | 4 ++++ src/__tests__/base-rest-resource.test.ts | 20 ++++++++++++++++++++ src/__tests__/fake-resource.ts | 4 ++++ src/base-rest-resource.ts | 8 ++++++-- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e79d87830..ebddd2ec9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/__tests__/base-rest-resource.test.ts b/src/__tests__/base-rest-resource.test.ts index 1f2fdfa95..9956b4922 100644 --- a/src/__tests__/base-rest-resource.test.ts +++ b/src/__tests__/base-rest-resource.test.ts @@ -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({})); diff --git a/src/__tests__/fake-resource.ts b/src/__tests__/fake-resource.ts index 2036674c5..e3359263e 100644 --- a/src/__tests__/fake-resource.ts +++ b/src/__tests__/fake-resource.ts @@ -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, }; diff --git a/src/base-rest-resource.ts b/src/base-rest-resource.ts index 593425531..681a3aba4 100644 --- a/src/base-rest-resource.ts +++ b/src/base-rest-resource.ts @@ -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} = {}; @@ -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; } From 47ca13816b45c75657279f2a40c9613f7b9f6571 Mon Sep 17 00:00:00 2001 From: Paulo Margarido <64600052+paulomarg@users.noreply.github.com> Date: Thu, 7 Apr 2022 14:08:15 -0400 Subject: [PATCH 2/2] Rename attribute list to read_only_attributes --- src/__tests__/fake-resource.ts | 4 +--- src/base-rest-resource.ts | 6 +++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/__tests__/fake-resource.ts b/src/__tests__/fake-resource.ts index e3359263e..866b97dc2 100644 --- a/src/__tests__/fake-resource.ts +++ b/src/__tests__/fake-resource.ts @@ -25,9 +25,7 @@ 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 READ_ONLY_ATTRIBUTES: string[] = ['unsaveable_attribute']; protected static HAS_ONE = { has_one_attribute: FakeResource, diff --git a/src/base-rest-resource.ts b/src/base-rest-resource.ts index 681a3aba4..1fb470883 100644 --- a/src/base-rest-resource.ts +++ b/src/base-rest-resource.ts @@ -62,7 +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 READ_ONLY_ATTRIBUTES: string[] = []; protected static HAS_ONE: {[attribute: string]: typeof Base} = {}; protected static HAS_MANY: {[attribute: string]: typeof Base} = {}; @@ -289,12 +289,12 @@ class Base { } public serialize(): Body { - const {HAS_MANY, HAS_ONE, ATTRIBUTE_SAVE_DENY_LIST} = 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) || - ATTRIBUTE_SAVE_DENY_LIST.includes(attribute) + READ_ONLY_ATTRIBUTES.includes(attribute) ) { return acc; }