@remkoj/optimizely-cms-api 2.0.3 → 2.1.0

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.
package/README.md CHANGED
@@ -1,44 +1,45 @@
1
- [![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](./LICENSE)
2
- ![Static Badge](https://img.shields.io/badge/Optimizely-Content_Management_System-blue?cacheSeconds=7200)
3
-
4
- # Optimizely CMS - Integration API Client <!-- omit in toc -->
5
- A Javascript client for the Integration API provided by the Optimizely CMS. It wraps the [Optimizely CMS SaaS Core REST API](https://docs.developers.optimizely.com/content-management-system/v1.0.0-SaaS-Core/reference/introduction-to-the-cms-content-api).
6
-
7
- ## Installation
8
- This package is provides as part of one of the mono-repo style demo sites. Install this package by using a mono-repo style structure for your project and copying this package into your mono-repo.
9
-
10
- ## Requirements
11
- This package works across browser and modern Node.JS environments, which provide an implementation for the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). The library has been tested with the Fetch API implementation in [Node.JS v18](https://nodejs.org/dist/latest-v20.x/docs/api/globals.html#fetch) and above.
12
-
13
- ## Usage
14
- ```typescript
15
- // Import the Client
16
- import createClient, { ContentRoots } from '@remkoj/optimizely-cms-integration-api'
17
-
18
- // Create an instance of the client
19
- const client = createClient({
20
- base: "https://example.com",
21
- clientId: "apiClientID",
22
- clientSecret: "apiClientSecret"
23
- });
24
-
25
- // Start using client
26
- client.content.contentListItems(ContentRoots.SystemRoot).then(response => {
27
- const items = response.items
28
- items?.map(item => {
29
- // Do something with the item
30
- })
31
- })
32
- ```
33
-
34
- ## Alternative configuration for Node.JS
35
- The configuration object for the `createClient()` method is optional, if it is omitted, the configuration will be read from the environment variables.
36
-
37
- ## Configuration options
38
- The following configuration options are available for
39
- | Environment variable | Configuration key | Required | Usage |
40
- | --- | --- | --- | --- |
41
- | OPTIMIZELY_CMS_URL | base | yes | The URL where the CMS is available |
42
- | OPTIMIZELY_CMS_CLIENT_ID | clientId | yes | The API Client ID, as configured within the CMS |
43
- | OPTIMIZELY_CMS_CLIENT_SECRET | clientSecret | yes | The API Client Secret as generated by the CMS when creating the client |
44
- | OPTIMIZELY_CMS_USER_ID | actAs | no | The username of the CMS user to impersonate |
1
+ [![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](./LICENSE)
2
+ ![Static Badge](https://img.shields.io/badge/Optimizely-Content_Management_System-blue?cacheSeconds=7200)
3
+
4
+ # Optimizely CMS - Integration API Client <!-- omit in toc -->
5
+ A Javascript client for the Integration API provided by the Optimizely CMS. It wraps the [Optimizely CMS SaaS Core REST API](https://docs.developers.optimizely.com/content-management-system/v1.0.0-SaaS-Core/reference/introduction-to-the-cms-content-api).
6
+
7
+ ## Installation
8
+ This package is provides as part of one of the mono-repo style demo sites. Install this package by using a mono-repo style structure for your project and copying this package into your mono-repo.
9
+
10
+ ## Requirements
11
+ This package works across browser and modern Node.JS environments, which provide an implementation for the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). The library has been tested with the Fetch API implementation in [Node.JS v18](https://nodejs.org/dist/latest-v20.x/docs/api/globals.html#fetch) and above.
12
+
13
+ ## Usage
14
+ ```typescript
15
+ // Import the Client
16
+ import createClient, { ContentRoots } from '@remkoj/optimizely-cms-integration-api'
17
+
18
+ // Create an instance of the client
19
+ const client = createClient({
20
+ base: "https://example.com",
21
+ clientId: "apiClientID",
22
+ clientSecret: "apiClientSecret"
23
+ });
24
+
25
+ // Start using client
26
+ client.content.contentListItems(ContentRoots.SystemRoot).then(response => {
27
+ const items = response.items
28
+ items?.map(item => {
29
+ // Do something with the item
30
+ })
31
+ })
32
+ ```
33
+
34
+ ## Alternative configuration for Node.JS
35
+ The configuration object for the `createClient()` method is optional, if it is omitted, the configuration will be read from the environment variables.
36
+
37
+ ## Configuration options
38
+ The following configuration options are available for
39
+ | Environment variable | Configuration key | Required | Usage |
40
+ | --- | --- | --- | --- |
41
+ | OPTIMIZELY_CMS_URL | base | yes | The URL where the CMS is available |
42
+ | OPTIMIZELY_CMS_CLIENT_ID | clientId | yes | The API Client ID, as configured within the CMS |
43
+ | OPTIMIZELY_CMS_CLIENT_SECRET | clientSecret | yes | The API Client Secret as generated by the CMS when creating the client |
44
+ | OPTIMIZELY_CMS_USER_ID | actAs | no | The username of the CMS user to impersonate |
45
+ | OPTIMIZELY_CMS_SCHEMA | cmsVersion | no | Override the target Optimizely CMS Version:<br/><br/>`OPTI-CMS-12`: Optimizely CMS 12, With [CMS Service Preview 1](https://nuget.optimizely.com/package/?id=Optimizely.Cms.Preview1.Service) installed and working.<br/><br/>`OPTI-CMS-13`: Optimizely SaaS CMS / CMS 13<br/><br/>**Default:** SaaS CMS / CMS 13 |
@@ -1,6 +1,6 @@
1
1
  import { CmsIntegrationApiClient } from "./client";
2
2
  import { type CmsIntegrationApiOptions } from "./config";
3
- import type { InstanceApiVersionInfo } from "./types";
3
+ import { type InstanceApiVersionInfo, OptiCmsVersion } from "./types";
4
4
  import type { CancelablePromise } from "./client";
5
5
  export declare class ApiClient extends CmsIntegrationApiClient {
6
6
  private _config;
@@ -10,6 +10,13 @@ export declare class ApiClient extends CmsIntegrationApiClient {
10
10
  * @param config The instance configuration
11
11
  */
12
12
  constructor(config?: CmsIntegrationApiOptions);
13
+ /**
14
+ * Get the runtime configuration of the target CMS version.
15
+ *
16
+ * If this differs from the cmsVersion the client may not work fully or not
17
+ * at all.
18
+ */
19
+ get runtimeCmsVersion(): OptiCmsVersion;
13
20
  /**
14
21
  * The URL of the CMS instance
15
22
  */
@@ -7,6 +7,7 @@ exports.ApiClient = void 0;
7
7
  const client_1 = require("./client");
8
8
  const getaccesstoken_1 = require("./getaccesstoken");
9
9
  const config_1 = require("./config");
10
+ const types_1 = require("./types");
10
11
  const version_json_1 = __importDefault(require("./version.json"));
11
12
  const OpenAPI_1 = require("./client/core/OpenAPI");
12
13
  class ApiClient extends client_1.CmsIntegrationApiClient {
@@ -18,6 +19,9 @@ class ApiClient extends client_1.CmsIntegrationApiClient {
18
19
  constructor(config) {
19
20
  const options = config ?? (0, config_1.getCmsIntegrationApiConfigFromEnvironment)();
20
21
  options.base = new URL(OpenAPI_1.OpenAPI.BASE, options.base);
22
+ if (options.cmsVersion == types_1.OptiCmsVersion.CMS12)
23
+ options.base.pathname = options.base.pathname.replace('preview2', 'preview1');
24
+ const apiVersion = options.cmsVersion == types_1.OptiCmsVersion.CMS12 ? 'preview1' : OpenAPI_1.OpenAPI.VERSION;
21
25
  let access_token = undefined;
22
26
  super({
23
27
  BASE: options.base.href,
@@ -36,10 +40,20 @@ class ApiClient extends client_1.CmsIntegrationApiClient {
36
40
  Connection: "Close"
37
41
  },
38
42
  WITH_CREDENTIALS: true,
39
- CREDENTIALS: "include"
43
+ CREDENTIALS: "include",
44
+ VERSION: apiVersion
40
45
  });
41
46
  this._config = options;
42
47
  }
48
+ /**
49
+ * Get the runtime configuration of the target CMS version.
50
+ *
51
+ * If this differs from the cmsVersion the client may not work fully or not
52
+ * at all.
53
+ */
54
+ get runtimeCmsVersion() {
55
+ return this._config.cmsVersion ?? types_1.OptiCmsVersion.CMS13;
56
+ }
43
57
  /**
44
58
  * The URL of the CMS instance
45
59
  */
@@ -53,7 +67,7 @@ class ApiClient extends client_1.CmsIntegrationApiClient {
53
67
  return this._config.debug ?? false;
54
68
  }
55
69
  get version() {
56
- return OpenAPI_1.OpenAPI.VERSION;
70
+ return this._config.cmsVersion == types_1.OptiCmsVersion.CMS12 ? 'preview1' : OpenAPI_1.OpenAPI.VERSION;
57
71
  }
58
72
  /**
59
73
  * The API version for which this client was build
@@ -39,6 +39,7 @@ export type { DisplaySettingChoice } from './models/DisplaySettingChoice';
39
39
  export type { DisplayTemplate } from './models/DisplayTemplate';
40
40
  export type { DisplayTemplatePage } from './models/DisplayTemplatePage';
41
41
  export type { DoubleEnumerationSettings } from './models/DoubleEnumerationSettings';
42
+ export type { DoubleEnumerationValue } from './models/DoubleEnumerationValue';
42
43
  export type { FloatListItem } from './models/FloatListItem';
43
44
  export type { FloatProperty } from './models/FloatProperty';
44
45
  export type { ImageDescriptor } from './models/ImageDescriptor';
@@ -46,6 +47,7 @@ export type { ImportPackageMessage } from './models/ImportPackageMessage';
46
47
  export type { ImportPackageResult } from './models/ImportPackageResult';
47
48
  export { IndexingType } from './models/IndexingType';
48
49
  export type { Int32EnumerationSettings } from './models/Int32EnumerationSettings';
50
+ export type { Int32EnumerationValue } from './models/Int32EnumerationValue';
49
51
  export type { IntegerListItem } from './models/IntegerListItem';
50
52
  export type { IntegerProperty } from './models/IntegerProperty';
51
53
  export type { JsonStringProperty } from './models/JsonStringProperty';
@@ -66,6 +68,7 @@ export type { PropertyFormatPage } from './models/PropertyFormatPage';
66
68
  export type { PropertyGroup } from './models/PropertyGroup';
67
69
  export type { PropertyGroupPage } from './models/PropertyGroupPage';
68
70
  export type { StringEnumerationSettings } from './models/StringEnumerationSettings';
71
+ export type { StringEnumerationValue } from './models/StringEnumerationValue';
69
72
  export type { StringListItem } from './models/StringListItem';
70
73
  export type { StringProperty } from './models/StringProperty';
71
74
  export type { UrlListItem } from './models/UrlListItem';
@@ -1,3 +1,4 @@
1
+ import type { DoubleEnumerationValue } from './DoubleEnumerationValue';
1
2
  /**
2
3
  * A writable implementation of an EnumerationSettings`1.
3
4
  */
@@ -5,5 +6,5 @@ export type DoubleEnumerationSettings = {
5
6
  /**
6
7
  * Enumeration values for this property or format.
7
8
  */
8
- values?: Record<string, string>;
9
+ values?: Array<DoubleEnumerationValue>;
9
10
  };
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Describes an enumeration value.
3
+ */
4
+ export type DoubleEnumerationValue = {
5
+ /**
6
+ * The display name of the enumeration.
7
+ */
8
+ displayName?: string;
9
+ /**
10
+ * The value of the enumeration.
11
+ */
12
+ value?: number;
13
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,3 +1,4 @@
1
+ import type { Int32EnumerationValue } from './Int32EnumerationValue';
1
2
  /**
2
3
  * A writable implementation of an EnumerationSettings`1.
3
4
  */
@@ -5,5 +6,5 @@ export type Int32EnumerationSettings = {
5
6
  /**
6
7
  * Enumeration values for this property or format.
7
8
  */
8
- values?: Record<string, string>;
9
+ values?: Array<Int32EnumerationValue>;
9
10
  };
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Describes an enumeration value.
3
+ */
4
+ export type Int32EnumerationValue = {
5
+ /**
6
+ * The display name of the enumeration.
7
+ */
8
+ displayName?: string;
9
+ /**
10
+ * The value of the enumeration.
11
+ */
12
+ value?: number;
13
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,3 +1,4 @@
1
+ import type { StringEnumerationValue } from './StringEnumerationValue';
1
2
  /**
2
3
  * A writable implementation of an EnumerationSettings`1.
3
4
  */
@@ -5,5 +6,5 @@ export type StringEnumerationSettings = {
5
6
  /**
6
7
  * Enumeration values for this property or format.
7
8
  */
8
- values?: Record<string, string>;
9
+ values?: Array<StringEnumerationValue>;
9
10
  };
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Describes an enumeration value.
3
+ */
4
+ export type StringEnumerationValue = {
5
+ /**
6
+ * The display name of the enumeration.
7
+ */
8
+ displayName?: string;
9
+ /**
10
+ * The value of the enumeration.
11
+ */
12
+ value?: string;
13
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/dist/config.d.ts CHANGED
@@ -1,8 +1,13 @@
1
+ import { OptiCmsVersion } from './types';
1
2
  export type CmsIntegrationApiOptions = {
2
3
  base: URL;
3
4
  clientId?: string;
4
5
  clientSecret?: string;
5
6
  actAs?: string;
6
7
  debug?: boolean;
8
+ /**
9
+ * The CMS Schema version that is used
10
+ */
11
+ cmsVersion?: OptiCmsVersion;
7
12
  };
8
13
  export declare function getCmsIntegrationApiConfigFromEnvironment(): CmsIntegrationApiOptions;
package/dist/config.js CHANGED
@@ -2,17 +2,21 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getCmsIntegrationApiConfigFromEnvironment = getCmsIntegrationApiConfigFromEnvironment;
4
4
  const OpenAPI_1 = require("./client/core/OpenAPI");
5
+ const types_1 = require("./types");
5
6
  function getCmsIntegrationApiConfigFromEnvironment() {
6
- const cmsUrl = getMandatory('OPTIMIZELY_CMS_URL');
7
+ const cmsUrl = getOptional('OPTIMIZELY_CMS_URL', 'https://example.cms.optimizely.com');
7
8
  const clientId = getMandatory('OPTIMIZELY_CMS_CLIENT_ID');
8
9
  const clientSecret = getMandatory('OPTIMIZELY_CMS_CLIENT_SECRET');
9
10
  const actAs = getOptional('OPTIMIZELY_CMS_USER_ID');
10
11
  const debug = getOptional('OPTIMIZELY_DEBUG', "0") == "1";
12
+ const cmsVersion = getSelection('OPTIMIZELY_CMS_SCHEMA', [types_1.OptiCmsVersion.CMS12, types_1.OptiCmsVersion.CMS13], types_1.OptiCmsVersion.CMS13);
11
13
  let baseUrl;
12
14
  try {
13
15
  baseUrl = new URL(OpenAPI_1.OpenAPI.BASE, cmsUrl);
16
+ if (cmsVersion == types_1.OptiCmsVersion.CMS12)
17
+ baseUrl.pathname = baseUrl.pathname.replace('preview2', 'preview1');
14
18
  }
15
- catch {
19
+ catch (e) {
16
20
  throw new Error("Invalid URL provided");
17
21
  }
18
22
  if (debug)
@@ -22,7 +26,8 @@ function getCmsIntegrationApiConfigFromEnvironment() {
22
26
  clientId,
23
27
  clientSecret,
24
28
  actAs,
25
- debug
29
+ debug,
30
+ cmsVersion
26
31
  };
27
32
  }
28
33
  function getOptional(variable, defaultValue) {
@@ -37,3 +42,11 @@ function getMandatory(variable) {
37
42
  throw new Error(`The environment variable ${variable} is missing or empty`);
38
43
  return envValue;
39
44
  }
45
+ function getSelection(envVarName, allowedValues, defaultValue) {
46
+ const rawValue = getOptional(envVarName, defaultValue);
47
+ if (!rawValue)
48
+ return defaultValue;
49
+ if (allowedValues.some(av => av == rawValue))
50
+ return rawValue;
51
+ return defaultValue;
52
+ }
@@ -3,13 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getAccessToken = getAccessToken;
4
4
  const config_1 = require("./config");
5
5
  const OpenAPI_1 = require("./client/core/OpenAPI");
6
+ const types_1 = require("./types");
6
7
  function isErrorResponse(response) {
7
8
  return typeof (response.error) == 'string';
8
9
  }
9
10
  async function getAccessToken(config) {
10
11
  const options = config ?? (0, config_1.getCmsIntegrationApiConfigFromEnvironment)();
11
- const authUrl = new URL(`${OpenAPI_1.OpenAPI.BASE}/oauth/token`, options.base).href;
12
+ let authUrl = new URL(`${OpenAPI_1.OpenAPI.BASE}/oauth/token`, options.base).href;
12
13
  const headers = new Headers();
14
+ if (options.cmsVersion == types_1.OptiCmsVersion.CMS12)
15
+ authUrl = authUrl.replace('preview2', 'preview1');
13
16
  headers.append('Authorization', `Basic ${base64Encode(`${options.clientId}:${options.clientSecret}`)}`);
14
17
  headers.append('Content-Type', 'application/x-www-form-urlencoded');
15
18
  headers.append('Connection', 'close');
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export * as IntegrationApi from './client';
3
3
  export { CmsIntegrationApiClient as CoreClient } from './client';
4
4
  export { ApiClient } from './api-client';
5
5
  export { ApiClient as CmsIntegrationApiClient } from './api-client';
6
+ export { OptiCmsVersion } from "./types";
6
7
  import { type CmsIntegrationApiOptions } from './config';
7
8
  import { ApiClient as CmsIntegrationApiClient } from './api-client';
8
9
  export declare function createClient(config?: CmsIntegrationApiOptions): CmsIntegrationApiClient;
package/dist/index.js CHANGED
@@ -26,7 +26,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
26
26
  return result;
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.ContentTypeKeys = exports.ContentRoots = exports.CmsIntegrationApiClient = exports.ApiClient = exports.CoreClient = exports.IntegrationApi = void 0;
29
+ exports.ContentTypeKeys = exports.ContentRoots = exports.OptiCmsVersion = exports.CmsIntegrationApiClient = exports.ApiClient = exports.CoreClient = exports.IntegrationApi = void 0;
30
30
  exports.createClient = createClient;
31
31
  __exportStar(require("./config"), exports);
32
32
  exports.IntegrationApi = __importStar(require("./client"));
@@ -36,6 +36,8 @@ var api_client_1 = require("./api-client");
36
36
  Object.defineProperty(exports, "ApiClient", { enumerable: true, get: function () { return api_client_1.ApiClient; } });
37
37
  var api_client_2 = require("./api-client");
38
38
  Object.defineProperty(exports, "CmsIntegrationApiClient", { enumerable: true, get: function () { return api_client_2.ApiClient; } });
39
+ var types_1 = require("./types");
40
+ Object.defineProperty(exports, "OptiCmsVersion", { enumerable: true, get: function () { return types_1.OptiCmsVersion; } });
39
41
  const api_client_3 = require("./api-client");
40
42
  function createClient(config) {
41
43
  return new api_client_3.ApiClient(config);
package/dist/types.d.ts CHANGED
@@ -6,3 +6,7 @@ export type InstanceApiVersionInfo = {
6
6
  cmsVersion: string;
7
7
  results: Record<string, any>;
8
8
  };
9
+ export declare enum OptiCmsVersion {
10
+ CMS12 = "OPTI-CMS-12",
11
+ CMS13 = "OPTI-CMS-13"
12
+ }
package/dist/types.js CHANGED
@@ -1,2 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OptiCmsVersion = void 0;
4
+ var OptiCmsVersion;
5
+ (function (OptiCmsVersion) {
6
+ OptiCmsVersion["CMS12"] = "OPTI-CMS-12";
7
+ OptiCmsVersion["CMS13"] = "OPTI-CMS-13";
8
+ })(OptiCmsVersion || (exports.OptiCmsVersion = OptiCmsVersion = {}));
package/dist/version.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "api": "preview2-1.0.0",
3
- "service": "2024.08.05.0953",
4
- "cms": "2024.08.05.0953"
3
+ "service": "2024.09.13.0710",
4
+ "cms": "2024.09.13.0710"
5
5
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@remkoj/optimizely-cms-api",
3
3
  "displayName": "Optimizely CMS - Integration API Client",
4
4
  "description": "A Javascript client for the Integration API provided by the Optimizely CMS.",
5
- "version": "2.0.3",
5
+ "version": "2.1.0",
6
6
  "packageManager": "yarn@4.3.0",
7
7
  "type": "commonjs",
8
8
  "license": "Apache-2.0",
@@ -12,12 +12,12 @@
12
12
  "./dist"
13
13
  ],
14
14
  "devDependencies": {
15
- "@types/node": "^22.2.0",
15
+ "@types/node": "^22.7.4",
16
16
  "dotenv": "^16.4.5",
17
17
  "glob": "^11.0.0",
18
18
  "openapi-typescript-codegen": "^0.29.0",
19
- "tslib": "^2.6.3",
20
- "typescript": "^5.5.4"
19
+ "tslib": "^2.7.0",
20
+ "typescript": "^5.6.2"
21
21
  },
22
22
  "scripts": {
23
23
  "clean": "tsc --build --clean",