firebase-app-distribution
Version:
A library that uses Firebase App Distribution APIs.
248 lines (245 loc) • 8.48 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/testers.ts
var testers_exports = {};
__export(testers_exports, {
Testers: () => Testers
});
module.exports = __toCommonJS(testers_exports);
// src/utils.ts
var APP_DISTRIBUTION_ENDPOINT = "https://firebaseappdistribution.googleapis.com";
var ENDPOINT_VERSION = "v1";
function makeRequest(input, init) {
return __async(this, null, function* () {
const response = yield fetch(input, init);
if (response.status !== 200) {
const errMessage = yield response.text();
throw new Error(
`Request failed with status code ${response.status} and message: ${errMessage}`
);
}
const responseText = yield response.text();
const responseObj = JSON.parse(responseText);
return responseObj;
});
}
// src/testers.ts
function getEmailFromName(testerName) {
return testerName.split("/").pop();
}
var Testers = class {
constructor(parent) {
this.parent = parent;
}
add(emails) {
return __async(this, null, function* () {
const accessToken = yield this.parent.getAccessToken();
const projectNumber = yield this.parent.getProjectNumber();
const testers = [];
const url = new URL(
`${APP_DISTRIBUTION_ENDPOINT}/${ENDPOINT_VERSION}/projects/${projectNumber}/testers:batchAdd`
);
const requestBody = JSON.stringify({
emails
});
const response = yield makeRequest(url.toString(), {
headers: {
authorization: `Bearer ${accessToken}`
},
body: requestBody,
method: "POST"
});
for (let tester of response.testers) {
testers.push(__spreadProps(__spreadValues({}, tester), {
email: getEmailFromName(tester.name)
}));
}
return testers;
});
}
remove(emails) {
return __async(this, null, function* () {
const accessToken = yield this.parent.getAccessToken();
const projectNumber = yield this.parent.getProjectNumber();
const url = new URL(
`${APP_DISTRIBUTION_ENDPOINT}/${ENDPOINT_VERSION}/projects/${projectNumber}/testers:batchRemove`
);
const requestBody = JSON.stringify({
emails
});
const response = yield makeRequest(url.toString(), {
headers: {
authorization: `Bearer ${accessToken}`
},
body: requestBody,
method: "POST"
});
return response.emails || [];
});
}
list() {
return __async(this, arguments, function* ({
pageSize = 50,
email,
displayName,
groups,
maxPages = 10
} = {}) {
const accessToken = yield this.parent.getAccessToken();
const projectNumber = yield this.parent.getProjectNumber();
const testerList = [];
const url = new URL(
`${APP_DISTRIBUTION_ENDPOINT}/${ENDPOINT_VERSION}/projects/${projectNumber}/testers`
);
url.searchParams.set("pageSize", pageSize.toString());
let filterParts = [];
if (email) filterParts.push(`name="projects/-/testers/${email}"`);
if (displayName) filterParts.push(`displayName="${displayName}"`);
if (groups) filterParts.push(`groups="projects/*/groups/${groups}"`);
if (filterParts.length > 0) {
url.searchParams.set("filter", filterParts.join(" "));
}
let nextPageToken = "";
for (let page = 0; page < maxPages; page++) {
if (nextPageToken) {
url.searchParams.set("pageToken", nextPageToken);
} else {
url.searchParams.delete("pageToken");
}
const response = yield makeRequest(url.toString(), {
headers: {
authorization: `Bearer ${accessToken}`
},
body: null,
method: "GET"
});
if (response.testers === void 0) {
break;
}
for (let tester of response.testers) {
testerList.push(__spreadProps(__spreadValues({}, tester), {
email: getEmailFromName(tester.name)
}));
}
if (response.nextPageToken !== void 0) {
nextPageToken = response.nextPageToken;
} else {
break;
}
}
return testerList;
});
}
get(email) {
return __async(this, null, function* () {
const accessToken = yield this.parent.getAccessToken();
const projectNumber = yield this.parent.getProjectNumber();
const url = new URL(
`${APP_DISTRIBUTION_ENDPOINT}/${ENDPOINT_VERSION}/projects/${projectNumber}/testers`
);
url.searchParams.append("pageSize", "1");
url.searchParams.append("filter", `name="projects/-/testers/${email}"`);
const response = yield makeRequest(url.toString(), {
headers: {
authorization: `Bearer ${accessToken}`
},
body: null,
method: "GET"
});
return response.testers !== void 0 ? __spreadProps(__spreadValues({}, response.testers[0]), {
email: getEmailFromName(response.testers[0].name)
}) : null;
});
}
update(_0) {
return __async(this, arguments, function* (email, { displayName, groups } = {}) {
const accessToken = yield this.parent.getAccessToken();
const projectNumber = yield this.parent.getProjectNumber();
const url = new URL(
`${APP_DISTRIBUTION_ENDPOINT}/${ENDPOINT_VERSION}/projects/${projectNumber}/testers/${email}`
);
let updateMaskParts = [];
if (displayName !== void 0) {
updateMaskParts.push("displayName");
}
if (groups !== void 0) {
updateMaskParts.push("groups");
}
if (updateMaskParts.length > 0) {
url.searchParams.append("updateMask", updateMaskParts.join(","));
}
const formattedGroups = groups == null ? void 0 : groups.map(
(group) => `projects/${projectNumber}/groups/${group}`
);
const response = yield makeRequest(url.toString(), {
headers: {
authorization: `Bearer ${accessToken}`
},
body: JSON.stringify({
displayName: displayName || void 0,
groups: formattedGroups || void 0
}),
method: "PATCH"
});
return __spreadProps(__spreadValues({}, response), { email: getEmailFromName(response.name) });
});
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Testers
});
//# sourceMappingURL=testers.js.map