@prismicio/e2e-tests-utils 1.2.0 → 1.3.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.
- package/dist/clients/authenticationApi.cjs +21 -2
- package/dist/clients/authenticationApi.cjs.map +1 -1
- package/dist/clients/authenticationApi.d.ts +3 -2
- package/dist/clients/authenticationApi.js +22 -3
- package/dist/clients/authenticationApi.js.map +1 -1
- package/dist/clients/manageV2.cjs +145 -0
- package/dist/clients/manageV2.cjs.map +1 -0
- package/dist/clients/manageV2.d.ts +91 -0
- package/dist/clients/manageV2.js +145 -0
- package/dist/clients/manageV2.js.map +1 -0
- package/dist/clients/wroom.cjs +28 -0
- package/dist/clients/wroom.cjs.map +1 -1
- package/dist/clients/wroom.d.ts +22 -2
- package/dist/clients/wroom.js +28 -0
- package/dist/clients/wroom.js.map +1 -1
- package/dist/managers/repositories.cjs +20 -17
- package/dist/managers/repositories.cjs.map +1 -1
- package/dist/managers/repositories.d.ts +12 -22
- package/dist/managers/repositories.js +20 -17
- package/dist/managers/repositories.js.map +1 -1
- package/dist/managers/repository.cjs +136 -41
- package/dist/managers/repository.cjs.map +1 -1
- package/dist/managers/repository.d.ts +52 -22
- package/dist/managers/repository.js +136 -41
- package/dist/managers/repository.js.map +1 -1
- package/dist/types.d.ts +18 -1
- package/package.json +3 -1
- package/src/clients/authenticationApi.ts +33 -4
- package/src/clients/manageV2.ts +178 -0
- package/src/clients/wroom.ts +42 -2
- package/src/managers/repositories.ts +39 -37
- package/src/managers/repository.ts +197 -71
- package/src/types.ts +27 -1
|
@@ -19,7 +19,9 @@ const createAuthenticationApiClient = (baseURL, auth) => {
|
|
|
19
19
|
log.logHttpResponse(result);
|
|
20
20
|
throw new Error("Authentication failed, no token received.");
|
|
21
21
|
}
|
|
22
|
-
profiler.done({
|
|
22
|
+
profiler.done({
|
|
23
|
+
message: `generated user token for ${auth.email} from auth service`
|
|
24
|
+
});
|
|
23
25
|
return result.data;
|
|
24
26
|
}
|
|
25
27
|
async function getToken() {
|
|
@@ -28,8 +30,25 @@ const createAuthenticationApiClient = (baseURL, auth) => {
|
|
|
28
30
|
}
|
|
29
31
|
return authToken;
|
|
30
32
|
}
|
|
33
|
+
async function getMachine2MachineToken(repository) {
|
|
34
|
+
if (!authToken) {
|
|
35
|
+
authToken = await login();
|
|
36
|
+
}
|
|
37
|
+
const result = await client.get("machine2machine", {
|
|
38
|
+
headers: {
|
|
39
|
+
Authorization: `Bearer ${authToken}`,
|
|
40
|
+
repository
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
if (![200].includes(result.status) || !result.data.token) {
|
|
44
|
+
log.logHttpResponse(result);
|
|
45
|
+
throw new Error("Machine2Machine token generation failed, no token received.");
|
|
46
|
+
}
|
|
47
|
+
return result.data.token;
|
|
48
|
+
}
|
|
31
49
|
return {
|
|
32
|
-
getToken
|
|
50
|
+
getToken,
|
|
51
|
+
getMachine2MachineToken
|
|
33
52
|
};
|
|
34
53
|
};
|
|
35
54
|
exports.createAuthenticationApiClient = createAuthenticationApiClient;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authenticationApi.cjs","sources":["../../../src/clients/authenticationApi.ts"],"sourcesContent":["import axios, { AxiosInstance } from \"axios\";\n\nimport {
|
|
1
|
+
{"version":3,"file":"authenticationApi.cjs","sources":["../../../src/clients/authenticationApi.ts"],"sourcesContent":["import axios, { AxiosInstance } from \"axios\";\n\nimport { AuthConfig } from \"../types\";\n\nimport { logHttpResponse, logger } from \"../utils/log\";\n\nexport type AuthenticationClient = {\n\tgetToken: () => Promise<string>;\n\tgetMachine2MachineToken: (repository: string) => Promise<string>;\n};\n\n/** Client for interacting with the authentication service to manage tokens */\nexport const createAuthenticationApiClient = (\n\tbaseURL: string,\n\tauth: AuthConfig,\n): AuthenticationClient => {\n\tlet authToken: string;\n\n\tconst client: AxiosInstance = axios.create({\n\t\tbaseURL,\n\t\tvalidateStatus: () => true, // Don't throw on 4XX errors\n\t});\n\n\tasync function login(): Promise<string> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst result = await client.post(\"login\", {\n\t\t\temail: auth.email,\n\t\t\tpassword: auth.password,\n\t\t});\n\n\t\tif (!result.data || typeof result.data !== \"string\") {\n\t\t\tlogHttpResponse(result);\n\t\t\tthrow new Error(\"Authentication failed, no token received.\");\n\t\t}\n\n\t\tprofiler.done({\n\t\t\tmessage: `generated user token for ${auth.email} from auth service`,\n\t\t});\n\n\t\treturn result.data;\n\t}\n\n\t/** Return an api user token. Creates one if needed. */\n\tasync function getToken(): Promise<string> {\n\t\tif (!authToken) {\n\t\t\tauthToken = await login();\n\t\t}\n\n\t\treturn authToken;\n\t}\n\n\t/** Return an api user token. Creates one if needed. */\n\tasync function getMachine2MachineToken(repository: string): Promise<string> {\n\t\tif (!authToken) {\n\t\t\tauthToken = await login();\n\t\t}\n\t\tconst result = await client.get<{ token: string; timestamp: number }>(\n\t\t\t\"machine2machine\",\n\t\t\t{\n\t\t\t\theaders: {\n\t\t\t\t\tAuthorization: `Bearer ${authToken}`,\n\t\t\t\t\trepository,\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\n\t\tif (![200].includes(result.status) || !result.data.token) {\n\t\t\tlogHttpResponse(result);\n\t\t\tthrow new Error(\n\t\t\t\t\"Machine2Machine token generation failed, no token received.\",\n\t\t\t);\n\t\t}\n\n\t\treturn result.data.token;\n\t}\n\n\treturn {\n\t\tgetToken,\n\t\tgetMachine2MachineToken,\n\t};\n};\n"],"names":["logger","logHttpResponse"],"mappings":";;;;AAYa,MAAA,gCAAgC,CAC5C,SACA,SACyB;AACrB,MAAA;AAEE,QAAA,SAAwB,MAAM,OAAO;AAAA,IAC1C;AAAA,IACA,gBAAgB,MAAM;AAAA;AAAA,EAAA,CACtB;AAED,iBAAe,QAAK;AACb,UAAA,WAAWA,WAAO;AAExB,UAAM,SAAS,MAAM,OAAO,KAAK,SAAS;AAAA,MACzC,OAAO,KAAK;AAAA,MACZ,UAAU,KAAK;AAAA,IAAA,CACf;AAED,QAAI,CAAC,OAAO,QAAQ,OAAO,OAAO,SAAS,UAAU;AACpDC,UAAA,gBAAgB,MAAM;AAChB,YAAA,IAAI,MAAM,2CAA2C;AAAA,IAC3D;AAED,aAAS,KAAK;AAAA,MACb,SAAS,4BAA4B,KAAK,KAAK;AAAA,IAAA,CAC/C;AAED,WAAO,OAAO;AAAA,EACf;AAGA,iBAAe,WAAQ;AACtB,QAAI,CAAC,WAAW;AACf,kBAAY,MAAM;IAClB;AAEM,WAAA;AAAA,EACR;AAGA,iBAAe,wBAAwB,YAAkB;AACxD,QAAI,CAAC,WAAW;AACf,kBAAY,MAAM;IAClB;AACD,UAAM,SAAS,MAAM,OAAO,IAC3B,mBACA;AAAA,MACC,SAAS;AAAA,QACR,eAAe,UAAU,SAAS;AAAA,QAClC;AAAA,MACA;AAAA,IAAA,CACD;AAGE,QAAA,CAAC,CAAC,GAAG,EAAE,SAAS,OAAO,MAAM,KAAK,CAAC,OAAO,KAAK,OAAO;AACzDA,UAAA,gBAAgB,MAAM;AAChB,YAAA,IAAI,MACT,6DAA6D;AAAA,IAE9D;AAED,WAAO,OAAO,KAAK;AAAA,EACpB;AAEO,SAAA;AAAA,IACN;AAAA,IACA;AAAA,EAAA;AAEF;;"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AuthConfig } from "../types";
|
|
2
2
|
export type AuthenticationClient = {
|
|
3
3
|
getToken: () => Promise<string>;
|
|
4
|
+
getMachine2MachineToken: (repository: string) => Promise<string>;
|
|
4
5
|
};
|
|
5
6
|
/** Client for interacting with the authentication service to manage tokens */
|
|
6
|
-
export declare const createAuthenticationApiClient: (baseURL: string, auth:
|
|
7
|
+
export declare const createAuthenticationApiClient: (baseURL: string, auth: AuthConfig) => AuthenticationClient;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import axios from "axios";
|
|
2
|
-
import {
|
|
2
|
+
import { logHttpResponse, logger } from "../utils/log.js";
|
|
3
3
|
const createAuthenticationApiClient = (baseURL, auth) => {
|
|
4
4
|
let authToken;
|
|
5
5
|
const client = axios.create({
|
|
@@ -17,7 +17,9 @@ const createAuthenticationApiClient = (baseURL, auth) => {
|
|
|
17
17
|
logHttpResponse(result);
|
|
18
18
|
throw new Error("Authentication failed, no token received.");
|
|
19
19
|
}
|
|
20
|
-
profiler.done({
|
|
20
|
+
profiler.done({
|
|
21
|
+
message: `generated user token for ${auth.email} from auth service`
|
|
22
|
+
});
|
|
21
23
|
return result.data;
|
|
22
24
|
}
|
|
23
25
|
async function getToken() {
|
|
@@ -26,8 +28,25 @@ const createAuthenticationApiClient = (baseURL, auth) => {
|
|
|
26
28
|
}
|
|
27
29
|
return authToken;
|
|
28
30
|
}
|
|
31
|
+
async function getMachine2MachineToken(repository) {
|
|
32
|
+
if (!authToken) {
|
|
33
|
+
authToken = await login();
|
|
34
|
+
}
|
|
35
|
+
const result = await client.get("machine2machine", {
|
|
36
|
+
headers: {
|
|
37
|
+
Authorization: `Bearer ${authToken}`,
|
|
38
|
+
repository
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
if (![200].includes(result.status) || !result.data.token) {
|
|
42
|
+
logHttpResponse(result);
|
|
43
|
+
throw new Error("Machine2Machine token generation failed, no token received.");
|
|
44
|
+
}
|
|
45
|
+
return result.data.token;
|
|
46
|
+
}
|
|
29
47
|
return {
|
|
30
|
-
getToken
|
|
48
|
+
getToken,
|
|
49
|
+
getMachine2MachineToken
|
|
31
50
|
};
|
|
32
51
|
};
|
|
33
52
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authenticationApi.js","sources":["../../../src/clients/authenticationApi.ts"],"sourcesContent":["import axios, { AxiosInstance } from \"axios\";\n\nimport {
|
|
1
|
+
{"version":3,"file":"authenticationApi.js","sources":["../../../src/clients/authenticationApi.ts"],"sourcesContent":["import axios, { AxiosInstance } from \"axios\";\n\nimport { AuthConfig } from \"../types\";\n\nimport { logHttpResponse, logger } from \"../utils/log\";\n\nexport type AuthenticationClient = {\n\tgetToken: () => Promise<string>;\n\tgetMachine2MachineToken: (repository: string) => Promise<string>;\n};\n\n/** Client for interacting with the authentication service to manage tokens */\nexport const createAuthenticationApiClient = (\n\tbaseURL: string,\n\tauth: AuthConfig,\n): AuthenticationClient => {\n\tlet authToken: string;\n\n\tconst client: AxiosInstance = axios.create({\n\t\tbaseURL,\n\t\tvalidateStatus: () => true, // Don't throw on 4XX errors\n\t});\n\n\tasync function login(): Promise<string> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst result = await client.post(\"login\", {\n\t\t\temail: auth.email,\n\t\t\tpassword: auth.password,\n\t\t});\n\n\t\tif (!result.data || typeof result.data !== \"string\") {\n\t\t\tlogHttpResponse(result);\n\t\t\tthrow new Error(\"Authentication failed, no token received.\");\n\t\t}\n\n\t\tprofiler.done({\n\t\t\tmessage: `generated user token for ${auth.email} from auth service`,\n\t\t});\n\n\t\treturn result.data;\n\t}\n\n\t/** Return an api user token. Creates one if needed. */\n\tasync function getToken(): Promise<string> {\n\t\tif (!authToken) {\n\t\t\tauthToken = await login();\n\t\t}\n\n\t\treturn authToken;\n\t}\n\n\t/** Return an api user token. Creates one if needed. */\n\tasync function getMachine2MachineToken(repository: string): Promise<string> {\n\t\tif (!authToken) {\n\t\t\tauthToken = await login();\n\t\t}\n\t\tconst result = await client.get<{ token: string; timestamp: number }>(\n\t\t\t\"machine2machine\",\n\t\t\t{\n\t\t\t\theaders: {\n\t\t\t\t\tAuthorization: `Bearer ${authToken}`,\n\t\t\t\t\trepository,\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\n\t\tif (![200].includes(result.status) || !result.data.token) {\n\t\t\tlogHttpResponse(result);\n\t\t\tthrow new Error(\n\t\t\t\t\"Machine2Machine token generation failed, no token received.\",\n\t\t\t);\n\t\t}\n\n\t\treturn result.data.token;\n\t}\n\n\treturn {\n\t\tgetToken,\n\t\tgetMachine2MachineToken,\n\t};\n};\n"],"names":[],"mappings":";;AAYa,MAAA,gCAAgC,CAC5C,SACA,SACyB;AACrB,MAAA;AAEE,QAAA,SAAwB,MAAM,OAAO;AAAA,IAC1C;AAAA,IACA,gBAAgB,MAAM;AAAA;AAAA,EAAA,CACtB;AAED,iBAAe,QAAK;AACb,UAAA,WAAW,OAAO;AAExB,UAAM,SAAS,MAAM,OAAO,KAAK,SAAS;AAAA,MACzC,OAAO,KAAK;AAAA,MACZ,UAAU,KAAK;AAAA,IAAA,CACf;AAED,QAAI,CAAC,OAAO,QAAQ,OAAO,OAAO,SAAS,UAAU;AACpD,sBAAgB,MAAM;AAChB,YAAA,IAAI,MAAM,2CAA2C;AAAA,IAC3D;AAED,aAAS,KAAK;AAAA,MACb,SAAS,4BAA4B,KAAK,KAAK;AAAA,IAAA,CAC/C;AAED,WAAO,OAAO;AAAA,EACf;AAGA,iBAAe,WAAQ;AACtB,QAAI,CAAC,WAAW;AACf,kBAAY,MAAM;IAClB;AAEM,WAAA;AAAA,EACR;AAGA,iBAAe,wBAAwB,YAAkB;AACxD,QAAI,CAAC,WAAW;AACf,kBAAY,MAAM;IAClB;AACD,UAAM,SAAS,MAAM,OAAO,IAC3B,mBACA;AAAA,MACC,SAAS;AAAA,QACR,eAAe,UAAU,SAAS;AAAA,QAClC;AAAA,MACA;AAAA,IAAA,CACD;AAGE,QAAA,CAAC,CAAC,GAAG,EAAE,SAAS,OAAO,MAAM,KAAK,CAAC,OAAO,KAAK,OAAO;AACzD,sBAAgB,MAAM;AAChB,YAAA,IAAI,MACT,6DAA6D;AAAA,IAE9D;AAED,WAAO,OAAO,KAAK;AAAA,EACpB;AAEO,SAAA;AAAA,IACN;AAAA,IACA;AAAA,EAAA;AAEF;"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
|
+
var __publicField = (obj, key, value) => {
|
|
5
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
6
|
+
return value;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
9
|
+
const axios = require("axios");
|
|
10
|
+
const jwt = require("jsonwebtoken");
|
|
11
|
+
const cookies = require("../utils/cookies.cjs");
|
|
12
|
+
const ManageV2StaticAuthor = "prismic-e2e-tests-utils";
|
|
13
|
+
class ManageV2Client {
|
|
14
|
+
/**
|
|
15
|
+
* @param baseURL - The base URL of the Wroom app. ex: https://prismic.io
|
|
16
|
+
* @param config - ManageV2 configuration variable to call ManageV2.
|
|
17
|
+
*/
|
|
18
|
+
constructor(baseUrl, config) {
|
|
19
|
+
__publicField(this, "client");
|
|
20
|
+
// Not private to have it available on tests
|
|
21
|
+
__publicField(this, "token", null);
|
|
22
|
+
/**
|
|
23
|
+
* The function `toggleRolePerLocal` enable or disable the role per local
|
|
24
|
+
* feature
|
|
25
|
+
*
|
|
26
|
+
* @param repository - The Repository name
|
|
27
|
+
* @param enabled - The feature new status
|
|
28
|
+
*/
|
|
29
|
+
__publicField(this, "toggleRolePerLocal", this.assertTokenExist((params) => {
|
|
30
|
+
return this.client.post("/repository/toggleRolesPerLocale", {
|
|
31
|
+
domain: params.repository,
|
|
32
|
+
enabled: params.enabled,
|
|
33
|
+
author: ManageV2StaticAuthor
|
|
34
|
+
});
|
|
35
|
+
}));
|
|
36
|
+
/**
|
|
37
|
+
* The function `changePlan` changes the plan of a repository the database
|
|
38
|
+
*
|
|
39
|
+
* @param repository - The Repository name
|
|
40
|
+
* @param newPlanId - The new planId
|
|
41
|
+
* @param bypassManualBilling - Beware that using this will not verify that
|
|
42
|
+
* the database and Stripe are in sync
|
|
43
|
+
*/
|
|
44
|
+
__publicField(this, "changePlan", this.assertTokenExist(({ repository, newPlanId, bypassManualBilling = false }) => {
|
|
45
|
+
return this.client.post(`/repository/changePlan?bypassManualBilling=${bypassManualBilling}`, {
|
|
46
|
+
domain: repository,
|
|
47
|
+
newPlan: newPlanId,
|
|
48
|
+
author: ManageV2StaticAuthor
|
|
49
|
+
});
|
|
50
|
+
}));
|
|
51
|
+
/**
|
|
52
|
+
* The function `addUserToRepository` adds a user corresponding to the email
|
|
53
|
+
* to the given repository
|
|
54
|
+
*
|
|
55
|
+
* @param repository - The Repository name
|
|
56
|
+
* @param email - The user email
|
|
57
|
+
*/
|
|
58
|
+
__publicField(this, "addUserToRepository", this.assertTokenExist(({ repository, email }) => {
|
|
59
|
+
return this.client.post("/repository/addUser", {
|
|
60
|
+
domain: repository,
|
|
61
|
+
email,
|
|
62
|
+
author: ManageV2StaticAuthor
|
|
63
|
+
});
|
|
64
|
+
}));
|
|
65
|
+
/**
|
|
66
|
+
* The function `removeUserFromRepository` removes a user corresponding to the
|
|
67
|
+
* email from the given repository
|
|
68
|
+
*
|
|
69
|
+
* @param repository - The Repository name
|
|
70
|
+
* @param email - The user email
|
|
71
|
+
*/
|
|
72
|
+
__publicField(this, "removeUserFromRepository", this.assertTokenExist(({ repository, email }) => {
|
|
73
|
+
return this.client.post("/repository/removeUser", {
|
|
74
|
+
domain: repository,
|
|
75
|
+
email,
|
|
76
|
+
author: ManageV2StaticAuthor
|
|
77
|
+
});
|
|
78
|
+
}));
|
|
79
|
+
this.token = config ? this.generateToken(config) : null;
|
|
80
|
+
this.client = axios.create({
|
|
81
|
+
baseURL: `${baseUrl}/manageroutes/`,
|
|
82
|
+
withCredentials: true,
|
|
83
|
+
validateStatus: () => true,
|
|
84
|
+
headers: {
|
|
85
|
+
"User-Agent": "prismic-cli/prismic-e2e-tests-utils",
|
|
86
|
+
Authorization: `Bearer ${this.token}`
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
this.client.interceptors.response.use((response) => {
|
|
90
|
+
const cookies$1 = response.headers["set-cookie"];
|
|
91
|
+
if (cookies$1 && cookies.extractCookie(cookies$1, "SESSION")) {
|
|
92
|
+
this.client.defaults.headers["Cookie"] = cookies$1;
|
|
93
|
+
}
|
|
94
|
+
return response;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The function generates a JWT token using the provided configuration
|
|
99
|
+
* parameters.
|
|
100
|
+
*
|
|
101
|
+
* @param {ManageV2Config} config - The `config` parameter in the
|
|
102
|
+
* `generateToken` function is of type `ManageV2Config`. It contains the
|
|
103
|
+
* following properties:
|
|
104
|
+
*
|
|
105
|
+
* @returns A JSON Web Token (JWT) is being returned by the `generateToken`
|
|
106
|
+
* function. The token is signed using the provided `config.secret` with the
|
|
107
|
+
* HS256 algorithm and includes the specified audience and issuer values.
|
|
108
|
+
*/
|
|
109
|
+
generateToken(config) {
|
|
110
|
+
return jwt.sign({}, config.secret, {
|
|
111
|
+
algorithm: "HS256",
|
|
112
|
+
audience: config.audience,
|
|
113
|
+
issuer: "prismic.io"
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The function `assertTokenExist` checks if a token exists before executing a
|
|
118
|
+
* given function.
|
|
119
|
+
*
|
|
120
|
+
* @param f - The `assertTokenExist` function takes a function `f` as a
|
|
121
|
+
* parameter. This function `f` should accept a parameter of type
|
|
122
|
+
* `Parameters` and return a Promise that resolves to an AxiosResponse. The
|
|
123
|
+
* `assertTokenExist` function ensures that a token is not null before
|
|
124
|
+
* calling the provided
|
|
125
|
+
*
|
|
126
|
+
* @returns A function is being returned that takes a parameter of type
|
|
127
|
+
* Parameters and checks if the token is null. If the token is null, an
|
|
128
|
+
* error is thrown. Otherwise, the function f is called with the provided
|
|
129
|
+
* parameters.
|
|
130
|
+
*/
|
|
131
|
+
assertTokenExist(f) {
|
|
132
|
+
return (params) => {
|
|
133
|
+
if (this.token === null) {
|
|
134
|
+
throw new Error("Undefined configuration for ManageV2 while trying to use it's routes");
|
|
135
|
+
} else {
|
|
136
|
+
return f(params);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
getBaseURL() {
|
|
141
|
+
return this.client.getUri();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
exports.ManageV2Client = ManageV2Client;
|
|
145
|
+
//# sourceMappingURL=manageV2.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manageV2.cjs","sources":["../../../src/clients/manageV2.ts"],"sourcesContent":["import axios, { AxiosInstance, AxiosResponse } from \"axios\";\nimport jwt from \"jsonwebtoken\";\n\nimport { ManageV2Config } from \"../types\";\n\nimport { extractCookie } from \"../utils/cookies\";\n\nconst ManageV2StaticAuthor = \"prismic-e2e-tests-utils\";\n\n/**\n * Client for interacting with ManageV2 routes to perform operations on\n * repositories.\n */\nexport class ManageV2Client {\n\treadonly client: AxiosInstance; // Not private to have it available on tests\n\tprivate token: string | null = null;\n\n\t/**\n\t * @param baseURL - The base URL of the Wroom app. ex: https://prismic.io\n\t * @param config - ManageV2 configuration variable to call ManageV2.\n\t */\n\tconstructor(baseUrl: string, config?: ManageV2Config | undefined) {\n\t\tthis.token = config ? this.generateToken(config) : null;\n\n\t\tthis.client = axios.create({\n\t\t\tbaseURL: `${baseUrl}/manageroutes/`,\n\t\t\twithCredentials: true,\n\t\t\tvalidateStatus: () => true,\n\t\t\theaders: {\n\t\t\t\t\"User-Agent\": \"prismic-cli/prismic-e2e-tests-utils\",\n\t\t\t\tAuthorization: `Bearer ${this.token}`,\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\t/**\n\t * The function generates a JWT token using the provided configuration\n\t * parameters.\n\t *\n\t * @param {ManageV2Config} config - The `config` parameter in the\n\t * `generateToken` function is of type `ManageV2Config`. It contains the\n\t * following properties:\n\t *\n\t * @returns A JSON Web Token (JWT) is being returned by the `generateToken`\n\t * function. The token is signed using the provided `config.secret` with the\n\t * HS256 algorithm and includes the specified audience and issuer values.\n\t */\n\tprivate generateToken(config: ManageV2Config): string {\n\t\treturn jwt.sign({}, config.secret, {\n\t\t\talgorithm: \"HS256\",\n\t\t\taudience: config.audience,\n\t\t\tissuer: \"prismic.io\",\n\t\t});\n\t}\n\n\t/**\n\t * The function `assertTokenExist` checks if a token exists before executing a\n\t * given function.\n\t *\n\t * @param f - The `assertTokenExist` function takes a function `f` as a\n\t * parameter. This function `f` should accept a parameter of type\n\t * `Parameters` and return a Promise that resolves to an AxiosResponse. The\n\t * `assertTokenExist` function ensures that a token is not null before\n\t * calling the provided\n\t *\n\t * @returns A function is being returned that takes a parameter of type\n\t * Parameters and checks if the token is null. If the token is null, an\n\t * error is thrown. Otherwise, the function f is called with the provided\n\t * parameters.\n\t */\n\tprivate assertTokenExist<Parameters>(\n\t\tf: (_: Parameters) => Promise<AxiosResponse>,\n\t) {\n\t\treturn (params: Parameters) => {\n\t\t\tif (this.token === null) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"Undefined configuration for ManageV2 while trying to use it's routes\",\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\treturn f(params);\n\t\t\t}\n\t\t};\n\t}\n\n\tgetBaseURL(): string {\n\t\treturn this.client.getUri();\n\t}\n\n\t/**\n\t * The function `toggleRolePerLocal` enable or disable the role per local\n\t * feature\n\t *\n\t * @param repository - The Repository name\n\t * @param enabled - The feature new status\n\t */\n\ttoggleRolePerLocal = this.assertTokenExist(\n\t\t(params: { repository: string; enabled: boolean }) => {\n\t\t\treturn this.client.post(\"/repository/toggleRolesPerLocale\", {\n\t\t\t\tdomain: params.repository,\n\t\t\t\tenabled: params.enabled,\n\t\t\t\tauthor: ManageV2StaticAuthor,\n\t\t\t});\n\t\t},\n\t);\n\n\t/**\n\t * The function `changePlan` changes the plan of a repository the database\n\t *\n\t * @param repository - The Repository name\n\t * @param newPlanId - The new planId\n\t * @param bypassManualBilling - Beware that using this will not verify that\n\t * the database and Stripe are in sync\n\t */\n\tchangePlan = this.assertTokenExist(\n\t\t({\n\t\t\trepository,\n\t\t\tnewPlanId,\n\t\t\tbypassManualBilling = false,\n\t\t}: {\n\t\t\trepository: string;\n\t\t\tnewPlanId: string;\n\t\t\tbypassManualBilling?: boolean;\n\t\t}) => {\n\t\t\treturn this.client.post(\n\t\t\t\t`/repository/changePlan?bypassManualBilling=${bypassManualBilling}`,\n\t\t\t\t{\n\t\t\t\t\tdomain: repository,\n\t\t\t\t\tnewPlan: newPlanId,\n\t\t\t\t\tauthor: ManageV2StaticAuthor,\n\t\t\t\t},\n\t\t\t);\n\t\t},\n\t);\n\n\t/**\n\t * The function `addUserToRepository` adds a user corresponding to the email\n\t * to the given repository\n\t *\n\t * @param repository - The Repository name\n\t * @param email - The user email\n\t */\n\taddUserToRepository = this.assertTokenExist(\n\t\t({ repository, email }: { repository: string; email: string }) => {\n\t\t\treturn this.client.post(\"/repository/addUser\", {\n\t\t\t\tdomain: repository,\n\t\t\t\temail: email,\n\t\t\t\tauthor: ManageV2StaticAuthor,\n\t\t\t});\n\t\t},\n\t);\n\n\t/**\n\t * The function `removeUserFromRepository` removes a user corresponding to the\n\t * email from the given repository\n\t *\n\t * @param repository - The Repository name\n\t * @param email - The user email\n\t */\n\tremoveUserFromRepository = this.assertTokenExist(\n\t\t({ repository, email }: { repository: string; email: string }) => {\n\t\t\treturn this.client.post(\"/repository/removeUser\", {\n\t\t\t\tdomain: repository,\n\t\t\t\temail: email,\n\t\t\t\tauthor: ManageV2StaticAuthor,\n\t\t\t});\n\t\t},\n\t);\n}\n"],"names":["cookies","extractCookie"],"mappings":";;;;;;;;;;;AAOA,MAAM,uBAAuB;MAMhB,eAAc;AAAA;AAAA;AAAA;AAAA;AAAA,EAQ1B,YAAY,SAAiB,QAAmC;AAPvD;AACD;AAAA,iC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,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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcQ,cAAc,QAAsB;AAC3C,WAAO,IAAI,KAAK,IAAI,OAAO,QAAQ;AAAA,MAClC,WAAW;AAAA,MACX,UAAU,OAAO;AAAA,MACjB,QAAQ;AAAA,IAAA,CACR;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBQ,iBACP,GAA4C;AAE5C,WAAO,CAAC,WAAsB;AACzB,UAAA,KAAK,UAAU,MAAM;AAClB,cAAA,IAAI,MACT,sEAAsE;AAAA,MAAA,OAEjE;AACN,eAAO,EAAE,MAAM;AAAA,MACf;AAAA,IAAA;AAAA,EAEH;AAAA,EAEA,aAAU;AACF,WAAA,KAAK,OAAO;EACpB;AAiFA;;"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { AxiosInstance, AxiosResponse } from "axios";
|
|
2
|
+
import { ManageV2Config } from "../types";
|
|
3
|
+
/**
|
|
4
|
+
* Client for interacting with ManageV2 routes to perform operations on
|
|
5
|
+
* repositories.
|
|
6
|
+
*/
|
|
7
|
+
export declare class ManageV2Client {
|
|
8
|
+
readonly client: AxiosInstance;
|
|
9
|
+
private token;
|
|
10
|
+
/**
|
|
11
|
+
* @param baseURL - The base URL of the Wroom app. ex: https://prismic.io
|
|
12
|
+
* @param config - ManageV2 configuration variable to call ManageV2.
|
|
13
|
+
*/
|
|
14
|
+
constructor(baseUrl: string, config?: ManageV2Config | undefined);
|
|
15
|
+
/**
|
|
16
|
+
* The function generates a JWT token using the provided configuration
|
|
17
|
+
* parameters.
|
|
18
|
+
*
|
|
19
|
+
* @param {ManageV2Config} config - The `config` parameter in the
|
|
20
|
+
* `generateToken` function is of type `ManageV2Config`. It contains the
|
|
21
|
+
* following properties:
|
|
22
|
+
*
|
|
23
|
+
* @returns A JSON Web Token (JWT) is being returned by the `generateToken`
|
|
24
|
+
* function. The token is signed using the provided `config.secret` with the
|
|
25
|
+
* HS256 algorithm and includes the specified audience and issuer values.
|
|
26
|
+
*/
|
|
27
|
+
private generateToken;
|
|
28
|
+
/**
|
|
29
|
+
* The function `assertTokenExist` checks if a token exists before executing a
|
|
30
|
+
* given function.
|
|
31
|
+
*
|
|
32
|
+
* @param f - The `assertTokenExist` function takes a function `f` as a
|
|
33
|
+
* parameter. This function `f` should accept a parameter of type
|
|
34
|
+
* `Parameters` and return a Promise that resolves to an AxiosResponse. The
|
|
35
|
+
* `assertTokenExist` function ensures that a token is not null before
|
|
36
|
+
* calling the provided
|
|
37
|
+
*
|
|
38
|
+
* @returns A function is being returned that takes a parameter of type
|
|
39
|
+
* Parameters and checks if the token is null. If the token is null, an
|
|
40
|
+
* error is thrown. Otherwise, the function f is called with the provided
|
|
41
|
+
* parameters.
|
|
42
|
+
*/
|
|
43
|
+
private assertTokenExist;
|
|
44
|
+
getBaseURL(): string;
|
|
45
|
+
/**
|
|
46
|
+
* The function `toggleRolePerLocal` enable or disable the role per local
|
|
47
|
+
* feature
|
|
48
|
+
*
|
|
49
|
+
* @param repository - The Repository name
|
|
50
|
+
* @param enabled - The feature new status
|
|
51
|
+
*/
|
|
52
|
+
toggleRolePerLocal: (params: {
|
|
53
|
+
repository: string;
|
|
54
|
+
enabled: boolean;
|
|
55
|
+
}) => Promise<AxiosResponse<any, any>>;
|
|
56
|
+
/**
|
|
57
|
+
* The function `changePlan` changes the plan of a repository the database
|
|
58
|
+
*
|
|
59
|
+
* @param repository - The Repository name
|
|
60
|
+
* @param newPlanId - The new planId
|
|
61
|
+
* @param bypassManualBilling - Beware that using this will not verify that
|
|
62
|
+
* the database and Stripe are in sync
|
|
63
|
+
*/
|
|
64
|
+
changePlan: (params: {
|
|
65
|
+
repository: string;
|
|
66
|
+
newPlanId: string;
|
|
67
|
+
bypassManualBilling?: boolean | undefined;
|
|
68
|
+
}) => Promise<AxiosResponse<any, any>>;
|
|
69
|
+
/**
|
|
70
|
+
* The function `addUserToRepository` adds a user corresponding to the email
|
|
71
|
+
* to the given repository
|
|
72
|
+
*
|
|
73
|
+
* @param repository - The Repository name
|
|
74
|
+
* @param email - The user email
|
|
75
|
+
*/
|
|
76
|
+
addUserToRepository: (params: {
|
|
77
|
+
repository: string;
|
|
78
|
+
email: string;
|
|
79
|
+
}) => Promise<AxiosResponse<any, any>>;
|
|
80
|
+
/**
|
|
81
|
+
* The function `removeUserFromRepository` removes a user corresponding to the
|
|
82
|
+
* email from the given repository
|
|
83
|
+
*
|
|
84
|
+
* @param repository - The Repository name
|
|
85
|
+
* @param email - The user email
|
|
86
|
+
*/
|
|
87
|
+
removeUserFromRepository: (params: {
|
|
88
|
+
repository: string;
|
|
89
|
+
email: string;
|
|
90
|
+
}) => Promise<AxiosResponse<any, any>>;
|
|
91
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => {
|
|
4
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
return value;
|
|
6
|
+
};
|
|
7
|
+
import axios from "axios";
|
|
8
|
+
import jwt from "jsonwebtoken";
|
|
9
|
+
import { extractCookie } from "../utils/cookies.js";
|
|
10
|
+
const ManageV2StaticAuthor = "prismic-e2e-tests-utils";
|
|
11
|
+
class ManageV2Client {
|
|
12
|
+
/**
|
|
13
|
+
* @param baseURL - The base URL of the Wroom app. ex: https://prismic.io
|
|
14
|
+
* @param config - ManageV2 configuration variable to call ManageV2.
|
|
15
|
+
*/
|
|
16
|
+
constructor(baseUrl, config) {
|
|
17
|
+
__publicField(this, "client");
|
|
18
|
+
// Not private to have it available on tests
|
|
19
|
+
__publicField(this, "token", null);
|
|
20
|
+
/**
|
|
21
|
+
* The function `toggleRolePerLocal` enable or disable the role per local
|
|
22
|
+
* feature
|
|
23
|
+
*
|
|
24
|
+
* @param repository - The Repository name
|
|
25
|
+
* @param enabled - The feature new status
|
|
26
|
+
*/
|
|
27
|
+
__publicField(this, "toggleRolePerLocal", this.assertTokenExist((params) => {
|
|
28
|
+
return this.client.post("/repository/toggleRolesPerLocale", {
|
|
29
|
+
domain: params.repository,
|
|
30
|
+
enabled: params.enabled,
|
|
31
|
+
author: ManageV2StaticAuthor
|
|
32
|
+
});
|
|
33
|
+
}));
|
|
34
|
+
/**
|
|
35
|
+
* The function `changePlan` changes the plan of a repository the database
|
|
36
|
+
*
|
|
37
|
+
* @param repository - The Repository name
|
|
38
|
+
* @param newPlanId - The new planId
|
|
39
|
+
* @param bypassManualBilling - Beware that using this will not verify that
|
|
40
|
+
* the database and Stripe are in sync
|
|
41
|
+
*/
|
|
42
|
+
__publicField(this, "changePlan", this.assertTokenExist(({ repository, newPlanId, bypassManualBilling = false }) => {
|
|
43
|
+
return this.client.post(`/repository/changePlan?bypassManualBilling=${bypassManualBilling}`, {
|
|
44
|
+
domain: repository,
|
|
45
|
+
newPlan: newPlanId,
|
|
46
|
+
author: ManageV2StaticAuthor
|
|
47
|
+
});
|
|
48
|
+
}));
|
|
49
|
+
/**
|
|
50
|
+
* The function `addUserToRepository` adds a user corresponding to the email
|
|
51
|
+
* to the given repository
|
|
52
|
+
*
|
|
53
|
+
* @param repository - The Repository name
|
|
54
|
+
* @param email - The user email
|
|
55
|
+
*/
|
|
56
|
+
__publicField(this, "addUserToRepository", this.assertTokenExist(({ repository, email }) => {
|
|
57
|
+
return this.client.post("/repository/addUser", {
|
|
58
|
+
domain: repository,
|
|
59
|
+
email,
|
|
60
|
+
author: ManageV2StaticAuthor
|
|
61
|
+
});
|
|
62
|
+
}));
|
|
63
|
+
/**
|
|
64
|
+
* The function `removeUserFromRepository` removes a user corresponding to the
|
|
65
|
+
* email from the given repository
|
|
66
|
+
*
|
|
67
|
+
* @param repository - The Repository name
|
|
68
|
+
* @param email - The user email
|
|
69
|
+
*/
|
|
70
|
+
__publicField(this, "removeUserFromRepository", this.assertTokenExist(({ repository, email }) => {
|
|
71
|
+
return this.client.post("/repository/removeUser", {
|
|
72
|
+
domain: repository,
|
|
73
|
+
email,
|
|
74
|
+
author: ManageV2StaticAuthor
|
|
75
|
+
});
|
|
76
|
+
}));
|
|
77
|
+
this.token = config ? this.generateToken(config) : null;
|
|
78
|
+
this.client = axios.create({
|
|
79
|
+
baseURL: `${baseUrl}/manageroutes/`,
|
|
80
|
+
withCredentials: true,
|
|
81
|
+
validateStatus: () => true,
|
|
82
|
+
headers: {
|
|
83
|
+
"User-Agent": "prismic-cli/prismic-e2e-tests-utils",
|
|
84
|
+
Authorization: `Bearer ${this.token}`
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
this.client.interceptors.response.use((response) => {
|
|
88
|
+
const cookies = response.headers["set-cookie"];
|
|
89
|
+
if (cookies && extractCookie(cookies, "SESSION")) {
|
|
90
|
+
this.client.defaults.headers["Cookie"] = cookies;
|
|
91
|
+
}
|
|
92
|
+
return response;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The function generates a JWT token using the provided configuration
|
|
97
|
+
* parameters.
|
|
98
|
+
*
|
|
99
|
+
* @param {ManageV2Config} config - The `config` parameter in the
|
|
100
|
+
* `generateToken` function is of type `ManageV2Config`. It contains the
|
|
101
|
+
* following properties:
|
|
102
|
+
*
|
|
103
|
+
* @returns A JSON Web Token (JWT) is being returned by the `generateToken`
|
|
104
|
+
* function. The token is signed using the provided `config.secret` with the
|
|
105
|
+
* HS256 algorithm and includes the specified audience and issuer values.
|
|
106
|
+
*/
|
|
107
|
+
generateToken(config) {
|
|
108
|
+
return jwt.sign({}, config.secret, {
|
|
109
|
+
algorithm: "HS256",
|
|
110
|
+
audience: config.audience,
|
|
111
|
+
issuer: "prismic.io"
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The function `assertTokenExist` checks if a token exists before executing a
|
|
116
|
+
* given function.
|
|
117
|
+
*
|
|
118
|
+
* @param f - The `assertTokenExist` function takes a function `f` as a
|
|
119
|
+
* parameter. This function `f` should accept a parameter of type
|
|
120
|
+
* `Parameters` and return a Promise that resolves to an AxiosResponse. The
|
|
121
|
+
* `assertTokenExist` function ensures that a token is not null before
|
|
122
|
+
* calling the provided
|
|
123
|
+
*
|
|
124
|
+
* @returns A function is being returned that takes a parameter of type
|
|
125
|
+
* Parameters and checks if the token is null. If the token is null, an
|
|
126
|
+
* error is thrown. Otherwise, the function f is called with the provided
|
|
127
|
+
* parameters.
|
|
128
|
+
*/
|
|
129
|
+
assertTokenExist(f) {
|
|
130
|
+
return (params) => {
|
|
131
|
+
if (this.token === null) {
|
|
132
|
+
throw new Error("Undefined configuration for ManageV2 while trying to use it's routes");
|
|
133
|
+
} else {
|
|
134
|
+
return f(params);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
getBaseURL() {
|
|
139
|
+
return this.client.getUri();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
export {
|
|
143
|
+
ManageV2Client
|
|
144
|
+
};
|
|
145
|
+
//# sourceMappingURL=manageV2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manageV2.js","sources":["../../../src/clients/manageV2.ts"],"sourcesContent":["import axios, { AxiosInstance, AxiosResponse } from \"axios\";\nimport jwt from \"jsonwebtoken\";\n\nimport { ManageV2Config } from \"../types\";\n\nimport { extractCookie } from \"../utils/cookies\";\n\nconst ManageV2StaticAuthor = \"prismic-e2e-tests-utils\";\n\n/**\n * Client for interacting with ManageV2 routes to perform operations on\n * repositories.\n */\nexport class ManageV2Client {\n\treadonly client: AxiosInstance; // Not private to have it available on tests\n\tprivate token: string | null = null;\n\n\t/**\n\t * @param baseURL - The base URL of the Wroom app. ex: https://prismic.io\n\t * @param config - ManageV2 configuration variable to call ManageV2.\n\t */\n\tconstructor(baseUrl: string, config?: ManageV2Config | undefined) {\n\t\tthis.token = config ? this.generateToken(config) : null;\n\n\t\tthis.client = axios.create({\n\t\t\tbaseURL: `${baseUrl}/manageroutes/`,\n\t\t\twithCredentials: true,\n\t\t\tvalidateStatus: () => true,\n\t\t\theaders: {\n\t\t\t\t\"User-Agent\": \"prismic-cli/prismic-e2e-tests-utils\",\n\t\t\t\tAuthorization: `Bearer ${this.token}`,\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\t/**\n\t * The function generates a JWT token using the provided configuration\n\t * parameters.\n\t *\n\t * @param {ManageV2Config} config - The `config` parameter in the\n\t * `generateToken` function is of type `ManageV2Config`. It contains the\n\t * following properties:\n\t *\n\t * @returns A JSON Web Token (JWT) is being returned by the `generateToken`\n\t * function. The token is signed using the provided `config.secret` with the\n\t * HS256 algorithm and includes the specified audience and issuer values.\n\t */\n\tprivate generateToken(config: ManageV2Config): string {\n\t\treturn jwt.sign({}, config.secret, {\n\t\t\talgorithm: \"HS256\",\n\t\t\taudience: config.audience,\n\t\t\tissuer: \"prismic.io\",\n\t\t});\n\t}\n\n\t/**\n\t * The function `assertTokenExist` checks if a token exists before executing a\n\t * given function.\n\t *\n\t * @param f - The `assertTokenExist` function takes a function `f` as a\n\t * parameter. This function `f` should accept a parameter of type\n\t * `Parameters` and return a Promise that resolves to an AxiosResponse. The\n\t * `assertTokenExist` function ensures that a token is not null before\n\t * calling the provided\n\t *\n\t * @returns A function is being returned that takes a parameter of type\n\t * Parameters and checks if the token is null. If the token is null, an\n\t * error is thrown. Otherwise, the function f is called with the provided\n\t * parameters.\n\t */\n\tprivate assertTokenExist<Parameters>(\n\t\tf: (_: Parameters) => Promise<AxiosResponse>,\n\t) {\n\t\treturn (params: Parameters) => {\n\t\t\tif (this.token === null) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"Undefined configuration for ManageV2 while trying to use it's routes\",\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\treturn f(params);\n\t\t\t}\n\t\t};\n\t}\n\n\tgetBaseURL(): string {\n\t\treturn this.client.getUri();\n\t}\n\n\t/**\n\t * The function `toggleRolePerLocal` enable or disable the role per local\n\t * feature\n\t *\n\t * @param repository - The Repository name\n\t * @param enabled - The feature new status\n\t */\n\ttoggleRolePerLocal = this.assertTokenExist(\n\t\t(params: { repository: string; enabled: boolean }) => {\n\t\t\treturn this.client.post(\"/repository/toggleRolesPerLocale\", {\n\t\t\t\tdomain: params.repository,\n\t\t\t\tenabled: params.enabled,\n\t\t\t\tauthor: ManageV2StaticAuthor,\n\t\t\t});\n\t\t},\n\t);\n\n\t/**\n\t * The function `changePlan` changes the plan of a repository the database\n\t *\n\t * @param repository - The Repository name\n\t * @param newPlanId - The new planId\n\t * @param bypassManualBilling - Beware that using this will not verify that\n\t * the database and Stripe are in sync\n\t */\n\tchangePlan = this.assertTokenExist(\n\t\t({\n\t\t\trepository,\n\t\t\tnewPlanId,\n\t\t\tbypassManualBilling = false,\n\t\t}: {\n\t\t\trepository: string;\n\t\t\tnewPlanId: string;\n\t\t\tbypassManualBilling?: boolean;\n\t\t}) => {\n\t\t\treturn this.client.post(\n\t\t\t\t`/repository/changePlan?bypassManualBilling=${bypassManualBilling}`,\n\t\t\t\t{\n\t\t\t\t\tdomain: repository,\n\t\t\t\t\tnewPlan: newPlanId,\n\t\t\t\t\tauthor: ManageV2StaticAuthor,\n\t\t\t\t},\n\t\t\t);\n\t\t},\n\t);\n\n\t/**\n\t * The function `addUserToRepository` adds a user corresponding to the email\n\t * to the given repository\n\t *\n\t * @param repository - The Repository name\n\t * @param email - The user email\n\t */\n\taddUserToRepository = this.assertTokenExist(\n\t\t({ repository, email }: { repository: string; email: string }) => {\n\t\t\treturn this.client.post(\"/repository/addUser\", {\n\t\t\t\tdomain: repository,\n\t\t\t\temail: email,\n\t\t\t\tauthor: ManageV2StaticAuthor,\n\t\t\t});\n\t\t},\n\t);\n\n\t/**\n\t * The function `removeUserFromRepository` removes a user corresponding to the\n\t * email from the given repository\n\t *\n\t * @param repository - The Repository name\n\t * @param email - The user email\n\t */\n\tremoveUserFromRepository = this.assertTokenExist(\n\t\t({ repository, email }: { repository: string; email: string }) => {\n\t\t\treturn this.client.post(\"/repository/removeUser\", {\n\t\t\t\tdomain: repository,\n\t\t\t\temail: email,\n\t\t\t\tauthor: ManageV2StaticAuthor,\n\t\t\t});\n\t\t},\n\t);\n}\n"],"names":[],"mappings":";;;;;;;;;AAOA,MAAM,uBAAuB;MAMhB,eAAc;AAAA;AAAA;AAAA;AAAA;AAAA,EAQ1B,YAAY,SAAiB,QAAmC;AAPvD;AACD;AAAA,iCAAuB;AA0F/B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,8CAAqB,KAAK,iBACzB,CAAC,WAAoD;AAC7C,aAAA,KAAK,OAAO,KAAK,oCAAoC;AAAA,QAC3D,QAAQ,OAAO;AAAA,QACf,SAAS,OAAO;AAAA,QAChB,QAAQ;AAAA,MAAA,CACR;AAAA,IAAA,CACD;AAWF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sCAAa,KAAK,iBACjB,CAAC,EACA,YACA,WACA,sBAAsB,YAKlB;AACJ,aAAO,KAAK,OAAO,KAClB,8CAA8C,mBAAmB,IACjE;AAAA,QACC,QAAQ;AAAA,QACR,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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBQ,iBACP,GAA4C;AAE5C,WAAO,CAAC,WAAsB;AACzB,UAAA,KAAK,UAAU,MAAM;AAClB,cAAA,IAAI,MACT,sEAAsE;AAAA,MAAA,OAEjE;AACN,eAAO,EAAE,MAAM;AAAA,MACf;AAAA,IAAA;AAAA,EAEH;AAAA,EAEA,aAAU;AACF,WAAA,KAAK,OAAO;EACpB;AAiFA;"}
|
package/dist/clients/wroom.cjs
CHANGED
|
@@ -67,6 +67,34 @@ class WroomClient {
|
|
|
67
67
|
});
|
|
68
68
|
return response;
|
|
69
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Update the role of a user in the repository
|
|
72
|
+
*
|
|
73
|
+
* @param {string} repository - The repository name
|
|
74
|
+
* @param {string} email - The email of the user
|
|
75
|
+
* @param {string} role - The new role to be given
|
|
76
|
+
*
|
|
77
|
+
* @returns The Axios Reponse
|
|
78
|
+
*/
|
|
79
|
+
async updateRole(repository, email, role) {
|
|
80
|
+
const params = new URLSearchParams({ email, profile: role });
|
|
81
|
+
const path = `/app/settings/users/profiles?${params.toString()}`;
|
|
82
|
+
return this.post(repository, path);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Update the role of a user in the repository
|
|
86
|
+
*
|
|
87
|
+
* @param {string} repository - The repository name
|
|
88
|
+
* @param {string} email - The email of the user
|
|
89
|
+
* @param {string} rolePerLocal - The role per local object
|
|
90
|
+
*
|
|
91
|
+
* @returns The Axios Reponse
|
|
92
|
+
*/
|
|
93
|
+
async updateRolePerLocal(repository, email, rolePerLocal) {
|
|
94
|
+
const params = new URLSearchParams({ email });
|
|
95
|
+
const path = `/app/settings/users/rolesperlocale?${params.toString()}`;
|
|
96
|
+
return this.post(repository, path, rolePerLocal);
|
|
97
|
+
}
|
|
70
98
|
/**
|
|
71
99
|
* Authenticate Wroom to call Wroom unofficial endpoints.
|
|
72
100
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wroom.cjs","sources":["../../../src/clients/wroom.ts"],"sourcesContent":["import axios, { AxiosInstance, AxiosResponse } from \"axios\";\n\nimport {
|
|
1
|
+
{"version":3,"file":"wroom.cjs","sources":["../../../src/clients/wroom.ts"],"sourcesContent":["import axios, { AxiosInstance, AxiosResponse } from \"axios\";\n\nimport { AuthConfig } 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: AuthConfig,\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 * Update the role of a user in the repository\n\t *\n\t * @param {string} repository - The repository name\n\t * @param {string} email - The email of the user\n\t * @param {string} role - The new role to be given\n\t *\n\t * @returns The Axios Reponse\n\t */\n\tasync updateRole(\n\t\trepository: string,\n\t\temail: string,\n\t\trole: string,\n\t): Promise<AxiosResponse> {\n\t\tconst params = new URLSearchParams({ email, profile: role });\n\t\tconst path = `/app/settings/users/profiles?${params.toString()}`;\n\n\t\treturn this.post(repository, path);\n\t}\n\n\t/**\n\t * Update the role of a user in the repository\n\t *\n\t * @param {string} repository - The repository name\n\t * @param {string} email - The email of the user\n\t * @param {string} rolePerLocal - The role per local object\n\t *\n\t * @returns The Axios Reponse\n\t */\n\tasync updateRolePerLocal(\n\t\trepository: string,\n\t\temail: string,\n\t\trolePerLocal: Record<string, string>,\n\t): Promise<AxiosResponse> {\n\t\tconst params = new URLSearchParams({ email });\n\t\tconst path = `/app/settings/users/rolesperlocale?${params.toString()}`;\n\n\t\treturn this.post(repository, path, rolePerLocal);\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,MAAgB;AAAhB;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,IAAI,YAA2B,MAAY;AAC5C,QAAA,CAAC,KAAK,UAAU;AACnB,YAAM,KAAK;IACX;AAEG,QAAA,MAAM,IAAI,IAAI,MAAM,KAAK,OAAO,OAAA,CAAQ,EAAE;AAC9C,QAAI,YAAY;AACT,YAAAE,KAAA,iBAAiB,KAAK,UAAU;AAAA,IACtC;AAEM,WAAA,KAAK,OAAO,IAAI,GAAG;AAAA,EAC3B;AAAA;AAAA,EAGA,MAAM,KACL,YACA,MACA,MAAc;AAEV,QAAA,CAAC,KAAK,UAAU;AACnB,Y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AA,EAClD;AACA;;"}
|
package/dist/clients/wroom.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AxiosResponse } from "axios";
|
|
2
|
-
import {
|
|
2
|
+
import { AuthConfig } from "../types";
|
|
3
3
|
/**
|
|
4
4
|
* Client for interacting with the Wroom service to perform operations on
|
|
5
5
|
* repositories.
|
|
@@ -12,12 +12,32 @@ export declare class WroomClient {
|
|
|
12
12
|
* @param baseURL - The base URL of the Wroom app. ex: https://prismic.io
|
|
13
13
|
* @param auth - Authentication credentials.
|
|
14
14
|
*/
|
|
15
|
-
constructor(baseURL: string, auth:
|
|
15
|
+
constructor(baseURL: string, auth: AuthConfig);
|
|
16
16
|
getBaseURL(): string;
|
|
17
17
|
/** authenticated GET request on the wroom backend */
|
|
18
18
|
get(repository: string | null, path: string): Promise<AxiosResponse>;
|
|
19
19
|
/** authenticated POST request on the wroom backend */
|
|
20
20
|
post(repository: string | null, path: string, data?: unknown): Promise<AxiosResponse>;
|
|
21
|
+
/**
|
|
22
|
+
* Update the role of a user in the repository
|
|
23
|
+
*
|
|
24
|
+
* @param {string} repository - The repository name
|
|
25
|
+
* @param {string} email - The email of the user
|
|
26
|
+
* @param {string} role - The new role to be given
|
|
27
|
+
*
|
|
28
|
+
* @returns The Axios Reponse
|
|
29
|
+
*/
|
|
30
|
+
updateRole(repository: string, email: string, role: string): Promise<AxiosResponse>;
|
|
31
|
+
/**
|
|
32
|
+
* Update the role of a user in the repository
|
|
33
|
+
*
|
|
34
|
+
* @param {string} repository - The repository name
|
|
35
|
+
* @param {string} email - The email of the user
|
|
36
|
+
* @param {string} rolePerLocal - The role per local object
|
|
37
|
+
*
|
|
38
|
+
* @returns The Axios Reponse
|
|
39
|
+
*/
|
|
40
|
+
updateRolePerLocal(repository: string, email: string, rolePerLocal: Record<string, string>): Promise<AxiosResponse>;
|
|
21
41
|
/**
|
|
22
42
|
* Authenticate Wroom to call Wroom unofficial endpoints.
|
|
23
43
|
*
|