@commercelayer/provisioning-sdk 1.0.0-beta.1 → 1.0.0-beta.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/README.md +107 -126
  2. package/lib/cjs/api.d.ts +7 -5
  3. package/lib/cjs/api.js +11 -1
  4. package/lib/cjs/client.d.ts +6 -3
  5. package/lib/cjs/client.js +40 -12
  6. package/lib/cjs/commercelayer.d.ts +3 -0
  7. package/lib/cjs/commercelayer.js +8 -4
  8. package/lib/cjs/error.d.ts +7 -7
  9. package/lib/cjs/index.js +1 -1
  10. package/lib/cjs/jsonapi.d.ts +1 -0
  11. package/lib/cjs/jsonapi.js +1 -1
  12. package/lib/cjs/model.d.ts +1 -0
  13. package/lib/cjs/resource.d.ts +3 -3
  14. package/lib/cjs/resource.js +18 -15
  15. package/lib/cjs/resources/api_credentials.d.ts +8 -4
  16. package/lib/cjs/resources/application_memberships.d.ts +64 -0
  17. package/lib/cjs/resources/application_memberships.js +45 -0
  18. package/lib/cjs/resources/memberships.d.ts +15 -0
  19. package/lib/cjs/resources/memberships.js +12 -0
  20. package/lib/cjs/resources/organizations.d.ts +11 -5
  21. package/lib/cjs/resources/organizations.js +8 -0
  22. package/lib/cjs/resources/permissions.d.ts +1 -1
  23. package/lib/cjs/resources/roles.d.ts +1 -1
  24. package/lib/cjs/resources/user.d.ts +1 -1
  25. package/lib/cjs/resources/user.js +3 -2
  26. package/lib/cjs/resources/versions.d.ts +5 -5
  27. package/lib/cjs/static.d.ts +4 -1
  28. package/lib/cjs/static.js +8 -2
  29. package/lib/cjs/util.d.ts +1 -1
  30. package/lib/cjs/util.js +1 -0
  31. package/lib/esm/api.d.ts +7 -5
  32. package/lib/esm/api.js +9 -0
  33. package/lib/esm/client.d.ts +6 -3
  34. package/lib/esm/client.js +40 -12
  35. package/lib/esm/commercelayer.d.ts +3 -0
  36. package/lib/esm/commercelayer.js +8 -4
  37. package/lib/esm/error.d.ts +7 -7
  38. package/lib/esm/index.js +1 -1
  39. package/lib/esm/jsonapi.d.ts +1 -0
  40. package/lib/esm/jsonapi.js +1 -1
  41. package/lib/esm/model.d.ts +1 -0
  42. package/lib/esm/resource.d.ts +3 -3
  43. package/lib/esm/resource.js +18 -15
  44. package/lib/esm/resources/api_credentials.d.ts +8 -4
  45. package/lib/esm/resources/application_memberships.d.ts +64 -0
  46. package/lib/esm/resources/application_memberships.js +43 -0
  47. package/lib/esm/resources/memberships.d.ts +15 -0
  48. package/lib/esm/resources/memberships.js +12 -0
  49. package/lib/esm/resources/organizations.d.ts +11 -5
  50. package/lib/esm/resources/organizations.js +8 -0
  51. package/lib/esm/resources/permissions.d.ts +1 -1
  52. package/lib/esm/resources/roles.d.ts +1 -1
  53. package/lib/esm/resources/user.d.ts +1 -1
  54. package/lib/esm/resources/user.js +3 -2
  55. package/lib/esm/resources/versions.d.ts +5 -5
  56. package/lib/esm/static.d.ts +4 -1
  57. package/lib/esm/static.js +8 -2
  58. package/lib/esm/util.d.ts +1 -1
  59. package/lib/esm/util.js +27 -1
  60. package/lib/tsconfig.esm.tsbuildinfo +1 -1
  61. package/lib/tsconfig.tsbuildinfo +1 -1
  62. package/package.json +13 -13
@@ -2,8 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const resource_1 = require("../resource");
4
4
  class Users extends resource_1.ApiSingleton {
5
- async retrieve(params, options) {
6
- return this.resources.singleton({ type: Users.TYPE }, params, options);
5
+ async update(resource, params, options) {
6
+ const res = await this.retrieve(params, options); // JsonAPI requires id in the request body
7
+ return this.resources.update({ ...resource, id: res.id, type: Users.TYPE }, params, options);
7
8
  }
8
9
  isUser(resource) {
9
10
  return resource.type && (resource.type === Users.TYPE);
@@ -6,11 +6,11 @@ type VersionRel = ResourceRel & {
6
6
  };
7
7
  interface Version extends Resource {
8
8
  readonly type: VersionType;
9
- resource_type?: string | null;
10
- resource_id?: string | null;
11
- event?: string | null;
12
- changes?: Record<string, any> | null;
13
- who?: Record<string, any> | null;
9
+ resource_type: string;
10
+ resource_id: string;
11
+ event: string;
12
+ changes: Record<string, any>;
13
+ who: Record<string, any>;
14
14
  }
15
15
  declare class Versions extends ApiResource<Version> {
16
16
  static readonly TYPE: VersionType;
@@ -1,7 +1,10 @@
1
+ import * as api from './api';
1
2
  import { SdkError, ApiError } from './error';
2
3
  import type { CommerceLayerProvisioningClient, CommerceLayerInitConfig } from './commercelayer';
3
4
  export declare const CommerceLayerProvisioningStatic: {
4
- resources: () => readonly string[];
5
+ resources: (sort?: boolean) => readonly string[];
6
+ singletons: (sort?: boolean) => readonly string[];
7
+ isSingleton: (resource: api.ResourceTypeLock) => boolean;
5
8
  isSdkError: (error: unknown) => error is SdkError;
6
9
  isApiError: (error: unknown) => error is ApiError;
7
10
  init: (config: CommerceLayerInitConfig) => CommerceLayerProvisioningClient;
package/lib/cjs/static.js CHANGED
@@ -29,8 +29,14 @@ const error_1 = require("./error");
29
29
  const commercelayer_1 = __importStar(require("./commercelayer"));
30
30
  /* Static functions */
31
31
  exports.CommerceLayerProvisioningStatic = {
32
- resources: () => {
33
- return api.resourceList;
32
+ resources: (sort) => {
33
+ return sort ? [...api.resourceList].sort() : api.resourceList;
34
+ },
35
+ singletons: (sort) => {
36
+ return sort ? [...api.singletonList].sort() : api.singletonList;
37
+ },
38
+ isSingleton: (resource) => {
39
+ return api.singletonList.includes(resource);
34
40
  },
35
41
  isSdkError: (error) => {
36
42
  return error_1.SdkError.isSdkError(error);
package/lib/cjs/util.d.ts CHANGED
@@ -6,4 +6,4 @@
6
6
  import type { ObjectType } from "../src/common";
7
7
  declare const sleep: (ms: number) => Promise<NodeJS.Timeout>;
8
8
  declare const sortObjectFields: (obj: ObjectType) => ObjectType;
9
- export { sleep, sortObjectFields };
9
+ export { sleep, sortObjectFields, };
package/lib/cjs/util.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.sortObjectFields = exports.sleep = void 0;
4
+ // import path from 'path'
4
5
  const sleep = async (ms) => {
5
6
  return new Promise(resolve => setTimeout(resolve, ms));
6
7
  };
package/lib/esm/api.d.ts CHANGED
@@ -4,14 +4,16 @@ import type { VersionType } from './resources/versions';
4
4
  * ©2023 Commerce Layer Inc.
5
5
  **/
6
6
  export { default as ApiCredentials } from './resources/api_credentials';
7
+ export { default as ApplicationMemberships } from './resources/application_memberships';
7
8
  export { default as Memberships } from './resources/memberships';
8
9
  export { default as Organizations } from './resources/organizations';
9
10
  export { default as Permissions } from './resources/permissions';
10
11
  export { default as Roles } from './resources/roles';
11
12
  export { default as Users } from './resources/user';
12
13
  export { default as Versions } from './resources/versions';
13
- export type ResourceTypeLock = 'api_credentials' | 'memberships' | 'organizations' | 'permissions' | 'roles' | 'user' | 'versions';
14
- export declare const resourceList: readonly ["api_credentials", "memberships", "organizations", "permissions", "roles", "user", "versions"];
14
+ export type ResourceTypeLock = 'api_credentials' | 'application_memberships' | 'memberships' | 'organizations' | 'permissions' | 'roles' | 'user' | 'versions';
15
+ export declare const resourceList: readonly ["api_credentials", "application_memberships", "memberships", "organizations", "permissions", "roles", "user", "versions"];
16
+ export declare const singletonList: readonly ["user"];
15
17
  export type RetrievableResourceType = ResourceTypeLock;
16
18
  export type RetrievableResource = Resource & {
17
19
  type: RetrievableResourceType;
@@ -20,15 +22,15 @@ export type ListableResourceType = Exclude<ResourceTypeLock, 'user'>;
20
22
  export type ListableResource = Resource & {
21
23
  type: ListableResourceType;
22
24
  };
23
- export type CreatableResourceType = 'api_credentials' | 'memberships' | 'organizations' | 'permissions' | 'roles';
25
+ export type CreatableResourceType = 'api_credentials' | 'application_memberships' | 'memberships' | 'organizations' | 'permissions' | 'roles';
24
26
  export type CreatableResource = Resource & {
25
27
  type: CreatableResourceType;
26
28
  };
27
- export type UpdatableResourceType = 'api_credentials' | 'memberships' | 'organizations' | 'permissions' | 'roles';
29
+ export type UpdatableResourceType = 'api_credentials' | 'application_memberships' | 'memberships' | 'organizations' | 'permissions' | 'roles';
28
30
  export type UpdatableResource = Resource & {
29
31
  type: UpdatableResourceType;
30
32
  };
31
- export type DeletableResourceType = 'api_credentials' | 'memberships';
33
+ export type DeletableResourceType = 'api_credentials' | 'application_memberships' | 'memberships';
32
34
  export type DeletableResource = Resource & {
33
35
  type: DeletableResourceType;
34
36
  };
package/lib/esm/api.js CHANGED
@@ -1,9 +1,12 @@
1
+ 'user';
2
+ 'user';
1
3
  // ##__API_RESOURCES_START__##
2
4
  // ##__API_RESOURCES_TEMPLATE:: export { default as ##__RESOURCE_CLASS__## } from './resources/##__RESOURCE_TYPE__##'
3
5
  /**
4
6
  * ©2023 Commerce Layer Inc.
5
7
  **/
6
8
  export { default as ApiCredentials } from './resources/api_credentials';
9
+ export { default as ApplicationMemberships } from './resources/application_memberships';
7
10
  export { default as Memberships } from './resources/memberships';
8
11
  export { default as Organizations } from './resources/organizations';
9
12
  export { default as Permissions } from './resources/permissions';
@@ -14,6 +17,7 @@ export { default as Versions } from './resources/versions';
14
17
  export const resourceList = [
15
18
  // ##__API_RESOURCE_LIST_START__##
16
19
  'api_credentials',
20
+ 'application_memberships',
17
21
  'memberships',
18
22
  'organizations',
19
23
  'permissions',
@@ -22,3 +26,8 @@ export const resourceList = [
22
26
  'versions'
23
27
  // ##__API_RESOURCE_LIST_STOP__##
24
28
  ];
29
+ export const singletonList = [
30
+ // ##__API_RESOURCE_SINGLETON_START__##
31
+ 'user'
32
+ // ##__API_RESOURCE_SINGLETON_STOP__##
33
+ ];
@@ -16,13 +16,15 @@ type RequestConfig = {
16
16
  proxy?: ProxyConfig;
17
17
  headers?: RequestHeaders;
18
18
  };
19
+ type RequestConfigExtra = {
20
+ adapter?: Adapter;
21
+ userAgent?: string;
22
+ };
19
23
  type ApiConfig = {
20
24
  domain?: string;
21
25
  accessToken: string;
22
26
  };
23
- type ApiClientInitConfig = ApiConfig & RequestConfig & {
24
- adapter?: Adapter;
25
- };
27
+ type ApiClientInitConfig = ApiConfig & RequestConfig & RequestConfigExtra;
26
28
  type ApiClientConfig = Partial<ApiClientInitConfig>;
27
29
  declare class ApiClient {
28
30
  #private;
@@ -31,6 +33,7 @@ declare class ApiClient {
31
33
  interceptors: InterceptorManager;
32
34
  private constructor();
33
35
  config(config: ApiClientConfig): ApiClient;
36
+ userAgent(userAgent: string): ApiClient;
34
37
  adapter(adapter: Adapter): ApiClient;
35
38
  request(method: Method, path: string, body?: any, options?: ApiClientConfig): Promise<any>;
36
39
  private customHeaders;
package/lib/esm/client.js CHANGED
@@ -13,6 +13,7 @@ var _ApiClient_accessToken, _ApiClient_client;
13
13
  import axios from 'axios';
14
14
  import { SdkError, handleError } from './error';
15
15
  import config from './config';
16
+ // import { packageInfo } from './util'
16
17
  import Debug from './debug';
17
18
  const debug = Debug('client');
18
19
  const baseURL = (domain) => {
@@ -35,19 +36,28 @@ class ApiClient {
35
36
  timeout: options.timeout || config.client.timeout,
36
37
  proxy: options.proxy,
37
38
  httpAgent: options.httpAgent,
38
- httpsAgent: options.httpsAgent,
39
+ httpsAgent: options.httpsAgent
39
40
  };
40
41
  // Set custom headers
41
42
  const customHeaders = this.customHeaders(options.headers);
43
+ // Set headers
44
+ const headers = {
45
+ ...customHeaders,
46
+ 'Accept': 'application/vnd.api+json',
47
+ 'Content-Type': 'application/vnd.api+json',
48
+ 'Authorization': 'Bearer ' + __classPrivateFieldGet(this, _ApiClient_accessToken, "f")
49
+ };
50
+ // Set User-Agent
51
+ let userAgent = options.userAgent; // || `SDK-provisioning axios/${axios.VERSION}`
52
+ if (userAgent) {
53
+ if (!userAgent.includes('axios/'))
54
+ userAgent += ` axios/${axios.VERSION}`;
55
+ headers['User-Agent'] = userAgent;
56
+ }
42
57
  const axiosOptions = {
43
58
  baseURL: this.baseUrl,
44
59
  timeout: config.client.timeout,
45
- headers: {
46
- ...customHeaders,
47
- 'Accept': 'application/vnd.api+json',
48
- 'Content-Type': 'application/vnd.api+json',
49
- 'Authorization': 'Bearer ' + __classPrivateFieldGet(this, _ApiClient_accessToken, "f")
50
- },
60
+ headers,
51
61
  ...axiosConfig
52
62
  };
53
63
  if (options.adapter)
@@ -68,6 +78,10 @@ class ApiClient {
68
78
  def.httpAgent = config.httpAgent;
69
79
  if (config.httpsAgent)
70
80
  def.httpsAgent = config.httpsAgent;
81
+ if (config.adapter)
82
+ this.adapter(config.adapter);
83
+ if (config.userAgent)
84
+ this.userAgent(config.userAgent);
71
85
  // API Client config
72
86
  if (config.accessToken) {
73
87
  __classPrivateFieldSet(this, _ApiClient_accessToken, config.accessToken, "f");
@@ -75,8 +89,18 @@ class ApiClient {
75
89
  }
76
90
  if (config.headers)
77
91
  def.headers.common = this.customHeaders(config.headers);
78
- if (config.adapter)
79
- this.adapter(config.adapter);
92
+ return this;
93
+ }
94
+ userAgent(userAgent) {
95
+ if (userAgent) {
96
+ let ua = userAgent;
97
+ if (!ua.includes('axios/')) {
98
+ // const axiosVer = packageInfo(['dependencies.axios'], { nestedName: true })
99
+ if (axios.VERSION)
100
+ ua += ` axios/${axios.VERSION}`;
101
+ }
102
+ __classPrivateFieldGet(this, _ApiClient_client, "f").defaults.headers['User-Agent'] = ua;
103
+ }
80
104
  return this;
81
105
  }
82
106
  adapter(adapter) {
@@ -86,10 +110,14 @@ class ApiClient {
86
110
  }
87
111
  async request(method, path, body, options) {
88
112
  debug('request %s %s, %O, %O', method, path, body || {}, options || {});
113
+ // Ignored params alerts (in debug mode)
114
+ if (options === null || options === void 0 ? void 0 : options.adapter)
115
+ debug('Adapter ignored in request config');
116
+ if (options === null || options === void 0 ? void 0 : options.userAgent)
117
+ debug('User-Agent header ignored in request config');
89
118
  const data = body ? { data: body } : undefined;
90
119
  const url = path;
91
120
  // Runtime request parameters
92
- // const baseUrl = options?.organization ? baseURL(options.organization, options.domain) : undefined
93
121
  const accessToken = (options === null || options === void 0 ? void 0 : options.accessToken) || __classPrivateFieldGet(this, _ApiClient_accessToken, "f");
94
122
  const headers = this.customHeaders(options === null || options === void 0 ? void 0 : options.headers);
95
123
  if (accessToken)
@@ -99,14 +127,14 @@ class ApiClient {
99
127
  // const start = Date.now()
100
128
  return __classPrivateFieldGet(this, _ApiClient_client, "f").request(requestParams)
101
129
  .then(response => response.data)
102
- .catch(error => handleError(error));
130
+ .catch((error) => handleError(error));
103
131
  // .finally(() => console.log(`<<-- ${method} ${path} ${Date.now() - start}`))
104
132
  }
105
133
  customHeaders(headers) {
106
134
  const customHeaders = {};
107
135
  if (headers) {
108
136
  for (const [name, value] of Object.entries(headers))
109
- if (!['accept', 'content-type', 'authorization'].includes(name.toLowerCase()))
137
+ if (!['accept', 'content-type', 'authorization', 'user-agent'].includes(name.toLowerCase()))
110
138
  customHeaders[name] = value;
111
139
  }
112
140
  return customHeaders;
@@ -11,6 +11,7 @@ declare class CommerceLayerProvisioningClient {
11
11
  #private;
12
12
  readonly openApiSchemaVersion = "1.0.0";
13
13
  api_credentials: api.ApiCredentials;
14
+ application_memberships: api.ApplicationMemberships;
14
15
  memberships: api.Memberships;
15
16
  organizations: api.Organizations;
16
17
  permissions: api.Permissions;
@@ -21,6 +22,8 @@ declare class CommerceLayerProvisioningClient {
21
22
  private localConfig;
22
23
  config(config: CommerceLayerConfig): CommerceLayerProvisioningClient;
23
24
  resources(): readonly string[];
25
+ singletons(): readonly string[];
26
+ isSingleton(resource: api.ResourceTypeLock): boolean;
24
27
  isApiError(error: any): error is ApiError;
25
28
  addRequestInterceptor(onFulfilled?: RequestInterceptor, onRejected?: ErrorInterceptor): number;
26
29
  addResponseInterceptor(onFulfilled?: ResponseInterceptor, onRejected?: ErrorInterceptor): number;
@@ -30,6 +30,7 @@ class CommerceLayerProvisioningClient {
30
30
  // ##__CL_RESOURCES_INIT_START__##
31
31
  // ##__CL_RESOURCES_INIT_TEMPLATE:: ##__TAB__####__TAB__##this.##__RESOURCE_TYPE__## = new api.##__RESOURCE_CLASS__##(this.#adapter)
32
32
  this.api_credentials = new api.ApiCredentials(__classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f"));
33
+ this.application_memberships = new api.ApplicationMemberships(__classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f"));
33
34
  this.memberships = new api.Memberships(__classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f"));
34
35
  this.organizations = new api.Organizations(__classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f"));
35
36
  this.permissions = new api.Permissions(__classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f"));
@@ -39,22 +40,25 @@ class CommerceLayerProvisioningClient {
39
40
  // ##__CL_RESOURCES_INIT_STOP__##
40
41
  }
41
42
  // get environment(): ApiMode { return this.#environment }
42
- localConfig(config /* & { organization?: string } */) {
43
+ localConfig(config) {
43
44
  }
44
45
  config(config) {
45
46
  debug('config %o', config);
46
47
  // CommerceLayer config
47
48
  this.localConfig(config);
48
49
  // ResourceAdapter config
49
- // To rebuild baseUrl in client in case only the domain is defined
50
- // if (!config.organization) config.organization = this.currentOrganization
51
50
  __classPrivateFieldGet(this, _CommerceLayerProvisioningClient_adapter, "f").config(config);
52
51
  return this;
53
52
  }
54
53
  resources() {
55
54
  return CommerceLayerProvisioningStatic.resources();
56
55
  }
57
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
56
+ singletons() {
57
+ return CommerceLayerProvisioningStatic.singletons();
58
+ }
59
+ isSingleton(resource) {
60
+ return CommerceLayerProvisioningStatic.isSingleton(resource);
61
+ }
58
62
  isApiError(error) {
59
63
  return CommerceLayerProvisioningStatic.isApiError(error);
60
64
  }
@@ -1,15 +1,15 @@
1
1
  declare enum ErrorType {
2
- CLIENT = "client",
3
- REQUEST = "request",
4
- RESPONSE = "response",
5
- CANCEL = "cancel",
6
- PARSE = "parse",
2
+ CLIENT = "client",// Error instantiating the client
3
+ REQUEST = "request",// Error preparing API request
4
+ RESPONSE = "response",// Error response from API
5
+ CANCEL = "cancel",// Forced request abort using interceptor
6
+ PARSE = "parse",// Error parsing API resource
7
7
  GENERIC = "generic"
8
8
  }
9
9
  declare class SdkError extends Error {
10
10
  static NAME: string;
11
- static isSdkError(error: any): error is ApiError;
12
- type: string;
11
+ static isSdkError(error: any): error is SdkError;
12
+ type: ErrorType;
13
13
  code?: string;
14
14
  source?: Error;
15
15
  request?: any;
package/lib/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  // SDK
2
2
  export { default, CommerceLayerProvisioning } from './commercelayer';
3
- // Commerce Layer static functions
3
+ // Commerce Layer Provisioning static functions
4
4
  export { CommerceLayerProvisioningStatic } from './static';
@@ -1,5 +1,6 @@
1
1
  import type { DocWithData, ResourceObject as JSONAPIObject } from 'jsonapi-typescript';
2
2
  import type { ResourceCreate, ResourceUpdate, ResourceId, ResourceType, Resource } from './resource';
3
+ export type { DocWithData };
3
4
  declare const denormalize: <R extends Resource>(response: DocWithData) => R | R[];
4
5
  declare const normalize: (resource: (ResourceCreate & ResourceType) | (ResourceUpdate & ResourceId)) => JSONAPIObject;
5
6
  export { denormalize, normalize };
@@ -38,7 +38,7 @@ const denormalizeResource = (res, included) => {
38
38
  const rel = res.relationships[key].data;
39
39
  if (rel) {
40
40
  if (Array.isArray(rel))
41
- resource[key] = rel.map(r => denormalizeResource(findIncluded(r, included), included));
41
+ resource[key] = rel.map((r) => denormalizeResource(findIncluded(r, included), included));
42
42
  else
43
43
  resource[key] = denormalizeResource(findIncluded(rel, included), included);
44
44
  }
@@ -2,6 +2,7 @@
2
2
  * ©2023 Commerce Layer Inc.
3
3
  **/
4
4
  export type { ApiCredential, ApiCredentialCreate, ApiCredentialUpdate } from './resources/api_credentials';
5
+ export type { ApplicationMembership, ApplicationMembershipCreate, ApplicationMembershipUpdate } from './resources/application_memberships';
5
6
  export type { Membership, MembershipCreate, MembershipUpdate } from './resources/memberships';
6
7
  export type { Organization, OrganizationCreate, OrganizationUpdate } from './resources/organizations';
7
8
  export type { Permission, PermissionCreate, PermissionUpdate } from './resources/permissions';
@@ -56,13 +56,13 @@ declare class ResourceAdapter {
56
56
  get interceptors(): InterceptorManager;
57
57
  private localConfig;
58
58
  config(config: ResourcesConfig): ResourceAdapter;
59
- singleton<R extends Resource>(resource: ResourceType, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<R>;
60
- retrieve<R extends Resource>(resource: ResourceId, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<R>;
59
+ retrieve<R extends Resource>(resource: ResourceId | ResourceType, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<R>;
61
60
  list<R extends Resource>(resource: ResourceType, params?: QueryParamsList, options?: ResourcesConfig): Promise<ListResponse<R>>;
62
61
  create<C extends ResourceCreate, R extends Resource>(resource: C & ResourceType, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<R>;
63
62
  update<U extends ResourceUpdate, R extends Resource>(resource: U & ResourceId, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<R>;
64
63
  delete(resource: ResourceId, options?: ResourcesConfig): Promise<void>;
65
64
  fetch<R extends Resource>(resource: string | ResourceType, path: string, params?: QueryParams, options?: ResourcesConfig): Promise<R | ListResponse<R>>;
65
+ action(cmd: 'post' | 'patch', path: string, payload?: any, options?: ResourcesConfig): Promise<void>;
66
66
  }
67
67
  declare abstract class ApiResourceBase<R extends Resource> {
68
68
  static readonly TYPE: ResourceTypeLock;
@@ -70,7 +70,7 @@ declare abstract class ApiResourceBase<R extends Resource> {
70
70
  constructor(adapter: ResourceAdapter);
71
71
  abstract relationship(id: string | ResourceId | null): ResourceRel;
72
72
  abstract type(): ResourceTypeLock;
73
- parse(resource: any): R | R[];
73
+ parse(resource: string): R | R[];
74
74
  update(resource: ResourceUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<R>;
75
75
  }
76
76
  declare abstract class ApiResource<R extends Resource> extends ApiResourceBase<R> {
@@ -14,8 +14,9 @@ import ApiClient from './client';
14
14
  import { denormalize, normalize } from './jsonapi';
15
15
  import { generateQueryStringParams, isParamsList } from './query';
16
16
  import config from './config';
17
- import Debug from './debug';
18
17
  import { ErrorType, SdkError } from './error';
18
+ import { CommerceLayerProvisioningStatic } from './static';
19
+ import Debug from './debug';
19
20
  const debug = Debug('resource');
20
21
  class ListResponse extends Array {
21
22
  constructor(meta, data) {
@@ -56,21 +57,14 @@ class ResourceAdapter {
56
57
  return this.#client
57
58
  }
58
59
  */
59
- async singleton(resource, params, options) {
60
- debug('singleton: %o, %O, %O', resource, params || {}, options || {});
61
- const queryParams = generateQueryStringParams(params, resource);
62
- if (options === null || options === void 0 ? void 0 : options.params)
63
- Object.assign(queryParams, options === null || options === void 0 ? void 0 : options.params);
64
- const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('get', `${resource.type}`, undefined, { ...options, params: queryParams });
65
- const r = denormalize(res);
66
- return r;
67
- }
68
60
  async retrieve(resource, params, options) {
69
- debug('retrieve: %o, %O, %O', resource, params || {}, options || {});
61
+ const singleton = !('id' in resource) || CommerceLayerProvisioningStatic.isSingleton(resource.type);
62
+ debug('retrieve:%s %o, %O, %O', (singleton ? ' singleton,' : ''), resource, params || {}, options || {});
70
63
  const queryParams = generateQueryStringParams(params, resource);
71
64
  if (options === null || options === void 0 ? void 0 : options.params)
72
65
  Object.assign(queryParams, options === null || options === void 0 ? void 0 : options.params);
73
- const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('get', `${resource.type}/${resource.id}`, undefined, { ...options, params: queryParams });
66
+ const path = `${resource.type}${singleton ? '' : `/${resource.id}`}`;
67
+ const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('get', path, undefined, { ...options, params: queryParams });
74
68
  const r = denormalize(res);
75
69
  return r;
76
70
  }
@@ -101,12 +95,14 @@ class ResourceAdapter {
101
95
  return r;
102
96
  }
103
97
  async update(resource, params, options) {
104
- debug('update: %o, %O, %O', resource, params || {}, options || {});
98
+ const singleton = !('id' in resource) || CommerceLayerProvisioningStatic.isSingleton(resource.type);
99
+ debug('update:%s %o, %O, %O', (singleton ? ' singleton,' : ''), resource, params || {}, options || {});
105
100
  const queryParams = generateQueryStringParams(params, resource);
106
101
  if (options === null || options === void 0 ? void 0 : options.params)
107
102
  Object.assign(queryParams, options === null || options === void 0 ? void 0 : options.params);
103
+ const path = `${resource.type}${singleton ? '' : `/${resource.id}`}`;
108
104
  const data = normalize(resource);
109
- const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('patch', `${resource.type}/${resource.id}`, data, { ...options, params: queryParams });
105
+ const res = await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request('patch', path, data, { ...options, params: queryParams });
110
106
  const r = denormalize(res);
111
107
  return r;
112
108
  }
@@ -135,6 +131,13 @@ class ResourceAdapter {
135
131
  else
136
132
  return r;
137
133
  }
134
+ async action(cmd, path, payload, options) {
135
+ debug('action: %o %o, %O', cmd, path, options || {});
136
+ const queryParams = {};
137
+ if (options === null || options === void 0 ? void 0 : options.params)
138
+ Object.assign(queryParams, options === null || options === void 0 ? void 0 : options.params);
139
+ await __classPrivateFieldGet(this, _ResourceAdapter_client, "f").request(cmd, path, payload, { ...options, params: queryParams });
140
+ }
138
141
  }
139
142
  _ResourceAdapter_client = new WeakMap(), _ResourceAdapter_config = new WeakMap();
140
143
  class ApiResourceBase {
@@ -177,7 +180,7 @@ class ApiResource extends ApiResourceBase {
177
180
  }
178
181
  class ApiSingleton extends ApiResourceBase {
179
182
  async retrieve(params, options) {
180
- return this.resources.singleton({ type: this.type() }, params, options);
183
+ return this.resources.retrieve({ type: this.type() }, params, options);
181
184
  }
182
185
  }
183
186
  export default ResourceAdapter;
@@ -17,12 +17,14 @@ interface ApiCredential extends Resource {
17
17
  readonly type: ApiCredentialType;
18
18
  name: string;
19
19
  kind: string;
20
- confidential?: boolean | null;
20
+ confidential: boolean;
21
21
  redirect_uri?: string | null;
22
- client_id?: string | null;
23
- client_secret?: string | null;
24
- scopes?: string | null;
22
+ client_id: string;
23
+ client_secret: string;
24
+ scopes: string;
25
25
  expires_in?: number | null;
26
+ mode?: string | null;
27
+ custom?: boolean | null;
26
28
  organization?: Organization | null;
27
29
  role?: Role | null;
28
30
  }
@@ -31,6 +33,8 @@ interface ApiCredentialCreate extends ResourceCreate {
31
33
  kind: string;
32
34
  redirect_uri?: string | null;
33
35
  expires_in?: number | null;
36
+ mode?: string | null;
37
+ custom?: boolean | null;
34
38
  organization: OrganizationRel;
35
39
  role?: RoleRel | null;
36
40
  }
@@ -0,0 +1,64 @@
1
+ import { ApiResource } from '../resource';
2
+ import type { Resource, ResourceCreate, ResourceUpdate, ResourceId, ResourcesConfig, ResourceRel } from '../resource';
3
+ import type { QueryParamsRetrieve } from '../query';
4
+ import type { ApiCredential, ApiCredentialType } from './api_credentials';
5
+ import type { Membership, MembershipType } from './memberships';
6
+ import type { User, UserType } from './user';
7
+ import type { Organization, OrganizationType } from './organizations';
8
+ import type { Role, RoleType } from './roles';
9
+ type ApplicationMembershipType = 'application_memberships';
10
+ type ApplicationMembershipRel = ResourceRel & {
11
+ type: ApplicationMembershipType;
12
+ };
13
+ type ApiCredentialRel = ResourceRel & {
14
+ type: ApiCredentialType;
15
+ };
16
+ type MembershipRel = ResourceRel & {
17
+ type: MembershipType;
18
+ };
19
+ type UserRel = ResourceRel & {
20
+ type: UserType;
21
+ };
22
+ type OrganizationRel = ResourceRel & {
23
+ type: OrganizationType;
24
+ };
25
+ type RoleRel = ResourceRel & {
26
+ type: RoleType;
27
+ };
28
+ interface ApplicationMembership extends Resource {
29
+ readonly type: ApplicationMembershipType;
30
+ filters?: Record<string, any> | null;
31
+ api_credential?: ApiCredential | null;
32
+ membership?: Membership | null;
33
+ user?: User | null;
34
+ organization?: Organization | null;
35
+ role?: Role | null;
36
+ }
37
+ interface ApplicationMembershipCreate extends ResourceCreate {
38
+ filters?: Record<string, any> | null;
39
+ api_credential: ApiCredentialRel;
40
+ membership: MembershipRel;
41
+ user: UserRel;
42
+ organization: OrganizationRel;
43
+ role: RoleRel;
44
+ }
45
+ interface ApplicationMembershipUpdate extends ResourceUpdate {
46
+ filters?: Record<string, any> | null;
47
+ role?: RoleRel | null;
48
+ }
49
+ declare class ApplicationMemberships extends ApiResource<ApplicationMembership> {
50
+ static readonly TYPE: ApplicationMembershipType;
51
+ create(resource: ApplicationMembershipCreate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<ApplicationMembership>;
52
+ update(resource: ApplicationMembershipUpdate, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<ApplicationMembership>;
53
+ delete(id: string | ResourceId, options?: ResourcesConfig): Promise<void>;
54
+ api_credential(applicationMembershipId: string | ApplicationMembership, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<ApiCredential>;
55
+ membership(applicationMembershipId: string | ApplicationMembership, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<Membership>;
56
+ user(applicationMembershipId: string | ApplicationMembership, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<User>;
57
+ organization(applicationMembershipId: string | ApplicationMembership, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<Organization>;
58
+ role(applicationMembershipId: string | ApplicationMembership, params?: QueryParamsRetrieve, options?: ResourcesConfig): Promise<Role>;
59
+ isApplicationMembership(resource: any): resource is ApplicationMembership;
60
+ relationship(id: string | ResourceId | null): ApplicationMembershipRel;
61
+ type(): ApplicationMembershipType;
62
+ }
63
+ export default ApplicationMemberships;
64
+ export type { ApplicationMembership, ApplicationMembershipCreate, ApplicationMembershipUpdate, ApplicationMembershipType };
@@ -0,0 +1,43 @@
1
+ import { ApiResource } from '../resource';
2
+ class ApplicationMemberships extends ApiResource {
3
+ async create(resource, params, options) {
4
+ return this.resources.create({ ...resource, type: ApplicationMemberships.TYPE }, params, options);
5
+ }
6
+ async update(resource, params, options) {
7
+ return this.resources.update({ ...resource, type: ApplicationMemberships.TYPE }, params, options);
8
+ }
9
+ async delete(id, options) {
10
+ await this.resources.delete((typeof id === 'string') ? { id, type: ApplicationMemberships.TYPE } : id, options);
11
+ }
12
+ async api_credential(applicationMembershipId, params, options) {
13
+ const _applicationMembershipId = applicationMembershipId.id || applicationMembershipId;
14
+ return this.resources.fetch({ type: 'api_credentials' }, `application_memberships/${_applicationMembershipId}/api_credential`, params, options);
15
+ }
16
+ async membership(applicationMembershipId, params, options) {
17
+ const _applicationMembershipId = applicationMembershipId.id || applicationMembershipId;
18
+ return this.resources.fetch({ type: 'memberships' }, `application_memberships/${_applicationMembershipId}/membership`, params, options);
19
+ }
20
+ async user(applicationMembershipId, params, options) {
21
+ const _applicationMembershipId = applicationMembershipId.id || applicationMembershipId;
22
+ return this.resources.fetch({ type: 'user' }, `application_memberships/${_applicationMembershipId}/user`, params, options);
23
+ }
24
+ async organization(applicationMembershipId, params, options) {
25
+ const _applicationMembershipId = applicationMembershipId.id || applicationMembershipId;
26
+ return this.resources.fetch({ type: 'organizations' }, `application_memberships/${_applicationMembershipId}/organization`, params, options);
27
+ }
28
+ async role(applicationMembershipId, params, options) {
29
+ const _applicationMembershipId = applicationMembershipId.id || applicationMembershipId;
30
+ return this.resources.fetch({ type: 'roles' }, `application_memberships/${_applicationMembershipId}/role`, params, options);
31
+ }
32
+ isApplicationMembership(resource) {
33
+ return resource.type && (resource.type === ApplicationMemberships.TYPE);
34
+ }
35
+ relationship(id) {
36
+ return ((id === null) || (typeof id === 'string')) ? { id, type: ApplicationMemberships.TYPE } : { id: id.id, type: ApplicationMemberships.TYPE };
37
+ }
38
+ type() {
39
+ return ApplicationMemberships.TYPE;
40
+ }
41
+ }
42
+ ApplicationMemberships.TYPE = 'application_memberships';
43
+ export default ApplicationMemberships;