@prismicio/e2e-tests-utils 1.0.0-alpha.4 → 1.1.0-alpha.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.
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const axios = require("axios");
4
+ const log = require("../utils/log.cjs");
5
+ const createCoreApiClient = (baseURL, authClient) => {
6
+ const client = axios.create({
7
+ baseURL,
8
+ validateStatus: () => true
9
+ // Don't throw on 4XX errors
10
+ });
11
+ client.interceptors.request.use(async (config) => {
12
+ const auth = "Authorization";
13
+ if (!config.headers[auth]) {
14
+ const token = await authClient.getToken();
15
+ config.headers[auth] = `Bearer ${token}`;
16
+ }
17
+ return config;
18
+ });
19
+ async function getLanguages() {
20
+ const profiler = log.logger.startTimer();
21
+ const result = await client.get("core/repository");
22
+ if (![200].includes(result.status) || !result.data.languages) {
23
+ log.logHttpResponse(result);
24
+ throw new Error("Could not get languages from the core api.");
25
+ }
26
+ profiler.done({ message: "retrieved languages configuration" });
27
+ return result.data.languages;
28
+ }
29
+ return {
30
+ getLanguages
31
+ };
32
+ };
33
+ exports.createCoreApiClient = createCoreApiClient;
34
+ //# sourceMappingURL=coreApi.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"coreApi.cjs","sources":["../../../src/clients/coreApi.ts"],"sourcesContent":["import axios, { AxiosInstance } from \"axios\";\n\nimport { logHttpResponse, logger } from \"../utils/log\";\n\nimport { AuthenticationClient } from \"./authenticationApi\";\n\ntype Language = {\n\tid: string;\n\tname: string;\n\tis_master: boolean;\n};\n\nexport type CoreClient = {\n\tgetLanguages(): Promise<Language[]>;\n};\n\n/** Client for interacting with the core API */\nexport const createCoreApiClient = (\n\tbaseURL: string,\n\tauthClient: AuthenticationClient,\n): CoreClient => {\n\tconst client: AxiosInstance = axios.create({\n\t\tbaseURL,\n\t\tvalidateStatus: () => true, // Don't throw on 4XX errors\n\t});\n\n\t// Add an interceptor to authenticate requests\n\tclient.interceptors.request.use(async (config) => {\n\t\tconst auth = \"Authorization\";\n\t\tif (!config.headers[auth]) {\n\t\t\tconst token = await authClient.getToken();\n\t\t\tconfig.headers[auth] = `Bearer ${token}`;\n\t\t}\n\n\t\treturn config;\n\t});\n\n\tasync function getLanguages(): Promise<Language[]> {\n\t\tconst profiler = logger.startTimer();\n\t\tconst result = await client.get<{ languages: Language[] }>(\n\t\t\t\"core/repository\",\n\t\t);\n\n\t\tif (![200].includes(result.status) || !result.data.languages) {\n\t\t\tlogHttpResponse(result);\n\t\t\tthrow new Error(\"Could not get languages from the core api.\");\n\t\t}\n\n\t\tprofiler.done({ message: \"retrieved languages configuration\" });\n\n\t\treturn result.data.languages;\n\t}\n\n\treturn {\n\t\tgetLanguages,\n\t};\n};\n"],"names":["logger","logHttpResponse"],"mappings":";;;;AAiBa,MAAA,sBAAsB,CAClC,SACA,eACe;AACT,QAAA,SAAwB,MAAM,OAAO;AAAA,IAC1C;AAAA,IACA,gBAAgB,MAAM;AAAA;AAAA,EAAA,CACtB;AAGD,SAAO,aAAa,QAAQ,IAAI,OAAO,WAAU;AAChD,UAAM,OAAO;AACb,QAAI,CAAC,OAAO,QAAQ,IAAI,GAAG;AACpB,YAAA,QAAQ,MAAM,WAAW;AAC/B,aAAO,QAAQ,IAAI,IAAI,UAAU,KAAK;AAAA,IACtC;AAEM,WAAA;AAAA,EAAA,CACP;AAED,iBAAe,eAAY;AACpB,UAAA,WAAWA,WAAO;AACxB,UAAM,SAAS,MAAM,OAAO,IAC3B,iBAAiB;AAGd,QAAA,CAAC,CAAC,GAAG,EAAE,SAAS,OAAO,MAAM,KAAK,CAAC,OAAO,KAAK,WAAW;AAC7DC,UAAA,gBAAgB,MAAM;AAChB,YAAA,IAAI,MAAM,4CAA4C;AAAA,IAC5D;AAED,aAAS,KAAK,EAAE,SAAS,oCAAqC,CAAA;AAE9D,WAAO,OAAO,KAAK;AAAA,EACpB;AAEO,SAAA;AAAA,IACN;AAAA,EAAA;AAEF;;"}
@@ -0,0 +1,12 @@
1
+ import { AuthenticationClient } from "./authenticationApi";
2
+ type Language = {
3
+ id: string;
4
+ name: string;
5
+ is_master: boolean;
6
+ };
7
+ export type CoreClient = {
8
+ getLanguages(): Promise<Language[]>;
9
+ };
10
+ /** Client for interacting with the core API */
11
+ export declare const createCoreApiClient: (baseURL: string, authClient: AuthenticationClient) => CoreClient;
12
+ export {};
@@ -0,0 +1,34 @@
1
+ import axios from "axios";
2
+ import { logger, logHttpResponse } from "../utils/log.js";
3
+ const createCoreApiClient = (baseURL, authClient) => {
4
+ const client = axios.create({
5
+ baseURL,
6
+ validateStatus: () => true
7
+ // Don't throw on 4XX errors
8
+ });
9
+ client.interceptors.request.use(async (config) => {
10
+ const auth = "Authorization";
11
+ if (!config.headers[auth]) {
12
+ const token = await authClient.getToken();
13
+ config.headers[auth] = `Bearer ${token}`;
14
+ }
15
+ return config;
16
+ });
17
+ async function getLanguages() {
18
+ const profiler = logger.startTimer();
19
+ const result = await client.get("core/repository");
20
+ if (![200].includes(result.status) || !result.data.languages) {
21
+ logHttpResponse(result);
22
+ throw new Error("Could not get languages from the core api.");
23
+ }
24
+ profiler.done({ message: "retrieved languages configuration" });
25
+ return result.data.languages;
26
+ }
27
+ return {
28
+ getLanguages
29
+ };
30
+ };
31
+ export {
32
+ createCoreApiClient
33
+ };
34
+ //# sourceMappingURL=coreApi.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"coreApi.js","sources":["../../../src/clients/coreApi.ts"],"sourcesContent":["import axios, { AxiosInstance } from \"axios\";\n\nimport { logHttpResponse, logger } from \"../utils/log\";\n\nimport { AuthenticationClient } from \"./authenticationApi\";\n\ntype Language = {\n\tid: string;\n\tname: string;\n\tis_master: boolean;\n};\n\nexport type CoreClient = {\n\tgetLanguages(): Promise<Language[]>;\n};\n\n/** Client for interacting with the core API */\nexport const createCoreApiClient = (\n\tbaseURL: string,\n\tauthClient: AuthenticationClient,\n): CoreClient => {\n\tconst client: AxiosInstance = axios.create({\n\t\tbaseURL,\n\t\tvalidateStatus: () => true, // Don't throw on 4XX errors\n\t});\n\n\t// Add an interceptor to authenticate requests\n\tclient.interceptors.request.use(async (config) => {\n\t\tconst auth = \"Authorization\";\n\t\tif (!config.headers[auth]) {\n\t\t\tconst token = await authClient.getToken();\n\t\t\tconfig.headers[auth] = `Bearer ${token}`;\n\t\t}\n\n\t\treturn config;\n\t});\n\n\tasync function getLanguages(): Promise<Language[]> {\n\t\tconst profiler = logger.startTimer();\n\t\tconst result = await client.get<{ languages: Language[] }>(\n\t\t\t\"core/repository\",\n\t\t);\n\n\t\tif (![200].includes(result.status) || !result.data.languages) {\n\t\t\tlogHttpResponse(result);\n\t\t\tthrow new Error(\"Could not get languages from the core api.\");\n\t\t}\n\n\t\tprofiler.done({ message: \"retrieved languages configuration\" });\n\n\t\treturn result.data.languages;\n\t}\n\n\treturn {\n\t\tgetLanguages,\n\t};\n};\n"],"names":[],"mappings":";;AAiBa,MAAA,sBAAsB,CAClC,SACA,eACe;AACT,QAAA,SAAwB,MAAM,OAAO;AAAA,IAC1C;AAAA,IACA,gBAAgB,MAAM;AAAA;AAAA,EAAA,CACtB;AAGD,SAAO,aAAa,QAAQ,IAAI,OAAO,WAAU;AAChD,UAAM,OAAO;AACb,QAAI,CAAC,OAAO,QAAQ,IAAI,GAAG;AACpB,YAAA,QAAQ,MAAM,WAAW;AAC/B,aAAO,QAAQ,IAAI,IAAI,UAAU,KAAK;AAAA,IACtC;AAEM,WAAA;AAAA,EAAA,CACP;AAED,iBAAe,eAAY;AACpB,UAAA,WAAW,OAAO;AACxB,UAAM,SAAS,MAAM,OAAO,IAC3B,iBAAiB;AAGd,QAAA,CAAC,CAAC,GAAG,EAAE,SAAS,OAAO,MAAM,KAAK,CAAC,OAAO,KAAK,WAAW;AAC7D,sBAAgB,MAAM;AAChB,YAAA,IAAI,MAAM,4CAA4C;AAAA,IAC5D;AAED,aAAS,KAAK,EAAE,SAAS,oCAAqC,CAAA;AAE9D,WAAO,OAAO,KAAK;AAAA,EACpB;AAEO,SAAA;AAAA,IACN;AAAA,EAAA;AAEF;"}
@@ -9,6 +9,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
9
9
  const axios = require("axios");
10
10
  const cookies = require("../utils/cookies.cjs");
11
11
  const log = require("../utils/log.cjs");
12
+ const urls = require("../utils/urls.cjs");
12
13
  class WroomClient {
13
14
  /**
14
15
  * @param baseURL - The base URL of the Wroom app. ex: https://prismic.io
@@ -39,14 +40,25 @@ class WroomClient {
39
40
  getBaseURL() {
40
41
  return this.client.getUri();
41
42
  }
43
+ /** authenticated GET request on the wroom backend */
44
+ async get(repository, path) {
45
+ if (!this.loggedIn) {
46
+ await this.login();
47
+ }
48
+ let url = new URL(path, this.client.getUri()).toString();
49
+ if (repository) {
50
+ url = urls.getRepositoryUrl(url, repository);
51
+ }
52
+ return this.client.get(url);
53
+ }
42
54
  /** authenticated POST request on the wroom backend */
43
55
  async post(repository, path, data) {
44
56
  if (!this.loggedIn) {
45
57
  await this.login();
46
58
  }
47
- const url = new URL(path, this.client.getUri());
59
+ let url = new URL(path, this.client.getUri()).toString();
48
60
  if (repository) {
49
- url.hostname = `${repository}.${url.hostname}`;
61
+ url = urls.getRepositoryUrl(url, repository);
50
62
  }
51
63
  const response = await this.client.post(url.toString(), data, {
52
64
  params: {
@@ -1 +1 @@
1
- {"version":3,"file":"wroom.cjs","sources":["../../../src/clients/wroom.ts"],"sourcesContent":["import axios, { AxiosInstance, AxiosResponse } from \"axios\";\n\nimport { Credentials } from \"../types\";\n\nimport { extractCookie } from \"../utils/cookies\";\nimport { logHttpResponse, logger } from \"../utils/log\";\n\n/**\n * Client for interacting with the Wroom service to perform operations on\n * repositories.\n */\nexport class WroomClient {\n\tprivate readonly client: AxiosInstance;\n\tprivate loggedIn = false;\n\n\t/**\n\t * @param baseURL - The base URL of the Wroom app. ex: https://prismic.io\n\t * @param auth - Authentication credentials.\n\t */\n\tconstructor(\n\t\tbaseURL: string,\n\t\tprivate readonly auth: Credentials,\n\t) {\n\t\tthis.client = axios.create({\n\t\t\tbaseURL,\n\t\t\twithCredentials: true,\n\t\t\tvalidateStatus: () => true,\n\t\t\txsrfCookieName: \"X_XSRF\",\n\t\t\theaders: {\n\t\t\t\t\"User-Agent\": \"prismic-cli/prismic-e2e-tests-utils\",\n\t\t\t},\n\t\t});\n\n\t\t// cookies are not forwarded automatically in a non-browser env\n\t\tthis.client.interceptors.response.use((response) => {\n\t\t\tconst cookies = response.headers[\"set-cookie\"];\n\t\t\tif (cookies && extractCookie(cookies, \"SESSION\")) {\n\t\t\t\tthis.client.defaults.headers[\"Cookie\"] = cookies;\n\t\t\t}\n\n\t\t\treturn response;\n\t\t});\n\t}\n\n\tgetBaseURL(): string {\n\t\treturn this.client.getUri();\n\t}\n\n\t/** authenticated POST request on the wroom backend */\n\tasync post(\n\t\trepository: string | null,\n\t\tpath: string,\n\t\tdata?: unknown,\n\t): Promise<AxiosResponse> {\n\t\tif (!this.loggedIn) {\n\t\t\tawait this.login();\n\t\t}\n\n\t\tconst url = new URL(path, this.client.getUri());\n\t\tif (repository) {\n\t\t\turl.hostname = `${repository}.${url.hostname}`;\n\t\t}\n\t\tconst response = await this.client.post(url.toString(), data, {\n\t\t\tparams: {\n\t\t\t\t_: extractCookie(this.client.defaults.headers[\"Cookie\"], \"X_XSRF\"),\n\t\t\t},\n\t\t});\n\n\t\treturn response;\n\t}\n\n\t/**\n\t * Authenticate Wroom to call Wroom unofficial endpoints.\n\t *\n\t * @throws Error if the login to Wroom fails.\n\t */\n\tprivate async login(): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\t\tconst response = await this.client.post<string>(\"/authentication/signin\", {\n\t\t\temail: this.auth.email,\n\t\t\tpassword: this.auth.password,\n\t\t});\n\n\t\tif (response.status !== 200) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(\"Could not login to Prismic, check your credentials\");\n\t\t}\n\t\tthis.loggedIn = true;\n\t\tprofiler.done({ message: \"logged in to Prismic\" });\n\t}\n}\n"],"names":["cookies","extractCookie","logger","logHttpResponse"],"mappings":";;;;;;;;;;;MAWa,YAAW;AAAA;AAAA;AAAA;AAAA;AAAA,EAQvB,YACC,SACiB,MAAiB;AAAjB;AATD;AACT,oCAAW;AAQD,SAAI,OAAJ;AAEZ,SAAA,SAAS,MAAM,OAAO;AAAA,MAC1B;AAAA,MACA,iBAAiB;AAAA,MACjB,gBAAgB,MAAM;AAAA,MACtB,gBAAgB;AAAA,MAChB,SAAS;AAAA,QACR,cAAc;AAAA,MACd;AAAA,IAAA,CACD;AAGD,SAAK,OAAO,aAAa,SAAS,IAAI,CAAC,aAAY;AAC5C,YAAAA,YAAU,SAAS,QAAQ,YAAY;AAC7C,UAAIA,aAAWC,QAAAA,cAAcD,WAAS,SAAS,GAAG;AACjD,aAAK,OAAO,SAAS,QAAQ,QAAQ,IAAIA;AAAAA,MACzC;AAEM,aAAA;AAAA,IAAA,CACP;AAAA,EACF;AAAA,EAEA,aAAU;AACF,WAAA,KAAK,OAAO;EACpB;AAAA;AAAA,EAGA,MAAM,KACL,YACA,MACA,MAAc;AAEV,QAAA,CAAC,KAAK,UAAU;AACnB,YAAM,KAAK;IACX;AAED,UAAM,MAAM,IAAI,IAAI,MAAM,KAAK,OAAO,QAAQ;AAC9C,QAAI,YAAY;AACf,UAAI,WAAW,GAAG,UAAU,IAAI,IAAI,QAAQ;AAAA,IAC5C;AACK,UAAA,WAAW,MAAM,KAAK,OAAO,KAAK,IAAI,YAAY,MAAM;AAAA,MAC7D,QAAQ;AAAA,QACP,GAAGC,QAAAA,cAAc,KAAK,OAAO,SAAS,QAAQ,QAAQ,GAAG,QAAQ;AAAA,MACjE;AAAA,IAAA,CACD;AAEM,WAAA;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOQ,MAAM,QAAK;AACZ,UAAA,WAAWC,WAAO;AACxB,UAAM,WAAW,MAAM,KAAK,OAAO,KAAa,0BAA0B;AAAA,MACzE,OAAO,KAAK,KAAK;AAAA,MACjB,UAAU,KAAK,KAAK;AAAA,IAAA,CACpB;AAEG,QAAA,SAAS,WAAW,KAAK;AAC5BC,UAAA,gBAAgB,QAAQ;AAClB,YAAA,IAAI,MAAM,oDAAoD;AAAA,IACpE;AACD,SAAK,WAAW;AAChB,aAAS,KAAK,EAAE,SAAS,uBAAwB,CAAA;AAAA,EAClD;AACA;;"}
1
+ {"version":3,"file":"wroom.cjs","sources":["../../../src/clients/wroom.ts"],"sourcesContent":["import axios, { AxiosInstance, AxiosResponse } from \"axios\";\n\nimport { Credentials } from \"../types\";\n\nimport { extractCookie } from \"../utils/cookies\";\nimport { logHttpResponse, logger } from \"../utils/log\";\nimport { getRepositoryUrl } from \"../utils/urls\";\n\n/**\n * Client for interacting with the Wroom service to perform operations on\n * repositories.\n */\nexport class WroomClient {\n\tprivate readonly client: AxiosInstance;\n\tprivate loggedIn = false;\n\n\t/**\n\t * @param baseURL - The base URL of the Wroom app. ex: https://prismic.io\n\t * @param auth - Authentication credentials.\n\t */\n\tconstructor(\n\t\tbaseURL: string,\n\t\tprivate readonly auth: Credentials,\n\t) {\n\t\tthis.client = axios.create({\n\t\t\tbaseURL,\n\t\t\twithCredentials: true,\n\t\t\tvalidateStatus: () => true,\n\t\t\txsrfCookieName: \"X_XSRF\",\n\t\t\theaders: {\n\t\t\t\t\"User-Agent\": \"prismic-cli/prismic-e2e-tests-utils\",\n\t\t\t},\n\t\t});\n\n\t\t// cookies are not forwarded automatically in a non-browser env\n\t\tthis.client.interceptors.response.use((response) => {\n\t\t\tconst cookies = response.headers[\"set-cookie\"];\n\t\t\tif (cookies && extractCookie(cookies, \"SESSION\")) {\n\t\t\t\tthis.client.defaults.headers[\"Cookie\"] = cookies;\n\t\t\t}\n\n\t\t\treturn response;\n\t\t});\n\t}\n\n\tgetBaseURL(): string {\n\t\treturn this.client.getUri();\n\t}\n\n\t/** authenticated GET request on the wroom backend */\n\tasync get(repository: string | null, path: string): Promise<AxiosResponse> {\n\t\tif (!this.loggedIn) {\n\t\t\tawait this.login();\n\t\t}\n\n\t\tlet url = new URL(path, this.client.getUri()).toString();\n\t\tif (repository) {\n\t\t\turl = getRepositoryUrl(url, repository);\n\t\t}\n\n\t\treturn this.client.get(url);\n\t}\n\n\t/** authenticated POST request on the wroom backend */\n\tasync post(\n\t\trepository: string | null,\n\t\tpath: string,\n\t\tdata?: unknown,\n\t): Promise<AxiosResponse> {\n\t\tif (!this.loggedIn) {\n\t\t\tawait this.login();\n\t\t}\n\n\t\tlet url = new URL(path, this.client.getUri()).toString();\n\t\tif (repository) {\n\t\t\turl = getRepositoryUrl(url, repository);\n\t\t}\n\t\tconst response = await this.client.post(url.toString(), data, {\n\t\t\tparams: {\n\t\t\t\t_: extractCookie(this.client.defaults.headers[\"Cookie\"], \"X_XSRF\"),\n\t\t\t},\n\t\t});\n\n\t\treturn response;\n\t}\n\n\t/**\n\t * Authenticate Wroom to call Wroom unofficial endpoints.\n\t *\n\t * @throws Error if the login to Wroom fails.\n\t */\n\tprivate async login(): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\t\tconst response = await this.client.post<string>(\"/authentication/signin\", {\n\t\t\temail: this.auth.email,\n\t\t\tpassword: this.auth.password,\n\t\t});\n\n\t\tif (response.status !== 200) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(\"Could not login to Prismic, check your credentials\");\n\t\t}\n\t\tthis.loggedIn = true;\n\t\tprofiler.done({ message: \"logged in to Prismic\" });\n\t}\n}\n"],"names":["cookies","extractCookie","getRepositoryUrl","logger","logHttpResponse"],"mappings":";;;;;;;;;;;;MAYa,YAAW;AAAA;AAAA;AAAA;AAAA;AAAA,EAQvB,YACC,SACiB,MAAiB;AAAjB;AATD;AACT,oCAAW;AAQD,SAAI,OAAJ;AAEZ,SAAA,SAAS,MAAM,OAAO;AAAA,MAC1B;AAAA,MACA,iBAAiB;AAAA,MACjB,gBAAgB,M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;AAAA,MACzE,OAAO,KAAK,KAAK;AAAA,MACjB,UAAU,KAAK,KAAK;AAAA,IAAA,CACpB;AAEG,QAAA,SAAS,WAAW,KAAK;AAC5BC,UAAA,gBAAgB,QAAQ;AAClB,YAAA,IAAI,MAAM,oDAAoD;AAAA,IACpE;AACD,SAAK,WAAW;AAChB,aAAS,KAAK,EAAE,SAAS,uBAAwB,CAAA;AAAA,EAClD;AACA;;"}
@@ -14,6 +14,8 @@ export declare class WroomClient {
14
14
  */
15
15
  constructor(baseURL: string, auth: Credentials);
16
16
  getBaseURL(): string;
17
+ /** authenticated GET request on the wroom backend */
18
+ get(repository: string | null, path: string): Promise<AxiosResponse>;
17
19
  /** authenticated POST request on the wroom backend */
18
20
  post(repository: string | null, path: string, data?: unknown): Promise<AxiosResponse>;
19
21
  /**
@@ -7,6 +7,7 @@ var __publicField = (obj, key, value) => {
7
7
  import axios from "axios";
8
8
  import { extractCookie } from "../utils/cookies.js";
9
9
  import { logger, logHttpResponse } from "../utils/log.js";
10
+ import { getRepositoryUrl } from "../utils/urls.js";
10
11
  class WroomClient {
11
12
  /**
12
13
  * @param baseURL - The base URL of the Wroom app. ex: https://prismic.io
@@ -37,14 +38,25 @@ class WroomClient {
37
38
  getBaseURL() {
38
39
  return this.client.getUri();
39
40
  }
41
+ /** authenticated GET request on the wroom backend */
42
+ async get(repository, path) {
43
+ if (!this.loggedIn) {
44
+ await this.login();
45
+ }
46
+ let url = new URL(path, this.client.getUri()).toString();
47
+ if (repository) {
48
+ url = getRepositoryUrl(url, repository);
49
+ }
50
+ return this.client.get(url);
51
+ }
40
52
  /** authenticated POST request on the wroom backend */
41
53
  async post(repository, path, data) {
42
54
  if (!this.loggedIn) {
43
55
  await this.login();
44
56
  }
45
- const url = new URL(path, this.client.getUri());
57
+ let url = new URL(path, this.client.getUri()).toString();
46
58
  if (repository) {
47
- url.hostname = `${repository}.${url.hostname}`;
59
+ url = getRepositoryUrl(url, repository);
48
60
  }
49
61
  const response = await this.client.post(url.toString(), data, {
50
62
  params: {
@@ -1 +1 @@
1
- {"version":3,"file":"wroom.js","sources":["../../../src/clients/wroom.ts"],"sourcesContent":["import axios, { AxiosInstance, AxiosResponse } from \"axios\";\n\nimport { Credentials } from \"../types\";\n\nimport { extractCookie } from \"../utils/cookies\";\nimport { logHttpResponse, logger } from \"../utils/log\";\n\n/**\n * Client for interacting with the Wroom service to perform operations on\n * repositories.\n */\nexport class WroomClient {\n\tprivate readonly client: AxiosInstance;\n\tprivate loggedIn = false;\n\n\t/**\n\t * @param baseURL - The base URL of the Wroom app. ex: https://prismic.io\n\t * @param auth - Authentication credentials.\n\t */\n\tconstructor(\n\t\tbaseURL: string,\n\t\tprivate readonly auth: Credentials,\n\t) {\n\t\tthis.client = axios.create({\n\t\t\tbaseURL,\n\t\t\twithCredentials: true,\n\t\t\tvalidateStatus: () => true,\n\t\t\txsrfCookieName: \"X_XSRF\",\n\t\t\theaders: {\n\t\t\t\t\"User-Agent\": \"prismic-cli/prismic-e2e-tests-utils\",\n\t\t\t},\n\t\t});\n\n\t\t// cookies are not forwarded automatically in a non-browser env\n\t\tthis.client.interceptors.response.use((response) => {\n\t\t\tconst cookies = response.headers[\"set-cookie\"];\n\t\t\tif (cookies && extractCookie(cookies, \"SESSION\")) {\n\t\t\t\tthis.client.defaults.headers[\"Cookie\"] = cookies;\n\t\t\t}\n\n\t\t\treturn response;\n\t\t});\n\t}\n\n\tgetBaseURL(): string {\n\t\treturn this.client.getUri();\n\t}\n\n\t/** authenticated POST request on the wroom backend */\n\tasync post(\n\t\trepository: string | null,\n\t\tpath: string,\n\t\tdata?: unknown,\n\t): Promise<AxiosResponse> {\n\t\tif (!this.loggedIn) {\n\t\t\tawait this.login();\n\t\t}\n\n\t\tconst url = new URL(path, this.client.getUri());\n\t\tif (repository) {\n\t\t\turl.hostname = `${repository}.${url.hostname}`;\n\t\t}\n\t\tconst response = await this.client.post(url.toString(), data, {\n\t\t\tparams: {\n\t\t\t\t_: extractCookie(this.client.defaults.headers[\"Cookie\"], \"X_XSRF\"),\n\t\t\t},\n\t\t});\n\n\t\treturn response;\n\t}\n\n\t/**\n\t * Authenticate Wroom to call Wroom unofficial endpoints.\n\t *\n\t * @throws Error if the login to Wroom fails.\n\t */\n\tprivate async login(): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\t\tconst response = await this.client.post<string>(\"/authentication/signin\", {\n\t\t\temail: this.auth.email,\n\t\t\tpassword: this.auth.password,\n\t\t});\n\n\t\tif (response.status !== 200) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(\"Could not login to Prismic, check your credentials\");\n\t\t}\n\t\tthis.loggedIn = true;\n\t\tprofiler.done({ message: \"logged in to Prismic\" });\n\t}\n}\n"],"names":[],"mappings":";;;;;;;;;MAWa,YAAW;AAAA;AAAA;AAAA;AAAA;AAAA,EAQvB,YACC,SACiB,MAAiB;AAAjB;AATD;AACT,oCAAW;AAQD,SAAI,OAAJ;AAEZ,SAAA,SAAS,MAAM,OAAO;AAAA,MAC1B;AAAA,MACA,iBAAiB;AAAA,MACjB,gBAAgB,MAAM;AAAA,MACtB,gBAAgB;AAAA,MAChB,SAAS;AAAA,QACR,cAAc;AAAA,MACd;AAAA,IAAA,CACD;AAGD,SAAK,OAAO,aAAa,SAAS,IAAI,CAAC,aAAY;AAC5C,YAAA,UAAU,SAAS,QAAQ,YAAY;AAC7C,UAAI,WAAW,cAAc,SAAS,SAAS,GAAG;AACjD,aAAK,OAAO,SAAS,QAAQ,QAAQ,IAAI;AAAA,MACzC;AAEM,aAAA;AAAA,IAAA,CACP;AAAA,EACF;AAAA,EAEA,aAAU;AACF,WAAA,KAAK,OAAO;EACpB;AAAA;AAAA,EAGA,MAAM,KACL,YACA,MACA,MAAc;AAEV,QAAA,CAAC,KAAK,UAAU;AACnB,YAAM,KAAK;IACX;AAED,UAAM,MAAM,IAAI,IAAI,MAAM,KAAK,OAAO,QAAQ;AAC9C,QAAI,YAAY;AACf,UAAI,WAAW,GAAG,UAAU,IAAI,IAAI,QAAQ;AAAA,IAC5C;AACK,UAAA,WAAW,MAAM,KAAK,OAAO,KAAK,IAAI,YAAY,MAAM;AAAA,MAC7D,QAAQ;AAAA,QACP,GAAG,cAAc,KAAK,OAAO,SAAS,QAAQ,QAAQ,GAAG,QAAQ;AAAA,MACjE;AAAA,IAAA,CACD;AAEM,WAAA;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOQ,MAAM,QAAK;AACZ,UAAA,WAAW,OAAO;AACxB,UAAM,WAAW,MAAM,KAAK,OAAO,KAAa,0BAA0B;AAAA,MACzE,OAAO,KAAK,KAAK;AAAA,MACjB,UAAU,KAAK,KAAK;AAAA,IAAA,CACpB;AAEG,QAAA,SAAS,WAAW,KAAK;AAC5B,sBAAgB,QAAQ;AAClB,YAAA,IAAI,MAAM,oDAAoD;AAAA,IACpE;AACD,SAAK,WAAW;AAChB,aAAS,KAAK,EAAE,SAAS,uBAAwB,CAAA;AAAA,EAClD;AACA;"}
1
+ {"version":3,"file":"wroom.js","sources":["../../../src/clients/wroom.ts"],"sourcesContent":["import axios, { AxiosInstance, AxiosResponse } from \"axios\";\n\nimport { Credentials } from \"../types\";\n\nimport { extractCookie } from \"../utils/cookies\";\nimport { logHttpResponse, logger } from \"../utils/log\";\nimport { getRepositoryUrl } from \"../utils/urls\";\n\n/**\n * Client for interacting with the Wroom service to perform operations on\n * repositories.\n */\nexport class WroomClient {\n\tprivate readonly client: AxiosInstance;\n\tprivate loggedIn = false;\n\n\t/**\n\t * @param baseURL - The base URL of the Wroom app. ex: https://prismic.io\n\t * @param auth - Authentication credentials.\n\t */\n\tconstructor(\n\t\tbaseURL: string,\n\t\tprivate readonly auth: Credentials,\n\t) {\n\t\tthis.client = axios.create({\n\t\t\tbaseURL,\n\t\t\twithCredentials: true,\n\t\t\tvalidateStatus: () => true,\n\t\t\txsrfCookieName: \"X_XSRF\",\n\t\t\theaders: {\n\t\t\t\t\"User-Agent\": \"prismic-cli/prismic-e2e-tests-utils\",\n\t\t\t},\n\t\t});\n\n\t\t// cookies are not forwarded automatically in a non-browser env\n\t\tthis.client.interceptors.response.use((response) => {\n\t\t\tconst cookies = response.headers[\"set-cookie\"];\n\t\t\tif (cookies && extractCookie(cookies, \"SESSION\")) {\n\t\t\t\tthis.client.defaults.headers[\"Cookie\"] = cookies;\n\t\t\t}\n\n\t\t\treturn response;\n\t\t});\n\t}\n\n\tgetBaseURL(): string {\n\t\treturn this.client.getUri();\n\t}\n\n\t/** authenticated GET request on the wroom backend */\n\tasync get(repository: string | null, path: string): Promise<AxiosResponse> {\n\t\tif (!this.loggedIn) {\n\t\t\tawait this.login();\n\t\t}\n\n\t\tlet url = new URL(path, this.client.getUri()).toString();\n\t\tif (repository) {\n\t\t\turl = getRepositoryUrl(url, repository);\n\t\t}\n\n\t\treturn this.client.get(url);\n\t}\n\n\t/** authenticated POST request on the wroom backend */\n\tasync post(\n\t\trepository: string | null,\n\t\tpath: string,\n\t\tdata?: unknown,\n\t): Promise<AxiosResponse> {\n\t\tif (!this.loggedIn) {\n\t\t\tawait this.login();\n\t\t}\n\n\t\tlet url = new URL(path, this.client.getUri()).toString();\n\t\tif (repository) {\n\t\t\turl = getRepositoryUrl(url, repository);\n\t\t}\n\t\tconst response = await this.client.post(url.toString(), data, {\n\t\t\tparams: {\n\t\t\t\t_: extractCookie(this.client.defaults.headers[\"Cookie\"], \"X_XSRF\"),\n\t\t\t},\n\t\t});\n\n\t\treturn response;\n\t}\n\n\t/**\n\t * Authenticate Wroom to call Wroom unofficial endpoints.\n\t *\n\t * @throws Error if the login to Wroom fails.\n\t */\n\tprivate async login(): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\t\tconst response = await this.client.post<string>(\"/authentication/signin\", {\n\t\t\temail: this.auth.email,\n\t\t\tpassword: this.auth.password,\n\t\t});\n\n\t\tif (response.status !== 200) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(\"Could not login to Prismic, check your credentials\");\n\t\t}\n\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AAA,QACP,GAAG,cAAc,KAAK,OAAO,SAAS,QAAQ,QAAQ,GAAG,QAAQ;AAAA,MACjE;AAAA,IAAA,CACD;AAEM,WAAA;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOQ,MAAM,QAAK;AACZ,UAAA,WAAW,OAAO;AACxB,UAAM,WAAW,MAAM,KAAK,OAAO,KAAa,0BAA0B;AAAA,MACzE,OAAO,KAAK,KAAK;AAAA,MACjB,UAAU,KAAK,KAAK;AAAA,IAAA,CACpB;AAEG,QAAA,SAAS,WAAW,KAAK;AAC5B,sBAAgB,QAAQ;AAClB,YAAA,IAAI,MAAM,oDAAoD;AAAA,IACpE;AACD,SAAK,WAAW;AAChB,aAAS,KAAK,EAAE,SAAS,uBAAwB,CAAA;AAAA,EAClD;AACA;"}
@@ -7,21 +7,23 @@ var __publicField = (obj, key, value) => {
7
7
  };
8
8
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
9
9
  const authenticationApi = require("../clients/authenticationApi.cjs");
10
+ const coreApi = require("../clients/coreApi.cjs");
10
11
  const customTypesApi = require("../clients/customTypesApi.cjs");
11
12
  const wroom = require("../clients/wroom.cjs");
12
13
  const envVariableManager = require("../utils/envVariableManager.cjs");
13
14
  const log = require("../utils/log.cjs");
14
15
  const random = require("../utils/random.cjs");
16
+ const urls = require("../utils/urls.cjs");
15
17
  const repository = require("./repository.cjs");
16
18
  const createRepositoriesManager = (urlConfig, auth) => {
17
19
  return new RepositoriesManager(urlConfig, auth);
18
20
  };
19
21
  class RepositoriesManager {
20
22
  /**
21
- * @param prismicUrl - The base URL of the Wroom app.
23
+ * @param urlConfig - The base URL of the Wroom app.
22
24
  * @param credentials - Authentication credentials (email and password)
23
25
  */
24
- constructor(urls, credentials) {
26
+ constructor(urlConfig, credentials) {
25
27
  __publicField(this, "credentials");
26
28
  __publicField(this, "urls");
27
29
  __publicField(this, "wroomClient");
@@ -32,10 +34,10 @@ class RepositoriesManager {
32
34
  */
33
35
  __publicField(this, "repositories", new envVariableManager.EnvVariableManager("_PRISMIC_E2E_TESTS_REPOS"));
34
36
  this.credentials = credentials;
35
- if (typeof urls === "string") {
36
- urls = this.inferUrls(urls);
37
+ if (typeof urlConfig === "string") {
38
+ urlConfig = this.inferUrls(urlConfig);
37
39
  }
38
- this.urls = urls;
40
+ this.urls = urlConfig;
39
41
  this.wroomClient = new wroom.WroomClient(this.urls.baseURL, this.credentials);
40
42
  this.authClient = authenticationApi.createAuthenticationApiClient(this.urls.authenticationApi, this.credentials);
41
43
  }
@@ -129,7 +131,9 @@ class RepositoriesManager {
129
131
  */
130
132
  getRepositoryManager(name) {
131
133
  const customTypeClient = customTypesApi.createCustomTypesApiClient(this.urls.customTypesApi, name, this.authClient);
132
- return new repository.RepositoryManager(name, this.wroomClient, customTypeClient);
134
+ const coreApiUrl = urls.getRepositoryUrl(this.urls.baseURL, name);
135
+ const coreApiClient = coreApi.createCoreApiClient(coreApiUrl, this.authClient);
136
+ return new repository.RepositoryManager(name, coreApiClient, this.wroomClient, customTypeClient);
133
137
  }
134
138
  }
135
139
  exports.RepositoriesManager = RepositoriesManager;
@@ -1 +1 @@
1
- {"version":3,"file":"repositories.cjs","sources":["../../../src/managers/repositories.ts"],"sourcesContent":["import { Credentials } from \"../types\";\n\nimport {\n\tAuthenticationClient,\n\tcreateAuthenticationApiClient,\n} from \"../clients/authenticationApi\";\nimport { createCustomTypesApiClient } from \"../clients/customTypesApi\";\nimport { WroomClient } from \"../clients/wroom\";\nimport { EnvVariableManager } from \"../utils/envVariableManager\";\nimport { logHttpResponse, logger } from \"../utils/log\";\nimport { randomString } from \"../utils/random\";\n\nimport { RepositoryConfig, RepositoryManager } from \"./repository\";\n\n/**\n * Factory method to create a RepositoriesManager to manage repositories test\n * data.\n *\n * @param urlConfig - If provided as an object:\n *\n * - {@link UrlConfig.baseURL}: The Prismic base URL.\n * - {@link UrlConfig.customTypesApi}: The base URL for the Custom Types API.\n * - {@link UrlConfig.authenticationApi}: The base URL for the Authentication API.\n *\n * If provided as a string: It is treated as the base URL for Prismic. Other\n * URLs (Custom Types API, Auth API) will be derived from it.\n * @param auth - The authentication credentials\n *\n * @returns An instance of {@link RepositoriesManager} to manage test data.\n */\nexport const createRepositoriesManager = (\n\turlConfig: UrlConfig | string,\n\tauth: Credentials,\n): RepositoriesManager => {\n\treturn new RepositoriesManager(urlConfig, auth);\n};\n\n/** Utility object to manage Prismic test data */\nexport class RepositoriesManager {\n\tprivate readonly urls: UrlConfig;\n\tprivate readonly wroomClient: WroomClient;\n\tprivate readonly authClient: AuthenticationClient;\n\t/**\n\t * helper to keep track of repositories across independent test phases (setup,\n\t * teardown)\n\t */\n\tprivate readonly repositories = new EnvVariableManager(\n\t\t\"_PRISMIC_E2E_TESTS_REPOS\",\n\t);\n\n\t/**\n\t * @param prismicUrl - The base URL of the Wroom app.\n\t * @param credentials - Authentication credentials (email and password)\n\t */\n\tconstructor(\n\t\turls: UrlConfig | string,\n\t\tprivate readonly credentials: Credentials,\n\t) {\n\t\tif (typeof urls === \"string\") {\n\t\t\t// When a string is provided, treat it as the 'base' property\n\t\t\turls = this.inferUrls(urls);\n\t\t}\n\t\tthis.urls = urls;\n\t\tthis.wroomClient = new WroomClient(this.urls.baseURL, this.credentials);\n\t\tthis.authClient = createAuthenticationApiClient(\n\t\t\tthis.urls.authenticationApi,\n\t\t\tthis.credentials,\n\t\t);\n\t}\n\n\t/**\n\t * If the baseURL is prismic.io, assume the other services have the format\n\t * <name>.prismic.io\n\t */\n\tprivate inferUrls(baseURL: string): UrlConfig {\n\t\tconst url = new URL(baseURL);\n\t\tconst protocol = url.protocol.slice(0, -1); // Remove the trailing colon from the protocol\n\n\t\treturn {\n\t\t\tbaseURL: baseURL,\n\t\t\tauthenticationApi: `${protocol}://auth.${url.hostname}`,\n\t\t\tcustomTypesApi: `${protocol}://customtypes.${url.hostname}`,\n\t\t};\n\t}\n\n\t/**\n\t * Generate a user token from the authentication service.\n\t *\n\t * @returns a JWT token\n\t */\n\tasync getUserApiToken(): Promise<string> {\n\t\treturn this.authClient.getToken();\n\t}\n\n\t/**\n\t * Create a new repository in Wroom.\n\t *\n\t * @param repositoryName - The name of the repository.\n\t *\n\t * @throws Error if the repository creation fails.\n\t */\n\tasync createRepository(\n\t\tconfig: {\n\t\t\tprefix?: string;\n\t\t} & RepositoryConfig = {},\n\t): Promise<RepositoryManager> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst { prefix = \"e2e-tests\" } = config;\n\t\tconst repositoryName = `${prefix}-${randomString()}`;\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tnull,\n\t\t\t\"authentication/newrepository\",\n\t\t\t{\n\t\t\t\tdomain: repositoryName,\n\t\t\t\tframework: \"vue\",\n\t\t\t\tplan: \"personal\",\n\t\t\t\tisAnnual: \"false\",\n\t\t\t\trole: \"developer\",\n\t\t\t},\n\t\t);\n\t\tif (response.status !== 200) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not create repository ${repositoryName}`);\n\t\t}\n\t\tthis.repositories.add(repositoryName);\n\n\t\tprofiler.done({ message: `created repository '${repositoryName}'` });\n\n\t\tconst repository = this.getRepositoryManager(repositoryName);\n\t\tawait repository.configure(config);\n\n\t\treturn repository;\n\t}\n\n\t/**\n\t * Delete a repository from Wroom.\n\t *\n\t * @param repository - The name of the repository.\n\t *\n\t * @throws Error if the repository deletion fails.\n\t */\n\tasync deleteRepository(repository: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst post = async () =>\n\t\t\tthis.wroomClient.post(repository, \"app/settings/delete\", {\n\t\t\t\tconfirm: repository,\n\t\t\t\tpassword: this.credentials.password,\n\t\t\t});\n\t\tconst response = await post();\n\t\tif (response.status !== 200) {\n\t\t\t// sometimes the deletion returns 500 but actually succeeds\n\t\t\t// we run the query again and check the repo is actually deleted (404)\n\t\t\tconst retry = await post();\n\t\t\tif (retry.status !== 404) {\n\t\t\t\tlogHttpResponse(response);\n\t\t\t\tthrow new Error(`Could not delete repository ${repository}`);\n\t\t\t}\n\t\t}\n\t\tthis.repositories.remove(repository);\n\t\tprofiler.done({ message: `deleted repository '${repository}'` });\n\t}\n\n\t/**\n\t * Cleanup test data. For example, it deletes repositories created using\n\t * createRepository()\n\t */\n\tasync tearDown(): Promise<void> {\n\t\tconst repositories = this.repositories.getAll();\n\t\tfor (const repository of repositories) {\n\t\t\tawait this.deleteRepository(repository);\n\t\t}\n\t}\n\n\t/**\n\t * Return a repository utilities object for a repository.\n\t *\n\t * @param name - The name of the repository.\n\t */\n\tgetRepositoryManager(name: string): RepositoryManager {\n\t\tconst customTypeClient = createCustomTypesApiClient(\n\t\t\tthis.urls.customTypesApi,\n\t\t\tname,\n\t\t\tthis.authClient,\n\t\t);\n\n\t\treturn new RepositoryManager(name, this.wroomClient, customTypeClient);\n\t}\n}\n\nexport type UrlConfig = {\n\t/** Prismic base url */\n\tbaseURL: string;\n\t/** Custom types api base url */\n\tcustomTypesApi: string;\n\t/** Auth service api base url */\n\tauthenticationApi: string;\n};\n"],"names":["EnvVariableManager","WroomClient","createAuthenticationApiClient","logger","randomString","logHttpResponse","repository","createCustomTypesApiClient","RepositoryManager"],"mappings":";;;;;;;;;;;;;;;AA8Ba,MAAA,4BAA4B,CACxC,WACA,SACwB;AACjB,SAAA,IAAI,oBAAoB,WAAW,IAAI;AAC/C;MAGa,oBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA,EAgB/B,YACC,MACiB,aAAwB;AAAxB;AAjBD;AACA;AACA;AAKA;AAAA;AAAA;AAAA;AAAA,wCAAe,IAAIA,sCACnC,0BAA0B;AAST,SAAW,cAAX;AAEb,QAAA,OAAO,SAAS,UAAU;AAEtB,aAAA,KAAK,UAAU,IAAI;AAAA,IAC1B;AACD,SAAK,OAAO;AACZ,SAAK,cAAc,IAAIC,kBAAY,KAAK,KAAK,SAAS,KAAK,WAAW;AACtE,SAAK,aAAaC,kBAAAA,8BACjB,KAAK,KAAK,mBACV,KAAK,WAAW;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,KAAK,YAAY;AAAA,IAAA,CAC3B;AACI,UAAA,WAAW,MAAM;AACnB,QAAA,SAAS,WAAW,KAAK;AAGtB,YAAA,QAAQ,MAAM;AAChB,UAAA,MAAM,WAAW,KAAK;AACzBD,YAAA,gBAAgB,QAAQ;AACxB,cAAM,IAAI,MAAM,+BAA+BC,WAAU,EAAE;AAAA,MAC3D;AAAA,IACD;AACI,SAAA,aAAa,OAAOA,WAAU;AACnC,aAAS,KAAK,EAAE,SAAS,uBAAuBA,WAAU,KAAK;AAAA,EAChE;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,WAAQ;AACP,UAAA,eAAe,KAAK,aAAa;AACvC,eAAWA,eAAc,cAAc;AAChC,YAAA,KAAK,iBAAiBA,WAAU;AAAA,IACtC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,qBAAqB,MAAY;AAChC,UAAM,mBAAmBC,eACxB,2BAAA,KAAK,KAAK,gBACV,MACA,KAAK,UAAU;AAGhB,WAAO,IAAIC,WAAA,kBAAkB,MAAM,KAAK,aAAa,gBAAgB;AAAA,EACtE;AACA;;;"}
1
+ {"version":3,"file":"repositories.cjs","sources":["../../../src/managers/repositories.ts"],"sourcesContent":["import { Credentials } from \"../types\";\n\nimport {\n\tAuthenticationClient,\n\tcreateAuthenticationApiClient,\n} from \"../clients/authenticationApi\";\nimport { createCoreApiClient } from \"../clients/coreApi\";\nimport { createCustomTypesApiClient } from \"../clients/customTypesApi\";\nimport { WroomClient } from \"../clients/wroom\";\nimport { EnvVariableManager } from \"../utils/envVariableManager\";\nimport { logHttpResponse, logger } from \"../utils/log\";\nimport { randomString } from \"../utils/random\";\nimport { getRepositoryUrl } from \"../utils/urls\";\n\nimport { RepositoryConfig, RepositoryManager } from \"./repository\";\n\n/**\n * Factory method to create a RepositoriesManager to manage repositories test\n * data.\n *\n * @param urlConfig - If provided as an object:\n *\n * - {@link UrlConfig.baseURL}: The Prismic base URL.\n * - {@link UrlConfig.customTypesApi}: The base URL for the Custom Types API.\n * - {@link UrlConfig.authenticationApi}: The base URL for the Authentication API.\n *\n * If provided as a string: It is treated as the base URL for Prismic. Other\n * URLs (Custom Types API, Auth API) will be derived from it.\n * @param auth - The authentication credentials\n *\n * @returns An instance of {@link RepositoriesManager} to manage test data.\n */\nexport const createRepositoriesManager = (\n\turlConfig: UrlConfig | string,\n\tauth: Credentials,\n): RepositoriesManager => {\n\treturn new RepositoriesManager(urlConfig, auth);\n};\n\n/** Utility object to manage Prismic test data */\nexport class RepositoriesManager {\n\tprivate readonly urls: UrlConfig;\n\tprivate readonly wroomClient: WroomClient;\n\tprivate readonly authClient: AuthenticationClient;\n\t/**\n\t * helper to keep track of repositories across independent test phases (setup,\n\t * teardown)\n\t */\n\tprivate readonly repositories = new EnvVariableManager(\n\t\t\"_PRISMIC_E2E_TESTS_REPOS\",\n\t);\n\n\t/**\n\t * @param urlConfig - The base URL of the Wroom app.\n\t * @param credentials - Authentication credentials (email and password)\n\t */\n\tconstructor(\n\t\turlConfig: UrlConfig | string,\n\t\tprivate readonly credentials: Credentials,\n\t) {\n\t\tif (typeof urlConfig === \"string\") {\n\t\t\t// When a string is provided, treat it as the 'base' property\n\t\t\turlConfig = this.inferUrls(urlConfig);\n\t\t}\n\t\tthis.urls = urlConfig;\n\t\tthis.wroomClient = new WroomClient(this.urls.baseURL, this.credentials);\n\t\tthis.authClient = createAuthenticationApiClient(\n\t\t\tthis.urls.authenticationApi,\n\t\t\tthis.credentials,\n\t\t);\n\t}\n\n\t/**\n\t * If the baseURL is prismic.io, assume the other services have the format\n\t * <name>.prismic.io\n\t */\n\tprivate inferUrls(baseURL: string): UrlConfig {\n\t\tconst url = new URL(baseURL);\n\t\tconst protocol = url.protocol.slice(0, -1); // Remove the trailing colon from the protocol\n\n\t\treturn {\n\t\t\tbaseURL: baseURL,\n\t\t\tauthenticationApi: `${protocol}://auth.${url.hostname}`,\n\t\t\tcustomTypesApi: `${protocol}://customtypes.${url.hostname}`,\n\t\t};\n\t}\n\n\t/**\n\t * Generate a user token from the authentication service.\n\t *\n\t * @returns a JWT token\n\t */\n\tasync getUserApiToken(): Promise<string> {\n\t\treturn this.authClient.getToken();\n\t}\n\n\t/**\n\t * Create a new repository in Wroom.\n\t *\n\t * @param repositoryName - The name of the repository.\n\t *\n\t * @throws Error if the repository creation fails.\n\t */\n\tasync createRepository(\n\t\tconfig: {\n\t\t\tprefix?: string;\n\t\t} & RepositoryConfig = {},\n\t): Promise<RepositoryManager> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst { prefix = \"e2e-tests\" } = config;\n\t\tconst repositoryName = `${prefix}-${randomString()}`;\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tnull,\n\t\t\t\"authentication/newrepository\",\n\t\t\t{\n\t\t\t\tdomain: repositoryName,\n\t\t\t\tframework: \"vue\",\n\t\t\t\tplan: \"personal\",\n\t\t\t\tisAnnual: \"false\",\n\t\t\t\trole: \"developer\",\n\t\t\t},\n\t\t);\n\t\tif (response.status !== 200) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not create repository ${repositoryName}`);\n\t\t}\n\t\tthis.repositories.add(repositoryName);\n\n\t\tprofiler.done({ message: `created repository '${repositoryName}'` });\n\n\t\tconst repository = this.getRepositoryManager(repositoryName);\n\t\tawait repository.configure(config);\n\n\t\treturn repository;\n\t}\n\n\t/**\n\t * Delete a repository from Wroom.\n\t *\n\t * @param repository - The name of the repository.\n\t *\n\t * @throws Error if the repository deletion fails.\n\t */\n\tasync deleteRepository(repository: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst post = async () =>\n\t\t\tthis.wroomClient.post(repository, \"app/settings/delete\", {\n\t\t\t\tconfirm: repository,\n\t\t\t\tpassword: this.credentials.password,\n\t\t\t});\n\t\tconst response = await post();\n\t\tif (response.status !== 200) {\n\t\t\t// sometimes the deletion returns 500 but actually succeeds\n\t\t\t// we run the query again and check the repo is actually deleted (404)\n\t\t\tconst retry = await post();\n\t\t\tif (retry.status !== 404) {\n\t\t\t\tlogHttpResponse(response);\n\t\t\t\tthrow new Error(`Could not delete repository ${repository}`);\n\t\t\t}\n\t\t}\n\t\tthis.repositories.remove(repository);\n\t\tprofiler.done({ message: `deleted repository '${repository}'` });\n\t}\n\n\t/**\n\t * Cleanup test data. For example, it deletes repositories created using\n\t * createRepository()\n\t */\n\tasync tearDown(): Promise<void> {\n\t\tconst repositories = this.repositories.getAll();\n\t\tfor (const repository of repositories) {\n\t\t\tawait this.deleteRepository(repository);\n\t\t}\n\t}\n\n\t/**\n\t * Return a repository utilities object for a repository.\n\t *\n\t * @param name - The name of the repository.\n\t */\n\tgetRepositoryManager(name: string): RepositoryManager {\n\t\tconst customTypeClient = createCustomTypesApiClient(\n\t\t\tthis.urls.customTypesApi,\n\t\t\tname,\n\t\t\tthis.authClient,\n\t\t);\n\t\tconst coreApiUrl = getRepositoryUrl(this.urls.baseURL, name);\n\t\tconst coreApiClient = createCoreApiClient(coreApiUrl, this.authClient);\n\n\t\treturn new RepositoryManager(\n\t\t\tname,\n\t\t\tcoreApiClient,\n\t\t\tthis.wroomClient,\n\t\t\tcustomTypeClient,\n\t\t);\n\t}\n}\n\nexport type UrlConfig = {\n\t/** Prismic base url */\n\tbaseURL: string;\n\t/** Custom types api base url */\n\tcustomTypesApi: string;\n\t/** Auth service api base url */\n\tauthenticationApi: string;\n};\n"],"names":["EnvVariableManager","WroomClient","createAuthenticationApiClient","logger","randomString","logHttpResponse","repository","createCustomTypesApiClient","getRepositoryUrl","createCoreApiClient","RepositoryManager"],"mappings":";;;;;;;;;;;;;;;;;AAgCa,MAAA,4BAA4B,CACxC,WACA,SACwB;AACjB,SAAA,IAAI,oBAAoB,WAAW,IAAI;AAC/C;MAGa,oBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA,EAgB/B,YACC,WACiB,aAAwB;AAAxB;AAjBD;AACA;AACA;AAKA;AAAA;AAAA;AAAA;AAAA,wCAAe,IAAIA,sCACnC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D,UAAAA,YAAW,UAAU,MAAM;AAE1B,WAAAA;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAM,iBAAiBA,aAAkB;AAClC,UAAA,WAAWH,WAAO;AAExB,UAAM,OAAO,YACZ,KAAK,YAAY,KAAKG,aAAY,uBAAuB;AAAA,MACxD,SAASA;AAAA,MACT,UAAU,KAAK,YAAY;AAAA,IAAA,CAC3B;AACI,UAAA,WAAW,MAAM;AACnB,QAAA,SAAS,WAAW,KAAK;AAGtB,YAAA,QAAQ,MAAM;AAChB,UAAA,MAAM,WAAW,KAAK;AACzBD,YAAA,gBAAgB,QAAQ;AACxB,cAAM,IAAI,MAAM,+BAA+BC,WAAU,EAAE;AAAA,MAC3D;AAAA,IACD;AACI,SAAA,aAAa,OAAOA,WAAU;AACnC,aAAS,KAAK,EAAE,SAAS,uBAAuBA,WAAU,KAAK;AAAA,EAChE;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,WAAQ;AACP,UAAA,eAAe,KAAK,aAAa;AACvC,eAAWA,eAAc,cAAc;AAChC,YAAA,KAAK,iBAAiBA,WAAU;AAAA,IACtC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,qBAAqB,MAAY;AAChC,UAAM,mBAAmBC,eACxB,2BAAA,KAAK,KAAK,gBACV,MACA,KAAK,UAAU;AAEhB,UAAM,aAAaC,KAAAA,iBAAiB,KAAK,KAAK,SAAS,IAAI;AAC3D,UAAM,gBAAgBC,QAAA,oBAAoB,YAAY,KAAK,UAAU;AAErE,WAAO,IAAIC,WACV,kBAAA,MACA,eACA,KAAK,aACL,gBAAgB;AAAA,EAElB;AACA;;;"}
@@ -29,10 +29,10 @@ export declare class RepositoriesManager {
29
29
  */
30
30
  private readonly repositories;
31
31
  /**
32
- * @param prismicUrl - The base URL of the Wroom app.
32
+ * @param urlConfig - The base URL of the Wroom app.
33
33
  * @param credentials - Authentication credentials (email and password)
34
34
  */
35
- constructor(urls: UrlConfig | string, credentials: Credentials);
35
+ constructor(urlConfig: UrlConfig | string, credentials: Credentials);
36
36
  /**
37
37
  * If the baseURL is prismic.io, assume the other services have the format
38
38
  * <name>.prismic.io
@@ -5,21 +5,23 @@ var __publicField = (obj, key, value) => {
5
5
  return value;
6
6
  };
7
7
  import { createAuthenticationApiClient } from "../clients/authenticationApi.js";
8
+ import { createCoreApiClient } from "../clients/coreApi.js";
8
9
  import { createCustomTypesApiClient } from "../clients/customTypesApi.js";
9
10
  import { WroomClient } from "../clients/wroom.js";
10
11
  import { EnvVariableManager } from "../utils/envVariableManager.js";
11
12
  import { logger, logHttpResponse } from "../utils/log.js";
12
13
  import { randomString } from "../utils/random.js";
14
+ import { getRepositoryUrl } from "../utils/urls.js";
13
15
  import { RepositoryManager } from "./repository.js";
14
16
  const createRepositoriesManager = (urlConfig, auth) => {
15
17
  return new RepositoriesManager(urlConfig, auth);
16
18
  };
17
19
  class RepositoriesManager {
18
20
  /**
19
- * @param prismicUrl - The base URL of the Wroom app.
21
+ * @param urlConfig - The base URL of the Wroom app.
20
22
  * @param credentials - Authentication credentials (email and password)
21
23
  */
22
- constructor(urls, credentials) {
24
+ constructor(urlConfig, credentials) {
23
25
  __publicField(this, "credentials");
24
26
  __publicField(this, "urls");
25
27
  __publicField(this, "wroomClient");
@@ -30,10 +32,10 @@ class RepositoriesManager {
30
32
  */
31
33
  __publicField(this, "repositories", new EnvVariableManager("_PRISMIC_E2E_TESTS_REPOS"));
32
34
  this.credentials = credentials;
33
- if (typeof urls === "string") {
34
- urls = this.inferUrls(urls);
35
+ if (typeof urlConfig === "string") {
36
+ urlConfig = this.inferUrls(urlConfig);
35
37
  }
36
- this.urls = urls;
38
+ this.urls = urlConfig;
37
39
  this.wroomClient = new WroomClient(this.urls.baseURL, this.credentials);
38
40
  this.authClient = createAuthenticationApiClient(this.urls.authenticationApi, this.credentials);
39
41
  }
@@ -127,7 +129,9 @@ class RepositoriesManager {
127
129
  */
128
130
  getRepositoryManager(name) {
129
131
  const customTypeClient = createCustomTypesApiClient(this.urls.customTypesApi, name, this.authClient);
130
- return new RepositoryManager(name, this.wroomClient, customTypeClient);
132
+ const coreApiUrl = getRepositoryUrl(this.urls.baseURL, name);
133
+ const coreApiClient = createCoreApiClient(coreApiUrl, this.authClient);
134
+ return new RepositoryManager(name, coreApiClient, this.wroomClient, customTypeClient);
131
135
  }
132
136
  }
133
137
  export {
@@ -1 +1 @@
1
- {"version":3,"file":"repositories.js","sources":["../../../src/managers/repositories.ts"],"sourcesContent":["import { Credentials } from \"../types\";\n\nimport {\n\tAuthenticationClient,\n\tcreateAuthenticationApiClient,\n} from \"../clients/authenticationApi\";\nimport { createCustomTypesApiClient } from \"../clients/customTypesApi\";\nimport { WroomClient } from \"../clients/wroom\";\nimport { EnvVariableManager } from \"../utils/envVariableManager\";\nimport { logHttpResponse, logger } from \"../utils/log\";\nimport { randomString } from \"../utils/random\";\n\nimport { RepositoryConfig, RepositoryManager } from \"./repository\";\n\n/**\n * Factory method to create a RepositoriesManager to manage repositories test\n * data.\n *\n * @param urlConfig - If provided as an object:\n *\n * - {@link UrlConfig.baseURL}: The Prismic base URL.\n * - {@link UrlConfig.customTypesApi}: The base URL for the Custom Types API.\n * - {@link UrlConfig.authenticationApi}: The base URL for the Authentication API.\n *\n * If provided as a string: It is treated as the base URL for Prismic. Other\n * URLs (Custom Types API, Auth API) will be derived from it.\n * @param auth - The authentication credentials\n *\n * @returns An instance of {@link RepositoriesManager} to manage test data.\n */\nexport const createRepositoriesManager = (\n\turlConfig: UrlConfig | string,\n\tauth: Credentials,\n): RepositoriesManager => {\n\treturn new RepositoriesManager(urlConfig, auth);\n};\n\n/** Utility object to manage Prismic test data */\nexport class RepositoriesManager {\n\tprivate readonly urls: UrlConfig;\n\tprivate readonly wroomClient: WroomClient;\n\tprivate readonly authClient: AuthenticationClient;\n\t/**\n\t * helper to keep track of repositories across independent test phases (setup,\n\t * teardown)\n\t */\n\tprivate readonly repositories = new EnvVariableManager(\n\t\t\"_PRISMIC_E2E_TESTS_REPOS\",\n\t);\n\n\t/**\n\t * @param prismicUrl - The base URL of the Wroom app.\n\t * @param credentials - Authentication credentials (email and password)\n\t */\n\tconstructor(\n\t\turls: UrlConfig | string,\n\t\tprivate readonly credentials: Credentials,\n\t) {\n\t\tif (typeof urls === \"string\") {\n\t\t\t// When a string is provided, treat it as the 'base' property\n\t\t\turls = this.inferUrls(urls);\n\t\t}\n\t\tthis.urls = urls;\n\t\tthis.wroomClient = new WroomClient(this.urls.baseURL, this.credentials);\n\t\tthis.authClient = createAuthenticationApiClient(\n\t\t\tthis.urls.authenticationApi,\n\t\t\tthis.credentials,\n\t\t);\n\t}\n\n\t/**\n\t * If the baseURL is prismic.io, assume the other services have the format\n\t * <name>.prismic.io\n\t */\n\tprivate inferUrls(baseURL: string): UrlConfig {\n\t\tconst url = new URL(baseURL);\n\t\tconst protocol = url.protocol.slice(0, -1); // Remove the trailing colon from the protocol\n\n\t\treturn {\n\t\t\tbaseURL: baseURL,\n\t\t\tauthenticationApi: `${protocol}://auth.${url.hostname}`,\n\t\t\tcustomTypesApi: `${protocol}://customtypes.${url.hostname}`,\n\t\t};\n\t}\n\n\t/**\n\t * Generate a user token from the authentication service.\n\t *\n\t * @returns a JWT token\n\t */\n\tasync getUserApiToken(): Promise<string> {\n\t\treturn this.authClient.getToken();\n\t}\n\n\t/**\n\t * Create a new repository in Wroom.\n\t *\n\t * @param repositoryName - The name of the repository.\n\t *\n\t * @throws Error if the repository creation fails.\n\t */\n\tasync createRepository(\n\t\tconfig: {\n\t\t\tprefix?: string;\n\t\t} & RepositoryConfig = {},\n\t): Promise<RepositoryManager> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst { prefix = \"e2e-tests\" } = config;\n\t\tconst repositoryName = `${prefix}-${randomString()}`;\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tnull,\n\t\t\t\"authentication/newrepository\",\n\t\t\t{\n\t\t\t\tdomain: repositoryName,\n\t\t\t\tframework: \"vue\",\n\t\t\t\tplan: \"personal\",\n\t\t\t\tisAnnual: \"false\",\n\t\t\t\trole: \"developer\",\n\t\t\t},\n\t\t);\n\t\tif (response.status !== 200) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not create repository ${repositoryName}`);\n\t\t}\n\t\tthis.repositories.add(repositoryName);\n\n\t\tprofiler.done({ message: `created repository '${repositoryName}'` });\n\n\t\tconst repository = this.getRepositoryManager(repositoryName);\n\t\tawait repository.configure(config);\n\n\t\treturn repository;\n\t}\n\n\t/**\n\t * Delete a repository from Wroom.\n\t *\n\t * @param repository - The name of the repository.\n\t *\n\t * @throws Error if the repository deletion fails.\n\t */\n\tasync deleteRepository(repository: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst post = async () =>\n\t\t\tthis.wroomClient.post(repository, \"app/settings/delete\", {\n\t\t\t\tconfirm: repository,\n\t\t\t\tpassword: this.credentials.password,\n\t\t\t});\n\t\tconst response = await post();\n\t\tif (response.status !== 200) {\n\t\t\t// sometimes the deletion returns 500 but actually succeeds\n\t\t\t// we run the query again and check the repo is actually deleted (404)\n\t\t\tconst retry = await post();\n\t\t\tif (retry.status !== 404) {\n\t\t\t\tlogHttpResponse(response);\n\t\t\t\tthrow new Error(`Could not delete repository ${repository}`);\n\t\t\t}\n\t\t}\n\t\tthis.repositories.remove(repository);\n\t\tprofiler.done({ message: `deleted repository '${repository}'` });\n\t}\n\n\t/**\n\t * Cleanup test data. For example, it deletes repositories created using\n\t * createRepository()\n\t */\n\tasync tearDown(): Promise<void> {\n\t\tconst repositories = this.repositories.getAll();\n\t\tfor (const repository of repositories) {\n\t\t\tawait this.deleteRepository(repository);\n\t\t}\n\t}\n\n\t/**\n\t * Return a repository utilities object for a repository.\n\t *\n\t * @param name - The name of the repository.\n\t */\n\tgetRepositoryManager(name: string): RepositoryManager {\n\t\tconst customTypeClient = createCustomTypesApiClient(\n\t\t\tthis.urls.customTypesApi,\n\t\t\tname,\n\t\t\tthis.authClient,\n\t\t);\n\n\t\treturn new RepositoryManager(name, this.wroomClient, customTypeClient);\n\t}\n}\n\nexport type UrlConfig = {\n\t/** Prismic base url */\n\tbaseURL: string;\n\t/** Custom types api base url */\n\tcustomTypesApi: string;\n\t/** Auth service api base url */\n\tauthenticationApi: string;\n};\n"],"names":[],"mappings":";;;;;;;;;;;;;AA8Ba,MAAA,4BAA4B,CACxC,WACA,SACwB;AACjB,SAAA,IAAI,oBAAoB,WAAW,IAAI;AAC/C;MAGa,oBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA,EAgB/B,YACC,MACiB,aAAwB;AAAxB;AAjBD;AACA;AACA;AAKA;AAAA;AAAA;AAAA;AAAA,wCAAe,IAAI,mBACnC,0BAA0B;AAST,SAAW,cAAX;AAEb,QAAA,OAAO,SAAS,UAAU;AAEtB,aAAA,KAAK,UAAU,IAAI;AAAA,IAC1B;AACD,SAAK,OAAO;AACZ,SAAK,cAAc,IAAI,YAAY,KAAK,KAAK,SAAS,KAAK,WAAW;AACtE,SAAK,aAAa,8BACjB,KAAK,KAAK,mBACV,KAAK,WAAW;AAAA,EAElB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMQ,UAAU,SAAe;AAC1B,UAAA,MAAM,IAAI,IAAI,OAAO;AAC3B,UAAM,WAAW,IAAI,SAAS,MAAM,GAAG,EAAE;AAElC,WAAA;AAAA,MACN;AAAA,MACA,mBAAmB,GAAG,QAAQ,WAAW,IAAI,QAAQ;AAAA,MACrD,gBAAgB,GAAG,QAAQ,kBAAkB,IAAI,QAAQ;AAAA,IAAA;AAAA,EAE3D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,kBAAe;AACb,WAAA,KAAK,WAAW;EACxB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAM,iBACL,SAEuB,IAAE;AAEnB,UAAA,WAAW,OAAO;AAElB,UAAA,EAAE,SAAS,YAAgB,IAA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mBAAmB,2BACxB,KAAK,KAAK,gBACV,MACA,KAAK,UAAU;AAGhB,WAAO,IAAI,kBAAkB,MAAM,KAAK,aAAa,gBAAgB;AAAA,EACtE;AACA;"}
1
+ {"version":3,"file":"repositories.js","sources":["../../../src/managers/repositories.ts"],"sourcesContent":["import { Credentials } from \"../types\";\n\nimport {\n\tAuthenticationClient,\n\tcreateAuthenticationApiClient,\n} from \"../clients/authenticationApi\";\nimport { createCoreApiClient } from \"../clients/coreApi\";\nimport { createCustomTypesApiClient } from \"../clients/customTypesApi\";\nimport { WroomClient } from \"../clients/wroom\";\nimport { EnvVariableManager } from \"../utils/envVariableManager\";\nimport { logHttpResponse, logger } from \"../utils/log\";\nimport { randomString } from \"../utils/random\";\nimport { getRepositoryUrl } from \"../utils/urls\";\n\nimport { RepositoryConfig, RepositoryManager } from \"./repository\";\n\n/**\n * Factory method to create a RepositoriesManager to manage repositories test\n * data.\n *\n * @param urlConfig - If provided as an object:\n *\n * - {@link UrlConfig.baseURL}: The Prismic base URL.\n * - {@link UrlConfig.customTypesApi}: The base URL for the Custom Types API.\n * - {@link UrlConfig.authenticationApi}: The base URL for the Authentication API.\n *\n * If provided as a string: It is treated as the base URL for Prismic. Other\n * URLs (Custom Types API, Auth API) will be derived from it.\n * @param auth - The authentication credentials\n *\n * @returns An instance of {@link RepositoriesManager} to manage test data.\n */\nexport const createRepositoriesManager = (\n\turlConfig: UrlConfig | string,\n\tauth: Credentials,\n): RepositoriesManager => {\n\treturn new RepositoriesManager(urlConfig, auth);\n};\n\n/** Utility object to manage Prismic test data */\nexport class RepositoriesManager {\n\tprivate readonly urls: UrlConfig;\n\tprivate readonly wroomClient: WroomClient;\n\tprivate readonly authClient: AuthenticationClient;\n\t/**\n\t * helper to keep track of repositories across independent test phases (setup,\n\t * teardown)\n\t */\n\tprivate readonly repositories = new EnvVariableManager(\n\t\t\"_PRISMIC_E2E_TESTS_REPOS\",\n\t);\n\n\t/**\n\t * @param urlConfig - The base URL of the Wroom app.\n\t * @param credentials - Authentication credentials (email and password)\n\t */\n\tconstructor(\n\t\turlConfig: UrlConfig | string,\n\t\tprivate readonly credentials: Credentials,\n\t) {\n\t\tif (typeof urlConfig === \"string\") {\n\t\t\t// When a string is provided, treat it as the 'base' property\n\t\t\turlConfig = this.inferUrls(urlConfig);\n\t\t}\n\t\tthis.urls = urlConfig;\n\t\tthis.wroomClient = new WroomClient(this.urls.baseURL, this.credentials);\n\t\tthis.authClient = createAuthenticationApiClient(\n\t\t\tthis.urls.authenticationApi,\n\t\t\tthis.credentials,\n\t\t);\n\t}\n\n\t/**\n\t * If the baseURL is prismic.io, assume the other services have the format\n\t * <name>.prismic.io\n\t */\n\tprivate inferUrls(baseURL: string): UrlConfig {\n\t\tconst url = new URL(baseURL);\n\t\tconst protocol = url.protocol.slice(0, -1); // Remove the trailing colon from the protocol\n\n\t\treturn {\n\t\t\tbaseURL: baseURL,\n\t\t\tauthenticationApi: `${protocol}://auth.${url.hostname}`,\n\t\t\tcustomTypesApi: `${protocol}://customtypes.${url.hostname}`,\n\t\t};\n\t}\n\n\t/**\n\t * Generate a user token from the authentication service.\n\t *\n\t * @returns a JWT token\n\t */\n\tasync getUserApiToken(): Promise<string> {\n\t\treturn this.authClient.getToken();\n\t}\n\n\t/**\n\t * Create a new repository in Wroom.\n\t *\n\t * @param repositoryName - The name of the repository.\n\t *\n\t * @throws Error if the repository creation fails.\n\t */\n\tasync createRepository(\n\t\tconfig: {\n\t\t\tprefix?: string;\n\t\t} & RepositoryConfig = {},\n\t): Promise<RepositoryManager> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst { prefix = \"e2e-tests\" } = config;\n\t\tconst repositoryName = `${prefix}-${randomString()}`;\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tnull,\n\t\t\t\"authentication/newrepository\",\n\t\t\t{\n\t\t\t\tdomain: repositoryName,\n\t\t\t\tframework: \"vue\",\n\t\t\t\tplan: \"personal\",\n\t\t\t\tisAnnual: \"false\",\n\t\t\t\trole: \"developer\",\n\t\t\t},\n\t\t);\n\t\tif (response.status !== 200) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not create repository ${repositoryName}`);\n\t\t}\n\t\tthis.repositories.add(repositoryName);\n\n\t\tprofiler.done({ message: `created repository '${repositoryName}'` });\n\n\t\tconst repository = this.getRepositoryManager(repositoryName);\n\t\tawait repository.configure(config);\n\n\t\treturn repository;\n\t}\n\n\t/**\n\t * Delete a repository from Wroom.\n\t *\n\t * @param repository - The name of the repository.\n\t *\n\t * @throws Error if the repository deletion fails.\n\t */\n\tasync deleteRepository(repository: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst post = async () =>\n\t\t\tthis.wroomClient.post(repository, \"app/settings/delete\", {\n\t\t\t\tconfirm: repository,\n\t\t\t\tpassword: this.credentials.password,\n\t\t\t});\n\t\tconst response = await post();\n\t\tif (response.status !== 200) {\n\t\t\t// sometimes the deletion returns 500 but actually succeeds\n\t\t\t// we run the query again and check the repo is actually deleted (404)\n\t\t\tconst retry = await post();\n\t\t\tif (retry.status !== 404) {\n\t\t\t\tlogHttpResponse(response);\n\t\t\t\tthrow new Error(`Could not delete repository ${repository}`);\n\t\t\t}\n\t\t}\n\t\tthis.repositories.remove(repository);\n\t\tprofiler.done({ message: `deleted repository '${repository}'` });\n\t}\n\n\t/**\n\t * Cleanup test data. For example, it deletes repositories created using\n\t * createRepository()\n\t */\n\tasync tearDown(): Promise<void> {\n\t\tconst repositories = this.repositories.getAll();\n\t\tfor (const repository of repositories) {\n\t\t\tawait this.deleteRepository(repository);\n\t\t}\n\t}\n\n\t/**\n\t * Return a repository utilities object for a repository.\n\t *\n\t * @param name - The name of the repository.\n\t */\n\tgetRepositoryManager(name: string): RepositoryManager {\n\t\tconst customTypeClient = createCustomTypesApiClient(\n\t\t\tthis.urls.customTypesApi,\n\t\t\tname,\n\t\t\tthis.authClient,\n\t\t);\n\t\tconst coreApiUrl = getRepositoryUrl(this.urls.baseURL, name);\n\t\tconst coreApiClient = createCoreApiClient(coreApiUrl, this.authClient);\n\n\t\treturn new RepositoryManager(\n\t\t\tname,\n\t\t\tcoreApiClient,\n\t\t\tthis.wroomClient,\n\t\t\tcustomTypeClient,\n\t\t);\n\t}\n}\n\nexport type UrlConfig = {\n\t/** Prismic base url */\n\tbaseURL: string;\n\t/** Custom types api base url */\n\tcustomTypesApi: string;\n\t/** Auth service api base url */\n\tauthenticationApi: string;\n};\n"],"names":[],"mappings":";;;;;;;;;;;;;;;AAgCa,MAAA,4BAA4B,CACxC,WACA,SACwB;AACjB,SAAA,IAAI,oBAAoB,WAAW,IAAI;AAC/C;MAGa,oBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA,EAgB/B,YACC,WACiB,aAAwB;AAAxB;AAjBD;AACA;AACA;AAKA;AAAA;AAAA;AAAA;AAAA,wCAAe,IAAI,mBACnC,0BAA0B;AAST,SAAW,cAAX;AAEb,QAAA,OAAO,cAAc,UAAU;AAEtB,kBAAA,KAAK,UAAU,SAAS;AAAA,IACpC;AACD,SAAK,OAAO;AACZ,SAAK,cAAc,IAAI,YAAY,KAAK,KAAK,SAAS,KAAK,WAAW;AACtE,SAAK,aAAa,8BACjB,KAAK,KAAK,mBACV,KAAK,WAAW;AAAA,EAElB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMQ,UAAU,SAAe;AAC1B,UAAA,MAAM,IAAI,IAAI,OAAO;AAC3B,UAAM,WAAW,IAAI,SAAS,MAAM,GAAG,EAAE;AAElC,WAAA;AAAA,MACN;AAAA,MACA,mBAAmB,GAAG,QAAQ,WAAW,IAAI,QAAQ;AAAA,MACrD,gBAAgB,GAAG,QAAQ,kBAAkB,IAAI,QAAQ;AAAA,IAAA;AAAA,EAE3D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,kBAAe;AACb,WAAA,KAAK,WAAW;EACxB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAM,iBACL,S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ACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,qBAAqB,MAAY;AAChC,UAAM,mBAAmB,2BACxB,KAAK,KAAK,gBACV,MACA,KAAK,UAAU;AAEhB,UAAM,aAAa,iBAAiB,KAAK,KAAK,SAAS,IAAI;AAC3D,UAAM,gBAAgB,oBAAoB,YAAY,KAAK,UAAU;AAErE,WAAO,IAAI,kBACV,MACA,eACA,KAAK,aACL,gBAAgB;AAAA,EAElB;AACA;"}
@@ -7,24 +7,44 @@ var __publicField = (obj, key, value) => {
7
7
  };
8
8
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
9
9
  const log = require("../utils/log.cjs");
10
+ const urls = require("../utils/urls.cjs");
10
11
  class RepositoryManager {
11
- constructor(name, wroomClient, customTypesApiClient) {
12
+ constructor(name, coreApiClient, wroomClient, customTypesApiClient) {
12
13
  __publicField(this, "name");
14
+ __publicField(this, "coreApiClient");
13
15
  __publicField(this, "wroomClient");
14
16
  __publicField(this, "customTypesApiClient");
15
17
  this.name = name;
18
+ this.coreApiClient = coreApiClient;
16
19
  this.wroomClient = wroomClient;
17
20
  this.customTypesApiClient = customTypesApiClient;
18
21
  }
19
22
  async configure(config) {
20
- if (config.defaultLocale) {
21
- await this.setDefaultLocale(config.defaultLocale);
22
- }
23
- if (config.locales) {
24
- await this.createLocales(config.locales);
25
- }
26
- if (config.customLocale) {
27
- await this.createCustomLocale(config.customLocale);
23
+ const hasLangConfig = config.defaultLocale || config.locales || config.customLocale;
24
+ if (hasLangConfig) {
25
+ const languages = await this.coreApiClient.getLanguages();
26
+ if (config.defaultLocale) {
27
+ const master = languages.find(({ is_master }) => is_master === true);
28
+ if (!master) {
29
+ await this.setDefaultLocale(config.defaultLocale);
30
+ } else if (master.id !== config.defaultLocale) {
31
+ log.logger.warn(`Master locale is already set to '${master.id}', cannot update it to '${config.defaultLocale}'`);
32
+ }
33
+ }
34
+ if (config.locales) {
35
+ const localesToAdd = config.locales.filter((id) => !languages.some((language) => language.id === id));
36
+ if (localesToAdd.length > 0) {
37
+ await this.createLocales(localesToAdd);
38
+ }
39
+ }
40
+ if (config.customLocale) {
41
+ if (!languages.find(({ id }) => {
42
+ var _a;
43
+ return id === ((_a = config.customLocale) == null ? void 0 : _a.lang.id);
44
+ })) {
45
+ await this.createCustomLocale(config.customLocale);
46
+ }
47
+ }
28
48
  }
29
49
  if (config.slices) {
30
50
  await this.createSlices(config.slices);
@@ -38,9 +58,7 @@ class RepositoryManager {
38
58
  }
39
59
  /** @returns the repository base url, like https://my-repo.prismic.io */
40
60
  getBaseURL() {
41
- const url = new URL(this.wroomClient.getBaseURL());
42
- url.hostname = `${this.name}.${url.hostname}`;
43
- return url.toString();
61
+ return urls.getRepositoryUrl(this.wroomClient.getBaseURL(), this.name);
44
62
  }
45
63
  /**
46
64
  * Set additional standard locales for a repository in Wroom, does not fail if
@@ -169,7 +187,7 @@ class RepositoryManager {
169
187
  async deletePreview(id) {
170
188
  const profiler = log.logger.startTimer();
171
189
  const response = await this.wroomClient.post(this.name, `previews/delete/${id}`, {});
172
- this.failIfNot200(response, `Could not delete preview with id${id}`);
190
+ this.failIfNot200(response, `Could not delete preview with id ${id}`);
173
191
  profiler.done({ message: `preview '${id}' deleted` });
174
192
  }
175
193
  /**
@@ -188,6 +206,30 @@ class RepositoryManager {
188
206
  async createSlices(slices) {
189
207
  await this.customTypesApiClient.createSlices(slices);
190
208
  }
209
+ /**
210
+ * Create a permanent access token along with an application name
211
+ *
212
+ * @param appName - mandatory authorized application name
213
+ */
214
+ async createPermanentAccessToken(appName) {
215
+ const profiler = log.logger.startTimer();
216
+ const existingApps = await this.wroomClient.get(this.name, "settings/security/contentapi");
217
+ this.failIfNot200(existingApps, `Could not get status of existing applications to generate a permanent token`);
218
+ const existingApp = existingApps.data.find(({ name, wroom_auths }) => {
219
+ return name === appName && wroom_auths.length > 0;
220
+ });
221
+ if (existingApp) {
222
+ return existingApp.wroom_auths[0].token;
223
+ }
224
+ const response = await this.wroomClient.post(this.name, "settings/security/oauthapp", {
225
+ app_name: appName
226
+ });
227
+ this.failIfNot200(response, `Could not create permanent access token for app name ${appName}`);
228
+ profiler.done({
229
+ message: `permanent access token for app name '${appName}' created`
230
+ });
231
+ return response.data.wroom_auths[0].token;
232
+ }
191
233
  }
192
234
  exports.RepositoryManager = RepositoryManager;
193
235
  //# sourceMappingURL=repository.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"repository.cjs","sources":["../../../src/managers/repository.ts"],"sourcesContent":["import { AxiosResponse } from \"axios\";\n\nimport {\n\tCustomType,\n\tSharedSlice,\n} from \"@prismicio/types-internal/lib/customtypes\";\n\nimport { CustomTypesClient } from \"../clients/customTypesApi\";\nimport { WroomClient } from \"../clients/wroom\";\nimport { logHttpResponse, logger } from \"../utils/log\";\n\n/** Utility to manage test data for a Prismic repository */\nexport class RepositoryManager {\n\tconstructor(\n\t\treadonly name: string,\n\t\tprivate readonly wroomClient: WroomClient,\n\t\tprivate readonly customTypesApiClient: CustomTypesClient,\n\t) {}\n\n\tasync configure(config: RepositoryConfig): Promise<void> {\n\t\tif (config.defaultLocale) {\n\t\t\tawait this.setDefaultLocale(config.defaultLocale);\n\t\t}\n\n\t\tif (config.locales) {\n\t\t\tawait this.createLocales(config.locales);\n\t\t}\n\n\t\tif (config.customLocale) {\n\t\t\tawait this.createCustomLocale(config.customLocale);\n\t\t}\n\n\t\tif (config.slices) {\n\t\t\tawait this.createSlices(config.slices);\n\t\t}\n\n\t\tif (config.customTypes) {\n\t\t\tawait this.createCustomTypes(config.customTypes);\n\t\t}\n\n\t\tif (config.preview) {\n\t\t\tawait this.createPreview(config.preview);\n\t\t}\n\t}\n\t/** @returns the repository base url, like https://my-repo.prismic.io */\n\tgetBaseURL(): string {\n\t\tconst url = new URL(this.wroomClient.getBaseURL());\n\t\turl.hostname = `${this.name}.${url.hostname}`;\n\n\t\treturn url.toString();\n\t}\n\n\t/**\n\t * Set additional standard locales for a repository in Wroom, does not fail if\n\t * the locales already exist.\n\t *\n\t * @param locales - An array of all locales to be added to the repository.\n\t *\n\t * @throws Error if setting the locales fails.\n\t */\n\tasync createLocales(locales: string[]): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t\"app/settings/multilanguages\",\n\t\t\t{ languages: locales },\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 &&\n\t\t\t\t(data as string).includes(\"already existing languages\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not add locales ${locales.join(\", \")}`);\n\t\t}\n\t\tprofiler.done({ message: `locales '${locales}' created` });\n\t}\n\n\t/**\n\t * Set additional custom locale for a repository in Wroom, does not fail if\n\t * the locales already exist.\n\t *\n\t * @param locale - The locale to be added to the repository.\n\t *\n\t * @throws Error if setting the locale fails.\n\t */\n\tasync createCustomLocale(locale: CustomLocale): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t\"app/settings/multilanguages/custom\",\n\t\t\tlocale,\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 && JSON.stringify(data).includes(\"code is already used\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not create custom locale ${locale}`);\n\t\t}\n\t\tprofiler.done({\n\t\t\tmessage: `custom locale ${JSON.stringify(locale)} created`,\n\t\t});\n\t}\n\n\tprivate failIfNot200(response: AxiosResponse, msg: string) {\n\t\tif (response.status !== 200) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(msg);\n\t\t}\n\t}\n\n\t/**\n\t * Delete a locale from a repository.\n\t *\n\t * @param locale - locale to remove from the repository configuration.\n\t *\n\t * @throws Error if deleting the locale fails.\n\t */\n\tasync deleteLocale(locale: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`app/settings/multilanguages/${locale}/delete`,\n\t\t);\n\t\tthis.failIfNot200(response, `Could not delete locale ${locale}`);\n\t\tprofiler.done({ message: `locale '${locale}' deleted` });\n\t}\n\n\t/**\n\t * Set the default (master) locale for a repository in Wroom, the locale needs\n\t * to exist for the repository.\n\t *\n\t * @param locale - The locale to be set as the default.\n\t *\n\t * @throws Error if setting the default locale fails.\n\t */\n\tasync setDefaultLocale(locale: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`/app/settings/multilanguages/${locale}/createMasterLang`,\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 &&\n\t\t\t\t// returns 400 if master lang is already set\n\t\t\t\tJSON.stringify(data).includes(\"Unable to set as master language\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not set default locale to ${locale}`);\n\t\t}\n\t\tprofiler.done({ message: `default locale set to '${locale}'` });\n\t}\n\n\t/**\n\t * Creates a release.\n\t *\n\t * @param label - The label for the release.\n\t *\n\t * @returns A Promise that resolves when the release is created.\n\t */\n\tasync createRelease(label: string): Promise<{ id: string }> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(this.name, \"app/releases\", {\n\t\t\tlabel,\n\t\t});\n\t\tthis.failIfNot200(response, `Could not create release ${label}`);\n\t\tprofiler.done({ message: `release '${label}' created` });\n\n\t\treturn response.data;\n\t}\n\n\t/**\n\t * Deletes a repository release.\n\t *\n\t * @param repository - The name of the repository.\n\t * @param id - The ID of the release to be deleted.\n\t */\n\tasync deleteRelease(id: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`app/releases/${id}/delete`,\n\t\t\t{},\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not delete release with id ${id}`);\n\t\tprofiler.done({ message: `release '${id}' deleted` });\n\t}\n\n\t/**\n\t * Creates a preview for a repository.\n\t *\n\t * @param data - The data for creating the preview.\n\t */\n\tasync createPreview(data: Preview): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`previews/new`,\n\t\t\tdata,\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not create preview ${data.name}`);\n\t\tprofiler.done({ message: `preview '${data.name}' created` });\n\n\t\treturn response.data;\n\t}\n\n\t/**\n\t * Deletes a preview from a repository.\n\t *\n\t * @param id - The ID of the preview to be deleted.\n\t */\n\tasync deletePreview(id: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`previews/delete/${id}`,\n\t\t\t{},\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not delete preview with id${id}`);\n\t\tprofiler.done({ message: `preview '${id}' deleted` });\n\t}\n\n\t/**\n\t * Create Custom Types using the Custom types api.\n\t *\n\t * @param customTypes -\n\t */\n\tasync createCustomTypes(customTypes: CustomType[]): Promise<void> {\n\t\tawait this.customTypesApiClient.createCustomTypes(customTypes);\n\t}\n\n\t/**\n\t * Create slices using the Custom types api.\n\t *\n\t * @param slices -\n\t */\n\tasync createSlices(slices: SharedSlice[]): Promise<void> {\n\t\tawait this.customTypesApiClient.createSlices(slices);\n\t}\n}\n\nexport type RepositoryConfig = {\n\tcustomLocale?: CustomLocale;\n\tlocales?: string[];\n\tdefaultLocale?: string;\n\tslices?: SharedSlice[];\n\tcustomTypes?: CustomType[];\n\tpreview?: Preview;\n};\n\nexport type Preview = {\n\tname: string;\n\twebsiteURL: string;\n\tresolverPath: string;\n};\n\nexport type CustomLocale = {\n\tlang: {\n\t\tlabel: string;\n\t\tid: string;\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B,UAAA,WAAWA,WAAO;AAElB,UAAA,WAAW,MAAM,KAAK,YAAY,KACvC,KAAK,MACL,mBAAmB,EAAE,IACrB,CAAE,CAAA;AAGH,SAAK,aAAa,UAAU,mCAAmC,EAAE,EAAE;AACnE,aAAS,KAAK,EAAE,SAAS,YAAY,EAAE,aAAa;AAAA,EACrD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,kBAAkB,aAAyB;AAC1C,UAAA,KAAK,qBAAqB,kBAAkB,WAAW;AAAA,EAC9D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,aAAa,QAAqB;AACjC,UAAA,KAAK,qBAAqB,aAAa,MAAM;AAAA,EACpD;AACA;;"}
1
+ {"version":3,"file":"repository.cjs","sources":["../../../src/managers/repository.ts"],"sourcesContent":["import { AxiosResponse } from \"axios\";\n\nimport {\n\tCustomType,\n\tSharedSlice,\n} from \"@prismicio/types-internal/lib/customtypes\";\n\nimport type { CoreClient } from \"../clients/coreApi\";\nimport { CustomTypesClient } from \"../clients/customTypesApi\";\nimport { WroomClient } from \"../clients/wroom\";\nimport { logHttpResponse, logger } from \"../utils/log\";\nimport { getRepositoryUrl } from \"../utils/urls\";\n\n/** Utility to manage test data for a Prismic repository */\nexport class RepositoryManager {\n\tconstructor(\n\t\treadonly name: string,\n\t\tprivate readonly coreApiClient: CoreClient,\n\t\tprivate readonly wroomClient: WroomClient,\n\t\tprivate readonly customTypesApiClient: CustomTypesClient,\n\t) {}\n\n\tasync configure(config: RepositoryConfig): Promise<void> {\n\t\tconst hasLangConfig =\n\t\t\tconfig.defaultLocale || config.locales || config.customLocale;\n\t\tif (hasLangConfig) {\n\t\t\tconst languages = await this.coreApiClient.getLanguages();\n\n\t\t\t// default locale must be set first\n\t\t\tif (config.defaultLocale) {\n\t\t\t\tconst master = languages.find(({ is_master }) => is_master === true);\n\t\t\t\tif (!master) {\n\t\t\t\t\tawait this.setDefaultLocale(config.defaultLocale);\n\t\t\t\t} else if (master.id !== config.defaultLocale) {\n\t\t\t\t\tlogger.warn(\n\t\t\t\t\t\t`Master locale is already set to '${master.id}', cannot update it to '${config.defaultLocale}'`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (config.locales) {\n\t\t\t\tconst localesToAdd = config.locales.filter(\n\t\t\t\t\t(id) => !languages.some((language) => language.id === id),\n\t\t\t\t);\n\t\t\t\tif (localesToAdd.length > 0) {\n\t\t\t\t\tawait this.createLocales(localesToAdd);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (config.customLocale) {\n\t\t\t\tif (!languages.find(({ id }) => id === config.customLocale?.lang.id)) {\n\t\t\t\t\tawait this.createCustomLocale(config.customLocale);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (config.slices) {\n\t\t\tawait this.createSlices(config.slices);\n\t\t}\n\n\t\tif (config.customTypes) {\n\t\t\tawait this.createCustomTypes(config.customTypes);\n\t\t}\n\n\t\tif (config.preview) {\n\t\t\tawait this.createPreview(config.preview);\n\t\t}\n\t}\n\t/** @returns the repository base url, like https://my-repo.prismic.io */\n\tgetBaseURL(): string {\n\t\treturn getRepositoryUrl(this.wroomClient.getBaseURL(), this.name);\n\t}\n\n\t/**\n\t * Set additional standard locales for a repository in Wroom, does not fail if\n\t * the locales already exist.\n\t *\n\t * @param locales - An array of all locales to be added to the repository.\n\t *\n\t * @throws Error if setting the locales fails.\n\t */\n\tasync createLocales(locales: string[]): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t\"app/settings/multilanguages\",\n\t\t\t{ languages: locales },\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 &&\n\t\t\t\t(data as string).includes(\"already existing languages\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not add locales ${locales.join(\", \")}`);\n\t\t}\n\t\tprofiler.done({ message: `locales '${locales}' created` });\n\t}\n\n\t/**\n\t * Set additional custom locale for a repository in Wroom, does not fail if\n\t * the locales already exist.\n\t *\n\t * @param locale - The locale to be added to the repository.\n\t *\n\t * @throws Error if setting the locale fails.\n\t */\n\tasync createCustomLocale(locale: CustomLocale): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t\"app/settings/multilanguages/custom\",\n\t\t\tlocale,\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 && JSON.stringify(data).includes(\"code is already used\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not create custom locale ${locale}`);\n\t\t}\n\t\tprofiler.done({\n\t\t\tmessage: `custom locale ${JSON.stringify(locale)} created`,\n\t\t});\n\t}\n\n\tprivate failIfNot200(response: AxiosResponse, msg: string) {\n\t\tif (response.status !== 200) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(msg);\n\t\t}\n\t}\n\n\t/**\n\t * Delete a locale from a repository.\n\t *\n\t * @param locale - locale to remove from the repository configuration.\n\t *\n\t * @throws Error if deleting the locale fails.\n\t */\n\tasync deleteLocale(locale: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`app/settings/multilanguages/${locale}/delete`,\n\t\t);\n\t\tthis.failIfNot200(response, `Could not delete locale ${locale}`);\n\t\tprofiler.done({ message: `locale '${locale}' deleted` });\n\t}\n\n\t/**\n\t * Set the default (master) locale for a repository in Wroom, the locale needs\n\t * to exist for the repository.\n\t *\n\t * @param locale - The locale to be set as the default.\n\t *\n\t * @throws Error if setting the default locale fails.\n\t */\n\tasync setDefaultLocale(locale: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`/app/settings/multilanguages/${locale}/createMasterLang`,\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 &&\n\t\t\t\t// returns 400 if master lang is already set\n\t\t\t\tJSON.stringify(data).includes(\"Unable to set as master language\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not set default locale to ${locale}`);\n\t\t}\n\t\tprofiler.done({ message: `default locale set to '${locale}'` });\n\t}\n\n\t/**\n\t * Creates a release.\n\t *\n\t * @param label - The label for the release.\n\t *\n\t * @returns A Promise that resolves when the release is created.\n\t */\n\tasync createRelease(label: string): Promise<{ id: string }> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(this.name, \"app/releases\", {\n\t\t\tlabel,\n\t\t});\n\t\tthis.failIfNot200(response, `Could not create release ${label}`);\n\t\tprofiler.done({ message: `release '${label}' created` });\n\n\t\treturn response.data;\n\t}\n\n\t/**\n\t * Deletes a repository release.\n\t *\n\t * @param repository - The name of the repository.\n\t * @param id - The ID of the release to be deleted.\n\t */\n\tasync deleteRelease(id: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`app/releases/${id}/delete`,\n\t\t\t{},\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not delete release with id ${id}`);\n\t\tprofiler.done({ message: `release '${id}' deleted` });\n\t}\n\n\t/**\n\t * Creates a preview for a repository.\n\t *\n\t * @param data - The data for creating the preview.\n\t */\n\tasync createPreview(data: Preview): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`previews/new`,\n\t\t\tdata,\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not create preview ${data.name}`);\n\t\tprofiler.done({ message: `preview '${data.name}' created` });\n\n\t\treturn response.data;\n\t}\n\n\t/**\n\t * Deletes a preview from a repository.\n\t *\n\t * @param id - The ID of the preview to be deleted.\n\t */\n\tasync deletePreview(id: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`previews/delete/${id}`,\n\t\t\t{},\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not delete preview with id ${id}`);\n\t\tprofiler.done({ message: `preview '${id}' deleted` });\n\t}\n\n\t/**\n\t * Create Custom Types using the Custom types api.\n\t *\n\t * @param customTypes -\n\t */\n\tasync createCustomTypes(customTypes: CustomType[]): Promise<void> {\n\t\tawait this.customTypesApiClient.createCustomTypes(customTypes);\n\t}\n\n\t/**\n\t * Create slices using the Custom types api.\n\t *\n\t * @param slices -\n\t */\n\tasync createSlices(slices: SharedSlice[]): Promise<void> {\n\t\tawait this.customTypesApiClient.createSlices(slices);\n\t}\n\n\t/**\n\t * Create a permanent access token along with an application name\n\t *\n\t * @param appName - mandatory authorized application name\n\t */\n\tasync createPermanentAccessToken(appName: string): Promise<string> {\n\t\tconst profiler = logger.startTimer();\n\t\ttype AuthorizedAppResponse = {\n\t\t\tname: string;\n\t\t\twroom_auths: { token: string }[];\n\t\t};\n\t\tconst existingApps: AxiosResponse<AuthorizedAppResponse[]> =\n\t\t\tawait this.wroomClient.get(this.name, \"settings/security/contentapi\");\n\n\t\tthis.failIfNot200(\n\t\t\texistingApps,\n\t\t\t`Could not get status of existing applications to generate a permanent token`,\n\t\t);\n\t\tconst existingApp = existingApps.data.find(({ name, wroom_auths }) => {\n\t\t\treturn name === appName && wroom_auths.length > 0;\n\t\t});\n\t\tif (existingApp) {\n\t\t\treturn existingApp.wroom_auths[0].token;\n\t\t}\n\t\tconst response: AxiosResponse<AuthorizedAppResponse> =\n\t\t\tawait this.wroomClient.post(this.name, \"settings/security/oauthapp\", {\n\t\t\t\tapp_name: appName,\n\t\t\t});\n\n\t\tthis.failIfNot200(\n\t\t\tresponse,\n\t\t\t`Could not create permanent access token for app name ${appName}`,\n\t\t);\n\t\tprofiler.done({\n\t\t\tmessage: `permanent access token for app name '${appName}' created`,\n\t\t});\n\n\t\t// only return the generated token since there is no need to access more\n\t\treturn response.data.wroom_auths[0].token;\n\t}\n}\n\nexport type RepositoryConfig = {\n\tcustomLocale?: CustomLocale;\n\tlocales?: string[];\n\tdefaultLocale?: string;\n\tslices?: SharedSlice[];\n\tcustomTypes?: CustomType[];\n\tpreview?: Preview;\n};\n\nexport type Preview = {\n\tname: string;\n\twebsiteURL: string;\n\tresolverPath: string;\n};\n\nexport type CustomLocale = {\n\tlang: {\n\t\tlabel: string;\n\t\tid: string;\n\t\tuseStandardAnalyzer?: boolean;\n\t};\n\tregion: {\n\t\tlabel: string;\n\t\tid: string;\n\t};\n};\n"],"names":["logger","getRepositoryUrl","logHttpResponse"],"mappings":";;;;;;;;;;MAca,kBAAiB;AAAA,EAC7B,YACU,MACQ,eACA,aACA,sBAAuC;AAH/C;AACQ;AACA;AACA;AAHR,SAAI,OAAJ;AACQ,SAAa,gBAAb;AACA,SAAW,cAAX;AACA,SAAoB,uBAApB;AAAA,EACf;AAAA,EAEH,MAAM,UAAU,QAAwB;AACvC,UAAM,gBACL,OAAO,iBAAiB,OAAO,WAAW,OAAO;AAClD,QAAI,eAAe;AAClB,YAAM,YAAY,MAAM,KAAK,cAAc,aAAY;AAGvD,UAAI,OAAO,eAAe;AACnB,cAAA,SAAS,UAAU,KAAK,CAAC,EAAE,gBAAgB,cAAc,IAAI;AACnE,YAAI,CAAC,QAAQ;AACN,gBAAA,KAAK,iBAAiB,OAAO,aAAa;AAAA,QACtC,WAAA,OAAO,OAAO,OAAO,eAAe;AAC9CA,cAAAA,OAAO,KACN,oCAAoC,OAAO,EAAE,2BAA2B,OAAO,aAAa,GAAG;AAAA,QAEhG;AAAA,MACD;AAED,UAAI,OAAO,SAAS;AACnB,cAAM,eAAe,OAAO,QAAQ,OACnC,CAAC,OAAO,CAAC,UAAU,KAA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A;AAAA;AAAA;AAAA,EAOA,MAAM,kBAAkB,aAAyB;AAC1C,UAAA,KAAK,qBAAqB,kBAAkB,WAAW;AAAA,EAC9D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,aAAa,QAAqB;AACjC,UAAA,KAAK,qBAAqB,aAAa,MAAM;AAAA,EACpD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,2BAA2B,SAAe;AACzC,UAAA,WAAWA,WAAO;AAKxB,UAAM,eACL,MAAM,KAAK,YAAY,IAAI,KAAK,MAAM,8BAA8B;AAEhE,SAAA,aACJ,cACA,6EAA6E;AAExE,UAAA,cAAc,aAAa,KAAK,KAAK,CAAC,EAAE,MAAM,kBAAiB;AAC7D,aAAA,SAAS,WAAW,YAAY,SAAS;AAAA,IAAA,CAChD;AACD,QAAI,aAAa;AACT,aAAA,YAAY,YAAY,CAAC,EAAE;AAAA,IAClC;AACD,UAAM,WACL,MAAM,KAAK,YAAY,KAAK,KAAK,MAAM,8BAA8B;AAAA,MACpE,UAAU;AAAA,IAAA,CACV;AAEF,SAAK,aACJ,UACA,wDAAwD,OAAO,EAAE;AAElE,aAAS,KAAK;AAAA,MACb,SAAS,wCAAwC,OAAO;AAAA,IAAA,CACxD;AAGD,WAAO,SAAS,KAAK,YAAY,CAAC,EAAE;AAAA,EACrC;AACA;;"}
@@ -1,12 +1,14 @@
1
1
  import { CustomType, SharedSlice } from "@prismicio/types-internal/lib/customtypes";
2
+ import type { CoreClient } from "../clients/coreApi";
2
3
  import { CustomTypesClient } from "../clients/customTypesApi";
3
4
  import { WroomClient } from "../clients/wroom";
4
5
  /** Utility to manage test data for a Prismic repository */
5
6
  export declare class RepositoryManager {
6
7
  readonly name: string;
8
+ private readonly coreApiClient;
7
9
  private readonly wroomClient;
8
10
  private readonly customTypesApiClient;
9
- constructor(name: string, wroomClient: WroomClient, customTypesApiClient: CustomTypesClient);
11
+ constructor(name: string, coreApiClient: CoreClient, wroomClient: WroomClient, customTypesApiClient: CustomTypesClient);
10
12
  configure(config: RepositoryConfig): Promise<void>;
11
13
  /** @returns the repository base url, like https://my-repo.prismic.io */
12
14
  getBaseURL(): string;
@@ -87,6 +89,12 @@ export declare class RepositoryManager {
87
89
  * @param slices -
88
90
  */
89
91
  createSlices(slices: SharedSlice[]): Promise<void>;
92
+ /**
93
+ * Create a permanent access token along with an application name
94
+ *
95
+ * @param appName - mandatory authorized application name
96
+ */
97
+ createPermanentAccessToken(appName: string): Promise<string>;
90
98
  }
91
99
  export type RepositoryConfig = {
92
100
  customLocale?: CustomLocale;
@@ -5,24 +5,44 @@ var __publicField = (obj, key, value) => {
5
5
  return value;
6
6
  };
7
7
  import { logger, logHttpResponse } from "../utils/log.js";
8
+ import { getRepositoryUrl } from "../utils/urls.js";
8
9
  class RepositoryManager {
9
- constructor(name, wroomClient, customTypesApiClient) {
10
+ constructor(name, coreApiClient, wroomClient, customTypesApiClient) {
10
11
  __publicField(this, "name");
12
+ __publicField(this, "coreApiClient");
11
13
  __publicField(this, "wroomClient");
12
14
  __publicField(this, "customTypesApiClient");
13
15
  this.name = name;
16
+ this.coreApiClient = coreApiClient;
14
17
  this.wroomClient = wroomClient;
15
18
  this.customTypesApiClient = customTypesApiClient;
16
19
  }
17
20
  async configure(config) {
18
- if (config.defaultLocale) {
19
- await this.setDefaultLocale(config.defaultLocale);
20
- }
21
- if (config.locales) {
22
- await this.createLocales(config.locales);
23
- }
24
- if (config.customLocale) {
25
- await this.createCustomLocale(config.customLocale);
21
+ const hasLangConfig = config.defaultLocale || config.locales || config.customLocale;
22
+ if (hasLangConfig) {
23
+ const languages = await this.coreApiClient.getLanguages();
24
+ if (config.defaultLocale) {
25
+ const master = languages.find(({ is_master }) => is_master === true);
26
+ if (!master) {
27
+ await this.setDefaultLocale(config.defaultLocale);
28
+ } else if (master.id !== config.defaultLocale) {
29
+ logger.warn(`Master locale is already set to '${master.id}', cannot update it to '${config.defaultLocale}'`);
30
+ }
31
+ }
32
+ if (config.locales) {
33
+ const localesToAdd = config.locales.filter((id) => !languages.some((language) => language.id === id));
34
+ if (localesToAdd.length > 0) {
35
+ await this.createLocales(localesToAdd);
36
+ }
37
+ }
38
+ if (config.customLocale) {
39
+ if (!languages.find(({ id }) => {
40
+ var _a;
41
+ return id === ((_a = config.customLocale) == null ? void 0 : _a.lang.id);
42
+ })) {
43
+ await this.createCustomLocale(config.customLocale);
44
+ }
45
+ }
26
46
  }
27
47
  if (config.slices) {
28
48
  await this.createSlices(config.slices);
@@ -36,9 +56,7 @@ class RepositoryManager {
36
56
  }
37
57
  /** @returns the repository base url, like https://my-repo.prismic.io */
38
58
  getBaseURL() {
39
- const url = new URL(this.wroomClient.getBaseURL());
40
- url.hostname = `${this.name}.${url.hostname}`;
41
- return url.toString();
59
+ return getRepositoryUrl(this.wroomClient.getBaseURL(), this.name);
42
60
  }
43
61
  /**
44
62
  * Set additional standard locales for a repository in Wroom, does not fail if
@@ -167,7 +185,7 @@ class RepositoryManager {
167
185
  async deletePreview(id) {
168
186
  const profiler = logger.startTimer();
169
187
  const response = await this.wroomClient.post(this.name, `previews/delete/${id}`, {});
170
- this.failIfNot200(response, `Could not delete preview with id${id}`);
188
+ this.failIfNot200(response, `Could not delete preview with id ${id}`);
171
189
  profiler.done({ message: `preview '${id}' deleted` });
172
190
  }
173
191
  /**
@@ -186,6 +204,30 @@ class RepositoryManager {
186
204
  async createSlices(slices) {
187
205
  await this.customTypesApiClient.createSlices(slices);
188
206
  }
207
+ /**
208
+ * Create a permanent access token along with an application name
209
+ *
210
+ * @param appName - mandatory authorized application name
211
+ */
212
+ async createPermanentAccessToken(appName) {
213
+ const profiler = logger.startTimer();
214
+ const existingApps = await this.wroomClient.get(this.name, "settings/security/contentapi");
215
+ this.failIfNot200(existingApps, `Could not get status of existing applications to generate a permanent token`);
216
+ const existingApp = existingApps.data.find(({ name, wroom_auths }) => {
217
+ return name === appName && wroom_auths.length > 0;
218
+ });
219
+ if (existingApp) {
220
+ return existingApp.wroom_auths[0].token;
221
+ }
222
+ const response = await this.wroomClient.post(this.name, "settings/security/oauthapp", {
223
+ app_name: appName
224
+ });
225
+ this.failIfNot200(response, `Could not create permanent access token for app name ${appName}`);
226
+ profiler.done({
227
+ message: `permanent access token for app name '${appName}' created`
228
+ });
229
+ return response.data.wroom_auths[0].token;
230
+ }
189
231
  }
190
232
  export {
191
233
  RepositoryManager
@@ -1 +1 @@
1
- {"version":3,"file":"repository.js","sources":["../../../src/managers/repository.ts"],"sourcesContent":["import { AxiosResponse } from \"axios\";\n\nimport {\n\tCustomType,\n\tSharedSlice,\n} from \"@prismicio/types-internal/lib/customtypes\";\n\nimport { CustomTypesClient } from \"../clients/customTypesApi\";\nimport { WroomClient } from \"../clients/wroom\";\nimport { logHttpResponse, logger } from \"../utils/log\";\n\n/** Utility to manage test data for a Prismic repository */\nexport class RepositoryManager {\n\tconstructor(\n\t\treadonly name: string,\n\t\tprivate readonly wroomClient: WroomClient,\n\t\tprivate readonly customTypesApiClient: CustomTypesClient,\n\t) {}\n\n\tasync configure(config: RepositoryConfig): Promise<void> {\n\t\tif (config.defaultLocale) {\n\t\t\tawait this.setDefaultLocale(config.defaultLocale);\n\t\t}\n\n\t\tif (config.locales) {\n\t\t\tawait this.createLocales(config.locales);\n\t\t}\n\n\t\tif (config.customLocale) {\n\t\t\tawait this.createCustomLocale(config.customLocale);\n\t\t}\n\n\t\tif (config.slices) {\n\t\t\tawait this.createSlices(config.slices);\n\t\t}\n\n\t\tif (config.customTypes) {\n\t\t\tawait this.createCustomTypes(config.customTypes);\n\t\t}\n\n\t\tif (config.preview) {\n\t\t\tawait this.createPreview(config.preview);\n\t\t}\n\t}\n\t/** @returns the repository base url, like https://my-repo.prismic.io */\n\tgetBaseURL(): string {\n\t\tconst url = new URL(this.wroomClient.getBaseURL());\n\t\turl.hostname = `${this.name}.${url.hostname}`;\n\n\t\treturn url.toString();\n\t}\n\n\t/**\n\t * Set additional standard locales for a repository in Wroom, does not fail if\n\t * the locales already exist.\n\t *\n\t * @param locales - An array of all locales to be added to the repository.\n\t *\n\t * @throws Error if setting the locales fails.\n\t */\n\tasync createLocales(locales: string[]): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t\"app/settings/multilanguages\",\n\t\t\t{ languages: locales },\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 &&\n\t\t\t\t(data as string).includes(\"already existing languages\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not add locales ${locales.join(\", \")}`);\n\t\t}\n\t\tprofiler.done({ message: `locales '${locales}' created` });\n\t}\n\n\t/**\n\t * Set additional custom locale for a repository in Wroom, does not fail if\n\t * the locales already exist.\n\t *\n\t * @param locale - The locale to be added to the repository.\n\t *\n\t * @throws Error if setting the locale fails.\n\t */\n\tasync createCustomLocale(locale: CustomLocale): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t\"app/settings/multilanguages/custom\",\n\t\t\tlocale,\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 && JSON.stringify(data).includes(\"code is already used\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not create custom locale ${locale}`);\n\t\t}\n\t\tprofiler.done({\n\t\t\tmessage: `custom locale ${JSON.stringify(locale)} created`,\n\t\t});\n\t}\n\n\tprivate failIfNot200(response: AxiosResponse, msg: string) {\n\t\tif (response.status !== 200) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(msg);\n\t\t}\n\t}\n\n\t/**\n\t * Delete a locale from a repository.\n\t *\n\t * @param locale - locale to remove from the repository configuration.\n\t *\n\t * @throws Error if deleting the locale fails.\n\t */\n\tasync deleteLocale(locale: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`app/settings/multilanguages/${locale}/delete`,\n\t\t);\n\t\tthis.failIfNot200(response, `Could not delete locale ${locale}`);\n\t\tprofiler.done({ message: `locale '${locale}' deleted` });\n\t}\n\n\t/**\n\t * Set the default (master) locale for a repository in Wroom, the locale needs\n\t * to exist for the repository.\n\t *\n\t * @param locale - The locale to be set as the default.\n\t *\n\t * @throws Error if setting the default locale fails.\n\t */\n\tasync setDefaultLocale(locale: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`/app/settings/multilanguages/${locale}/createMasterLang`,\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 &&\n\t\t\t\t// returns 400 if master lang is already set\n\t\t\t\tJSON.stringify(data).includes(\"Unable to set as master language\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not set default locale to ${locale}`);\n\t\t}\n\t\tprofiler.done({ message: `default locale set to '${locale}'` });\n\t}\n\n\t/**\n\t * Creates a release.\n\t *\n\t * @param label - The label for the release.\n\t *\n\t * @returns A Promise that resolves when the release is created.\n\t */\n\tasync createRelease(label: string): Promise<{ id: string }> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(this.name, \"app/releases\", {\n\t\t\tlabel,\n\t\t});\n\t\tthis.failIfNot200(response, `Could not create release ${label}`);\n\t\tprofiler.done({ message: `release '${label}' created` });\n\n\t\treturn response.data;\n\t}\n\n\t/**\n\t * Deletes a repository release.\n\t *\n\t * @param repository - The name of the repository.\n\t * @param id - The ID of the release to be deleted.\n\t */\n\tasync deleteRelease(id: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`app/releases/${id}/delete`,\n\t\t\t{},\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not delete release with id ${id}`);\n\t\tprofiler.done({ message: `release '${id}' deleted` });\n\t}\n\n\t/**\n\t * Creates a preview for a repository.\n\t *\n\t * @param data - The data for creating the preview.\n\t */\n\tasync createPreview(data: Preview): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`previews/new`,\n\t\t\tdata,\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not create preview ${data.name}`);\n\t\tprofiler.done({ message: `preview '${data.name}' created` });\n\n\t\treturn response.data;\n\t}\n\n\t/**\n\t * Deletes a preview from a repository.\n\t *\n\t * @param id - The ID of the preview to be deleted.\n\t */\n\tasync deletePreview(id: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`previews/delete/${id}`,\n\t\t\t{},\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not delete preview with id${id}`);\n\t\tprofiler.done({ message: `preview '${id}' deleted` });\n\t}\n\n\t/**\n\t * Create Custom Types using the Custom types api.\n\t *\n\t * @param customTypes -\n\t */\n\tasync createCustomTypes(customTypes: CustomType[]): Promise<void> {\n\t\tawait this.customTypesApiClient.createCustomTypes(customTypes);\n\t}\n\n\t/**\n\t * Create slices using the Custom types api.\n\t *\n\t * @param slices -\n\t */\n\tasync createSlices(slices: SharedSlice[]): Promise<void> {\n\t\tawait this.customTypesApiClient.createSlices(slices);\n\t}\n}\n\nexport type RepositoryConfig = {\n\tcustomLocale?: CustomLocale;\n\tlocales?: string[];\n\tdefaultLocale?: string;\n\tslices?: SharedSlice[];\n\tcustomTypes?: CustomType[];\n\tpreview?: Preview;\n};\n\nexport type Preview = {\n\tname: string;\n\twebsiteURL: string;\n\tresolverPath: string;\n};\n\nexport type CustomLocale = {\n\tlang: {\n\t\tlabel: string;\n\t\t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AK,YAAY,KACvC,KAAK,MACL,mBAAmB,EAAE,IACrB,CAAE,CAAA;AAGH,SAAK,aAAa,UAAU,mCAAmC,EAAE,EAAE;AACnE,aAAS,KAAK,EAAE,SAAS,YAAY,EAAE,aAAa;AAAA,EACrD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,kBAAkB,aAAyB;AAC1C,UAAA,KAAK,qBAAqB,kBAAkB,WAAW;AAAA,EAC9D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,aAAa,QAAqB;AACjC,UAAA,KAAK,qBAAqB,aAAa,MAAM;AAAA,EACpD;AACA;"}
1
+ {"version":3,"file":"repository.js","sources":["../../../src/managers/repository.ts"],"sourcesContent":["import { AxiosResponse } from \"axios\";\n\nimport {\n\tCustomType,\n\tSharedSlice,\n} from \"@prismicio/types-internal/lib/customtypes\";\n\nimport type { CoreClient } from \"../clients/coreApi\";\nimport { CustomTypesClient } from \"../clients/customTypesApi\";\nimport { WroomClient } from \"../clients/wroom\";\nimport { logHttpResponse, logger } from \"../utils/log\";\nimport { getRepositoryUrl } from \"../utils/urls\";\n\n/** Utility to manage test data for a Prismic repository */\nexport class RepositoryManager {\n\tconstructor(\n\t\treadonly name: string,\n\t\tprivate readonly coreApiClient: CoreClient,\n\t\tprivate readonly wroomClient: WroomClient,\n\t\tprivate readonly customTypesApiClient: CustomTypesClient,\n\t) {}\n\n\tasync configure(config: RepositoryConfig): Promise<void> {\n\t\tconst hasLangConfig =\n\t\t\tconfig.defaultLocale || config.locales || config.customLocale;\n\t\tif (hasLangConfig) {\n\t\t\tconst languages = await this.coreApiClient.getLanguages();\n\n\t\t\t// default locale must be set first\n\t\t\tif (config.defaultLocale) {\n\t\t\t\tconst master = languages.find(({ is_master }) => is_master === true);\n\t\t\t\tif (!master) {\n\t\t\t\t\tawait this.setDefaultLocale(config.defaultLocale);\n\t\t\t\t} else if (master.id !== config.defaultLocale) {\n\t\t\t\t\tlogger.warn(\n\t\t\t\t\t\t`Master locale is already set to '${master.id}', cannot update it to '${config.defaultLocale}'`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (config.locales) {\n\t\t\t\tconst localesToAdd = config.locales.filter(\n\t\t\t\t\t(id) => !languages.some((language) => language.id === id),\n\t\t\t\t);\n\t\t\t\tif (localesToAdd.length > 0) {\n\t\t\t\t\tawait this.createLocales(localesToAdd);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (config.customLocale) {\n\t\t\t\tif (!languages.find(({ id }) => id === config.customLocale?.lang.id)) {\n\t\t\t\t\tawait this.createCustomLocale(config.customLocale);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (config.slices) {\n\t\t\tawait this.createSlices(config.slices);\n\t\t}\n\n\t\tif (config.customTypes) {\n\t\t\tawait this.createCustomTypes(config.customTypes);\n\t\t}\n\n\t\tif (config.preview) {\n\t\t\tawait this.createPreview(config.preview);\n\t\t}\n\t}\n\t/** @returns the repository base url, like https://my-repo.prismic.io */\n\tgetBaseURL(): string {\n\t\treturn getRepositoryUrl(this.wroomClient.getBaseURL(), this.name);\n\t}\n\n\t/**\n\t * Set additional standard locales for a repository in Wroom, does not fail if\n\t * the locales already exist.\n\t *\n\t * @param locales - An array of all locales to be added to the repository.\n\t *\n\t * @throws Error if setting the locales fails.\n\t */\n\tasync createLocales(locales: string[]): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t\"app/settings/multilanguages\",\n\t\t\t{ languages: locales },\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 &&\n\t\t\t\t(data as string).includes(\"already existing languages\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not add locales ${locales.join(\", \")}`);\n\t\t}\n\t\tprofiler.done({ message: `locales '${locales}' created` });\n\t}\n\n\t/**\n\t * Set additional custom locale for a repository in Wroom, does not fail if\n\t * the locales already exist.\n\t *\n\t * @param locale - The locale to be added to the repository.\n\t *\n\t * @throws Error if setting the locale fails.\n\t */\n\tasync createCustomLocale(locale: CustomLocale): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t\"app/settings/multilanguages/custom\",\n\t\t\tlocale,\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 && JSON.stringify(data).includes(\"code is already used\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not create custom locale ${locale}`);\n\t\t}\n\t\tprofiler.done({\n\t\t\tmessage: `custom locale ${JSON.stringify(locale)} created`,\n\t\t});\n\t}\n\n\tprivate failIfNot200(response: AxiosResponse, msg: string) {\n\t\tif (response.status !== 200) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(msg);\n\t\t}\n\t}\n\n\t/**\n\t * Delete a locale from a repository.\n\t *\n\t * @param locale - locale to remove from the repository configuration.\n\t *\n\t * @throws Error if deleting the locale fails.\n\t */\n\tasync deleteLocale(locale: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`app/settings/multilanguages/${locale}/delete`,\n\t\t);\n\t\tthis.failIfNot200(response, `Could not delete locale ${locale}`);\n\t\tprofiler.done({ message: `locale '${locale}' deleted` });\n\t}\n\n\t/**\n\t * Set the default (master) locale for a repository in Wroom, the locale needs\n\t * to exist for the repository.\n\t *\n\t * @param locale - The locale to be set as the default.\n\t *\n\t * @throws Error if setting the default locale fails.\n\t */\n\tasync setDefaultLocale(locale: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`/app/settings/multilanguages/${locale}/createMasterLang`,\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 &&\n\t\t\t\t// returns 400 if master lang is already set\n\t\t\t\tJSON.stringify(data).includes(\"Unable to set as master language\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not set default locale to ${locale}`);\n\t\t}\n\t\tprofiler.done({ message: `default locale set to '${locale}'` });\n\t}\n\n\t/**\n\t * Creates a release.\n\t *\n\t * @param label - The label for the release.\n\t *\n\t * @returns A Promise that resolves when the release is created.\n\t */\n\tasync createRelease(label: string): Promise<{ id: string }> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(this.name, \"app/releases\", {\n\t\t\tlabel,\n\t\t});\n\t\tthis.failIfNot200(response, `Could not create release ${label}`);\n\t\tprofiler.done({ message: `release '${label}' created` });\n\n\t\treturn response.data;\n\t}\n\n\t/**\n\t * Deletes a repository release.\n\t *\n\t * @param repository - The name of the repository.\n\t * @param id - The ID of the release to be deleted.\n\t */\n\tasync deleteRelease(id: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`app/releases/${id}/delete`,\n\t\t\t{},\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not delete release with id ${id}`);\n\t\tprofiler.done({ message: `release '${id}' deleted` });\n\t}\n\n\t/**\n\t * Creates a preview for a repository.\n\t *\n\t * @param data - The data for creating the preview.\n\t */\n\tasync createPreview(data: Preview): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`previews/new`,\n\t\t\tdata,\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not create preview ${data.name}`);\n\t\tprofiler.done({ message: `preview '${data.name}' created` });\n\n\t\treturn response.data;\n\t}\n\n\t/**\n\t * Deletes a preview from a repository.\n\t *\n\t * @param id - The ID of the preview to be deleted.\n\t */\n\tasync deletePreview(id: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`previews/delete/${id}`,\n\t\t\t{},\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not delete preview with id ${id}`);\n\t\tprofiler.done({ message: `preview '${id}' deleted` });\n\t}\n\n\t/**\n\t * Create Custom Types using the Custom types api.\n\t *\n\t * @param customTypes -\n\t */\n\tasync createCustomTypes(customTypes: CustomType[]): Promise<void> {\n\t\tawait this.customTypesApiClient.createCustomTypes(customTypes);\n\t}\n\n\t/**\n\t * Create slices using the Custom types api.\n\t *\n\t * @param slices -\n\t */\n\tasync createSlices(slices: SharedSlice[]): Promise<void> {\n\t\tawait this.customTypesApiClient.createSlices(slices);\n\t}\n\n\t/**\n\t * Create a permanent access token along with an application name\n\t *\n\t * @param appName - mandatory authorized application name\n\t */\n\tasync createPermanentAccessToken(appName: string): Promise<string> {\n\t\tconst profiler = logger.startTimer();\n\t\ttype AuthorizedAppResponse = {\n\t\t\tname: string;\n\t\t\twroom_auths: { token: string }[];\n\t\t};\n\t\tconst existingApps: AxiosResponse<AuthorizedAppResponse[]> =\n\t\t\tawait this.wroomClient.get(this.name, \"settings/security/contentapi\");\n\n\t\tthis.failIfNot200(\n\t\t\texistingApps,\n\t\t\t`Could not get status of existing applications to generate a permanent token`,\n\t\t);\n\t\tconst existingApp = existingApps.data.find(({ name, wroom_auths }) => {\n\t\t\treturn name === appName && wroom_auths.length > 0;\n\t\t});\n\t\tif (existingApp) {\n\t\t\treturn existingApp.wroom_auths[0].token;\n\t\t}\n\t\tconst response: AxiosResponse<AuthorizedAppResponse> =\n\t\t\tawait this.wroomClient.post(this.name, \"settings/security/oauthapp\", {\n\t\t\t\tapp_name: appName,\n\t\t\t});\n\n\t\tthis.failIfNot200(\n\t\t\tresponse,\n\t\t\t`Could not create permanent access token for app name ${appName}`,\n\t\t);\n\t\tprofiler.done({\n\t\t\tmessage: `permanent access token for app name '${appName}' created`,\n\t\t});\n\n\t\t// only return the generated token since there is no need to access more\n\t\treturn response.data.wroom_auths[0].token;\n\t}\n}\n\nexport type RepositoryConfig = {\n\tcustomLocale?: CustomLocale;\n\tlocales?: string[];\n\tdefaultLocale?: string;\n\tslices?: SharedSlice[];\n\tcustomTypes?: CustomType[];\n\tpreview?: Preview;\n};\n\nexport type Preview = {\n\tname: string;\n\twebsiteURL: string;\n\tresolverPath: string;\n};\n\nexport type CustomLocale = {\n\tlang: {\n\t\tlabel: string;\n\t\tid: string;\n\t\tuseStandardAnalyzer?: boolean;\n\t};\n\tregion: {\n\t\tlabel: string;\n\t\tid: string;\n\t};\n};\n"],"names":[],"mappings":";;;;;;;;MAca,kBAAiB;AAAA,EAC7B,YACU,MACQ,eACA,aACA,sBAAuC;AAH/C;AACQ;AACA;AACA;AAHR,SAAI,OAAJ;AACQ,SAAa,gBAAb;AACA,SAAW,cAAX;AACA,SAAoB,uBAApB;AAAA,EACf;AAAA,EAEH,MAAM,UAAU,QAAwB;AACvC,UAAM,gBACL,OAAO,iBAAiB,OAAO,WAAW,OAAO;AAClD,QAAI,eAAe;AAClB,YAAM,YAAY,MAAM,KAAK,cAAc,aAAY;AAGvD,UAAI,OAAO,eAAe;AACnB,cAAA,SAAS,UAAU,KAAK,CAAC,EAAE,gBAAgB,cAAc,IAAI;AACnE,YAAI,CAAC,QAAQ;AACN,gBAAA,KAAK,iBAAiB,OAAO,aAAa;AAAA,QACtC,WAAA,OAAO,OAAO,OAAO,eAAe;AAC9C,iBAAO,KACN,oCAAoC,OAAO,EAAE,2BAA2B,OAAO,aAAa,GAAG;AAAA,QAEhG;AAAA,MACD;AAED,UAAI,OAAO,SAAS;AACnB,cAAM,eAAe,OAAO,QAAQ,OACnC,CAAC,OAAO,CAAC,UAAU,KAAK,CAAC,aAAa,SAAS,OAAO,EAAE,CAAC;AAEtD,YAAA,aAAa,SAAS,GAAG;AACtB,gBAAA,KAA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AAAA,EAOA,MAAM,aAAa,QAAqB;AACjC,UAAA,KAAK,qBAAqB,aAAa,MAAM;AAAA,EACpD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,2BAA2B,SAAe;AACzC,UAAA,WAAW,OAAO;AAKxB,UAAM,eACL,MAAM,KAAK,YAAY,IAAI,KAAK,MAAM,8BAA8B;AAEhE,SAAA,aACJ,cACA,6EAA6E;AAExE,UAAA,cAAc,aAAa,KAAK,KAAK,CAAC,EAAE,MAAM,kBAAiB;AAC7D,aAAA,SAAS,WAAW,YAAY,SAAS;AAAA,IAAA,CAChD;AACD,QAAI,aAAa;AACT,aAAA,YAAY,YAAY,CAAC,EAAE;AAAA,IAClC;AACD,UAAM,WACL,MAAM,KAAK,YAAY,KAAK,KAAK,MAAM,8BAA8B;AAAA,MACpE,UAAU;AAAA,IAAA,CACV;AAEF,SAAK,aACJ,UACA,wDAAwD,OAAO,EAAE;AAElE,aAAS,KAAK;AAAA,MACb,SAAS,wCAAwC,OAAO;AAAA,IAAA,CACxD;AAGD,WAAO,SAAS,KAAK,YAAY,CAAC,EAAE;AAAA,EACrC;AACA;"}
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const getRepositoryUrl = (baseUrl, repository) => {
4
+ const url = new URL(baseUrl);
5
+ url.hostname = `${repository}.${url.hostname}`;
6
+ return url.toString();
7
+ };
8
+ exports.getRepositoryUrl = getRepositoryUrl;
9
+ //# sourceMappingURL=urls.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"urls.cjs","sources":["../../../src/utils/urls.ts"],"sourcesContent":["/**\n * Build a repository base url from the prismic base url\n *\n * @param baseUrl - prismic base url like prismic.io\n * @param repository - the repository name\n *\n * @returns the repository url like https://my-repo.prismic.io\n */\nexport const getRepositoryUrl = (\n\tbaseUrl: string,\n\trepository: string,\n): string => {\n\tconst url = new URL(baseUrl);\n\turl.hostname = `${repository}.${url.hostname}`;\n\n\treturn url.toString();\n};\n"],"names":[],"mappings":";;AAQa,MAAA,mBAAmB,CAC/B,SACA,eACW;AACL,QAAA,MAAM,IAAI,IAAI,OAAO;AAC3B,MAAI,WAAW,GAAG,UAAU,IAAI,IAAI,QAAQ;AAE5C,SAAO,IAAI;AACZ;;"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Build a repository base url from the prismic base url
3
+ *
4
+ * @param baseUrl - prismic base url like prismic.io
5
+ * @param repository - the repository name
6
+ *
7
+ * @returns the repository url like https://my-repo.prismic.io
8
+ */
9
+ export declare const getRepositoryUrl: (baseUrl: string, repository: string) => string;
@@ -0,0 +1,9 @@
1
+ const getRepositoryUrl = (baseUrl, repository) => {
2
+ const url = new URL(baseUrl);
3
+ url.hostname = `${repository}.${url.hostname}`;
4
+ return url.toString();
5
+ };
6
+ export {
7
+ getRepositoryUrl
8
+ };
9
+ //# sourceMappingURL=urls.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"urls.js","sources":["../../../src/utils/urls.ts"],"sourcesContent":["/**\n * Build a repository base url from the prismic base url\n *\n * @param baseUrl - prismic base url like prismic.io\n * @param repository - the repository name\n *\n * @returns the repository url like https://my-repo.prismic.io\n */\nexport const getRepositoryUrl = (\n\tbaseUrl: string,\n\trepository: string,\n): string => {\n\tconst url = new URL(baseUrl);\n\turl.hostname = `${repository}.${url.hostname}`;\n\n\treturn url.toString();\n};\n"],"names":[],"mappings":"AAQa,MAAA,mBAAmB,CAC/B,SACA,eACW;AACL,QAAA,MAAM,IAAI,IAAI,OAAO;AAC3B,MAAI,WAAW,GAAG,UAAU,IAAI,IAAI,QAAQ;AAE5C,SAAO,IAAI;AACZ;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/e2e-tests-utils",
3
- "version": "1.0.0-alpha.4",
3
+ "version": "1.1.0-alpha.0",
4
4
  "description": "A collection of utilities for to manage Prismic test data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -40,8 +40,8 @@
40
40
  "prepare": "npm run build",
41
41
  "release": "npm run test && standard-version && git push --follow-tags && npm run build && npm publish",
42
42
  "release:dry": "standard-version --dry-run",
43
- "release:alpha": "npm run test && standard-version --release-as major --prerelease alpha && git push --follow-tags && npm run build && npm publish --tag alpha",
44
- "release:alpha:dry": "standard-version --release-as major --prerelease alpha --dry-run",
43
+ "release:alpha": "npm run test && standard-version --prerelease alpha && git push --follow-tags && npm run build && npm publish --tag alpha",
44
+ "release:alpha:dry": "standard-version --prerelease alpha --dry-run",
45
45
  "lint": "eslint --ext .js,.ts .",
46
46
  "lint:fix": "eslint --fix --ext .js,.ts .",
47
47
  "types": "vitest --typecheck --run && tsc --noEmit",
@@ -0,0 +1,57 @@
1
+ import axios, { AxiosInstance } from "axios";
2
+
3
+ import { logHttpResponse, logger } from "../utils/log";
4
+
5
+ import { AuthenticationClient } from "./authenticationApi";
6
+
7
+ type Language = {
8
+ id: string;
9
+ name: string;
10
+ is_master: boolean;
11
+ };
12
+
13
+ export type CoreClient = {
14
+ getLanguages(): Promise<Language[]>;
15
+ };
16
+
17
+ /** Client for interacting with the core API */
18
+ export const createCoreApiClient = (
19
+ baseURL: string,
20
+ authClient: AuthenticationClient,
21
+ ): CoreClient => {
22
+ const client: AxiosInstance = axios.create({
23
+ baseURL,
24
+ validateStatus: () => true, // Don't throw on 4XX errors
25
+ });
26
+
27
+ // Add an interceptor to authenticate requests
28
+ client.interceptors.request.use(async (config) => {
29
+ const auth = "Authorization";
30
+ if (!config.headers[auth]) {
31
+ const token = await authClient.getToken();
32
+ config.headers[auth] = `Bearer ${token}`;
33
+ }
34
+
35
+ return config;
36
+ });
37
+
38
+ async function getLanguages(): Promise<Language[]> {
39
+ const profiler = logger.startTimer();
40
+ const result = await client.get<{ languages: Language[] }>(
41
+ "core/repository",
42
+ );
43
+
44
+ if (![200].includes(result.status) || !result.data.languages) {
45
+ logHttpResponse(result);
46
+ throw new Error("Could not get languages from the core api.");
47
+ }
48
+
49
+ profiler.done({ message: "retrieved languages configuration" });
50
+
51
+ return result.data.languages;
52
+ }
53
+
54
+ return {
55
+ getLanguages,
56
+ };
57
+ };
@@ -4,6 +4,7 @@ import { Credentials } from "../types";
4
4
 
5
5
  import { extractCookie } from "../utils/cookies";
6
6
  import { logHttpResponse, logger } from "../utils/log";
7
+ import { getRepositoryUrl } from "../utils/urls";
7
8
 
8
9
  /**
9
10
  * Client for interacting with the Wroom service to perform operations on
@@ -46,6 +47,20 @@ export class WroomClient {
46
47
  return this.client.getUri();
47
48
  }
48
49
 
50
+ /** authenticated GET request on the wroom backend */
51
+ async get(repository: string | null, path: string): Promise<AxiosResponse> {
52
+ if (!this.loggedIn) {
53
+ await this.login();
54
+ }
55
+
56
+ let url = new URL(path, this.client.getUri()).toString();
57
+ if (repository) {
58
+ url = getRepositoryUrl(url, repository);
59
+ }
60
+
61
+ return this.client.get(url);
62
+ }
63
+
49
64
  /** authenticated POST request on the wroom backend */
50
65
  async post(
51
66
  repository: string | null,
@@ -56,9 +71,9 @@ export class WroomClient {
56
71
  await this.login();
57
72
  }
58
73
 
59
- const url = new URL(path, this.client.getUri());
74
+ let url = new URL(path, this.client.getUri()).toString();
60
75
  if (repository) {
61
- url.hostname = `${repository}.${url.hostname}`;
76
+ url = getRepositoryUrl(url, repository);
62
77
  }
63
78
  const response = await this.client.post(url.toString(), data, {
64
79
  params: {
@@ -4,11 +4,13 @@ import {
4
4
  AuthenticationClient,
5
5
  createAuthenticationApiClient,
6
6
  } from "../clients/authenticationApi";
7
+ import { createCoreApiClient } from "../clients/coreApi";
7
8
  import { createCustomTypesApiClient } from "../clients/customTypesApi";
8
9
  import { WroomClient } from "../clients/wroom";
9
10
  import { EnvVariableManager } from "../utils/envVariableManager";
10
11
  import { logHttpResponse, logger } from "../utils/log";
11
12
  import { randomString } from "../utils/random";
13
+ import { getRepositoryUrl } from "../utils/urls";
12
14
 
13
15
  import { RepositoryConfig, RepositoryManager } from "./repository";
14
16
 
@@ -49,18 +51,18 @@ export class RepositoriesManager {
49
51
  );
50
52
 
51
53
  /**
52
- * @param prismicUrl - The base URL of the Wroom app.
54
+ * @param urlConfig - The base URL of the Wroom app.
53
55
  * @param credentials - Authentication credentials (email and password)
54
56
  */
55
57
  constructor(
56
- urls: UrlConfig | string,
58
+ urlConfig: UrlConfig | string,
57
59
  private readonly credentials: Credentials,
58
60
  ) {
59
- if (typeof urls === "string") {
61
+ if (typeof urlConfig === "string") {
60
62
  // When a string is provided, treat it as the 'base' property
61
- urls = this.inferUrls(urls);
63
+ urlConfig = this.inferUrls(urlConfig);
62
64
  }
63
- this.urls = urls;
65
+ this.urls = urlConfig;
64
66
  this.wroomClient = new WroomClient(this.urls.baseURL, this.credentials);
65
67
  this.authClient = createAuthenticationApiClient(
66
68
  this.urls.authenticationApi,
@@ -185,8 +187,15 @@ export class RepositoriesManager {
185
187
  name,
186
188
  this.authClient,
187
189
  );
190
+ const coreApiUrl = getRepositoryUrl(this.urls.baseURL, name);
191
+ const coreApiClient = createCoreApiClient(coreApiUrl, this.authClient);
188
192
 
189
- return new RepositoryManager(name, this.wroomClient, customTypeClient);
193
+ return new RepositoryManager(
194
+ name,
195
+ coreApiClient,
196
+ this.wroomClient,
197
+ customTypeClient,
198
+ );
190
199
  }
191
200
  }
192
201
 
@@ -5,29 +5,53 @@ import {
5
5
  SharedSlice,
6
6
  } from "@prismicio/types-internal/lib/customtypes";
7
7
 
8
+ import type { CoreClient } from "../clients/coreApi";
8
9
  import { CustomTypesClient } from "../clients/customTypesApi";
9
10
  import { WroomClient } from "../clients/wroom";
10
11
  import { logHttpResponse, logger } from "../utils/log";
12
+ import { getRepositoryUrl } from "../utils/urls";
11
13
 
12
14
  /** Utility to manage test data for a Prismic repository */
13
15
  export class RepositoryManager {
14
16
  constructor(
15
17
  readonly name: string,
18
+ private readonly coreApiClient: CoreClient,
16
19
  private readonly wroomClient: WroomClient,
17
20
  private readonly customTypesApiClient: CustomTypesClient,
18
21
  ) {}
19
22
 
20
23
  async configure(config: RepositoryConfig): Promise<void> {
21
- if (config.defaultLocale) {
22
- await this.setDefaultLocale(config.defaultLocale);
23
- }
24
-
25
- if (config.locales) {
26
- await this.createLocales(config.locales);
27
- }
28
-
29
- if (config.customLocale) {
30
- await this.createCustomLocale(config.customLocale);
24
+ const hasLangConfig =
25
+ config.defaultLocale || config.locales || config.customLocale;
26
+ if (hasLangConfig) {
27
+ const languages = await this.coreApiClient.getLanguages();
28
+
29
+ // default locale must be set first
30
+ if (config.defaultLocale) {
31
+ const master = languages.find(({ is_master }) => is_master === true);
32
+ if (!master) {
33
+ await this.setDefaultLocale(config.defaultLocale);
34
+ } else if (master.id !== config.defaultLocale) {
35
+ logger.warn(
36
+ `Master locale is already set to '${master.id}', cannot update it to '${config.defaultLocale}'`,
37
+ );
38
+ }
39
+ }
40
+
41
+ if (config.locales) {
42
+ const localesToAdd = config.locales.filter(
43
+ (id) => !languages.some((language) => language.id === id),
44
+ );
45
+ if (localesToAdd.length > 0) {
46
+ await this.createLocales(localesToAdd);
47
+ }
48
+ }
49
+
50
+ if (config.customLocale) {
51
+ if (!languages.find(({ id }) => id === config.customLocale?.lang.id)) {
52
+ await this.createCustomLocale(config.customLocale);
53
+ }
54
+ }
31
55
  }
32
56
 
33
57
  if (config.slices) {
@@ -44,10 +68,7 @@ export class RepositoryManager {
44
68
  }
45
69
  /** @returns the repository base url, like https://my-repo.prismic.io */
46
70
  getBaseURL(): string {
47
- const url = new URL(this.wroomClient.getBaseURL());
48
- url.hostname = `${this.name}.${url.hostname}`;
49
-
50
- return url.toString();
71
+ return getRepositoryUrl(this.wroomClient.getBaseURL(), this.name);
51
72
  }
52
73
 
53
74
  /**
@@ -235,7 +256,7 @@ export class RepositoryManager {
235
256
  {},
236
257
  );
237
258
 
238
- this.failIfNot200(response, `Could not delete preview with id${id}`);
259
+ this.failIfNot200(response, `Could not delete preview with id ${id}`);
239
260
  profiler.done({ message: `preview '${id}' deleted` });
240
261
  }
241
262
 
@@ -256,6 +277,47 @@ export class RepositoryManager {
256
277
  async createSlices(slices: SharedSlice[]): Promise<void> {
257
278
  await this.customTypesApiClient.createSlices(slices);
258
279
  }
280
+
281
+ /**
282
+ * Create a permanent access token along with an application name
283
+ *
284
+ * @param appName - mandatory authorized application name
285
+ */
286
+ async createPermanentAccessToken(appName: string): Promise<string> {
287
+ const profiler = logger.startTimer();
288
+ type AuthorizedAppResponse = {
289
+ name: string;
290
+ wroom_auths: { token: string }[];
291
+ };
292
+ const existingApps: AxiosResponse<AuthorizedAppResponse[]> =
293
+ await this.wroomClient.get(this.name, "settings/security/contentapi");
294
+
295
+ this.failIfNot200(
296
+ existingApps,
297
+ `Could not get status of existing applications to generate a permanent token`,
298
+ );
299
+ const existingApp = existingApps.data.find(({ name, wroom_auths }) => {
300
+ return name === appName && wroom_auths.length > 0;
301
+ });
302
+ if (existingApp) {
303
+ return existingApp.wroom_auths[0].token;
304
+ }
305
+ const response: AxiosResponse<AuthorizedAppResponse> =
306
+ await this.wroomClient.post(this.name, "settings/security/oauthapp", {
307
+ app_name: appName,
308
+ });
309
+
310
+ this.failIfNot200(
311
+ response,
312
+ `Could not create permanent access token for app name ${appName}`,
313
+ );
314
+ profiler.done({
315
+ message: `permanent access token for app name '${appName}' created`,
316
+ });
317
+
318
+ // only return the generated token since there is no need to access more
319
+ return response.data.wroom_auths[0].token;
320
+ }
259
321
  }
260
322
 
261
323
  export type RepositoryConfig = {
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Build a repository base url from the prismic base url
3
+ *
4
+ * @param baseUrl - prismic base url like prismic.io
5
+ * @param repository - the repository name
6
+ *
7
+ * @returns the repository url like https://my-repo.prismic.io
8
+ */
9
+ export const getRepositoryUrl = (
10
+ baseUrl: string,
11
+ repository: string,
12
+ ): string => {
13
+ const url = new URL(baseUrl);
14
+ url.hostname = `${repository}.${url.hostname}`;
15
+
16
+ return url.toString();
17
+ };