@remkoj/optimizely-cms-api 5.1.0 → 6.0.0-pre1

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 (198) hide show
  1. package/dist/api-client.d.ts +53 -10
  2. package/dist/api-client.js +142 -42
  3. package/dist/client/client/client.d.ts +2 -0
  4. package/dist/client/client/client.js +147 -0
  5. package/dist/client/client/index.d.ts +7 -0
  6. package/dist/client/client/index.js +14 -0
  7. package/dist/client/client/types.d.ts +119 -0
  8. package/dist/client/client/utils.d.ts +45 -0
  9. package/dist/client/client/utils.js +296 -0
  10. package/dist/client/client.gen.d.ts +12 -0
  11. package/dist/client/client.gen.js +9 -0
  12. package/dist/client/core/auth.d.ts +18 -0
  13. package/dist/client/core/auth.js +17 -0
  14. package/dist/client/core/bodySerializer.d.ts +17 -0
  15. package/dist/client/core/bodySerializer.js +56 -0
  16. package/dist/client/core/params.d.ts +23 -0
  17. package/dist/client/core/params.js +91 -0
  18. package/dist/client/core/pathSerializer.d.ts +33 -0
  19. package/dist/client/core/pathSerializer.js +122 -0
  20. package/dist/client/core/types.d.ts +73 -0
  21. package/dist/client/index.d.ts +4 -78
  22. package/dist/client/index.js +19 -46
  23. package/dist/client/sdk.gen.d.ts +256 -0
  24. package/dist/client/sdk.gen.js +638 -0
  25. package/dist/client/transformers.gen.d.ts +34 -0
  26. package/dist/client/transformers.gen.js +273 -0
  27. package/dist/client/types.gen.d.ts +2941 -0
  28. package/dist/client/types.gen.js +87 -0
  29. package/dist/client-config.d.ts +2 -0
  30. package/dist/client-config.js +27 -0
  31. package/dist/config.js +1 -2
  32. package/dist/getaccesstoken.js +1 -2
  33. package/dist/index.d.ts +3 -5
  34. package/dist/index.js +6 -8
  35. package/dist/version.json +2 -2
  36. package/package.json +18 -9
  37. package/dist/client/CmsIntegrationApiClient.d.ts +0 -22
  38. package/dist/client/CmsIntegrationApiClient.js +0 -34
  39. package/dist/client/core/ApiError.d.ts +0 -10
  40. package/dist/client/core/ApiError.js +0 -15
  41. package/dist/client/core/ApiRequestOptions.d.ts +0 -13
  42. package/dist/client/core/ApiResult.d.ts +0 -7
  43. package/dist/client/core/BaseHttpRequest.d.ts +0 -8
  44. package/dist/client/core/BaseHttpRequest.js +0 -9
  45. package/dist/client/core/CancelablePromise.d.ts +0 -20
  46. package/dist/client/core/CancelablePromise.js +0 -116
  47. package/dist/client/core/FetchHttpRequest.d.ts +0 -14
  48. package/dist/client/core/FetchHttpRequest.js +0 -20
  49. package/dist/client/core/OpenAPI.d.ts +0 -16
  50. package/dist/client/core/OpenAPI.js +0 -14
  51. package/dist/client/core/request.d.ts +0 -30
  52. package/dist/client/core/request.js +0 -294
  53. package/dist/client/models/BinaryProperty.d.ts +0 -8
  54. package/dist/client/models/BinaryProperty.js +0 -2
  55. package/dist/client/models/BooleanProperty.d.ts +0 -5
  56. package/dist/client/models/BooleanProperty.js +0 -2
  57. package/dist/client/models/Changeset.d.ts +0 -26
  58. package/dist/client/models/Changeset.js +0 -2
  59. package/dist/client/models/ChangesetItem.d.ts +0 -11
  60. package/dist/client/models/ChangesetItem.js +0 -2
  61. package/dist/client/models/ChangesetItemPage.d.ts +0 -19
  62. package/dist/client/models/ChangesetItemPage.js +0 -2
  63. package/dist/client/models/ChangesetPage.d.ts +0 -19
  64. package/dist/client/models/ChangesetPage.js +0 -2
  65. package/dist/client/models/ComponentListItem.d.ts +0 -10
  66. package/dist/client/models/ComponentListItem.js +0 -2
  67. package/dist/client/models/ComponentProperty.d.ts +0 -10
  68. package/dist/client/models/ComponentProperty.js +0 -2
  69. package/dist/client/models/CompositionBehavior.d.ts +0 -7
  70. package/dist/client/models/CompositionBehavior.js +0 -15
  71. package/dist/client/models/ContentBaseType.d.ts +0 -14
  72. package/dist/client/models/ContentBaseType.js +0 -22
  73. package/dist/client/models/ContentComponent.d.ts +0 -30
  74. package/dist/client/models/ContentComponent.js +0 -2
  75. package/dist/client/models/ContentItem.d.ts +0 -60
  76. package/dist/client/models/ContentItem.js +0 -2
  77. package/dist/client/models/ContentItemListWithContentTypes.d.ts +0 -27
  78. package/dist/client/models/ContentItemListWithContentTypes.js +0 -2
  79. package/dist/client/models/ContentItemPage.d.ts +0 -19
  80. package/dist/client/models/ContentItemPage.js +0 -2
  81. package/dist/client/models/ContentItemWithContentTypes.d.ts +0 -12
  82. package/dist/client/models/ContentItemWithContentTypes.js +0 -2
  83. package/dist/client/models/ContentListItem.d.ts +0 -14
  84. package/dist/client/models/ContentListItem.js +0 -2
  85. package/dist/client/models/ContentLocaleInfo.d.ts +0 -19
  86. package/dist/client/models/ContentLocaleInfo.js +0 -2
  87. package/dist/client/models/ContentMetadata.d.ts +0 -38
  88. package/dist/client/models/ContentMetadata.js +0 -2
  89. package/dist/client/models/ContentMetadataPage.d.ts +0 -19
  90. package/dist/client/models/ContentMetadataPage.js +0 -2
  91. package/dist/client/models/ContentProperty.d.ts +0 -14
  92. package/dist/client/models/ContentProperty.js +0 -2
  93. package/dist/client/models/ContentReference.d.ts +0 -17
  94. package/dist/client/models/ContentReference.js +0 -2
  95. package/dist/client/models/ContentReferenceListItem.d.ts +0 -14
  96. package/dist/client/models/ContentReferenceListItem.js +0 -2
  97. package/dist/client/models/ContentReferenceProperty.d.ts +0 -14
  98. package/dist/client/models/ContentReferenceProperty.js +0 -2
  99. package/dist/client/models/ContentType.d.ts +0 -84
  100. package/dist/client/models/ContentType.js +0 -2
  101. package/dist/client/models/ContentTypeFeature.d.ts +0 -10
  102. package/dist/client/models/ContentTypeFeature.js +0 -18
  103. package/dist/client/models/ContentTypePage.d.ts +0 -19
  104. package/dist/client/models/ContentTypePage.js +0 -2
  105. package/dist/client/models/ContentTypeProperty.d.ts +0 -47
  106. package/dist/client/models/ContentTypeProperty.js +0 -2
  107. package/dist/client/models/ContentTypeUsage.d.ts +0 -7
  108. package/dist/client/models/ContentTypeUsage.js +0 -15
  109. package/dist/client/models/CopyContentOptions.d.ts +0 -21
  110. package/dist/client/models/CopyContentOptions.js +0 -2
  111. package/dist/client/models/DateTimeListItem.d.ts +0 -14
  112. package/dist/client/models/DateTimeListItem.js +0 -2
  113. package/dist/client/models/DateTimeProperty.d.ts +0 -14
  114. package/dist/client/models/DateTimeProperty.js +0 -2
  115. package/dist/client/models/DisplaySetting.d.ts +0 -22
  116. package/dist/client/models/DisplaySetting.js +0 -2
  117. package/dist/client/models/DisplaySettingChoice.d.ts +0 -13
  118. package/dist/client/models/DisplaySettingChoice.js +0 -2
  119. package/dist/client/models/DisplayTemplate.d.ts +0 -49
  120. package/dist/client/models/DisplayTemplate.js +0 -2
  121. package/dist/client/models/DisplayTemplatePage.d.ts +0 -19
  122. package/dist/client/models/DisplayTemplatePage.js +0 -2
  123. package/dist/client/models/DoubleEnumerationSettings.d.ts +0 -10
  124. package/dist/client/models/DoubleEnumerationSettings.js +0 -2
  125. package/dist/client/models/DoubleEnumerationValue.d.ts +0 -13
  126. package/dist/client/models/DoubleEnumerationValue.js +0 -2
  127. package/dist/client/models/FloatListItem.d.ts +0 -16
  128. package/dist/client/models/FloatListItem.js +0 -2
  129. package/dist/client/models/FloatProperty.d.ts +0 -16
  130. package/dist/client/models/FloatProperty.js +0 -2
  131. package/dist/client/models/ImageDescriptor.d.ts +0 -17
  132. package/dist/client/models/ImageDescriptor.js +0 -2
  133. package/dist/client/models/IndexingType.d.ts +0 -8
  134. package/dist/client/models/IndexingType.js +0 -16
  135. package/dist/client/models/Int32EnumerationSettings.d.ts +0 -10
  136. package/dist/client/models/Int32EnumerationSettings.js +0 -2
  137. package/dist/client/models/Int32EnumerationValue.d.ts +0 -13
  138. package/dist/client/models/Int32EnumerationValue.js +0 -2
  139. package/dist/client/models/IntegerListItem.d.ts +0 -16
  140. package/dist/client/models/IntegerListItem.js +0 -2
  141. package/dist/client/models/IntegerProperty.d.ts +0 -16
  142. package/dist/client/models/IntegerProperty.js +0 -2
  143. package/dist/client/models/JsonStringProperty.d.ts +0 -5
  144. package/dist/client/models/JsonStringProperty.js +0 -2
  145. package/dist/client/models/ListProperty.d.ts +0 -26
  146. package/dist/client/models/ListProperty.js +0 -2
  147. package/dist/client/models/ListPropertyItem.d.ts +0 -11
  148. package/dist/client/models/ListPropertyItem.js +0 -2
  149. package/dist/client/models/LocaleStatus.d.ts +0 -7
  150. package/dist/client/models/LocaleStatus.js +0 -15
  151. package/dist/client/models/OauthToken.d.ts +0 -18
  152. package/dist/client/models/OauthToken.js +0 -2
  153. package/dist/client/models/OauthTokenError.d.ts +0 -13
  154. package/dist/client/models/OauthTokenError.js +0 -2
  155. package/dist/client/models/OauthTokenRequest.d.ts +0 -21
  156. package/dist/client/models/OauthTokenRequest.js +0 -2
  157. package/dist/client/models/ProblemDetails.d.ts +0 -1
  158. package/dist/client/models/ProblemDetails.js +0 -2
  159. package/dist/client/models/PropertyDataType.d.ts +0 -18
  160. package/dist/client/models/PropertyDataType.js +0 -26
  161. package/dist/client/models/PropertyFormat.d.ts +0 -37
  162. package/dist/client/models/PropertyFormat.js +0 -2
  163. package/dist/client/models/PropertyFormatPage.d.ts +0 -19
  164. package/dist/client/models/PropertyFormatPage.js +0 -2
  165. package/dist/client/models/PropertyGroup.d.ts +0 -21
  166. package/dist/client/models/PropertyGroup.js +0 -2
  167. package/dist/client/models/PropertyGroupPage.d.ts +0 -19
  168. package/dist/client/models/PropertyGroupPage.js +0 -2
  169. package/dist/client/models/StringEnumerationSettings.d.ts +0 -10
  170. package/dist/client/models/StringEnumerationSettings.js +0 -2
  171. package/dist/client/models/StringEnumerationValue.d.ts +0 -13
  172. package/dist/client/models/StringEnumerationValue.js +0 -2
  173. package/dist/client/models/StringListItem.d.ts +0 -20
  174. package/dist/client/models/StringListItem.js +0 -2
  175. package/dist/client/models/StringProperty.d.ts +0 -20
  176. package/dist/client/models/StringProperty.js +0 -2
  177. package/dist/client/models/UrlListItem.d.ts +0 -5
  178. package/dist/client/models/UrlListItem.js +0 -2
  179. package/dist/client/models/UrlProperty.d.ts +0 -5
  180. package/dist/client/models/UrlProperty.js +0 -2
  181. package/dist/client/models/VersionStatus.d.ts +0 -12
  182. package/dist/client/models/VersionStatus.js +0 -20
  183. package/dist/client/services/ChangesetsService.d.ts +0 -105
  184. package/dist/client/services/ChangesetsService.js +0 -239
  185. package/dist/client/services/ContentService.d.ts +0 -172
  186. package/dist/client/services/ContentService.js +0 -409
  187. package/dist/client/services/ContentTypesService.d.ts +0 -66
  188. package/dist/client/services/ContentTypesService.js +0 -149
  189. package/dist/client/services/DisplayTemplatesService.d.ts +0 -60
  190. package/dist/client/services/DisplayTemplatesService.js +0 -135
  191. package/dist/client/services/OauthService.d.ts +0 -17
  192. package/dist/client/services/OauthService.js +0 -28
  193. package/dist/client/services/PropertyFormatsService.d.ts +0 -26
  194. package/dist/client/services/PropertyFormatsService.js +0 -53
  195. package/dist/client/services/PropertyGroupsService.d.ts +0 -61
  196. package/dist/client/services/PropertyGroupsService.js +0 -135
  197. /package/dist/client/{core/ApiRequestOptions.js → client/types.js} +0 -0
  198. /package/dist/client/core/{ApiResult.js → types.js} +0 -0
@@ -1,14 +1,31 @@
1
- import { CmsIntegrationApiClient } from "./client";
1
+ import * as Operations from './client/sdk.gen';
2
2
  import { type CmsIntegrationApiOptions } from "./config";
3
3
  import { type InstanceApiVersionInfo, OptiCmsVersion } from "./types";
4
- import type { CancelablePromise } from "./client";
5
- export declare class ApiClient extends CmsIntegrationApiClient {
6
- private _config;
7
- /**
8
- * Create a new instance of the API Client
9
- *
10
- * @param config The instance configuration
11
- */
4
+ import { createClient, type RequestResult, type ResponseStyle } from './client/client';
5
+ import { type CreateClientConfig } from './client/client.gen';
6
+ type OperationsType = {
7
+ -readonly [KT in keyof typeof Operations]: (typeof Operations)[KT];
8
+ };
9
+ type OperationsNames = keyof OperationsType;
10
+ type OperationReturnType<RT extends (...args: any) => any> = ReturnType<RT> extends RequestResult<any, any, boolean, ResponseStyle> ? Promise<NonNullable<Awaited<ReturnType<RT>>['data']>> : never;
11
+ type ApiClientFunctions = {
12
+ readonly [KT in OperationsNames]: OperationsType[KT] extends Function ? (options?: Parameters<OperationsType[KT]>[0]) => OperationReturnType<OperationsType[KT]> : never;
13
+ };
14
+ type BaseClass = {
15
+ new (...args: any[]): BaseApiClient;
16
+ };
17
+ type NonConstructorKeys<T> = ({
18
+ [P in keyof T]: T[P] extends new () => any ? never : P;
19
+ })[keyof T];
20
+ type OmitConstructor<TBase extends BaseClass> = Pick<TBase, NonConstructorKeys<TBase>>;
21
+ export type ClassWithMixin<TBase extends BaseClass, Mixin> = OmitConstructor<TBase> & {
22
+ new (...args: ConstructorParameters<TBase>): InstanceType<TBase> & Mixin;
23
+ };
24
+ declare class BaseApiClient {
25
+ protected _config: CmsIntegrationApiOptions;
26
+ protected _client: ReturnType<typeof createClient>;
27
+ private _token;
28
+ protected getAccessToken(): Promise<string>;
12
29
  constructor(config?: CmsIntegrationApiOptions);
13
30
  /**
14
31
  * Get the runtime configuration of the target CMS version.
@@ -39,5 +56,31 @@ export declare class ApiClient extends CmsIntegrationApiClient {
39
56
  *
40
57
  * @returns The version information from the running instance
41
58
  */
42
- getInstanceInfo(): CancelablePromise<InstanceApiVersionInfo>;
59
+ getInstanceInfo(): Promise<InstanceApiVersionInfo>;
60
+ protected createClientConfig: CreateClientConfig;
61
+ }
62
+ export declare class ApiError extends Error {
63
+ protected _ctx: {
64
+ error: unknown;
65
+ request: Request;
66
+ response: Response;
67
+ };
68
+ constructor(data: {
69
+ error: unknown;
70
+ request: Request;
71
+ response: Response;
72
+ });
73
+ get data(): unknown;
74
+ /**
75
+ * @deprecated use data() instead
76
+ */
77
+ get body(): unknown;
78
+ get request(): unknown;
79
+ get response(): unknown;
80
+ get status(): number;
81
+ get statusText(): string;
43
82
  }
83
+ export declare const ApiClient: ClassWithMixin<typeof BaseApiClient, ApiClientFunctions>;
84
+ export type ApiClientStatic = typeof ApiClient;
85
+ export type CmsIntegrationApiClient = InstanceType<typeof ApiClient>;
86
+ export default ApiClient;
@@ -1,49 +1,111 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
37
  };
5
38
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ApiClient = void 0;
7
- const client_1 = require("./client");
8
- const getaccesstoken_1 = require("./getaccesstoken");
39
+ exports.ApiClient = exports.ApiError = void 0;
40
+ const Operations = __importStar(require("./client/sdk.gen"));
9
41
  const config_1 = require("./config");
10
42
  const types_1 = require("./types");
43
+ const client_1 = require("./client/client");
11
44
  const version_json_1 = __importDefault(require("./version.json"));
12
- const OpenAPI_1 = require("./client/core/OpenAPI");
13
- class ApiClient extends client_1.CmsIntegrationApiClient {
14
- /**
15
- * Create a new instance of the API Client
16
- *
17
- * @param config The instance configuration
18
- */
45
+ const getaccesstoken_1 = require("./getaccesstoken");
46
+ function applyOperations(Base) {
47
+ // Create the new class
48
+ class NewClass extends Base {
49
+ constructor(...args) {
50
+ super(...args);
51
+ }
52
+ }
53
+ // Bind the operations
54
+ Object.getOwnPropertyNames(Operations).filter(isApiClientFunction).forEach((propName) => {
55
+ async function wrapper(args) {
56
+ const operationArgs = {
57
+ throwOnError: false,
58
+ ...args,
59
+ //@ts-expect-error
60
+ client: this._client
61
+ };
62
+ //@ts-expect-error
63
+ const result = await Operations[propName](operationArgs);
64
+ if (result.data)
65
+ return result.data;
66
+ throw new ApiError(result);
67
+ }
68
+ //@ts-expect-error
69
+ NewClass.prototype[propName] = wrapper;
70
+ });
71
+ // Return the new class
72
+ return NewClass;
73
+ }
74
+ function isApiClientFunction(propName) {
75
+ return typeof (Operations[propName]) == 'function';
76
+ }
77
+ class BaseApiClient {
78
+ _config;
79
+ _client;
80
+ _token;
81
+ getAccessToken() {
82
+ if (!this._token)
83
+ this._token = (0, getaccesstoken_1.getAccessToken)(this._config);
84
+ return this._token;
85
+ }
19
86
  constructor(config) {
87
+ // Prepare config
20
88
  const options = config ?? (0, config_1.getCmsIntegrationApiConfigFromEnvironment)();
21
- options.base = new URL(OpenAPI_1.OpenAPI.BASE, options.base);
89
+ options.base = new URL("/_cms/preview2", options.base);
22
90
  if (options.cmsVersion == types_1.OptiCmsVersion.CMS12)
23
91
  options.base.pathname = options.base.pathname.replace('preview2', 'preview1');
24
- const apiVersion = options.cmsVersion == types_1.OptiCmsVersion.CMS12 ? 'preview1' : OpenAPI_1.OpenAPI.VERSION;
25
- let access_token = undefined;
26
- super({
27
- BASE: options.base.href,
28
- TOKEN: async () => {
29
- if (!access_token)
30
- access_token = await (0, getaccesstoken_1.getAccessToken)(options).catch(e => {
31
- if (options.debug) {
32
- console.error(`🔴 [CMS API] Failed to obtain an access token`);
33
- console.error(e);
34
- }
35
- return undefined;
36
- });
37
- return access_token ?? '';
38
- },
39
- HEADERS: {
40
- Connection: "Close"
41
- },
42
- WITH_CREDENTIALS: true,
43
- CREDENTIALS: "include",
44
- VERSION: apiVersion
45
- });
92
+ // Store instance variables
46
93
  this._config = options;
94
+ this._client = (0, client_1.createClient)(this.createClientConfig());
95
+ // Configure Client
96
+ this._client.interceptors.request.use(async (request) => {
97
+ const token = await this.getAccessToken();
98
+ request.headers.set('Authorization', 'Bearer ' + token);
99
+ if (this._config.debug) {
100
+ console.log(`🔍 Sending ${request.method} request to ${request.url}`);
101
+ }
102
+ return request;
103
+ });
104
+ if (this._config.debug)
105
+ this._client.interceptors.response.use((response, request) => {
106
+ console.log(`✨ Received response ${response.status} ${response.statusText} of type ${response.headers.get('Content-Type') ?? 'unknown'} for ${request.url}`);
107
+ return response;
108
+ });
47
109
  }
48
110
  /**
49
111
  * Get the runtime configuration of the target CMS version.
@@ -67,7 +129,7 @@ class ApiClient extends client_1.CmsIntegrationApiClient {
67
129
  return this._config.debug ?? false;
68
130
  }
69
131
  get version() {
70
- return this._config.cmsVersion == types_1.OptiCmsVersion.CMS12 ? 'preview1' : OpenAPI_1.OpenAPI.VERSION;
132
+ return this._config.cmsVersion == types_1.OptiCmsVersion.CMS12 ? 'preview1' : 'preview2';
71
133
  }
72
134
  /**
73
135
  * The API version for which this client was build
@@ -86,14 +148,52 @@ class ApiClient extends client_1.CmsIntegrationApiClient {
86
148
  *
87
149
  * @returns The version information from the running instance
88
150
  */
89
- getInstanceInfo() {
90
- return this.request.request({
91
- method: 'GET',
92
- url: '/info',
93
- errors: {
94
- 403: `Forbidden`,
95
- },
151
+ async getInstanceInfo() {
152
+ const result = await this._client.get({
153
+ url: '/info'
96
154
  });
155
+ if (result.data)
156
+ return result.data;
157
+ throw new ApiError(result);
158
+ }
159
+ createClientConfig = (override) => {
160
+ return {
161
+ ...override,
162
+ baseUrl: this._config.base.href
163
+ };
164
+ };
165
+ }
166
+ class ApiError extends Error {
167
+ _ctx;
168
+ constructor(data) {
169
+ if (typeof data.error == 'string')
170
+ super(data.error);
171
+ else
172
+ super(`Optimizely CMS API Error: ${data.response.status} ${data.response.statusText}`);
173
+ this._ctx = data;
174
+ }
175
+ get data() {
176
+ return this._ctx.error;
177
+ }
178
+ /**
179
+ * @deprecated use data() instead
180
+ */
181
+ get body() {
182
+ return this._ctx.error;
183
+ }
184
+ get request() {
185
+ return this._ctx.request;
186
+ }
187
+ get response() {
188
+ return this._ctx.response;
189
+ }
190
+ get status() {
191
+ return this._ctx.response.status;
192
+ }
193
+ get statusText() {
194
+ return this._ctx.response.statusText;
97
195
  }
98
196
  }
99
- exports.ApiClient = ApiClient;
197
+ exports.ApiError = ApiError;
198
+ exports.ApiClient = applyOperations(BaseApiClient);
199
+ exports.default = exports.ApiClient;
@@ -0,0 +1,2 @@
1
+ import type { Client, Config } from './types';
2
+ export declare const createClient: (config?: Config) => Client;
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createClient = void 0;
4
+ const utils_1 = require("./utils");
5
+ const createClient = (config = {}) => {
6
+ let _config = (0, utils_1.mergeConfigs)((0, utils_1.createConfig)(), config);
7
+ const getConfig = () => ({ ..._config });
8
+ const setConfig = (config) => {
9
+ _config = (0, utils_1.mergeConfigs)(_config, config);
10
+ return getConfig();
11
+ };
12
+ const interceptors = (0, utils_1.createInterceptors)();
13
+ const request = async (options) => {
14
+ const opts = {
15
+ ..._config,
16
+ ...options,
17
+ fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
18
+ headers: (0, utils_1.mergeHeaders)(_config.headers, options.headers),
19
+ };
20
+ if (opts.security) {
21
+ await (0, utils_1.setAuthParams)({
22
+ ...opts,
23
+ security: opts.security,
24
+ });
25
+ }
26
+ if (opts.requestValidator) {
27
+ await opts.requestValidator(opts);
28
+ }
29
+ if (opts.body && opts.bodySerializer) {
30
+ opts.body = opts.bodySerializer(opts.body);
31
+ }
32
+ // remove Content-Type header if body is empty to avoid sending invalid requests
33
+ if (opts.body === undefined || opts.body === '') {
34
+ opts.headers.delete('Content-Type');
35
+ }
36
+ const url = (0, utils_1.buildUrl)(opts);
37
+ const requestInit = {
38
+ redirect: 'follow',
39
+ ...opts,
40
+ };
41
+ let request = new Request(url, requestInit);
42
+ for (const fn of interceptors.request._fns) {
43
+ if (fn) {
44
+ request = await fn(request, opts);
45
+ }
46
+ }
47
+ // fetch must be assigned here, otherwise it would throw the error:
48
+ // TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
49
+ const _fetch = opts.fetch;
50
+ let response = await _fetch(request);
51
+ for (const fn of interceptors.response._fns) {
52
+ if (fn) {
53
+ response = await fn(response, request, opts);
54
+ }
55
+ }
56
+ const result = {
57
+ request,
58
+ response,
59
+ };
60
+ if (response.ok) {
61
+ if (response.status === 204 ||
62
+ response.headers.get('Content-Length') === '0') {
63
+ return opts.responseStyle === 'data'
64
+ ? {}
65
+ : {
66
+ data: {},
67
+ ...result,
68
+ };
69
+ }
70
+ const parseAs = (opts.parseAs === 'auto'
71
+ ? (0, utils_1.getParseAs)(response.headers.get('Content-Type'))
72
+ : opts.parseAs) ?? 'json';
73
+ let data;
74
+ switch (parseAs) {
75
+ case 'arrayBuffer':
76
+ case 'blob':
77
+ case 'formData':
78
+ case 'json':
79
+ case 'text':
80
+ data = await response[parseAs]();
81
+ break;
82
+ case 'stream':
83
+ return opts.responseStyle === 'data'
84
+ ? response.body
85
+ : {
86
+ data: response.body,
87
+ ...result,
88
+ };
89
+ }
90
+ if (parseAs === 'json') {
91
+ if (opts.responseValidator) {
92
+ await opts.responseValidator(data);
93
+ }
94
+ if (opts.responseTransformer) {
95
+ data = await opts.responseTransformer(data);
96
+ }
97
+ }
98
+ return opts.responseStyle === 'data'
99
+ ? data
100
+ : {
101
+ data,
102
+ ...result,
103
+ };
104
+ }
105
+ let error = await response.text();
106
+ try {
107
+ error = JSON.parse(error);
108
+ }
109
+ catch {
110
+ // noop
111
+ }
112
+ let finalError = error;
113
+ for (const fn of interceptors.error._fns) {
114
+ if (fn) {
115
+ finalError = (await fn(error, response, request, opts));
116
+ }
117
+ }
118
+ finalError = finalError || {};
119
+ if (opts.throwOnError) {
120
+ throw finalError;
121
+ }
122
+ // TODO: we probably want to return error and improve types
123
+ return opts.responseStyle === 'data'
124
+ ? undefined
125
+ : {
126
+ error: finalError,
127
+ ...result,
128
+ };
129
+ };
130
+ return {
131
+ buildUrl: utils_1.buildUrl,
132
+ connect: (options) => request({ ...options, method: 'CONNECT' }),
133
+ delete: (options) => request({ ...options, method: 'DELETE' }),
134
+ get: (options) => request({ ...options, method: 'GET' }),
135
+ getConfig,
136
+ head: (options) => request({ ...options, method: 'HEAD' }),
137
+ interceptors,
138
+ options: (options) => request({ ...options, method: 'OPTIONS' }),
139
+ patch: (options) => request({ ...options, method: 'PATCH' }),
140
+ post: (options) => request({ ...options, method: 'POST' }),
141
+ put: (options) => request({ ...options, method: 'PUT' }),
142
+ request,
143
+ setConfig,
144
+ trace: (options) => request({ ...options, method: 'TRACE' }),
145
+ };
146
+ };
147
+ exports.createClient = createClient;
@@ -0,0 +1,7 @@
1
+ export type { Auth } from '../core/auth';
2
+ export type { QuerySerializerOptions } from '../core/bodySerializer';
3
+ export { formDataBodySerializer, jsonBodySerializer, urlSearchParamsBodySerializer, } from '../core/bodySerializer';
4
+ export { buildClientParams } from '../core/params';
5
+ export { createClient } from './client';
6
+ export type { Client, ClientOptions, Config, CreateClientConfig, Options, OptionsLegacyParser, RequestOptions, RequestResult, ResponseStyle, TDataShape, } from './types';
7
+ export { createConfig, mergeHeaders } from './utils';
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mergeHeaders = exports.createConfig = exports.createClient = exports.buildClientParams = exports.urlSearchParamsBodySerializer = exports.jsonBodySerializer = exports.formDataBodySerializer = void 0;
4
+ var bodySerializer_1 = require("../core/bodySerializer");
5
+ Object.defineProperty(exports, "formDataBodySerializer", { enumerable: true, get: function () { return bodySerializer_1.formDataBodySerializer; } });
6
+ Object.defineProperty(exports, "jsonBodySerializer", { enumerable: true, get: function () { return bodySerializer_1.jsonBodySerializer; } });
7
+ Object.defineProperty(exports, "urlSearchParamsBodySerializer", { enumerable: true, get: function () { return bodySerializer_1.urlSearchParamsBodySerializer; } });
8
+ var params_1 = require("../core/params");
9
+ Object.defineProperty(exports, "buildClientParams", { enumerable: true, get: function () { return params_1.buildClientParams; } });
10
+ var client_1 = require("./client");
11
+ Object.defineProperty(exports, "createClient", { enumerable: true, get: function () { return client_1.createClient; } });
12
+ var utils_1 = require("./utils");
13
+ Object.defineProperty(exports, "createConfig", { enumerable: true, get: function () { return utils_1.createConfig; } });
14
+ Object.defineProperty(exports, "mergeHeaders", { enumerable: true, get: function () { return utils_1.mergeHeaders; } });
@@ -0,0 +1,119 @@
1
+ import type { Auth } from '../core/auth';
2
+ import type { Client as CoreClient, Config as CoreConfig } from '../core/types';
3
+ import type { Middleware } from './utils';
4
+ export type ResponseStyle = 'data' | 'fields';
5
+ export interface Config<T extends ClientOptions = ClientOptions> extends Omit<RequestInit, 'body' | 'headers' | 'method'>, CoreConfig {
6
+ /**
7
+ * Base URL for all requests made by this client.
8
+ */
9
+ baseUrl?: T['baseUrl'];
10
+ /**
11
+ * Fetch API implementation. You can use this option to provide a custom
12
+ * fetch instance.
13
+ *
14
+ * @default globalThis.fetch
15
+ */
16
+ fetch?: (request: Request) => ReturnType<typeof fetch>;
17
+ /**
18
+ * Please don't use the Fetch client for Next.js applications. The `next`
19
+ * options won't have any effect.
20
+ *
21
+ * Install {@link https://www.npmjs.com/package/@hey-api/client-next `@hey-api/client-next`} instead.
22
+ */
23
+ next?: never;
24
+ /**
25
+ * Return the response data parsed in a specified format. By default, `auto`
26
+ * will infer the appropriate method from the `Content-Type` response header.
27
+ * You can override this behavior with any of the {@link Body} methods.
28
+ * Select `stream` if you don't want to parse response data at all.
29
+ *
30
+ * @default 'auto'
31
+ */
32
+ parseAs?: 'arrayBuffer' | 'auto' | 'blob' | 'formData' | 'json' | 'stream' | 'text';
33
+ /**
34
+ * Should we return only data or multiple fields (data, error, response, etc.)?
35
+ *
36
+ * @default 'fields'
37
+ */
38
+ responseStyle?: ResponseStyle;
39
+ /**
40
+ * Throw an error instead of returning it in the response?
41
+ *
42
+ * @default false
43
+ */
44
+ throwOnError?: T['throwOnError'];
45
+ }
46
+ export interface RequestOptions<TResponseStyle extends ResponseStyle = 'fields', ThrowOnError extends boolean = boolean, Url extends string = string> extends Config<{
47
+ responseStyle: TResponseStyle;
48
+ throwOnError: ThrowOnError;
49
+ }> {
50
+ /**
51
+ * Any body that you want to add to your request.
52
+ *
53
+ * {@link https://developer.mozilla.org/docs/Web/API/fetch#body}
54
+ */
55
+ body?: unknown;
56
+ path?: Record<string, unknown>;
57
+ query?: Record<string, unknown>;
58
+ /**
59
+ * Security mechanism(s) to use for the request.
60
+ */
61
+ security?: ReadonlyArray<Auth>;
62
+ url: Url;
63
+ }
64
+ export type RequestResult<TData = unknown, TError = unknown, ThrowOnError extends boolean = boolean, TResponseStyle extends ResponseStyle = 'fields'> = ThrowOnError extends true ? Promise<TResponseStyle extends 'data' ? TData extends Record<string, unknown> ? TData[keyof TData] : TData : {
65
+ data: TData extends Record<string, unknown> ? TData[keyof TData] : TData;
66
+ request: Request;
67
+ response: Response;
68
+ }> : Promise<TResponseStyle extends 'data' ? (TData extends Record<string, unknown> ? TData[keyof TData] : TData) | undefined : ({
69
+ data: TData extends Record<string, unknown> ? TData[keyof TData] : TData;
70
+ error: undefined;
71
+ } | {
72
+ data: undefined;
73
+ error: TError extends Record<string, unknown> ? TError[keyof TError] : TError;
74
+ }) & {
75
+ request: Request;
76
+ response: Response;
77
+ }>;
78
+ export interface ClientOptions {
79
+ baseUrl?: string;
80
+ responseStyle?: ResponseStyle;
81
+ throwOnError?: boolean;
82
+ }
83
+ type MethodFn = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false, TResponseStyle extends ResponseStyle = 'fields'>(options: Omit<RequestOptions<TResponseStyle, ThrowOnError>, 'method'>) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
84
+ type RequestFn = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false, TResponseStyle extends ResponseStyle = 'fields'>(options: Omit<RequestOptions<TResponseStyle, ThrowOnError>, 'method'> & Pick<Required<RequestOptions<TResponseStyle, ThrowOnError>>, 'method'>) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
85
+ type BuildUrlFn = <TData extends {
86
+ body?: unknown;
87
+ path?: Record<string, unknown>;
88
+ query?: Record<string, unknown>;
89
+ url: string;
90
+ }>(options: Pick<TData, 'url'> & Options<TData>) => string;
91
+ export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn> & {
92
+ interceptors: Middleware<Request, Response, unknown, RequestOptions>;
93
+ };
94
+ /**
95
+ * The `createClientConfig()` function will be called on client initialization
96
+ * and the returned object will become the client's initial configuration.
97
+ *
98
+ * You may want to initialize your client this way instead of calling
99
+ * `setConfig()`. This is useful for example if you're using Next.js
100
+ * to ensure your client always has the correct values.
101
+ */
102
+ export type CreateClientConfig<T extends ClientOptions = ClientOptions> = (override?: Config<ClientOptions & T>) => Config<Required<ClientOptions> & T>;
103
+ export interface TDataShape {
104
+ body?: unknown;
105
+ headers?: unknown;
106
+ path?: unknown;
107
+ query?: unknown;
108
+ url: string;
109
+ }
110
+ type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>;
111
+ export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean, TResponseStyle extends ResponseStyle = 'fields'> = OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, 'body' | 'path' | 'query' | 'url'> & Omit<TData, 'url'>;
112
+ export type OptionsLegacyParser<TData = unknown, ThrowOnError extends boolean = boolean, TResponseStyle extends ResponseStyle = 'fields'> = TData extends {
113
+ body?: any;
114
+ } ? TData extends {
115
+ headers?: any;
116
+ } ? OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, 'body' | 'headers' | 'url'> & TData : OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, 'body' | 'url'> & TData & Pick<RequestOptions<TResponseStyle, ThrowOnError>, 'headers'> : TData extends {
117
+ headers?: any;
118
+ } ? OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, 'headers' | 'url'> & TData & Pick<RequestOptions<TResponseStyle, ThrowOnError>, 'body'> : OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, 'url'> & TData;
119
+ export {};
@@ -0,0 +1,45 @@
1
+ import type { QuerySerializer, QuerySerializerOptions } from '../core/bodySerializer';
2
+ import type { Client, ClientOptions, Config, RequestOptions } from './types';
3
+ export declare const createQuerySerializer: <T = unknown>({ allowReserved, array, object, }?: QuerySerializerOptions) => (queryParams: T) => string;
4
+ /**
5
+ * Infers parseAs value from provided Content-Type header.
6
+ */
7
+ export declare const getParseAs: (contentType: string | null) => Exclude<Config["parseAs"], "auto">;
8
+ export declare const setAuthParams: ({ security, ...options }: Pick<Required<RequestOptions>, "security"> & Pick<RequestOptions, "auth" | "query"> & {
9
+ headers: Headers;
10
+ }) => Promise<void>;
11
+ export declare const buildUrl: Client['buildUrl'];
12
+ export declare const getUrl: ({ baseUrl, path, query, querySerializer, url: _url, }: {
13
+ baseUrl?: string;
14
+ path?: Record<string, unknown>;
15
+ query?: Record<string, unknown>;
16
+ querySerializer: QuerySerializer;
17
+ url: string;
18
+ }) => string;
19
+ export declare const mergeConfigs: (a: Config, b: Config) => Config;
20
+ export declare const mergeHeaders: (...headers: Array<Required<Config>["headers"] | undefined>) => Headers;
21
+ type ErrInterceptor<Err, Res, Req, Options> = (error: Err, response: Res, request: Req, options: Options) => Err | Promise<Err>;
22
+ type ReqInterceptor<Req, Options> = (request: Req, options: Options) => Req | Promise<Req>;
23
+ type ResInterceptor<Res, Req, Options> = (response: Res, request: Req, options: Options) => Res | Promise<Res>;
24
+ declare class Interceptors<Interceptor> {
25
+ _fns: (Interceptor | null)[];
26
+ constructor();
27
+ clear(): void;
28
+ getInterceptorIndex(id: number | Interceptor): number;
29
+ exists(id: number | Interceptor): boolean;
30
+ eject(id: number | Interceptor): void;
31
+ update(id: number | Interceptor, fn: Interceptor): number | false | Interceptor;
32
+ use(fn: Interceptor): number;
33
+ }
34
+ export interface Middleware<Req, Res, Err, Options> {
35
+ error: Pick<Interceptors<ErrInterceptor<Err, Res, Req, Options>>, 'eject' | 'use'>;
36
+ request: Pick<Interceptors<ReqInterceptor<Req, Options>>, 'eject' | 'use'>;
37
+ response: Pick<Interceptors<ResInterceptor<Res, Req, Options>>, 'eject' | 'use'>;
38
+ }
39
+ export declare const createInterceptors: <Req, Res, Err, Options>() => {
40
+ error: Interceptors<ErrInterceptor<Err, Res, Req, Options>>;
41
+ request: Interceptors<ReqInterceptor<Req, Options>>;
42
+ response: Interceptors<ResInterceptor<Res, Req, Options>>;
43
+ };
44
+ export declare const createConfig: <T extends ClientOptions = ClientOptions>(override?: Config<Omit<ClientOptions, keyof T> & T>) => Config<Omit<ClientOptions, keyof T> & T>;
45
+ export {};