@prismicio/e2e-tests-utils 2.0.0-alpha.0 → 2.0.0-alpha.1

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 (60) hide show
  1. package/dist/clients/apiClient.cjs +1 -4
  2. package/dist/clients/apiClient.cjs.map +1 -1
  3. package/dist/clients/apiClient.js +1 -4
  4. package/dist/clients/apiClient.js.map +1 -1
  5. package/dist/clients/assetApi.cjs +48 -0
  6. package/dist/clients/assetApi.cjs.map +1 -0
  7. package/dist/clients/assetApi.d.ts +53 -0
  8. package/dist/clients/assetApi.js +48 -0
  9. package/dist/clients/assetApi.js.map +1 -0
  10. package/dist/clients/authenticationApi.cjs +1 -4
  11. package/dist/clients/authenticationApi.cjs.map +1 -1
  12. package/dist/clients/authenticationApi.js +1 -4
  13. package/dist/clients/authenticationApi.js.map +1 -1
  14. package/dist/clients/contentApi.cjs +1 -4
  15. package/dist/clients/contentApi.cjs.map +1 -1
  16. package/dist/clients/contentApi.js +1 -4
  17. package/dist/clients/contentApi.js.map +1 -1
  18. package/dist/clients/coreApi.cjs.map +1 -1
  19. package/dist/clients/coreApi.js.map +1 -1
  20. package/dist/clients/customTypesApi.cjs.map +1 -1
  21. package/dist/clients/customTypesApi.js.map +1 -1
  22. package/dist/clients/manageV2.cjs +2 -5
  23. package/dist/clients/manageV2.cjs.map +1 -1
  24. package/dist/clients/manageV2.d.ts +1 -1
  25. package/dist/clients/manageV2.js +2 -5
  26. package/dist/clients/manageV2.js.map +1 -1
  27. package/dist/clients/migrationApi.cjs.map +1 -1
  28. package/dist/clients/migrationApi.js.map +1 -1
  29. package/dist/clients/wroom.cjs +2 -5
  30. package/dist/clients/wroom.cjs.map +1 -1
  31. package/dist/clients/wroom.js +2 -5
  32. package/dist/clients/wroom.js.map +1 -1
  33. package/dist/managers/repositories.cjs +10 -6
  34. package/dist/managers/repositories.cjs.map +1 -1
  35. package/dist/managers/repositories.js +10 -6
  36. package/dist/managers/repositories.js.map +1 -1
  37. package/dist/managers/repository.cjs +40 -5
  38. package/dist/managers/repository.cjs.map +1 -1
  39. package/dist/managers/repository.d.ts +27 -1
  40. package/dist/managers/repository.js +40 -5
  41. package/dist/managers/repository.js.map +1 -1
  42. package/dist/types.d.ts +3 -0
  43. package/dist/utils/cookies.cjs.map +1 -1
  44. package/dist/utils/cookies.js.map +1 -1
  45. package/dist/utils/envVariableManager.cjs +1 -4
  46. package/dist/utils/envVariableManager.cjs.map +1 -1
  47. package/dist/utils/envVariableManager.js +1 -4
  48. package/dist/utils/envVariableManager.js.map +1 -1
  49. package/dist/utils/log.cjs.map +1 -1
  50. package/dist/utils/log.js +1 -1
  51. package/dist/utils/log.js.map +1 -1
  52. package/dist/utils/urls.cjs.map +1 -1
  53. package/dist/utils/urls.js.map +1 -1
  54. package/package.json +1 -1
  55. package/src/clients/assetApi.ts +76 -0
  56. package/src/clients/manageV2.ts +1 -1
  57. package/src/clients/wroom.ts +1 -1
  58. package/src/managers/repositories.ts +16 -0
  59. package/src/managers/repository.ts +66 -0
  60. package/src/types.ts +3 -0
@@ -0,0 +1,76 @@
1
+ import { logPlaywrightApiResponse } from "../utils/log";
2
+
3
+ import { AuthServerToken, AuthenticatedApiClient } from "./apiClient";
4
+
5
+ export interface AssetApiCreatePayload {
6
+ name: string;
7
+ mimeType: string;
8
+ buffer: Buffer;
9
+ }
10
+
11
+ export interface AssetApiCreateResponse {
12
+ id: string;
13
+ url: string;
14
+ filename: string;
15
+ size: number;
16
+ width: number;
17
+ height: number;
18
+ last_modified: number;
19
+ kind: string;
20
+ extension: string;
21
+ created_at: number;
22
+ uploader_id: string;
23
+ tags: string[];
24
+ }
25
+
26
+ export class AssetApiClient extends AuthenticatedApiClient {
27
+ /**
28
+ * @example To instantiate the class:
29
+ *
30
+ * ```js
31
+ * new AssetApiClient("https://asset-api.prismic.io", {
32
+ * authToken: "my-secret-token",
33
+ * repository: "my-repo-name",
34
+ * });
35
+ * ```
36
+ *
37
+ * @param baseURL - The API base URL, such as https://asset-api.prismic.io
38
+ * @param config - Optional configuration
39
+ *
40
+ * - {@link config.authToken}: API token generated from the auth service or a
41
+ * function to fetch it when it's needed.
42
+ * - {@link config.repository}: Repository name.
43
+ */
44
+ constructor(
45
+ baseURL: string,
46
+ config: { authToken: AuthServerToken; repository: string },
47
+ ) {
48
+ super(baseURL, config.authToken, { repository: config.repository });
49
+ }
50
+
51
+ /**
52
+ * Uploads an asset file to the server.
53
+ *
54
+ * @param data - The payload containing the file and filename.
55
+ *
56
+ * @returns The API response containing asset information.
57
+ *
58
+ * @throws An error if the upload fails.
59
+ */
60
+ async createAsset(
61
+ data: AssetApiCreatePayload,
62
+ ): Promise<AssetApiCreateResponse> {
63
+ const context = await this.getContext();
64
+
65
+ const result = await context.post("assets", {
66
+ multipart: { file: data },
67
+ });
68
+
69
+ if (200 !== result.status()) {
70
+ await logPlaywrightApiResponse(result);
71
+ throw new Error("Could not upload the asset with the asset API.");
72
+ }
73
+
74
+ return result.json();
75
+ }
76
+ }
@@ -35,7 +35,7 @@ export class ManageV2Client {
35
35
  // cookies are not forwarded automatically in a non-browser env
36
36
  this.client.interceptors.response.use((response) => {
37
37
  const cookies = response.headers["set-cookie"];
38
- if (cookies && extractCookie(cookies, "SESSION")) {
38
+ if (cookies && extractCookie(cookies, "prismic-auth")) {
39
39
  this.client.defaults.headers["Cookie"] = cookies;
40
40
  }
41
41
 
@@ -45,7 +45,7 @@ export class WroomClient {
45
45
  // cookies are not forwarded automatically in a non-browser env
46
46
  this.client.interceptors.response.use((response) => {
47
47
  const cookies = response.headers["set-cookie"];
48
- if (cookies && extractCookie(cookies, "prismic-auth")) {
48
+ if (cookies && extractCookie(cookies, "SESSION")) {
49
49
  this.client.defaults.headers["Cookie"] = cookies;
50
50
  }
51
51
 
@@ -1,5 +1,6 @@
1
1
  import { SetupConfiguration, UrlConfig } from "../types";
2
2
 
3
+ import { AssetApiClient } from "../clients/assetApi";
3
4
  import { AuthenticationApiClient } from "../clients/authenticationApi";
4
5
  import { CoreApiClient } from "../clients/coreApi";
5
6
  import { CustomTypesApiClient } from "../clients/customTypesApi";
@@ -77,6 +78,15 @@ export class RepositoriesManager {
77
78
  this.config.urlConfig.baseURL,
78
79
  this.config.manageV2Config,
79
80
  );
81
+
82
+ // This logic specific to staging environment allow us to target a specific version of the custom types API.
83
+ // This is only works for unify-exp at the moment.
84
+ // Having this logic here avoids duplicating it across different CI workflows.
85
+ this.config.urlConfig.customTypesApi =
86
+ configuration.environment === "stage" &&
87
+ configuration.cluster === "unify-exp"
88
+ ? `https://customtypes.wroom.io/${configuration.cluster}/`
89
+ : this.config.urlConfig.customTypesApi;
80
90
  }
81
91
 
82
92
  /**
@@ -92,6 +102,7 @@ export class RepositoriesManager {
92
102
  authenticationApi: `${protocol}://auth.${url.hostname}`,
93
103
  customTypesApi: `${protocol}://customtypes.${url.hostname}`,
94
104
  migrationApi: `${protocol}://migration.${url.hostname}`,
105
+ assetApi: `${protocol}://asset-api.${url.hostname}`,
95
106
  };
96
107
  }
97
108
 
@@ -212,6 +223,10 @@ export class RepositoriesManager {
212
223
  repository,
213
224
  })
214
225
  : undefined;
226
+ const assetApiClient = new AssetApiClient(urlConfig.assetApi, {
227
+ authToken,
228
+ repository,
229
+ });
215
230
 
216
231
  return new RepositoryManager(
217
232
  repository,
@@ -220,6 +235,7 @@ export class RepositoriesManager {
220
235
  this.wroomClient,
221
236
  customTypeClient,
222
237
  migrationApiClient,
238
+ assetApiClient,
223
239
  this.manageV2Client,
224
240
  );
225
241
  }
@@ -6,6 +6,7 @@ import {
6
6
  SharedSlice,
7
7
  } from "@prismicio/types-internal/lib/customtypes";
8
8
 
9
+ import { AssetApiClient } from "../clients/assetApi";
9
10
  import { AuthenticationApiClient } from "../clients/authenticationApi";
10
11
  import { ContentApiClient } from "../clients/contentApi";
11
12
  import {
@@ -29,6 +30,7 @@ export class RepositoryManager {
29
30
  private readonly wroomClient: WroomClient,
30
31
  private readonly customTypesApiClient: CustomTypesApiClient,
31
32
  private readonly migrationApiClient: MigrationApiClient | undefined,
33
+ private readonly assetApiClient: AssetApiClient,
32
34
  private readonly manageV2Client: ManageV2Client,
33
35
  ) {}
34
36
 
@@ -121,6 +123,14 @@ export class RepositoryManager {
121
123
  return this.migrationApiClient;
122
124
  }
123
125
 
126
+ /**
127
+ * @returns an instance of the Asset api client, see
128
+ * https://prismic.io/docs/asset-api-technical-reference
129
+ */
130
+ getAssetApiClient(): AssetApiClient {
131
+ return this.assetApiClient;
132
+ }
133
+
124
134
  /** @returns the Wroom commit hash deployed on the current repository */
125
135
  async getDeployedVersion(): Promise<string | undefined> {
126
136
  const response = await this.wroomClient.get(
@@ -330,6 +340,62 @@ export class RepositoryManager {
330
340
  profiler.done({ message: `preview '${id}' deleted` });
331
341
  }
332
342
 
343
+ /**
344
+ * Create a webhook.
345
+ *
346
+ * @returns A Promise that resolves with the webhook id.
347
+ */
348
+ async createWebhook(
349
+ name: string,
350
+ url: string,
351
+ secret: string,
352
+ triggers: {
353
+ documentsPublished?: boolean;
354
+ documentsUnpublished?: boolean;
355
+ releasesCreated?: boolean;
356
+ releasesUpdated?: boolean;
357
+ tagsCreated?: boolean;
358
+ tagsDeleted?: boolean;
359
+ },
360
+ active: boolean,
361
+ ): Promise<string> {
362
+ const profiler = logger.startTimer();
363
+
364
+ const response = await this.wroomClient.post(
365
+ this.name,
366
+ "app/settings/webhooks/create",
367
+ {
368
+ name,
369
+ url,
370
+ secret,
371
+ active: active ? "on" : "off",
372
+ ...triggers,
373
+ },
374
+ );
375
+ this.failIfNot200(response, `Could not create webhook ${name}`);
376
+ profiler.done({ message: `webhook '${name}' created` });
377
+
378
+ return response.data.created;
379
+ }
380
+
381
+ /**
382
+ * Deletes a webhook.
383
+ *
384
+ * @param id - The webhook ID.
385
+ */
386
+ async deleteWebhook(id: string): Promise<void> {
387
+ const profiler = logger.startTimer();
388
+
389
+ const response = await this.wroomClient.post(
390
+ this.name,
391
+ `app/settings/webhooks/${id}/delete`,
392
+ {},
393
+ );
394
+
395
+ this.failIfNot200(response, `Could not delete webhook with id ${id}`);
396
+ profiler.done({ message: `webhook '${id}' deleted` });
397
+ }
398
+
333
399
  /**
334
400
  * Create Custom Types using the Custom types api.
335
401
  *
package/src/types.ts CHANGED
@@ -7,6 +7,8 @@ export type UrlConfig = {
7
7
  authenticationApi: string;
8
8
  /** Prismic migration api base url */
9
9
  migrationApi?: string;
10
+ /** Asset api base url */
11
+ assetApi: string;
10
12
  };
11
13
 
12
14
  export type AuthConfig = {
@@ -27,4 +29,5 @@ export type SetupConfiguration = {
27
29
  authConfig: AuthConfig;
28
30
  manageV2Config?: ManageV2Config;
29
31
  cluster?: string;
32
+ environment?: string;
30
33
  };