From 4eccc0346b0ddb7d057c9565ee46cfdf81eba5c8 Mon Sep 17 00:00:00 2001 From: Pierluigi Viti Date: Thu, 19 Dec 2024 10:56:42 +0100 Subject: [PATCH] fix: fix singleton update requires id --- gen/generator.ts | 12 +++-- gen/templates/model.tpl | 2 +- pnpm-lock.yaml | 104 +++++++++++++++++++++------------------- specs/resource.spec.ts | 6 ++- src/resource.ts | 9 +++- src/resources/user.ts | 4 +- 6 files changed, 76 insertions(+), 61 deletions(-) diff --git a/gen/generator.ts b/gen/generator.ts index 46dbd45..570c546 100644 --- a/gen/generator.ts +++ b/gen/generator.ts @@ -740,9 +740,10 @@ const generateResource = (type: string, name: string, resource: Resource): strin typesArray.forEach(t => { const cudSuffix = getCUDSuffix(t) - resourceInterfaces.push(`Resource${cudSuffix}`) + const resIntf = ((singletonResource && (cudSuffix === 'Update'))? 'Singleton' : 'Resource') + cudSuffix + resourceInterfaces.push(resIntf) const component: Component = resource.components[t] - const tplCmp = templatedComponent(resName, t, component) + const tplCmp = templatedComponent(resName, t, component, singletonResource) tplCmp.models.forEach(m => { if (m !== 'Resource') declaredImportsModels.add(m) // Fix resource_errors issue }) @@ -904,7 +905,7 @@ const nullable = (type: string): string => { type ComponentEnums = { [key: string]: string } -const templatedComponent = (res: string, name: string, cmp: Component): { component: string, models: string[], enums: ComponentEnums, nullables: boolean } => { +const templatedComponent = (res: string, name: string, cmp: Component, singleton?: boolean): { component: string, models: string[], enums: ComponentEnums, nullables: boolean } => { const cudModel = isCUDModel(name) @@ -973,7 +974,10 @@ const templatedComponent = (res: string, name: string, cmp: Component): { compon let component = (fields.length || rels.length) ? templates.model : templates.model_empty component = component.replace(/##__RESOURCE_MODEL__##/g, name) - component = component.replace(/##__EXTEND_TYPE__##/g, getCUDSuffix(name)) + + const cudSuffix = getCUDSuffix(name) + const extendType = ((singleton && cudModel && (cudSuffix === 'Update'))? 'Singleton' : 'Resource') + cudSuffix + component = component.replace(/##__EXTEND_TYPE__##/, extendType) const fieldsStr = (fields.length ? '\n\t' : '') + fields.join('\n\t') + (fields.length && rels.length ? '\n' : '') const relsStr = rels.join('\n\t') + (rels.length ? '\n' : '') diff --git a/gen/templates/model.tpl b/gen/templates/model.tpl index 5139a1b..26abf77 100644 --- a/gen/templates/model.tpl +++ b/gen/templates/model.tpl @@ -1,4 +1,4 @@ -interface ##__RESOURCE_MODEL__## extends Resource##__EXTEND_TYPE__## { +interface ##__RESOURCE_MODEL__## extends ##__EXTEND_TYPE__## { ##__RESOURCE_MODEL_FIELDS__## ##__RESOURCE_MODEL_RELATIONSHIPS__## } \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 692464d..9d18930 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1699,8 +1699,8 @@ packages: resolution: {integrity: sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==} engines: {node: '>=10'} - caniuse-lite@1.0.30001689: - resolution: {integrity: sha512-CmeR2VBycfa+5/jOfnp/NpWPGd06nf1XYiefUvhXFfZE4GkRc9jv+eGPS4nT558WS/8lYCzV8SlANCIPvbWP1g==} + caniuse-lite@1.0.30001690: + resolution: {integrity: sha512-5ExiE3qQN6oF8Clf8ifIDcMRCRE/dMGcETG/XGMD8/XiXm6HXQgQTh1yZYLXXpSOsEUlJm1Xr7kGULZTuGtP/w==} chalk@2.4.2: resolution: {integrity: sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==} @@ -1710,16 +1710,16 @@ packages: resolution: {integrity: sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==} engines: {node: '>=10'} - chalk@5.3.0: - resolution: {integrity: sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==} + chalk@5.4.0: + resolution: {integrity: sha512-ZkD35Mx92acjB2yNJgziGqT9oKHEOxjTBTDRpOsRWtdecL/0jM3z5kM/CTzHWvHIen1GvkM85p6TuFfDGfc8/Q==} engines: {node: ^12.17.0 || ^14.13 || >=16.0.0} char-regex@1.0.2: resolution: {integrity: sha512-kWWXztvZ5SBQV+eRgKFeh8q5sLuZY2+8WUIzlxWVTg+oGwY14qylx1KbKzHd8P6ZYkAg0xyIDU9JMHhyJMZ1jw==} engines: {node: '>=10'} - chokidar@4.0.2: - resolution: {integrity: sha512-/b57FK+bblSU+dfewfFe0rT1YjVDfOmeLQwCAuC+vwvgLkXboATqqmy+Ipux6JrF6L5joe5CBnFOw+gLWH6yKg==} + chokidar@4.0.3: + resolution: {integrity: sha512-Qgzu8kfBvo+cA4962jnP1KkS6Dop5NS6g7R5LFYJr4b8Ub94PPQXUksCw9PvXoeXPRRddRNC5C1JQUR2SMGtnA==} engines: {node: '>= 14.16.0'} ci-info@3.9.0: @@ -1851,8 +1851,8 @@ packages: resolution: {integrity: sha512-4J7wRJD3ABAzr8wP+OcIcqq2dlUKp4DVflx++hs5h5ZKydWMI6/D/fAot+yh6g2tHh8fLFTvNOaVN357NvSrOQ==} engines: {node: '>= 0.4'} - data-view-byte-offset@1.0.0: - resolution: {integrity: sha512-t/Ygsytq+R995EJ5PZlD4Cu56sWa8InXySaViRzw9apusqsOO2bQP+SbYzAhR0pFKoB+43lYy8rWban9JSuXnA==} + data-view-byte-offset@1.0.1: + resolution: {integrity: sha512-BS8PfmtDGnrgYdOonGZQdLZslWIeCGFP9tpan0hi1Co2Zr2NKADsvGYA8XxuG/4UWgJ6Cjtv+YJnB6MM69QGlQ==} engines: {node: '>= 0.4'} debug@3.2.7: @@ -2386,8 +2386,9 @@ packages: engines: {node: '>=0.4.7'} hasBin: true - has-bigints@1.0.2: - resolution: {integrity: sha512-tSvCKtBr9lkF0Ex0aQiP9N+OpV4zi2r/Nee5VkRDbaqv35RLYMzbwQfFSZZH0kR+Rd6302UJZ2p/bJCEoR3VoQ==} + has-bigints@1.1.0: + resolution: {integrity: sha512-R3pbpkcIqv2Pm3dUwgjclDRVmWpTJW2DcMzcIhEXEx1oh/CEMObMm3KLmRJOdvhM7o4uQBnwr8pzRK2sJWIqfg==} + engines: {node: '>= 0.4'} has-flag@3.0.0: resolution: {integrity: sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==} @@ -2981,8 +2982,8 @@ packages: engines: {node: '>= 18'} hasBin: true - math-intrinsics@1.0.0: - resolution: {integrity: sha512-4MqMiKP90ybymYvsut0CH2g4XWbfLtmlCkXmtmdcDCxNB+mQcu1w/1+L/VD7vi/PSv7X2JYV7SCcR+jiPXnQtA==} + math-intrinsics@1.1.0: + resolution: {integrity: sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==} engines: {node: '>= 0.4'} meow@13.2.0: @@ -3166,8 +3167,8 @@ packages: resolution: {integrity: sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==} engines: {node: '>= 0.4'} - object.assign@4.1.5: - resolution: {integrity: sha512-byy+U7gp+FVwmyzKPYhW2h5l3crpmGsxl7X2s8y43IgxvG4g3QZ6CffDtsNQy1WsmZpQbO+ybo0AlW7TY6DcBQ==} + object.assign@4.1.7: + resolution: {integrity: sha512-nK28WOo+QIjBkDduTINE4JkF/UJJKyf2EJxvJKfblDpyg0Q+pkOHNTL0Qwy6NP6FhE/EnzV73BxxqcJaXY9anw==} engines: {node: '>= 0.4'} object.fromentries@2.0.8: @@ -3178,8 +3179,8 @@ packages: resolution: {integrity: sha512-+Lhy3TQTuzXI5hevh8sBGqbmurHbbIjAi0Z4S63nthVLmLxfbj4T54a4CfZrXIrt9iP4mVAPYMo/v99taj3wjQ==} engines: {node: '>= 0.4'} - object.values@1.2.0: - resolution: {integrity: sha512-yBYjY9QX2hnRmZHAjG/f13MzmBzxzYgQhFrke06TTyKY5zSTEqkOeukBzIdVA3j3ulu8Qa3MbVFShV7T2RmGtQ==} + object.values@1.2.1: + resolution: {integrity: sha512-gXah6aZrcUxjWg2zR2MwouP2eHlCBzdV4pygudehaKXSGW4v2AsRQUK+lwwXhii6KFZcunEnmSUoYp5CXibxtA==} engines: {node: '>= 0.4'} once@1.4.0: @@ -3432,8 +3433,8 @@ packages: resolution: {integrity: sha512-yDMz9g+VaZkqBYS/ozoBJwaBhTbZo3UNYQHNRw1D3UFQB8oHB4uS/tAODO+ZLjGWmUbKnIlOWO+aaIiAxrUWHA==} engines: {node: '>= 14.16.0'} - reflect.getprototypeof@1.0.8: - resolution: {integrity: sha512-B5dj6usc5dkk8uFliwjwDHM8To5/QwdKz9JcBZ8Ic4G1f0YmeeJTtE/ZTdgRFPAfxZFiUaPhZ1Jcs4qeagItGQ==} + reflect.getprototypeof@1.0.9: + resolution: {integrity: sha512-r0Ay04Snci87djAsI4U+WNRcSw5S4pOH7qFjd/veA5gC7TbqESR3tcj28ia95L/fYUDw11JKP7uqUKUAfVvV5Q==} engines: {node: '>= 0.4'} regenerate-unicode-properties@10.2.0: @@ -3867,8 +3868,8 @@ packages: resolution: {integrity: sha512-BaXgOuIxz8n8pIq3e7Atg/7s+DpiYrxn4vdot3w9KbnBhcRQq6o3xemQdIfynqSeXeDrF32x+WvfzmOjPiY9lg==} engines: {node: '>= 0.4'} - typed-array-byte-offset@1.0.3: - resolution: {integrity: sha512-GsvTyUHTriq6o/bHcTd0vM7OQ9JEdlvluu9YISaA7+KzDzPaIzEeDFNkTfhdE3MYcNhNi0vq/LlegYgIs5yPAw==} + typed-array-byte-offset@1.0.4: + resolution: {integrity: sha512-bTlAFB/FBYMcuX81gbL4OcpH5PmlFHqlCCpAl8AlEzMz5k53oNDvN8p1PNOWLEmI2x4orp3raOFB51tv9X+MFQ==} engines: {node: '>= 0.4'} typed-array-length@1.0.7: @@ -3975,8 +3976,8 @@ packages: resolution: {integrity: sha512-K4jVyjnBdgvc86Y6BkaLZEN933SwYOuBFkdmBu9ZfkcAbdVbpITnDmjvZ/aQjRXQrv5EPkTnD1s39GiiqbngCw==} engines: {node: '>= 0.4'} - which-typed-array@1.1.17: - resolution: {integrity: sha512-i2prb5irfKvNFV84NNLOZaNiMx20sm/AG2u59hU+JsjraeD5xs9LgQa+VzU95e2Tn0YMc/4drYPgPV3QvRAPPA==} + which-typed-array@1.1.18: + resolution: {integrity: sha512-qEcY+KJYlWyLH9vNbsr6/5j59AXk5ni5aakf8ldzBvGde6Iz4sxZGkJyWSAueTG7QhOvNRYb1lDdFmL5Td0QKA==} engines: {node: '>= 0.4'} which@2.0.2: @@ -5862,7 +5863,7 @@ snapshots: browserslist@4.24.3: dependencies: - caniuse-lite: 1.0.30001689 + caniuse-lite: 1.0.30001690 electron-to-chromium: 1.5.74 node-releases: 2.0.19 update-browserslist-db: 1.1.1(browserslist@4.24.3) @@ -5909,7 +5910,7 @@ snapshots: camelcase@6.3.0: {} - caniuse-lite@1.0.30001689: {} + caniuse-lite@1.0.30001690: {} chalk@2.4.2: dependencies: @@ -5922,11 +5923,11 @@ snapshots: ansi-styles: 4.3.0 supports-color: 7.2.0 - chalk@5.3.0: {} + chalk@5.4.0: {} char-regex@1.0.2: {} - chokidar@4.0.2: + chokidar@4.0.3: dependencies: readdirp: 4.0.2 @@ -6073,9 +6074,9 @@ snapshots: es-errors: 1.3.0 is-data-view: 1.0.2 - data-view-byte-offset@1.0.0: + data-view-byte-offset@1.0.1: dependencies: - call-bind: 1.0.8 + call-bound: 1.0.3 es-errors: 1.3.0 is-data-view: 1.0.2 @@ -6173,7 +6174,7 @@ snapshots: call-bound: 1.0.3 data-view-buffer: 1.0.1 data-view-byte-length: 1.0.1 - data-view-byte-offset: 1.0.0 + data-view-byte-offset: 1.0.1 es-define-property: 1.0.1 es-errors: 1.3.0 es-object-atoms: 1.0.0 @@ -6198,10 +6199,10 @@ snapshots: is-string: 1.1.1 is-typed-array: 1.1.15 is-weakref: 1.1.0 - math-intrinsics: 1.0.0 + math-intrinsics: 1.1.0 object-inspect: 1.13.3 object-keys: 1.1.1 - object.assign: 4.1.5 + object.assign: 4.1.7 regexp.prototype.flags: 1.5.3 safe-array-concat: 1.1.3 safe-regex-test: 1.1.0 @@ -6210,10 +6211,10 @@ snapshots: string.prototype.trimstart: 1.0.8 typed-array-buffer: 1.0.3 typed-array-byte-length: 1.0.3 - typed-array-byte-offset: 1.0.3 + typed-array-byte-offset: 1.0.4 typed-array-length: 1.0.7 unbox-primitive: 1.1.0 - which-typed-array: 1.1.17 + which-typed-array: 1.1.18 es-define-property@1.0.1: {} @@ -6375,7 +6376,7 @@ snapshots: minimatch: 3.1.2 object.fromentries: 2.0.8 object.groupby: 1.0.3 - object.values: 1.2.0 + object.values: 1.2.1 semver: 6.3.1 string.prototype.trimend: 1.0.9 tsconfig-paths: 3.15.0 @@ -6660,7 +6661,7 @@ snapshots: gopd: 1.2.0 has-symbols: 1.1.0 hasown: 2.0.2 - math-intrinsics: 1.0.0 + math-intrinsics: 1.1.0 get-package-type@0.1.0: {} @@ -6766,7 +6767,7 @@ snapshots: optionalDependencies: uglify-js: 3.19.3 - has-bigints@1.0.2: {} + has-bigints@1.1.0: {} has-flag@3.0.0: {} @@ -6887,7 +6888,7 @@ snapshots: is-bigint@1.1.0: dependencies: - has-bigints: 1.0.2 + has-bigints: 1.1.0 is-boolean-object@1.2.1: dependencies: @@ -6982,7 +6983,7 @@ snapshots: is-typed-array@1.1.15: dependencies: - which-typed-array: 1.1.17 + which-typed-array: 1.1.18 is-unicode-supported@2.1.0: {} @@ -7492,7 +7493,7 @@ snapshots: dependencies: ansi-escapes: 7.0.0 ansi-regex: 5.0.1 - chalk: 5.3.0 + chalk: 5.4.0 cli-highlight: 2.1.11 cli-table3: 0.6.5 marked: 12.0.2 @@ -7501,7 +7502,7 @@ snapshots: marked@12.0.2: {} - math-intrinsics@1.0.0: {} + math-intrinsics@1.1.0: {} meow@13.2.0: {} @@ -7592,10 +7593,12 @@ snapshots: object-keys@1.1.1: {} - object.assign@4.1.5: + object.assign@4.1.7: dependencies: call-bind: 1.0.8 + call-bound: 1.0.3 define-properties: 1.2.1 + es-object-atoms: 1.0.0 has-symbols: 1.1.0 object-keys: 1.1.1 @@ -7612,9 +7615,10 @@ snapshots: define-properties: 1.2.1 es-abstract: 1.23.6 - object.values@1.2.0: + object.values@1.2.1: dependencies: call-bind: 1.0.8 + call-bound: 1.0.3 define-properties: 1.2.1 es-object-atoms: 1.0.0 @@ -7832,7 +7836,7 @@ snapshots: readdirp@4.0.2: {} - reflect.getprototypeof@1.0.8: + reflect.getprototypeof@1.0.9: dependencies: call-bind: 1.0.8 define-properties: 1.2.1 @@ -8288,7 +8292,7 @@ snapshots: dependencies: bundle-require: 5.0.0(esbuild@0.24.0) cac: 6.7.14 - chokidar: 4.0.2 + chokidar: 4.0.3 consola: 3.2.3 debug: 4.4.0 esbuild: 0.24.0 @@ -8347,7 +8351,7 @@ snapshots: has-proto: 1.2.0 is-typed-array: 1.1.15 - typed-array-byte-offset@1.0.3: + typed-array-byte-offset@1.0.4: dependencies: available-typed-arrays: 1.0.7 call-bind: 1.0.8 @@ -8355,7 +8359,7 @@ snapshots: gopd: 1.2.0 has-proto: 1.2.0 is-typed-array: 1.1.15 - reflect.getprototypeof: 1.0.8 + reflect.getprototypeof: 1.0.9 typed-array-length@1.0.7: dependencies: @@ -8364,7 +8368,7 @@ snapshots: gopd: 1.2.0 is-typed-array: 1.1.15 possible-typed-array-names: 1.0.0 - reflect.getprototypeof: 1.0.8 + reflect.getprototypeof: 1.0.9 typescript@5.7.2: {} @@ -8374,7 +8378,7 @@ snapshots: unbox-primitive@1.1.0: dependencies: call-bound: 1.0.3 - has-bigints: 1.0.2 + has-bigints: 1.1.0 has-symbols: 1.1.0 which-boxed-primitive: 1.1.1 @@ -8464,7 +8468,7 @@ snapshots: isarray: 2.0.5 which-boxed-primitive: 1.1.1 which-collection: 1.0.2 - which-typed-array: 1.1.17 + which-typed-array: 1.1.18 which-collection@1.0.2: dependencies: @@ -8473,7 +8477,7 @@ snapshots: is-weakmap: 2.0.2 is-weakset: 2.0.4 - which-typed-array@1.1.17: + which-typed-array@1.1.18: dependencies: available-typed-arrays: 1.0.7 call-bind: 1.0.8 diff --git a/specs/resource.spec.ts b/specs/resource.spec.ts index 6041543..c3adada 100644 --- a/specs/resource.spec.ts +++ b/specs/resource.spec.ts @@ -55,9 +55,12 @@ describe('SDK:resource suite', () => { it('resource.singleton', async () => { - const user = await clp.user.retrieve() + let user = await clp.user.retrieve() expect(user.id).not.toBeNull() expect(user.id).not.toBeUndefined() + const testRef = 'reference_' + Date.now() + user = await clp.user.update({ reference: testRef, time_zone: 'UTC' }) + expect(user.reference).toBe(testRef) }) @@ -65,7 +68,6 @@ describe('SDK:resource suite', () => { const user_email = 'spec@provisioning-sdk-test.org' const params = organizationSlug ? { filters: { slug_eq: organizationSlug } } : undefined const org = (await clp.organizations.list(params)).first() - console.log(org) const role = (await clp.roles.list()).first() if (!org || !role) throw new Error('Missing role or organization') const ms = await clp.memberships.create({ diff --git a/src/resource.ts b/src/resource.ts index fbed7a5..4e1231f 100644 --- a/src/resource.ts +++ b/src/resource.ts @@ -56,6 +56,11 @@ interface ResourceUpdate extends ResourceBase { } +interface SingletonUpdate extends ResourceBase { + +} + + type ListMeta = { readonly pageCount: number readonly recordCount: number @@ -88,7 +93,7 @@ class ListResponse extends Array { } -export type { Metadata, ResourceType, ResourceId, Resource, ResourceCreate, ResourceUpdate, ListResponse, ListMeta, ResourceRel } +export type { Metadata, ResourceType, ResourceId, Resource, ResourceCreate, ResourceUpdate, ListResponse, ListMeta, ResourceRel, SingletonUpdate } export type ResourceSort = Pick export type ResourceFilter = Pick @@ -205,7 +210,7 @@ class ResourceAdapter { } - async update(resource: U & ResourceId, params?: QueryParamsRetrieve, options?: ResourcesConfig, path?: string): Promise { + async update(resource: U & ResourceType, params?: QueryParamsRetrieve, options?: ResourcesConfig, path?: string): Promise { const singleton = !('id' in resource) || CommerceLayerProvisioningStatic.isSingleton(resource.type) diff --git a/src/resources/user.ts b/src/resources/user.ts index b845aff..290f3e0 100644 --- a/src/resources/user.ts +++ b/src/resources/user.ts @@ -1,6 +1,6 @@ import type { Nullable } from '../types' import { ApiSingleton } from '../resource' -import type { Resource, ResourceUpdate, ResourceId, ResourcesConfig, ResourceRel, ListResponse, ResourceSort, /* ResourceFilter */ } from '../resource' +import type { Resource, SingletonUpdate, ResourceId, ResourcesConfig, ResourceRel, ListResponse, ResourceSort, /* ResourceFilter */ } from '../resource' import type { QueryParamsRetrieve, QueryParamsList } from '../query' import type { IdentityProvider } from './identity_providers' @@ -48,7 +48,7 @@ interface User extends Resource { } -interface UserUpdate extends ResourceUpdate { +interface UserUpdate extends SingletonUpdate { /** * The user email.