@simulacrum/github-api-simulator
Version:
Provides common functionality to frontend app and plugins.
180 lines • 9.28 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.openapi = void 0;
const utils_ts_1 = require("../utils.js");
const utils_ts_2 = require("./utils.js");
const handlers = (initialState, extendedHandlers) => (simulationStore) => {
if (!initialState)
return {};
// note for any cases where it `return`s an object,
// that will validate the response per the schema
return Object.assign({
// GET /user/installations
"apps/list-installations": (_context, _request, response) => __awaiter(void 0, void 0, void 0, function* () {
const ghOrgs = simulationStore.selectors.allGithubOrganizations(simulationStore.store.getState());
const data = ghOrgs.map((org, index) => ({
id: index,
account: org,
}));
response.status(200).json(data);
}),
// L#4134 /installation/repositories
"apps/list-repos-accessible-to-installation": (_context, _request, response) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
const repos = (_a = simulationStore.selectors.allReposWithOrgs(simulationStore.store.getState())) !== null && _a !== void 0 ? _a : [];
return {
status: 200,
json: {
total_count: repos.length,
repositories: repos,
},
};
}),
// GET /orgs/{org}/installation - Get an organization installation for the authenticated app
"apps/get-org-installation": (context, _request, response) => __awaiter(void 0, void 0, void 0, function* () {
const { org } = context.request.params;
const install = simulationStore.selectors.getAppInstallation(simulationStore.store.getState(), org);
if (!install)
return response.status(404).send("Not Found");
return { status: 200, json: install };
}),
// GET /repos/{owner}/{repo}/installation - Get a repository installation for the authenticated app
"apps/get-repo-installation": (context, _request, response) => __awaiter(void 0, void 0, void 0, function* () {
const { owner, repo } = context.request.params;
const install = simulationStore.selectors.getAppInstallation(simulationStore.store.getState(), owner, repo);
if (!install)
return response.status(404).send("Not Found");
return { status: 200, json: install };
}),
// GET /orgs/{org}/repos
"repos/list-for-org": (context, _request, response) => __awaiter(void 0, void 0, void 0, function* () {
const { org } = context.request.params;
const repos = simulationStore.selectors.allReposWithOrgs(simulationStore.store.getState(), org);
if (!repos)
return response.status(404).send("Not Found");
return { status: 200, json: repos };
}),
// L#29067 /repos/{owner}/{repo}/branches
"repos/list-branches": (_context, _request, _response) => __awaiter(void 0, void 0, void 0, function* () {
const branches = simulationStore.schema.branches.selectTableAsList(simulationStore.store.getState());
return { status: 200, json: branches };
}),
// GET /repos/{owner}/{repo}/commits/{ref}/status
"repos/get-combined-status-for-ref": (context, request, response) => __awaiter(void 0, void 0, void 0, function* () {
const { owner, repo, ref } = context.request.params;
const commitStatus = (0, utils_ts_2.commitStatusResponse)({
host: `${request.protocol}://${request.headers.host}`,
owner,
repo,
ref,
});
response.status(200).json(commitStatus);
}),
// GET /repos/{owner}/{repo}/contents/{path}
"repos/get-content": (context, request, response) => __awaiter(void 0, void 0, void 0, function* () {
const { owner, repo, path } = context.request.params;
const blob = simulationStore.selectors.getBlob(simulationStore.store.getState(), owner, repo, path);
if (!blob) {
response.status(404).send("fixture does not exist");
}
else {
const data = (0, utils_ts_2.blobAsBase64)({
blob,
host: `${request.protocol}://${request.headers.host}`,
owner,
repo,
ref: path,
});
response.status(200).json(data);
}
}),
// GET /repos/{owner}/{repo}/git/blobs/{file_sha}
"git/get-blob": (context, request, response) => __awaiter(void 0, void 0, void 0, function* () {
const { owner, repo, file_sha } = context.request.params;
const blob = simulationStore.selectors.getBlob(simulationStore.store.getState(), owner, repo, file_sha);
if (!blob) {
response.status(404).send("fixture does not exist");
}
else {
const data = (0, utils_ts_2.blobAsBase64)({
blob,
host: `${request.protocol}://${request.headers.host}`,
owner,
repo,
ref: file_sha,
});
response.status(200).json(data);
}
}),
// GET /repos/{owner}/{repo}/git/trees/{tree_sha}
"git/get-tree": (_context, request, response) => __awaiter(void 0, void 0, void 0, function* () {
const { owner, repo, ref } = request.params;
const blobs = simulationStore.selectors.getBlobAtOwnerRepo(simulationStore.store.getState(), owner, repo);
if (!blobs) {
response.status(404).send("fixture does not exist");
}
else {
const tree = (0, utils_ts_2.gitTrees)({
blobs,
host: `${request.protocol}://${request.headers.host}`,
owner,
repo,
ref,
});
response.status(200).json(tree);
}
}),
// GET /user
"users/get-authenticated": (_context, _request, response) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
const users = simulationStore.schema.users.selectTableAsList(simulationStore.store.getState());
const user = users[0];
const data = {
id: parseInt((_b = (_a = user === null || user === void 0 ? void 0 : user.id) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : "1", 10),
login: user === null || user === void 0 ? void 0 : user.login,
email: user === null || user === void 0 ? void 0 : user.email,
name: user === null || user === void 0 ? void 0 : user.name,
};
response.status(200).json(data);
}),
// GET /user/memberships/orgs
"orgs/list-memberships-for-authenticated-user": (_context, _request, response) => __awaiter(void 0, void 0, void 0, function* () {
const users = simulationStore.schema.users.selectTableAsList(simulationStore.store.getState());
const user = users[0];
const organizations = simulationStore.selectors.allGithubOrganizations(simulationStore.store.getState());
return {
status: 200,
json: organizations.map((organization) => ({
url: `${organization.url}/memberships`,
state: "active",
organization,
role: "admin",
organization_url: organization.url,
user: !user ? null : user,
})),
};
}) }, (extendedHandlers ? extendedHandlers(simulationStore) : {}));
};
const openapi = (initialState, apiRoot, apiSchema, openapiHandlers) => [
{
document: (0, utils_ts_1.getSchema)(apiSchema),
handlers: handlers(initialState, openapiHandlers),
apiRoot,
additionalOptions: {
// starts up quicker and avoids the precompile step which throws a ton of errors
// based on openapi-backend handling of GitHub schema
quick: true,
},
},
];
exports.openapi = openapi;
//# sourceMappingURL=index.js.map
;