Skip to content

Commit

Permalink
[MS-759] feat: Add API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrgrundas committed Oct 8, 2024
1 parent 3cc9cb9 commit bbabed9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 104 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@
"fastify-raw-body": "^4.3.0",
"fastify-type-provider-zod": "^2.0.0",
"graphql": "^16.9.0",
"graphql-tag": "^2.12.6",
"graphql-yoga": "^5.7.0",
"husky": "^9.1.4",
"jose": "^5.6.3",
"jsonwebtoken": "^9.0.2",
Expand Down
89 changes: 0 additions & 89 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 43 additions & 13 deletions src/api/rest/routes.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,73 @@
import { SendMessageCommand } from "@aws-sdk/client-sqs";
import type { FastifyPluginAsync } from "fastify/types/plugin";
import { type ZodTypeProvider } from "fastify-type-provider-zod";
import { z } from "zod";

import { CONFIG } from "@/config";
import { OrderCreatedSubscriptionDocument } from "@/graphql/operations/subscriptions/generated";
import { serializePayload } from "@/lib/emails/events/helpers";
import { validateDocumentAgainstData } from "@/lib/graphql/validate";
import { verifyJWTSignature } from "@/lib/saleor/auth";
import { saleorBearerHeader } from "@/lib/saleor/schema";
import { getJWKSProvider } from "@/providers/jwks";

import { saleorRoutes } from "./saleor";
import { EVENT_HANDLERS } from "./saleor/webhooks";

export const restRoutes: FastifyPluginAsync = async (fastify) => {
await fastify.register(saleorRoutes, { prefix: "/saleor" });

fastify.get("/healthcheck", () => "ok");

fastify.withTypeProvider<ZodTypeProvider>().get(
"/protected-route",
fastify.withTypeProvider<ZodTypeProvider>().post(
"/send-notification",
{
schema: { headers: saleorBearerHeader },
name: "send-notification",
schema: {
headers: saleorBearerHeader,
body: z.object({
data: z.any(),
event: z.enum(
EVENT_HANDLERS.map(({ event }) => event.toLowerCase()) as any
),
}),
},
},
async (request) => {

async (request, response) => {
await verifyJWTSignature({
jwksProvider: getJWKSProvider(),
jwt: request.headers.authorization,
});
return { status: "ok" };
}
);

fastify.withTypeProvider<ZodTypeProvider>().post(
"/test",

async (request, response) => {
const { isValid, error } = validateDocumentAgainstData({
data: request.body,
data: request.body.data,
document: OrderCreatedSubscriptionDocument,
});

return response.status(isValid ? 200 : 400).send({ isValid, error });
if (!isValid) {
const zodError = new z.ZodError([]);
zodError.addIssue({
path: ["body > data"],
message: error ?? "",
code: "custom",
});
throw zodError;
}

const payload = serializePayload({
data: request.body.data,
event: request.body.event,
});

const command = new SendMessageCommand({
QueueUrl: CONFIG.SQS_QUEUE_URL,
MessageBody: JSON.stringify(payload),
});

await fastify.sqs.send(command);

return response.send({ status: "ok" });
}
);
};

0 comments on commit bbabed9

Please sign in to comment.