Skip to content

Commit

Permalink
custom-predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
uriva committed Nov 1, 2024
1 parent 891bd61 commit d036e52
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 22 deletions.
2 changes: 1 addition & 1 deletion example/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const whatsappPath = "/whatsapp-url-suffix";
const handleMessage = async (task: AbstractIncomingMessage) => {
console.log("got task", task);
await withSpinner("waiting needlessly", sleep)(5000);
return reply("hi there i got " + JSON.stringify(task));
return reply(`hi there i got ${JSON.stringify(task)}`);
};

const url = coerce(Deno.env.get("URL"));
Expand Down
3 changes: 1 addition & 2 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ export const makeDatabaseHandler = (
authenticate: (token: string, userId: string) => Promise<boolean>,
): Endpoint => ({
bounce: true,
method: "POST",
path,
predicate: ({ url, method }) => url === path && method === "POST",
handler: async ({ from, text, token }: ClientRequest) => {
if (!await authenticate(token, from)) return;
await storer({ from, to: botName, key: makeKey(), text, time: now() });
Expand Down
3 changes: 1 addition & 2 deletions src/greenApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ export const greenApiHandler = (
doTask: TaskHandler,
): Endpoint => ({
bounce: true,
method: "POST",
path,
predicate: ({ url, method }) => url === path && method === "POST",
handler: (msg: GreenApiMessage) =>
communications(
greenApi.restAPI(credentials),
Expand Down
14 changes: 5 additions & 9 deletions src/taskBouncer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,25 +154,21 @@ type Handler = (payload: Task["payload"]) => Promise<any>;
export type Endpoint = {
bounce: boolean;
handler: Handler;
method: "POST" | "GET";
path: string;
predicate: (task: Task) => boolean;
};

export const bouncerHandler = (domain: string, endpoints: Endpoint[]) =>
bouncer(
domain,
(task: Task) =>
endpoints.find(({ path, method }) =>
path === task.url && method === task.method
)?.bounce ?? false,
endpoints.find(({ predicate }) => predicate(task))?.bounce ?? false,
(task: Task) => {
for (
const { handler } of endpoints.filter(({ path, method }) =>
path === task.url && method === task.method
const { handler } of endpoints.filter(({ predicate }) =>
predicate(task)
)
) return injectUrl(() => task.url)(handler)(task.payload);
console.log("no handler for request", task);
return Promise.resolve();
throw new Error("No handler for request");
},
);

Expand Down
2 changes: 1 addition & 1 deletion src/telegram.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ TEL;CELL;PREF:+${alicePhone}
TEL;HOME:+97236746666
END:VCARD`,
}),
"+" + alicePhone,
`+${alicePhone}`,
);
});
3 changes: 1 addition & 2 deletions src/telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ export const makeTelegramHandler = (
): Endpoint => (
{
bounce: true,
method: "POST",
path,
predicate: ({ url, method }) => url === path && method === "POST",
handler: ({ message }: grammy.Update) =>
message?.from && message.text
? pipe(
Expand Down
8 changes: 3 additions & 5 deletions src/whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,8 @@ export const whatsappWebhookVerificationHandler = (
verifyToken: string,
path: string,
): Endpoint => ({
method: "GET",
predicate: ({ url, method }) => url === path && method === "POST",
bounce: false,
path,
handler: (msg: WebhookVerification) =>
(
msg["hub.mode"] === "subscribe" &&
Expand Down Expand Up @@ -306,12 +305,11 @@ const getContacts = (

export const whatsappBusinessHandler = (
token: string,
whatsappPath: string,
path: string,
doTask: TaskHandler,
): Endpoint => ({
bounce: true,
method: "POST",
path: whatsappPath,
predicate: ({ url, method }) => url === path && method === "POST",
handler: (msg: WhatsappMessage) =>
msg.entry[0].changes[0].value.messages
? letIn(
Expand Down

0 comments on commit d036e52

Please sign in to comment.