@remkoj/optimizely-cms-api 6.0.0-pre3 → 6.0.0-pre4

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.
@@ -2,7 +2,7 @@ import * as Operations from './client/sdk.gen';
2
2
  import { type CmsIntegrationApiOptions } from "./config";
3
3
  import { type InstanceApiVersionInfo, OptiCmsVersion } from "./types";
4
4
  import { createClient, type RequestResult, type ResponseStyle } from './client/client';
5
- import { type CreateClientConfig } from './client/client.gen';
5
+ import type { CreateClientConfig } from './client/client.gen';
6
6
  type OperationsType = {
7
7
  -readonly [KT in keyof typeof Operations]: (typeof Operations)[KT];
8
8
  };
@@ -4,7 +4,7 @@ exports.createClientConfig = void 0;
4
4
  const config_1 = require("./config");
5
5
  const getaccesstoken_1 = require("./getaccesstoken");
6
6
  const createClientConfig = (config) => {
7
- const envConfig = (0, config_1.getCmsIntegrationApiConfigFromEnvironment)();
7
+ const envConfig = (0, config_1.readPartialEnvConfig)();
8
8
  const baseUrl = envConfig.base.href;
9
9
  const newClientConfig = {
10
10
  baseUrl,
package/dist/config.d.ts CHANGED
@@ -10,4 +10,8 @@ export type CmsIntegrationApiOptions = {
10
10
  */
11
11
  cmsVersion?: OptiCmsVersion;
12
12
  };
13
+ type ClientCredentials = 'clientId' | 'clientSecret';
14
+ export declare function readPartialEnvConfig(): CmsIntegrationApiOptions;
15
+ export declare function readEnvConfig(): Omit<CmsIntegrationApiOptions, ClientCredentials> & Pick<Required<CmsIntegrationApiOptions>, ClientCredentials>;
13
16
  export declare function getCmsIntegrationApiConfigFromEnvironment(): CmsIntegrationApiOptions;
17
+ export {};
package/dist/config.js CHANGED
@@ -1,9 +1,47 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readPartialEnvConfig = readPartialEnvConfig;
4
+ exports.readEnvConfig = readEnvConfig;
3
5
  exports.getCmsIntegrationApiConfigFromEnvironment = getCmsIntegrationApiConfigFromEnvironment;
4
6
  const types_1 = require("./types");
7
+ function readPartialEnvConfig() {
8
+ const cmsUrl = getOptional('OPTIMIZELY_CMS_URL', 'https://api.cms.optimizely.com');
9
+ const clientId = getOptional('OPTIMIZELY_CMS_CLIENT_ID');
10
+ const clientSecret = getOptional('OPTIMIZELY_CMS_CLIENT_SECRET');
11
+ const actAs = getOptional('OPTIMIZELY_CMS_USER_ID');
12
+ const debug = getOptional('OPTIMIZELY_DEBUG', "0") == "1";
13
+ const cmsVersion = getSelection('OPTIMIZELY_CMS_SCHEMA', [types_1.OptiCmsVersion.CMS12, types_1.OptiCmsVersion.CMS13], types_1.OptiCmsVersion.CMS13);
14
+ let baseUrl;
15
+ try {
16
+ const cmsUrlAdjusted = cmsUrl.includes("://") ? cmsUrl : 'https://' + cmsUrl;
17
+ baseUrl = new URL('/_cms/preview2', cmsUrlAdjusted);
18
+ if (cmsVersion == types_1.OptiCmsVersion.CMS12)
19
+ baseUrl.pathname = baseUrl.pathname.replace('preview2', 'preview1');
20
+ }
21
+ catch (e) {
22
+ throw new Error("Invalid Optimizely CMS URL provided");
23
+ }
24
+ if (debug)
25
+ console.log(`[Optimizely CMS API] Connecting to ${baseUrl} as ${clientId ?? 'Anonymous'}`);
26
+ return {
27
+ base: baseUrl,
28
+ clientId,
29
+ clientSecret,
30
+ actAs,
31
+ debug,
32
+ cmsVersion
33
+ };
34
+ }
35
+ function readEnvConfig() {
36
+ const partialConfig = readPartialEnvConfig();
37
+ if (!partialConfig.clientId)
38
+ throw new Error("The Client ID (OPTIMIZELY_CMS_CLIENT_ID) is a required environment variable");
39
+ if (!partialConfig.clientSecret)
40
+ throw new Error("The Client Secret (OPTIMIZELY_CMS_CLIENT_SECRET) is a required environment variable");
41
+ return partialConfig;
42
+ }
5
43
  function getCmsIntegrationApiConfigFromEnvironment() {
6
- const cmsUrl = getOptional('OPTIMIZELY_CMS_URL', 'https://example.cms.optimizely.com');
44
+ const cmsUrl = getOptional('OPTIMIZELY_CMS_URL', 'https://api.cms.optimizely.com');
7
45
  const clientId = getMandatory('OPTIMIZELY_CMS_CLIENT_ID');
8
46
  const clientSecret = getMandatory('OPTIMIZELY_CMS_CLIENT_SECRET');
9
47
  const actAs = getOptional('OPTIMIZELY_CMS_USER_ID');
@@ -12,7 +12,7 @@ async function getAccessToken(config) {
12
12
  const headers = new Headers();
13
13
  if (options.cmsVersion == types_1.OptiCmsVersion.CMS12)
14
14
  authUrl = authUrl.replace('preview2', 'preview1');
15
- headers.append('Authorization', `Basic ${base64Encode(`${options.clientId}:${options.clientSecret}`)}`);
15
+ headers.append('Authorization', `Basic ${base64Encode(`${options.clientId ?? ''}:${options.clientSecret ?? ''}`)}`);
16
16
  headers.append('Content-Type', 'application/x-www-form-urlencoded');
17
17
  headers.append('Connection', 'close');
18
18
  if (options.debug) {
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export { ApiClient, ApiError, ApiClient as CoreClient, type ApiClientStatic, typ
4
4
  export { OptiCmsVersion } from "./types";
5
5
  import { type CmsIntegrationApiOptions } from './config';
6
6
  import { type CmsIntegrationApiClient } from './api-client';
7
+ export { getCmsIntegrationApiConfigFromEnvironment as readEnvConfig } from './config';
7
8
  export declare function createClient(config?: CmsIntegrationApiOptions): CmsIntegrationApiClient;
8
9
  export declare enum ContentRoots {
9
10
  /**
package/dist/index.js CHANGED
@@ -36,7 +36,7 @@ var __importStar = (this && this.__importStar) || (function () {
36
36
  };
37
37
  })();
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.ContentTypeKeys = exports.ContentRoots = exports.OptiCmsVersion = exports.CoreClient = exports.ApiError = exports.ApiClient = exports.IntegrationApi = void 0;
39
+ exports.ContentTypeKeys = exports.ContentRoots = exports.readEnvConfig = exports.OptiCmsVersion = exports.CoreClient = exports.ApiError = exports.ApiClient = exports.IntegrationApi = void 0;
40
40
  exports.createClient = createClient;
41
41
  __exportStar(require("./config"), exports);
42
42
  exports.IntegrationApi = __importStar(require("./client/types.gen"));
@@ -47,6 +47,8 @@ Object.defineProperty(exports, "CoreClient", { enumerable: true, get: function (
47
47
  var types_1 = require("./types");
48
48
  Object.defineProperty(exports, "OptiCmsVersion", { enumerable: true, get: function () { return types_1.OptiCmsVersion; } });
49
49
  const api_client_2 = require("./api-client");
50
+ var config_1 = require("./config");
51
+ Object.defineProperty(exports, "readEnvConfig", { enumerable: true, get: function () { return config_1.getCmsIntegrationApiConfigFromEnvironment; } });
50
52
  function createClient(config) {
51
53
  return new api_client_2.ApiClient(config);
52
54
  }
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": "6.0.0-pre3",
5
+ "version": "6.0.0-pre4",
6
6
  "type": "commonjs",
7
7
  "license": "Apache-2.0",
8
8
  "main": "./dist/index.js",