@prismicio/e2e-tests-utils 1.18.0 → 1.19.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.
Files changed (159) hide show
  1. package/README.md +24 -27
  2. package/dist/_virtual/_rolldown/runtime.cjs +23 -0
  3. package/dist/clients/apiClient.cjs +38 -38
  4. package/dist/clients/apiClient.cjs.map +1 -1
  5. package/dist/clients/apiClient.d.cts +30 -0
  6. package/dist/clients/apiClient.d.cts.map +1 -0
  7. package/dist/clients/apiClient.d.ts +23 -23
  8. package/dist/clients/apiClient.d.ts.map +1 -0
  9. package/dist/clients/apiClient.js +38 -38
  10. package/dist/clients/apiClient.js.map +1 -1
  11. package/dist/clients/assetApi.cjs +81 -93
  12. package/dist/clients/assetApi.cjs.map +1 -1
  13. package/dist/clients/assetApi.d.cts +105 -0
  14. package/dist/clients/assetApi.d.cts.map +1 -0
  15. package/dist/clients/assetApi.d.ts +96 -97
  16. package/dist/clients/assetApi.d.ts.map +1 -0
  17. package/dist/clients/assetApi.js +79 -91
  18. package/dist/clients/assetApi.js.map +1 -1
  19. package/dist/clients/authenticationApi.cjs +66 -73
  20. package/dist/clients/authenticationApi.cjs.map +1 -1
  21. package/dist/clients/authenticationApi.d.cts +20 -0
  22. package/dist/clients/authenticationApi.d.cts.map +1 -0
  23. package/dist/clients/authenticationApi.d.ts +18 -13
  24. package/dist/clients/authenticationApi.d.ts.map +1 -0
  25. package/dist/clients/authenticationApi.js +65 -72
  26. package/dist/clients/authenticationApi.js.map +1 -1
  27. package/dist/clients/contentApi.cjs +94 -103
  28. package/dist/clients/contentApi.cjs.map +1 -1
  29. package/dist/clients/contentApi.d.cts +116 -0
  30. package/dist/clients/contentApi.d.cts.map +1 -0
  31. package/dist/clients/contentApi.d.ts +102 -98
  32. package/dist/clients/contentApi.d.ts.map +1 -0
  33. package/dist/clients/contentApi.js +94 -103
  34. package/dist/clients/contentApi.js.map +1 -1
  35. package/dist/clients/coreApi.cjs +123 -145
  36. package/dist/clients/coreApi.cjs.map +1 -1
  37. package/dist/clients/coreApi.d.cts +122 -0
  38. package/dist/clients/coreApi.d.cts.map +1 -0
  39. package/dist/clients/coreApi.d.ts +111 -109
  40. package/dist/clients/coreApi.d.ts.map +1 -0
  41. package/dist/clients/coreApi.js +122 -144
  42. package/dist/clients/coreApi.js.map +1 -1
  43. package/dist/clients/customTypesApi.cjs +90 -99
  44. package/dist/clients/customTypesApi.cjs.map +1 -1
  45. package/dist/clients/customTypesApi.d.cts +46 -0
  46. package/dist/clients/customTypesApi.d.cts.map +1 -0
  47. package/dist/clients/customTypesApi.d.ts +45 -45
  48. package/dist/clients/customTypesApi.d.ts.map +1 -0
  49. package/dist/clients/customTypesApi.js +87 -98
  50. package/dist/clients/customTypesApi.js.map +1 -1
  51. package/dist/clients/integrationFields.cjs +96 -87
  52. package/dist/clients/integrationFields.cjs.map +1 -1
  53. package/dist/clients/integrationFields.d.cts +30 -0
  54. package/dist/clients/integrationFields.d.cts.map +1 -0
  55. package/dist/clients/integrationFields.d.ts +27 -22
  56. package/dist/clients/integrationFields.d.ts.map +1 -0
  57. package/dist/clients/integrationFields.js +93 -86
  58. package/dist/clients/integrationFields.js.map +1 -1
  59. package/dist/clients/manageV2.cjs +154 -171
  60. package/dist/clients/manageV2.cjs.map +1 -1
  61. package/dist/clients/manageV2.d.cts +105 -0
  62. package/dist/clients/manageV2.d.cts.map +1 -0
  63. package/dist/clients/manageV2.d.ts +103 -111
  64. package/dist/clients/manageV2.d.ts.map +1 -0
  65. package/dist/clients/manageV2.js +149 -169
  66. package/dist/clients/manageV2.js.map +1 -1
  67. package/dist/clients/migrationApi.cjs +37 -36
  68. package/dist/clients/migrationApi.cjs.map +1 -1
  69. package/dist/clients/migrationApi.d.cts +48 -0
  70. package/dist/clients/migrationApi.d.cts.map +1 -0
  71. package/dist/clients/migrationApi.d.ts +45 -39
  72. package/dist/clients/migrationApi.d.ts.map +1 -0
  73. package/dist/clients/migrationApi.js +35 -34
  74. package/dist/clients/migrationApi.js.map +1 -1
  75. package/dist/clients/wroom.cjs +101 -122
  76. package/dist/clients/wroom.cjs.map +1 -1
  77. package/dist/clients/wroom.d.cts +48 -0
  78. package/dist/clients/wroom.d.cts.map +1 -0
  79. package/dist/clients/wroom.d.ts +46 -47
  80. package/dist/clients/wroom.d.ts.map +1 -0
  81. package/dist/clients/wroom.js +99 -122
  82. package/dist/clients/wroom.js.map +1 -1
  83. package/dist/index.cjs +13 -15
  84. package/dist/index.d.cts +8 -0
  85. package/dist/index.d.ts +8 -7
  86. package/dist/index.js +5 -14
  87. package/dist/managers/repositories.cjs +159 -169
  88. package/dist/managers/repositories.cjs.map +1 -1
  89. package/dist/managers/repositories.d.cts +67 -0
  90. package/dist/managers/repositories.d.cts.map +1 -0
  91. package/dist/managers/repositories.d.ts +56 -63
  92. package/dist/managers/repositories.d.ts.map +1 -0
  93. package/dist/managers/repositories.js +148 -159
  94. package/dist/managers/repositories.js.map +1 -1
  95. package/dist/managers/repository.cjs +529 -616
  96. package/dist/managers/repository.cjs.map +1 -1
  97. package/dist/managers/repository.d.cts +308 -0
  98. package/dist/managers/repository.d.cts.map +1 -0
  99. package/dist/managers/repository.d.ts +303 -307
  100. package/dist/managers/repository.d.ts.map +1 -0
  101. package/dist/managers/repository.js +529 -616
  102. package/dist/managers/repository.js.map +1 -1
  103. package/dist/types.d.cts +41 -0
  104. package/dist/types.d.cts.map +1 -0
  105. package/dist/types.d.ts +35 -37
  106. package/dist/types.d.ts.map +1 -0
  107. package/dist/utils/cookies.cjs +19 -17
  108. package/dist/utils/cookies.cjs.map +1 -1
  109. package/dist/utils/cookies.js +20 -18
  110. package/dist/utils/cookies.js.map +1 -1
  111. package/dist/utils/envVariableManager.cjs +33 -34
  112. package/dist/utils/envVariableManager.cjs.map +1 -1
  113. package/dist/utils/envVariableManager.js +34 -35
  114. package/dist/utils/envVariableManager.js.map +1 -1
  115. package/dist/utils/log.cjs +18 -19
  116. package/dist/utils/log.cjs.map +1 -1
  117. package/dist/utils/log.js +18 -21
  118. package/dist/utils/log.js.map +1 -1
  119. package/dist/utils/random.cjs +13 -6
  120. package/dist/utils/random.cjs.map +1 -1
  121. package/dist/utils/random.js +13 -6
  122. package/dist/utils/random.js.map +1 -1
  123. package/dist/utils/strings.cjs +13 -7
  124. package/dist/utils/strings.cjs.map +1 -1
  125. package/dist/utils/strings.js +14 -8
  126. package/dist/utils/strings.js.map +1 -1
  127. package/dist/utils/urls.cjs +21 -10
  128. package/dist/utils/urls.cjs.map +1 -1
  129. package/dist/utils/urls.js +22 -12
  130. package/dist/utils/urls.js.map +1 -1
  131. package/package.json +42 -67
  132. package/src/clients/apiClient.ts +16 -21
  133. package/src/clients/assetApi.ts +75 -82
  134. package/src/clients/authenticationApi.ts +54 -27
  135. package/src/clients/contentApi.ts +109 -129
  136. package/src/clients/coreApi.ts +113 -117
  137. package/src/clients/customTypesApi.ts +54 -76
  138. package/src/clients/integrationFields.ts +59 -74
  139. package/src/clients/manageV2.ts +72 -96
  140. package/src/clients/migrationApi.ts +35 -39
  141. package/src/clients/wroom.ts +42 -56
  142. package/src/index.ts +7 -7
  143. package/src/managers/repositories.ts +85 -108
  144. package/src/managers/repository.ts +303 -368
  145. package/src/types.ts +31 -31
  146. package/src/utils/cookies.ts +8 -8
  147. package/src/utils/envVariableManager.ts +15 -15
  148. package/src/utils/log.ts +17 -21
  149. package/src/utils/random.ts +5 -6
  150. package/src/utils/strings.ts +7 -9
  151. package/src/utils/urls.ts +10 -12
  152. package/dist/index.cjs.map +0 -1
  153. package/dist/index.js.map +0 -1
  154. package/dist/utils/cookies.d.ts +0 -8
  155. package/dist/utils/envVariableManager.d.ts +0 -16
  156. package/dist/utils/log.d.ts +0 -5
  157. package/dist/utils/random.d.ts +0 -8
  158. package/dist/utils/strings.d.ts +0 -11
  159. package/dist/utils/urls.d.ts +0 -16
@@ -1,618 +1,531 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
- var __publicField = (obj, key, value) => {
5
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
6
- return value;
1
+ const require_log = require("../utils/log.cjs");
2
+ const require_urls = require("../utils/urls.cjs");
3
+ const require_contentApi = require("../clients/contentApi.cjs");
4
+ let _playwright_test = require("@playwright/test");
5
+ //#region src/managers/repository.ts
6
+ /** Utility to manage test data for a Prismic repository */
7
+ var RepositoryManager = class {
8
+ name;
9
+ coreApiClient;
10
+ authApiClient;
11
+ wroomClient;
12
+ customTypesApiClient;
13
+ migrationApiClient;
14
+ assetApiClient;
15
+ manageV2Client;
16
+ integrationFieldsClient;
17
+ constructor(name, coreApiClient, authApiClient, wroomClient, customTypesApiClient, migrationApiClient, assetApiClient, manageV2Client, integrationFieldsClient) {
18
+ this.name = name;
19
+ this.coreApiClient = coreApiClient;
20
+ this.authApiClient = authApiClient;
21
+ this.wroomClient = wroomClient;
22
+ this.customTypesApiClient = customTypesApiClient;
23
+ this.migrationApiClient = migrationApiClient;
24
+ this.assetApiClient = assetApiClient;
25
+ this.manageV2Client = manageV2Client;
26
+ this.integrationFieldsClient = integrationFieldsClient;
27
+ }
28
+ async configure(config) {
29
+ if (config.defaultLocale || config.locales || config.customLocale) {
30
+ const languages = await this.coreApiClient.getLanguages();
31
+ let updateDefaultLocaleAtTheEnd = false;
32
+ if (config.defaultLocale) {
33
+ const master = languages.find(({ is_master }) => is_master === true);
34
+ if (!master) await this.setDefaultLocale(config.defaultLocale, "create");
35
+ else if (master.id !== config.defaultLocale) updateDefaultLocaleAtTheEnd = true;
36
+ }
37
+ if (config.locales) {
38
+ const localesToAdd = config.locales.filter((id) => !languages.some((language) => language.id === id));
39
+ if (localesToAdd.length > 0) await this.createLocales(localesToAdd);
40
+ }
41
+ if (config.customLocale) {
42
+ if (!languages.find(({ id }) => id === config.customLocale?.lang.id)) await this.createCustomLocale(config.customLocale);
43
+ }
44
+ if (config.defaultLocale && updateDefaultLocaleAtTheEnd) await this.setDefaultLocale(config.defaultLocale, "update");
45
+ }
46
+ if (config.slices) await this.createSlices(config.slices);
47
+ if (config.customTypes) await this.createCustomTypes(config.customTypes);
48
+ if (config.preview) await this.createPreview(config.preview);
49
+ if (config.simulatorURL) await this.setSimulatorURL(config.simulatorURL);
50
+ if (config.rolePerLocal) await this.toggleRolePerLocal(true);
51
+ if (config.customRoles) await this.toggleCustomRoles(true);
52
+ if (config.integrationFields) {
53
+ await this.toggleIntegrationFields(true);
54
+ await this.setupIntegrationFields(config.integrationFields);
55
+ }
56
+ }
57
+ /** @returns The repository base url, like https://my-repo.prismic.io */
58
+ getBaseURL() {
59
+ return require_urls.getRepositoryUrl(this.wroomClient.getBaseURL(), this.name);
60
+ }
61
+ /** @returns The repository base cdn url, like https://my-repo.cdn.prismic.io */
62
+ getBaseCdnURL() {
63
+ return require_urls.getRepositoryUrl(this.wroomClient.getBaseURL(), `${this.name}.cdn`);
64
+ }
65
+ /**
66
+ * @returns A Content Type api client configured to target the repository CDN url. Uses Playwright
67
+ * for requests so if your project uses Playwright too, any network request will be present in
68
+ * Playwright test reports.
69
+ */
70
+ getContentApiClient(config) {
71
+ return new require_contentApi.ContentApiClient(this.getBaseCdnURL(), config);
72
+ }
73
+ /**
74
+ * @returns An instance of the Migration api client, see
75
+ * https://prismic.io/docs/migration-api-technical-reference
76
+ */
77
+ getMigrationApiClient() {
78
+ if (!this.migrationApiClient) throw "No client found for the migration api. Did you provide a migration api url in the configuration?";
79
+ return this.migrationApiClient;
80
+ }
81
+ /**
82
+ * @returns An instance of the Asset api client, see
83
+ * https://prismic.io/docs/asset-api-technical-reference
84
+ */
85
+ getAssetApiClient() {
86
+ return this.assetApiClient;
87
+ }
88
+ /** @returns The Wroom commit hash deployed on the current repository */
89
+ async getDeployedVersion() {
90
+ return (await this.wroomClient.get(this.name, "underground/834b34f16f451e00f268dd5c8c81d16e3c020275")).data.version;
91
+ }
92
+ /**
93
+ * Set additional standard locales for a repository in Wroom, does not fail if the locales already
94
+ * exist.
95
+ *
96
+ * @param locales - An array of all locales to be added to the repository.
97
+ * @throws Error if setting the locales fails.
98
+ */
99
+ async createLocales(locales) {
100
+ const profiler = require_log.logger.startTimer();
101
+ const response = await this.wroomClient.post(this.name, "app/settings/multilanguages", { languages: locales });
102
+ const { data, status } = response;
103
+ if (!(status === 200 || status === 400 && data.includes("already existing languages"))) {
104
+ require_log.logHttpResponse(response);
105
+ throw new Error(`Could not add locales ${locales.join(", ")}`);
106
+ }
107
+ profiler.done({ message: `locales '${locales}' created` });
108
+ }
109
+ /**
110
+ * Set additional custom locale for a repository in Wroom, does not fail if the locales already
111
+ * exist.
112
+ *
113
+ * @param locale - The locale to be added to the repository.
114
+ * @throws Error if setting the locale fails.
115
+ */
116
+ async createCustomLocale(locale) {
117
+ const profiler = require_log.logger.startTimer();
118
+ const response = await this.wroomClient.post(this.name, "app/settings/multilanguages/custom", locale);
119
+ const { data, status } = response;
120
+ if (!(status === 200 || status === 400 && JSON.stringify(data).includes("code is already used"))) {
121
+ require_log.logHttpResponse(response);
122
+ throw new Error(`Could not create custom locale ${locale}`);
123
+ }
124
+ profiler.done({ message: `custom locale ${JSON.stringify(locale)} created` });
125
+ }
126
+ failIfNot200(response, msg) {
127
+ if (response.status !== 200) {
128
+ require_log.logHttpResponse(response);
129
+ throw new Error(msg);
130
+ }
131
+ }
132
+ /**
133
+ * Delete a locale from a repository.
134
+ *
135
+ * @param locale - Locale to remove from the repository configuration.
136
+ * @throws Error if deleting the locale fails.
137
+ */
138
+ async deleteLocale(locale) {
139
+ const profiler = require_log.logger.startTimer();
140
+ const response = await this.wroomClient.post(this.name, `app/settings/multilanguages/${locale}/delete`);
141
+ this.failIfNot200(response, `Could not delete locale ${locale}`);
142
+ profiler.done({ message: `locale '${locale}' deleted` });
143
+ }
144
+ /**
145
+ * Create or update the default (master) locale for a repository in Wroom, the locale needs to
146
+ * exist in the repository.
147
+ *
148
+ * @param locale - The locale to be set as the default.
149
+ * @param endpoint - "createMasterLang" to create it, "defineAsMaster" to update it.
150
+ * @throws Error if setting the default locale fails.
151
+ */
152
+ async setDefaultLocale(locale, action) {
153
+ const profiler = require_log.logger.startTimer();
154
+ const response = await this.wroomClient.post(this.name, `/app/settings/multilanguages/${locale}/${{
155
+ create: "createMasterLang",
156
+ update: "defineAsMaster"
157
+ }[action]}`);
158
+ const { data, status } = response;
159
+ if (!(status === 200 || status === 400 && JSON.stringify(data).includes("Unable to set as master language"))) {
160
+ require_log.logHttpResponse(response);
161
+ throw new Error(`Could not set default locale to ${locale}`);
162
+ }
163
+ profiler.done({ message: `default locale set to '${locale}'` });
164
+ }
165
+ /**
166
+ * Retrieves documents.
167
+ *
168
+ * @param args - Configure which documents are retrieved.
169
+ * @returns A paginated set of documents.
170
+ */
171
+ async getDocuments(args) {
172
+ return this.coreApiClient.getDocuments(args);
173
+ }
174
+ /**
175
+ * Creates a release.
176
+ *
177
+ * @param label - The label for the release.
178
+ * @returns A Promise that resolves when the release is created.
179
+ */
180
+ async createRelease(label) {
181
+ const profiler = require_log.logger.startTimer();
182
+ const response = await this.wroomClient.post(this.name, "app/releases", { label });
183
+ this.failIfNot200(response, `Could not create release ${label}`);
184
+ profiler.done({ message: `release '${label}' created` });
185
+ return response.data;
186
+ }
187
+ /**
188
+ * Retrieves the migration release.
189
+ *
190
+ * @returns The migration relese metadata.
191
+ */
192
+ async getMigrationRelease() {
193
+ const migrationRelease = (await this.coreApiClient.getReleases()).find((release) => release.migration);
194
+ if (!migrationRelease) throw new Error(`The repository does not have a migration release`);
195
+ return migrationRelease;
196
+ }
197
+ /**
198
+ * Publishes a release.
199
+ *
200
+ * @param id - The ID of the release.
201
+ * @returns A Promise that resolves when the release is published and a new master ref is
202
+ * available.
203
+ */
204
+ async publishRelease(id) {
205
+ const profiler = require_log.logger.startTimer();
206
+ const contentApi = this.getContentApiClient();
207
+ const masterRef = await contentApi.getMasterRef();
208
+ await this.coreApiClient.publishRelease(id);
209
+ await _playwright_test.expect.poll(async () => contentApi.getMasterRef(), {
210
+ timeout: 1e4,
211
+ message: "Waiting for new master ref to be available"
212
+ }).not.toBe(masterRef);
213
+ profiler.done({ message: `release '${id}' published and new master ref available` });
214
+ }
215
+ /**
216
+ * Deletes a repository release.
217
+ *
218
+ * @param id - The ID of the release to be deleted.
219
+ */
220
+ async deleteRelease(id) {
221
+ const profiler = require_log.logger.startTimer();
222
+ const response = await this.wroomClient.post(this.name, `app/releases/${id}/delete`, {});
223
+ this.failIfNot200(response, `Could not delete release with id ${id}`);
224
+ profiler.done({ message: `release '${id}' deleted` });
225
+ }
226
+ /**
227
+ * Creates a preview for a repository.
228
+ *
229
+ * @param data - The data for creating the preview.
230
+ */
231
+ async createPreview(data) {
232
+ const profiler = require_log.logger.startTimer();
233
+ const response = await this.wroomClient.post(this.name, `previews/new`, data);
234
+ this.failIfNot200(response, `Could not create preview ${data.name}`);
235
+ profiler.done({ message: `preview '${data.name}' created` });
236
+ return response.data;
237
+ }
238
+ /**
239
+ * Set the simulator URL (table of slices) for a repository.
240
+ *
241
+ * @param data - The simulator URL to set.
242
+ */
243
+ async setSimulatorURL(simulatorURL) {
244
+ const profiler = require_log.logger.startTimer();
245
+ const repository = await this.coreApiClient.getRepository();
246
+ await this.coreApiClient.updateRepository({
247
+ ...repository,
248
+ simulator_url: simulatorURL
249
+ });
250
+ profiler.done({ message: `simulator preview '${simulatorURL}' created` });
251
+ }
252
+ /**
253
+ * Deletes a preview from a repository.
254
+ *
255
+ * @param id - The ID of the preview to be deleted.
256
+ */
257
+ async deletePreview(id) {
258
+ const profiler = require_log.logger.startTimer();
259
+ const response = await this.wroomClient.post(this.name, `previews/delete/${id}`, {});
260
+ this.failIfNot200(response, `Could not delete preview with id ${id}`);
261
+ profiler.done({ message: `preview '${id}' deleted` });
262
+ }
263
+ /**
264
+ * Create a webhook.
265
+ *
266
+ * @returns A Promise that resolves with the webhook id.
267
+ */
268
+ async createWebhook(name, url, secret, triggers, active) {
269
+ const profiler = require_log.logger.startTimer();
270
+ const response = await this.wroomClient.post(this.name, "app/settings/webhooks/create", {
271
+ name,
272
+ url,
273
+ secret,
274
+ active: active ? "on" : "off",
275
+ ...triggers
276
+ });
277
+ this.failIfNot200(response, `Could not create webhook ${name}`);
278
+ profiler.done({ message: `webhook '${name}' created` });
279
+ return response.data.created;
280
+ }
281
+ /**
282
+ * Deletes a webhook.
283
+ *
284
+ * @param id - The webhook ID.
285
+ */
286
+ async deleteWebhook(id) {
287
+ const profiler = require_log.logger.startTimer();
288
+ const response = await this.wroomClient.post(this.name, `app/settings/webhooks/${id}/delete`, {});
289
+ this.failIfNot200(response, `Could not delete webhook with id ${id}`);
290
+ profiler.done({ message: `webhook '${id}' deleted` });
291
+ }
292
+ /**
293
+ * Create Custom Types using the Custom types api.
294
+ *
295
+ * @param customTypes
296
+ */
297
+ async createCustomTypes(customTypes) {
298
+ await this.customTypesApiClient.createCustomTypes(customTypes);
299
+ }
300
+ async deleteCustomType(customTypeId) {
301
+ await this.customTypesApiClient.deleteCustomType(customTypeId);
302
+ }
303
+ /**
304
+ * Create slices using the Custom types api.
305
+ *
306
+ * @param slices
307
+ */
308
+ async createSlices(slices) {
309
+ await this.customTypesApiClient.createSlices(slices);
310
+ }
311
+ /**
312
+ * Create an access token to query private items of the Content api.
313
+ *
314
+ * @param appName - Mandatory authorized application name
315
+ * @param tokenScope - Access token scope. Use "master" to when your content api is fully private
316
+ * or "master+releases" to access private releases
317
+ */
318
+ async createContentAPIToken(appName, tokenScope = "master") {
319
+ const profiler = require_log.logger.startTimer();
320
+ const existingApps = await this.wroomClient.get(this.name, "settings/security/contentapi");
321
+ this.failIfNot200(existingApps, `Could not get status of existing applications to generate a content api token`);
322
+ let app = existingApps.data.find(({ name, wroom_auths }) => {
323
+ return name === appName && wroom_auths.length > 0;
324
+ });
325
+ if (!app) {
326
+ const response = await this.wroomClient.post(this.name, "settings/security/oauthapp", { app_name: appName });
327
+ this.failIfNot200(response, `Could not create content api app ${appName}`);
328
+ app = response.data;
329
+ }
330
+ let tokenInfo = app.wroom_auths.find(({ scope }) => scope === tokenScope);
331
+ if (!tokenInfo) {
332
+ const response = await this.wroomClient.post(this.name, "settings/security/authorizations", {
333
+ app: app.id,
334
+ scope: tokenScope
335
+ });
336
+ this.failIfNot200(response, `Could not create content api access token for app ${appName} with scope ${tokenScope}`);
337
+ tokenInfo = response.data;
338
+ }
339
+ profiler.done({ message: `content api access token for app '${appName}' created` });
340
+ if (!tokenInfo?.token) throw `Could not create content api access token for app ${appName} with scope ${tokenScope}`;
341
+ return tokenInfo.token;
342
+ }
343
+ /** Create a permanent access token to authenticate to Prismic public apis. */
344
+ async createPermanentAccessToken() {
345
+ const profiler = require_log.logger.startTimer();
346
+ const token = await this.authApiClient.getMachine2MachineToken(this.name);
347
+ profiler.done({ message: `machine2machine token created` });
348
+ return token;
349
+ }
350
+ /**
351
+ * Toggle the Role per Locale workflow
352
+ *
353
+ * @param enabled - The feature new status
354
+ */
355
+ async toggleRolePerLocal(enabled) {
356
+ const profiler = require_log.logger.startTimer();
357
+ const response = await this.manageV2Client.toggleRolePerLocal({
358
+ repository: this.name,
359
+ enabled
360
+ });
361
+ this.failIfNot200(response, `Could not ${enabled ? "enable" : "disable"} Role per local for ${this.name}`);
362
+ profiler.done({ message: `Role per local ${enabled ? "enabled" : "disabled"}` });
363
+ }
364
+ /**
365
+ * Toggle the Custom Roles workflow
366
+ *
367
+ * @param enabled - The feature new status
368
+ */
369
+ async toggleCustomRoles(enabled) {
370
+ const profiler = require_log.logger.startTimer();
371
+ const response = await this.manageV2Client.toggleCustomRoles({
372
+ repository: this.name,
373
+ enabled
374
+ });
375
+ this.failIfNot200(response, `Could not ${enabled ? "enable" : "disable"} Custom Roles for ${this.name}`);
376
+ profiler.done({ message: `Custom Roles ${enabled ? "enabled" : "disabled"}` });
377
+ }
378
+ /**
379
+ * Toggle the Integration Fields feature
380
+ *
381
+ * @param enabled - The feature new status
382
+ */
383
+ async toggleIntegrationFields(enabled) {
384
+ const profiler = require_log.logger.startTimer();
385
+ const response = await this.manageV2Client.toggleIntegrationFields({
386
+ repository: this.name,
387
+ enabled
388
+ });
389
+ this.failIfNot200(response, `Could not ${enabled ? "enable" : "disable"} Integration Fields for ${this.name}`);
390
+ profiler.done({ message: `Integration Fields ${enabled ? "enabled" : "disabled"}` });
391
+ }
392
+ /**
393
+ * Change the Repository Plan
394
+ *
395
+ * @param {string} newPlanId - The Id of the new Plan to apply
396
+ */
397
+ async changePlan(newPlanId) {
398
+ const profiler = require_log.logger.startTimer();
399
+ const response = await this.manageV2Client.changePlan({
400
+ repository: this.name,
401
+ newPlanId,
402
+ bypassManualBilling: true
403
+ });
404
+ this.failIfNot200(response, "Could not change the Repository Plan");
405
+ profiler.done({ message: `Repository Plan changed to ${newPlanId}` });
406
+ }
407
+ /**
408
+ * Add a new user to the repository
409
+ *
410
+ * @param {string} email - The email of the user
411
+ */
412
+ async addUser(email) {
413
+ const profiler = require_log.logger.startTimer();
414
+ const response = await this.manageV2Client.addUserToRepository({
415
+ repository: this.name,
416
+ email
417
+ });
418
+ const { data, status } = response;
419
+ if (!(status === 200 || status === 409 && JSON.stringify(data).includes("already a member of the repo"))) {
420
+ require_log.logHttpResponse(response);
421
+ throw new Error(`Could not add a new user ${email} to the repository`);
422
+ }
423
+ profiler.done({ message: `User ${email} was added to the repository` });
424
+ }
425
+ /**
426
+ * Remove a user from the repository
427
+ *
428
+ * @param {string} email - The email of the user
429
+ */
430
+ async removeUser(email) {
431
+ const profiler = require_log.logger.startTimer();
432
+ const response = await this.manageV2Client.removeUserFromRepository({
433
+ repository: this.name,
434
+ email
435
+ });
436
+ this.failIfNot200(response, `Could not remove the user ${email} from the repository`);
437
+ profiler.done({ message: `User ${email} was removed from the repository` });
438
+ }
439
+ /**
440
+ * Update the role of a user in a repository
441
+ *
442
+ * @param {string} email - The email of the user
443
+ * @param {string | Record<string, string>} role - The new Role of the user, either a string for
444
+ * basic workflow or an object such as `{ "lang_id": "Writer" }`
445
+ *
446
+ * Example of roles are
447
+ *
448
+ * - Administrator
449
+ * - Writer
450
+ * - Contributor
451
+ * - Manager (publisher)
452
+ */
453
+ async updateUserRole(email, role) {
454
+ const profiler = require_log.logger.startTimer();
455
+ const response = typeof role === "string" ? await this.wroomClient.updateRole(this.name, email, role) : await this.wroomClient.updateRolePerLocal(this.name, email, role);
456
+ this.failIfNot200(response, `Could not update the ${typeof role === "string" ? "role" : "rolePerLocal"} of the user ${email}`);
457
+ profiler.done({ message: `Updated User's ${typeof role === "string" ? "role" : "rolePerLocal"} for user ${email}` });
458
+ }
459
+ /**
460
+ * Creates a document using the core api. If the Core api format is too specific, use the
461
+ * migration api client to create a document with the MigrationApiClient. See the
462
+ * `getMigrationApiClient()` method.
463
+ *
464
+ * @example
465
+ * To create a document by converting it from the Page Builder to
466
+ * Core api format
467
+ *
468
+ * ```js
469
+ * import { DocumentLegacy, UIDContentType } from '@prismicio/types-internal/lib/content';
470
+ *
471
+ * const document = {
472
+ * title: 'A new document',
473
+ * custom_type_id: 'home-page',
474
+ * locale: 'en-gb',
475
+ * integration_field_ids: [],
476
+ * tags: [],
477
+ * data: DocumentLegacy.encode({
478
+ * uid: {
479
+ * __TYPE__: UIDContentType,
480
+ * value: 'home-page',
481
+ * },
482
+ * })
483
+ * }
484
+ * await repository.createDocument(document, 'published');
485
+ * * ```
486
+ * ```
487
+ *
488
+ * @returns
489
+ *
490
+ * @param document - in the core api format.
491
+ *
492
+ * @param status - status of the document, if published, the function will
493
+ * wait for a new content api master ref to be created
494
+ */
495
+ async createDocument(document, status) {
496
+ const profiler = require_log.logger.startTimer();
497
+ const result = await this.coreApiClient.createDraft(document);
498
+ let message = `Document created with id '${result.id}'`;
499
+ if (status === "published") {
500
+ const contentApi = this.getContentApiClient();
501
+ const masterRef = await contentApi.getMasterRef();
502
+ await this.coreApiClient.publishDraft(result.id);
503
+ await _playwright_test.expect.poll(async () => contentApi.getMasterRef(), {
504
+ timeout: 1e4,
505
+ message: "Waiting for new master ref to be available"
506
+ }).not.toBe(masterRef);
507
+ message += ", status 'published' and new master ref available";
508
+ }
509
+ profiler.done({ message });
510
+ return result;
511
+ }
512
+ /**
513
+ * Retrieves data for a specific document version.
514
+ *
515
+ * @param versionId - The document's version ID.
516
+ * @returns The document's version data.
517
+ */
518
+ async getDocumentData(versionId) {
519
+ return await this.coreApiClient.getDocumentData(versionId);
520
+ }
521
+ async setupIntegrationFields(integrationFields) {
522
+ const profiler = require_log.logger.startTimer();
523
+ if (!this.integrationFieldsClient) throw new Error("Integration Fields client not found. Did you provide a integration fields api url in the configuration?");
524
+ await this.integrationFieldsClient.setupIntegrationFields(integrationFields);
525
+ profiler.done({ message: `Integration Fields setup` });
526
+ }
7
527
  };
8
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
9
- const test = require("@playwright/test");
10
- const contentApi = require("../clients/contentApi.cjs");
11
- const log = require("../utils/log.cjs");
12
- const urls = require("../utils/urls.cjs");
13
- class RepositoryManager {
14
- constructor(name, coreApiClient, authApiClient, wroomClient, customTypesApiClient, migrationApiClient, assetApiClient, manageV2Client, integrationFieldsClient) {
15
- __publicField(this, "name");
16
- __publicField(this, "coreApiClient");
17
- __publicField(this, "authApiClient");
18
- __publicField(this, "wroomClient");
19
- __publicField(this, "customTypesApiClient");
20
- __publicField(this, "migrationApiClient");
21
- __publicField(this, "assetApiClient");
22
- __publicField(this, "manageV2Client");
23
- __publicField(this, "integrationFieldsClient");
24
- this.name = name;
25
- this.coreApiClient = coreApiClient;
26
- this.authApiClient = authApiClient;
27
- this.wroomClient = wroomClient;
28
- this.customTypesApiClient = customTypesApiClient;
29
- this.migrationApiClient = migrationApiClient;
30
- this.assetApiClient = assetApiClient;
31
- this.manageV2Client = manageV2Client;
32
- this.integrationFieldsClient = integrationFieldsClient;
33
- }
34
- async configure(config) {
35
- const hasLangConfig = config.defaultLocale || config.locales || config.customLocale;
36
- if (hasLangConfig) {
37
- const languages = await this.coreApiClient.getLanguages();
38
- let updateDefaultLocaleAtTheEnd = false;
39
- if (config.defaultLocale) {
40
- const master = languages.find(({ is_master }) => is_master === true);
41
- if (!master) {
42
- await this.setDefaultLocale(config.defaultLocale, "create");
43
- } else if (master.id !== config.defaultLocale) {
44
- updateDefaultLocaleAtTheEnd = true;
45
- }
46
- }
47
- if (config.locales) {
48
- const localesToAdd = config.locales.filter((id) => !languages.some((language) => language.id === id));
49
- if (localesToAdd.length > 0) {
50
- await this.createLocales(localesToAdd);
51
- }
52
- }
53
- if (config.customLocale) {
54
- if (!languages.find(({ id }) => {
55
- var _a;
56
- return id === ((_a = config.customLocale) == null ? void 0 : _a.lang.id);
57
- })) {
58
- await this.createCustomLocale(config.customLocale);
59
- }
60
- }
61
- if (config.defaultLocale && updateDefaultLocaleAtTheEnd) {
62
- await this.setDefaultLocale(config.defaultLocale, "update");
63
- }
64
- }
65
- if (config.slices) {
66
- await this.createSlices(config.slices);
67
- }
68
- if (config.customTypes) {
69
- await this.createCustomTypes(config.customTypes);
70
- }
71
- if (config.preview) {
72
- await this.createPreview(config.preview);
73
- }
74
- if (config.simulatorURL) {
75
- await this.setSimulatorURL(config.simulatorURL);
76
- }
77
- if (config.rolePerLocal) {
78
- await this.toggleRolePerLocal(true);
79
- }
80
- if (config.customRoles) {
81
- await this.toggleCustomRoles(true);
82
- }
83
- if (config.integrationFields) {
84
- await this.toggleIntegrationFields(true);
85
- await this.setupIntegrationFields(config.integrationFields);
86
- }
87
- }
88
- /** @returns the repository base url, like https://my-repo.prismic.io */
89
- getBaseURL() {
90
- return urls.getRepositoryUrl(this.wroomClient.getBaseURL(), this.name);
91
- }
92
- /** @returns the repository base cdn url, like https://my-repo.cdn.prismic.io */
93
- getBaseCdnURL() {
94
- return urls.getRepositoryUrl(this.wroomClient.getBaseURL(), `${this.name}.cdn`);
95
- }
96
- /**
97
- * @returns a Content Type api client configured to target the repository CDN
98
- * url. Uses Playwright for requests so if your project uses Playwright too,
99
- * any network request will be present in Playwright test reports.
100
- */
101
- getContentApiClient(config) {
102
- return new contentApi.ContentApiClient(this.getBaseCdnURL(), config);
103
- }
104
- /**
105
- * @returns an instance of the Migration api client, see
106
- * https://prismic.io/docs/migration-api-technical-reference
107
- */
108
- getMigrationApiClient() {
109
- if (!this.migrationApiClient) {
110
- throw "No client found for the migration api. Did you provide a migration api url in the configuration?";
111
- }
112
- return this.migrationApiClient;
113
- }
114
- /**
115
- * @returns an instance of the Asset api client, see
116
- * https://prismic.io/docs/asset-api-technical-reference
117
- */
118
- getAssetApiClient() {
119
- return this.assetApiClient;
120
- }
121
- /** @returns the Wroom commit hash deployed on the current repository */
122
- async getDeployedVersion() {
123
- const response = await this.wroomClient.get(this.name, "underground/834b34f16f451e00f268dd5c8c81d16e3c020275");
124
- return response.data.version;
125
- }
126
- /**
127
- * Set additional standard locales for a repository in Wroom, does not fail if
128
- * the locales already exist.
129
- *
130
- * @param locales - An array of all locales to be added to the repository.
131
- *
132
- * @throws Error if setting the locales fails.
133
- */
134
- async createLocales(locales) {
135
- const profiler = log.logger.startTimer();
136
- const response = await this.wroomClient.post(this.name, "app/settings/multilanguages", { languages: locales });
137
- const { data, status } = response;
138
- const success = status === 200 || status === 400 && data.includes("already existing languages");
139
- if (!success) {
140
- log.logHttpResponse(response);
141
- throw new Error(`Could not add locales ${locales.join(", ")}`);
142
- }
143
- profiler.done({ message: `locales '${locales}' created` });
144
- }
145
- /**
146
- * Set additional custom locale for a repository in Wroom, does not fail if
147
- * the locales already exist.
148
- *
149
- * @param locale - The locale to be added to the repository.
150
- *
151
- * @throws Error if setting the locale fails.
152
- */
153
- async createCustomLocale(locale) {
154
- const profiler = log.logger.startTimer();
155
- const response = await this.wroomClient.post(this.name, "app/settings/multilanguages/custom", locale);
156
- const { data, status } = response;
157
- const success = status === 200 || status === 400 && JSON.stringify(data).includes("code is already used");
158
- if (!success) {
159
- log.logHttpResponse(response);
160
- throw new Error(`Could not create custom locale ${locale}`);
161
- }
162
- profiler.done({
163
- message: `custom locale ${JSON.stringify(locale)} created`
164
- });
165
- }
166
- failIfNot200(response, msg) {
167
- if (response.status !== 200) {
168
- log.logHttpResponse(response);
169
- throw new Error(msg);
170
- }
171
- }
172
- /**
173
- * Delete a locale from a repository.
174
- *
175
- * @param locale - locale to remove from the repository configuration.
176
- *
177
- * @throws Error if deleting the locale fails.
178
- */
179
- async deleteLocale(locale) {
180
- const profiler = log.logger.startTimer();
181
- const response = await this.wroomClient.post(this.name, `app/settings/multilanguages/${locale}/delete`);
182
- this.failIfNot200(response, `Could not delete locale ${locale}`);
183
- profiler.done({ message: `locale '${locale}' deleted` });
184
- }
185
- /**
186
- * Create or update the default (master) locale for a repository in Wroom, the
187
- * locale needs to exist in the repository.
188
- *
189
- * @param locale - The locale to be set as the default.
190
- * @param endpoint - "createMasterLang" to create it, "defineAsMaster" to
191
- * update it.
192
- *
193
- * @throws Error if setting the default locale fails.
194
- */
195
- async setDefaultLocale(locale, action) {
196
- const profiler = log.logger.startTimer();
197
- const endpoint = {
198
- create: "createMasterLang",
199
- update: "defineAsMaster"
200
- };
201
- const response = await this.wroomClient.post(this.name, `/app/settings/multilanguages/${locale}/${endpoint[action]}`);
202
- const { data, status } = response;
203
- const success = status === 200 || status === 400 && // returns 400 if master lang is already set
204
- JSON.stringify(data).includes("Unable to set as master language");
205
- if (!success) {
206
- log.logHttpResponse(response);
207
- throw new Error(`Could not set default locale to ${locale}`);
208
- }
209
- profiler.done({ message: `default locale set to '${locale}'` });
210
- }
211
- /**
212
- * Retrieves documents.
213
- *
214
- * @param args - Configure which documents are retrieved.
215
- *
216
- * @returns A paginated set of documents.
217
- */
218
- async getDocuments(args) {
219
- return this.coreApiClient.getDocuments(args);
220
- }
221
- /**
222
- * Creates a release.
223
- *
224
- * @param label - The label for the release.
225
- *
226
- * @returns A Promise that resolves when the release is created.
227
- */
228
- async createRelease(label) {
229
- const profiler = log.logger.startTimer();
230
- const response = await this.wroomClient.post(this.name, "app/releases", {
231
- label
232
- });
233
- this.failIfNot200(response, `Could not create release ${label}`);
234
- profiler.done({ message: `release '${label}' created` });
235
- return response.data;
236
- }
237
- /**
238
- * Retrieves the migration release.
239
- *
240
- * @returns The migration relese metadata.
241
- */
242
- async getMigrationRelease() {
243
- const releases = await this.coreApiClient.getReleases();
244
- const migrationRelease = releases.find((release) => release.migration);
245
- if (!migrationRelease) {
246
- throw new Error(`The repository does not have a migration release`);
247
- }
248
- return migrationRelease;
249
- }
250
- /**
251
- * Publishes a release.
252
- *
253
- * @param id - The ID of the release.
254
- *
255
- * @returns A Promise that resolves when the release is published and a new
256
- * master ref is available.
257
- */
258
- async publishRelease(id) {
259
- const profiler = log.logger.startTimer();
260
- const contentApi2 = this.getContentApiClient();
261
- const masterRef = await contentApi2.getMasterRef();
262
- await this.coreApiClient.publishRelease(id);
263
- await test.expect.poll(async () => contentApi2.getMasterRef(), {
264
- timeout: 1e4,
265
- message: "Waiting for new master ref to be available"
266
- }).not.toBe(masterRef);
267
- profiler.done({
268
- message: `release '${id}' published and new master ref available`
269
- });
270
- }
271
- /**
272
- * Deletes a repository release.
273
- *
274
- * @param id - The ID of the release to be deleted.
275
- */
276
- async deleteRelease(id) {
277
- const profiler = log.logger.startTimer();
278
- const response = await this.wroomClient.post(this.name, `app/releases/${id}/delete`, {});
279
- this.failIfNot200(response, `Could not delete release with id ${id}`);
280
- profiler.done({ message: `release '${id}' deleted` });
281
- }
282
- /**
283
- * Creates a preview for a repository.
284
- *
285
- * @param data - The data for creating the preview.
286
- */
287
- async createPreview(data) {
288
- const profiler = log.logger.startTimer();
289
- const response = await this.wroomClient.post(this.name, `previews/new`, data);
290
- this.failIfNot200(response, `Could not create preview ${data.name}`);
291
- profiler.done({ message: `preview '${data.name}' created` });
292
- return response.data;
293
- }
294
- /**
295
- * Set the simulator URL (table of slices) for a repository.
296
- *
297
- * @param data - The simulator URL to set.
298
- */
299
- async setSimulatorURL(simulatorURL) {
300
- const profiler = log.logger.startTimer();
301
- const repository = await this.coreApiClient.getRepository();
302
- await this.coreApiClient.updateRepository({
303
- ...repository,
304
- simulator_url: simulatorURL
305
- });
306
- profiler.done({
307
- message: `simulator preview '${simulatorURL}' created`
308
- });
309
- }
310
- /**
311
- * Deletes a preview from a repository.
312
- *
313
- * @param id - The ID of the preview to be deleted.
314
- */
315
- async deletePreview(id) {
316
- const profiler = log.logger.startTimer();
317
- const response = await this.wroomClient.post(this.name, `previews/delete/${id}`, {});
318
- this.failIfNot200(response, `Could not delete preview with id ${id}`);
319
- profiler.done({ message: `preview '${id}' deleted` });
320
- }
321
- /**
322
- * Create a webhook.
323
- *
324
- * @returns A Promise that resolves with the webhook id.
325
- */
326
- async createWebhook(name, url, secret, triggers, active) {
327
- const profiler = log.logger.startTimer();
328
- const response = await this.wroomClient.post(this.name, "app/settings/webhooks/create", {
329
- name,
330
- url,
331
- secret,
332
- active: active ? "on" : "off",
333
- ...triggers
334
- });
335
- this.failIfNot200(response, `Could not create webhook ${name}`);
336
- profiler.done({ message: `webhook '${name}' created` });
337
- return response.data.created;
338
- }
339
- /**
340
- * Deletes a webhook.
341
- *
342
- * @param id - The webhook ID.
343
- */
344
- async deleteWebhook(id) {
345
- const profiler = log.logger.startTimer();
346
- const response = await this.wroomClient.post(this.name, `app/settings/webhooks/${id}/delete`, {});
347
- this.failIfNot200(response, `Could not delete webhook with id ${id}`);
348
- profiler.done({ message: `webhook '${id}' deleted` });
349
- }
350
- /**
351
- * Create Custom Types using the Custom types api.
352
- *
353
- * @param customTypes -
354
- */
355
- async createCustomTypes(customTypes) {
356
- await this.customTypesApiClient.createCustomTypes(customTypes);
357
- }
358
- async deleteCustomType(customTypeId) {
359
- await this.customTypesApiClient.deleteCustomType(customTypeId);
360
- }
361
- /**
362
- * Create slices using the Custom types api.
363
- *
364
- * @param slices -
365
- */
366
- async createSlices(slices) {
367
- await this.customTypesApiClient.createSlices(slices);
368
- }
369
- /**
370
- * Create an access token to query private items of the Content api.
371
- *
372
- * @param appName - mandatory authorized application name
373
- * @param tokenScope - access token scope. Use "master" to when your content
374
- * api is fully private or "master+releases" to access private releases
375
- */
376
- async createContentAPIToken(appName, tokenScope = "master") {
377
- const profiler = log.logger.startTimer();
378
- const existingApps = await this.wroomClient.get(this.name, "settings/security/contentapi");
379
- this.failIfNot200(existingApps, `Could not get status of existing applications to generate a content api token`);
380
- let app = existingApps.data.find(({ name, wroom_auths }) => {
381
- return name === appName && wroom_auths.length > 0;
382
- });
383
- if (!app) {
384
- const response = await this.wroomClient.post(this.name, "settings/security/oauthapp", {
385
- app_name: appName
386
- });
387
- this.failIfNot200(response, `Could not create content api app ${appName}`);
388
- app = response.data;
389
- }
390
- let tokenInfo = app.wroom_auths.find(({ scope }) => scope === tokenScope);
391
- if (!tokenInfo) {
392
- const response = await this.wroomClient.post(this.name, "settings/security/authorizations", {
393
- app: app.id,
394
- scope: tokenScope
395
- });
396
- this.failIfNot200(response, `Could not create content api access token for app ${appName} with scope ${tokenScope}`);
397
- tokenInfo = response.data;
398
- }
399
- profiler.done({
400
- message: `content api access token for app '${appName}' created`
401
- });
402
- if (!(tokenInfo == null ? void 0 : tokenInfo.token)) {
403
- throw `Could not create content api access token for app ${appName} with scope ${tokenScope}`;
404
- }
405
- return tokenInfo.token;
406
- }
407
- /** Create a permanent access token to authenticate to Prismic public apis. */
408
- async createPermanentAccessToken() {
409
- const profiler = log.logger.startTimer();
410
- const token = await this.authApiClient.getMachine2MachineToken(this.name);
411
- profiler.done({
412
- message: `machine2machine token created`
413
- });
414
- return token;
415
- }
416
- /**
417
- * Toggle the Role per Locale workflow
418
- *
419
- * @param enabled - The feature new status
420
- */
421
- async toggleRolePerLocal(enabled) {
422
- const profiler = log.logger.startTimer();
423
- const response = await this.manageV2Client.toggleRolePerLocal({
424
- repository: this.name,
425
- enabled
426
- });
427
- this.failIfNot200(response, `Could not ${enabled ? "enable" : "disable"} Role per local for ${this.name}`);
428
- profiler.done({
429
- message: `Role per local ${enabled ? "enabled" : "disabled"}`
430
- });
431
- }
432
- /**
433
- * Toggle the Custom Roles workflow
434
- *
435
- * @param enabled - The feature new status
436
- */
437
- async toggleCustomRoles(enabled) {
438
- const profiler = log.logger.startTimer();
439
- const response = await this.manageV2Client.toggleCustomRoles({
440
- repository: this.name,
441
- enabled
442
- });
443
- this.failIfNot200(response, `Could not ${enabled ? "enable" : "disable"} Custom Roles for ${this.name}`);
444
- profiler.done({
445
- message: `Custom Roles ${enabled ? "enabled" : "disabled"}`
446
- });
447
- }
448
- /**
449
- * Toggle the Integration Fields feature
450
- *
451
- * @param enabled - The feature new status
452
- */
453
- async toggleIntegrationFields(enabled) {
454
- const profiler = log.logger.startTimer();
455
- const response = await this.manageV2Client.toggleIntegrationFields({
456
- repository: this.name,
457
- enabled
458
- });
459
- this.failIfNot200(response, `Could not ${enabled ? "enable" : "disable"} Integration Fields for ${this.name}`);
460
- profiler.done({
461
- message: `Integration Fields ${enabled ? "enabled" : "disabled"}`
462
- });
463
- }
464
- /**
465
- * Change the Repository Plan
466
- *
467
- * @param {string} newPlanId - The Id of the new Plan to apply
468
- */
469
- async changePlan(newPlanId) {
470
- const profiler = log.logger.startTimer();
471
- const response = await this.manageV2Client.changePlan({
472
- repository: this.name,
473
- newPlanId,
474
- bypassManualBilling: true
475
- // For now the library does not support Stripe features
476
- });
477
- this.failIfNot200(response, "Could not change the Repository Plan");
478
- profiler.done({
479
- message: `Repository Plan changed to ${newPlanId}`
480
- });
481
- }
482
- /**
483
- * Add a new user to the repository
484
- *
485
- * @param {string} email - The email of the user
486
- */
487
- async addUser(email) {
488
- const profiler = log.logger.startTimer();
489
- const response = await this.manageV2Client.addUserToRepository({
490
- repository: this.name,
491
- email
492
- });
493
- const { data, status } = response;
494
- const success = status === 200 || status === 409 && JSON.stringify(data).includes("already a member of the repo");
495
- if (!success) {
496
- log.logHttpResponse(response);
497
- throw new Error(`Could not add a new user ${email} to the repository`);
498
- }
499
- profiler.done({
500
- message: `User ${email} was added to the repository`
501
- });
502
- }
503
- /**
504
- * Remove a user from the repository
505
- *
506
- * @param {string} email - The email of the user
507
- */
508
- async removeUser(email) {
509
- const profiler = log.logger.startTimer();
510
- const response = await this.manageV2Client.removeUserFromRepository({
511
- repository: this.name,
512
- email
513
- });
514
- this.failIfNot200(response, `Could not remove the user ${email} from the repository`);
515
- profiler.done({
516
- message: `User ${email} was removed from the repository`
517
- });
518
- }
519
- /**
520
- * Update the role of a user in a repository
521
- *
522
- * @param {string} email - The email of the user
523
- * @param {string | Record<string, string>} role - The new Role of the user,
524
- * either a string for basic workflow or an object such as `{ "lang_id":
525
- * "Writer" }`
526
- *
527
- * Example of roles are
528
- *
529
- * - Administrator
530
- * - Writer
531
- * - Contributor
532
- * - Manager (publisher)
533
- */
534
- async updateUserRole(email, role) {
535
- const profiler = log.logger.startTimer();
536
- const response = typeof role === "string" ? await this.wroomClient.updateRole(this.name, email, role) : await this.wroomClient.updateRolePerLocal(this.name, email, role);
537
- this.failIfNot200(response, `Could not update the ${typeof role === "string" ? "role" : "rolePerLocal"} of the user ${email}`);
538
- profiler.done({
539
- message: `Updated User's ${typeof role === "string" ? "role" : "rolePerLocal"} for user ${email}`
540
- });
541
- }
542
- /**
543
- * Creates a document using the core api. If the Core api format is too
544
- * specific, use the migration api client to create a document with the
545
- * MigrationApiClient. See the `getMigrationApiClient()` method.
546
- *
547
- * @example To create a document by converting it from the Page Builder to
548
- * Core api format
549
- *
550
- * ```js
551
- * import { DocumentLegacy, UIDContentType } from '@prismicio/types-internal/lib/content';
552
- *
553
- * const document = {
554
- * title: 'A new document',
555
- * custom_type_id: 'home-page',
556
- * locale: 'en-gb',
557
- * integration_field_ids: [],
558
- * tags: [],
559
- * data: DocumentLegacy.encode({
560
- * uid: {
561
- * __TYPE__: UIDContentType,
562
- * value: 'home-page',
563
- * },
564
- * })
565
- * }
566
- * await repository.createDocument(document, 'published');
567
- * * ```
568
- * ```
569
- *
570
- * @returns
571
- *
572
- * @param document - in the core api format.
573
- *
574
- * @param status - status of the document, if published, the function will
575
- * wait for a new content api master ref to be created
576
- */
577
- async createDocument(document, status) {
578
- const profiler = log.logger.startTimer();
579
- const result = await this.coreApiClient.createDraft(document);
580
- let message = `Document created with id '${result.id}'`;
581
- if (status === "published") {
582
- const contentApi2 = this.getContentApiClient();
583
- const masterRef = await contentApi2.getMasterRef();
584
- await this.coreApiClient.publishDraft(result.id);
585
- await test.expect.poll(async () => contentApi2.getMasterRef(), {
586
- timeout: 1e4,
587
- message: "Waiting for new master ref to be available"
588
- }).not.toBe(masterRef);
589
- message += ", status 'published' and new master ref available";
590
- }
591
- profiler.done({
592
- message
593
- });
594
- return result;
595
- }
596
- /**
597
- * Retrieves data for a specific document version.
598
- *
599
- * @param versionId - The document's version ID.
600
- *
601
- * @returns The document's version data.
602
- */
603
- async getDocumentData(versionId) {
604
- return await this.coreApiClient.getDocumentData(versionId);
605
- }
606
- async setupIntegrationFields(integrationFields) {
607
- const profiler = log.logger.startTimer();
608
- if (!this.integrationFieldsClient) {
609
- throw new Error("Integration Fields client not found. Did you provide a integration fields api url in the configuration?");
610
- }
611
- await this.integrationFieldsClient.setupIntegrationFields(integrationFields);
612
- profiler.done({
613
- message: `Integration Fields setup`
614
- });
615
- }
616
- }
528
+ //#endregion
617
529
  exports.RepositoryManager = RepositoryManager;
618
- //# sourceMappingURL=repository.cjs.map
530
+
531
+ //# sourceMappingURL=repository.cjs.map