Skip to content

Commit

Permalink
migrate slack to new backend handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
dvargas92495 committed May 22, 2023
1 parent 834ab7f commit abc8ac7
Show file tree
Hide file tree
Showing 5 changed files with 335 additions and 204 deletions.
120 changes: 7 additions & 113 deletions api/backend/post.ts
Original file line number Diff line number Diff line change
@@ -1,120 +1,14 @@
import createAPIGatewayProxyHandler from "samepage/backend/createAPIGatewayProxyHandler";
import getAccessToken from "samepage/backend/getAccessToken";
import {
BackendRequest,
zCommandArgs,
zSamePageSchema,
zSamePageState,
zWorkflowContext,
} from "samepage/internal/types";
import { z } from "zod";
import debug from "samepage/utils/debugger";
import createApiBackendPostHandler from "samepage/backend/createApiBackendPostHandler";
import decodeState from "src/util/decodeState";
import encodeState from "src/util/encodeState";
import commands from "src/util/commands";
const log = debug("api:backend");

const zMessage = z.discriminatedUnion("type", [
z.object({ type: z.literal("SETUP") }),
z.object({
type: z.literal("OPEN_PAGE"),
notebookPageId: z.string(),
}),
z.object({
type: z.literal("ENSURE_PAGE_BY_TITLE"),
title: zSamePageSchema,
path: z.string().optional(),
}),
z.object({
type: z.literal("DELETE_PAGE"),
notebookPageId: z.string(),
}),
z.object({
type: z.literal("ENCODE_STATE"),
notebookPageId: z.string(),
notebookUuid: z.string(),
}),
z.object({
type: z.literal("DECODE_STATE"),
notebookPageId: z.string(),
state: zSamePageState,
}),
z.object({
type: z.literal("COMMAND_HANDLER"),
notebookUuid: z.string(),
args: zCommandArgs,
text: z.string(),
workflowContext: zWorkflowContext,
}),
]);

const logic = async (args: BackendRequest<typeof zMessage>) => {
const { authorization, ...data } = args;
if (!authorization) {
throw new Error("Unauthorized");
}
log("backend post", data.type);

const accessToken = authorization.startsWith("Basic")
? await getAccessToken({
authorization,
}).then(({ accessToken }) => accessToken)
: authorization.replace(/^Bearer /, "");
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 { notebookPageId: "TODO", preExisting: false };
}
case "DELETE_PAGE": {
const { notebookPageId } = data;
// TODO
return { data: notebookPageId };
}
case "OPEN_PAGE": {
const { notebookPageId } = data;
// TODO
return {
url: notebookPageId,
};
}
case "ENCODE_STATE": {
const { notebookPageId } = data;
// TODO
return encodeState({ notebookPageId, token: accessToken });
}
case "DECODE_STATE": {
const { notebookPageId, state } = data;
return decodeState(notebookPageId, state, accessToken);
}
case "COMMAND_HANDLER": {
const { notebookUuid: _, args, text, workflowContext } = data;
const response = await commands[text].handler(args, {
...workflowContext,
accessToken,
});
return { response };
}
default:
throw new Error(`Unknown type ${data["type"]}`);
}
} catch (e) {
log("error", e);
throw new Error(`Backend request ${data.type} failed`, {
cause: e as Error,
});
}
};

const backend = createAPIGatewayProxyHandler({
logic,
// @ts-ignore
bodySchema: zMessage,
allowedOrigins: [/^https:\/\/([\w]+\.)?notion\.so/],
const backend = createApiBackendPostHandler({
getCommandLibrary: commands,
getDecodeState: (credentials) => (notebookPageId, state) =>
decodeState(notebookPageId, state, credentials.accessToken),
getEncodeState: (credentials) => (notebookPageId) =>
encodeState({ notebookPageId, token: credentials.accessToken }),
});

export default backend;
56 changes: 28 additions & 28 deletions api/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import createBackendClientHandler from "samepage/backend/createBackendClientHandler";
import createApiMessageHandler from "samepage/backend/createApiMessageHandler";
import decodeState from "src/util/decodeState";
import encodeState from "src/util/encodeState";
import { notebookRequestNodeQuerySchema } from "samepage/internal/types";
Expand All @@ -12,35 +12,35 @@ const fireNodeQuery = async (
return [];
};

const message = (args: Record<string, unknown>) => {
return createBackendClientHandler({
getDecodeState: (token) => (id, state) => {
return decodeState(id, state, token);
const message = createApiMessageHandler({
getDecodeState:
({ accessToken }) =>
(id, state) => {
return decodeState(id, state, accessToken);
},
getNotebookRequestHandler:
(token) =>
async ({ request }) => {
// TODO
if (request.schema === "node-query") {
const result = notebookRequestNodeQuerySchema.safeParse(request);
if (!result.success) return {};
const results = await fireNodeQuery(result.data, token);
return {
results,
};
} else if (typeof request.notebookPageId === "string") {
const pageData = await encodeState({
notebookPageId: request.notebookPageId,
token,
});
return pageData;
}
return {};
},
getNotebookResponseHandler: (token) => async (response) => {
getNotebookRequestHandler:
({ token: accessToken }) =>
async ({ request }) => {
// TODO
if (request.schema === "node-query") {
const result = notebookRequestNodeQuerySchema.safeParse(request);
if (!result.success) return {};
const results = await fireNodeQuery(result.data, accessToken);
return {
results,
};
} else if (typeof request.notebookPageId === "string") {
const pageData = await encodeState({
notebookPageId: request.notebookPageId,
token: accessToken,
});
return pageData;
}
return {};
},
})(args);
};
getNotebookResponseHandler: (token) => async (response) => {
// TODO
},
});

export default message;
Loading

0 comments on commit abc8ac7

Please sign in to comment.