From 8b50af75686b808eb45319259d4c8395311b76bb Mon Sep 17 00:00:00 2001 From: John Conley <8932043+jfrconley@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:09:17 -0700 Subject: [PATCH] Add root readme --- README.md | 14 ++++++ packages/test/src/openapi.ts | 90 ++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 README.md create mode 100644 packages/test/src/openapi.ts diff --git a/README.md b/README.md new file mode 100644 index 0000000..a131d12 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# Nornir Event Processing + +This is a monorepo for the Nornir Event Processing framework. +It is a collection of packages that can be used together or independently to build event-driven applications. + +## Packages +### [Nornir Core](packages/core/README.md) +Provides the core functionality for Nornir. +It is a lightweight, functional, and type-safe event processing framework based on a middleware chain. + +### [Nornir Rest](packages/rest/README.md) +A ergonomic and type-safe REST framework based on Nornir chains. +It allows you to build strongly typed APIs exactly in sync with your documentation. +Supports both specification-first and code-first development. diff --git a/packages/test/src/openapi.ts b/packages/test/src/openapi.ts new file mode 100644 index 0000000..f22bd62 --- /dev/null +++ b/packages/test/src/openapi.ts @@ -0,0 +1,90 @@ +import nornir from "@nornir/core"; +import { ApiGatewayProxyV2, openAPIChain, OpenAPIRouter, OpenAPIV3_1, startLocalServer } from "@nornir/rest"; +import type { APIGatewayProxyEventV2, APIGatewayProxyHandlerV2 } from "aws-lambda"; + +const Spec = { + info: { + title: "Test API", + version: "1.0.0", + }, + openapi: "3.1.0", + components: { + schemas: { + "cool": { + type: "object", + properties: { + cool: { + type: "string", + }, + }, + }, + "csv": { + type: "string", + pattern: "^[a-zA-Z0-9,]+$", + }, + }, + }, + paths: { + "/cool/test": { + post: { + requestBody: { + required: true, + content: { + "application/json": { + schema: { + $ref: "#/components/schemas/cool", + }, + }, + "text/csv": { + schema: { + $ref: "#/components/schemas/csv", + }, + }, + }, + }, + responses: { + "200": { + description: "cool", + }, + "400": { + description: "bad", + headers: { + "x-foo": { + schema: { + type: "string", + }, + required: true, + }, + }, + }, + }, + }, + }, + }, +} as const satisfies OpenAPIV3_1.Document; + +const router = OpenAPIRouter.fromSpec(Spec); + +router.implementRoute("/cool/test", "post", chain => + chain.use(req => { + if (req.contentType === "text/csv") { + console.log(req.body.toUpperCase()); + } else if (req.contentType === "application/json") { + console.log(req.body.cool); + } + return { + contentType: "application/json", + statusCode: "400", + headers: { + "x-foo": "bar", + }, + } as const; + })); + +export const handler: APIGatewayProxyHandlerV2 = nornir() + .use(ApiGatewayProxyV2.toHttpEvent) + .useChain(openAPIChain(router)) + .use(ApiGatewayProxyV2.toResult) + .build(); + +startLocalServer(openAPIChain(router));