UNPKG

@samepage/backend

Version:

Backend utilities for the apis of SamePage integrations

136 lines 5.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const types_1 = require("../internal/types"); const createAPIGatewayProxyHandler_1 = tslib_1.__importDefault(require("./createAPIGatewayProxyHandler")); const zod_1 = require("zod"); const debugger_1 = tslib_1.__importDefault(require("../utils/debugger")); const getAccessToken_1 = tslib_1.__importDefault(require("./getAccessToken")); const crossNotebookWorkflows_1 = tslib_1.__importDefault(require("../protocols/crossNotebookWorkflows")); const log = (0, debugger_1.default)("api:backend"); const zMessage = zod_1.z.discriminatedUnion("type", [ zod_1.z.object({ type: zod_1.z.literal("SETUP") }), zod_1.z.object({ type: zod_1.z.literal("OPEN_PAGE"), notebookPageId: zod_1.z.string(), }), zod_1.z.object({ type: zod_1.z.literal("ENSURE_PAGE_BY_TITLE"), title: types_1.zSamePageSchema, path: zod_1.z.string().optional(), }), zod_1.z.object({ type: zod_1.z.literal("DELETE_PAGE"), notebookPageId: zod_1.z.string(), }), zod_1.z.object({ type: zod_1.z.literal("ENCODE_STATE"), notebookPageId: zod_1.z.string(), }), zod_1.z.object({ type: zod_1.z.literal("DECODE_STATE"), notebookPageId: zod_1.z.string(), state: types_1.zSamePageState, }), zod_1.z.object({ type: zod_1.z.literal("COMMAND_HANDLER"), args: types_1.zCommandArgs, text: zod_1.z.string(), workflowContext: types_1.zWorkflowContext, }), zod_1.z.object({ type: zod_1.z.literal("TRIGGER_WORKFLOW"), source: zod_1.z.string(), target: zod_1.z.string(), }), zod_1.z.object({ type: zod_1.z.literal("LIST_WORKFLOWS"), }), ]); const createApiBackendPostHandler = ({ getDecodeState = () => async () => ({}), getEncodeState = () => async (notebookPageId) => ({ $title: { content: notebookPageId, annotations: [] }, $body: { content: "", annotations: [] }, }), getEnsurePageByTitle = () => async (title) => ({ notebookPageId: title.content, preExisting: false, }), getCommandLibrary = () => ({}), getDeletePage = () => async () => { }, getOpenPage = () => async (notebookPageId) => ({ notebookPageId, url: notebookPageId, }), getListWorkflows = async () => ({ workflows: [] }), } = {}) => { const logic = async (args) => { const { authorization, ...data } = args; if (!authorization) { throw new Error("Unauthorized"); } log("backend post", data.type); const credentials = await (0, getAccessToken_1.default)({ authorization, }); try { switch (data.type) { case "SETUP": { // TODO: Do we need this anymore? return { success: true }; } case "ENSURE_PAGE_BY_TITLE": { const { path = "", title } = data; return getEnsurePageByTitle(credentials)(title, path); } case "DELETE_PAGE": { const { notebookPageId } = data; await getDeletePage(credentials)(notebookPageId); return { success: true }; } case "OPEN_PAGE": { const { notebookPageId } = data; return getOpenPage(credentials)(notebookPageId); } case "ENCODE_STATE": { const { notebookPageId } = data; return getEncodeState(credentials)(notebookPageId); } case "DECODE_STATE": { const { notebookPageId, state } = data; await getDecodeState(credentials)(notebookPageId, state); return { success: true }; } case "COMMAND_HANDLER": { const { args, text, workflowContext } = data; const commands = getCommandLibrary(credentials); const response = await commands[text].handler(args, workflowContext); return { response }; } case "TRIGGER_WORKFLOW": { const { source, target } = data; const { triggerWorkflow } = (0, crossNotebookWorkflows_1.default)({ decodeState: getDecodeState(credentials), encodeState: getEncodeState(credentials), appCommands: getCommandLibrary(credentials), }); await triggerWorkflow({ source, target }); return { success: true }; } case "LIST_WORKFLOWS": { const { workflows } = await getListWorkflows(credentials); return { workflows }; } default: throw new Error(`Unknown type ${data["type"]}`); } } catch (e) { log("error", e); throw new Error(`Backend request ${data.type} failed`, { cause: e, }); } }; return (0, createAPIGatewayProxyHandler_1.default)({ logic, bodySchema: zMessage, // TODO - use app's originRegex allowedOrigins: [/.*/], }); }; exports.default = createApiBackendPostHandler; //# sourceMappingURL=createApiBackendPostHandler.js.map