@flipdish/cdk-constructs
v0.88.0
Published
CDK Constructs for Flipdish serverless applications
Keywords
Readme
@flipdish/cdk-constructs
CDK Constructs for Flipdish serverless applications, with a focus on API constructs that provide automatic OpenAPI documentation generation and deployment.
Installation
npm install @flipdish/cdk-constructsRequirements
- Zod 4 (
^4.0.0). This package uses@asteasolutions/zod-to-openapi9.x, which supports Zod 4 only. - SST v2.
Breaking changes
Zod 4
This release requires Zod 4. Consumers still on Zod 3 must upgrade before adopting it. The Api route schema types and the OpenAPI generator are built against Zod 4; passing Zod 3 schemas can fail stack synthesis or produce an incorrect OpenAPI document.
Overview
This package provides CDK constructs and utilities for building serverless APIs with strong typing and automatic OpenAPI documentation. It is designed to be used with SST and integrates with Zod for schema validation and OpenAPI generation.
Features
- Api Construct: Drop-in replacement for SST's Api construct, with built-in OpenAPI documentation generation.
- Automatic OpenAPI Docs: Generates and deploys OpenAPI JSON, YAML, and HTML documentation for your API routes.
- Schema Integration: Supports Zod-based schema definitions for routes.
- TypeScript-first: All constructs and utilities are strictly typed.
Usage
Basic Example
import { Api, SchemaFunction } from '@flipdish/cdk-constructs';
import { z } from 'zod';
import { StackContext } from 'sst/constructs';
export function API({ stack }: StackContext) {
const api = new Api(stack, 'Api', {
routes: {
'GET /hello': {
function: new SchemaFunction(stack, 'HelloHandler', {
handler: 'src/hello.handler',
schema: {
responses: {
200: z.object({ message: z.string() }),
},
},
}),
},
},
});
// The following endpoints are automatically added:
// - /openapi.json
// - /openapi.yaml
// - /openapi.html
return { api };
}OpenAPI Utilities
You can also use the following utilities for advanced OpenAPI schema generation:
buildSchema(registry, serviceName, url): Generates an OpenAPI schema document from a registry.sstRoutesToOpenApiSchema(routes): Converts SST route definitions to OpenAPI route configs.
Types
The package exports several types to help with strong typing and schema integration:
SchemaSchemaFunctionSchemaFunctionPropsSchemaRoutePropsSchemaApiPropsSchemaApiFunctionRouteProps
Idle-limited agent
A keep-alive https.Agent for Lambda clients. It closes a pooled connection idle for over a minute instead of reusing it, so the first request after
a long idle gap no longer fails with socket hang up and gets retried. See
ADR 66.
Import it from the subpath @flipdish/cdk-constructs/idle-limited-agent. It loads no CDK code, so it is safe in a Lambda bundle. The main entry is
not.
How to use it
Add the package to the package that creates the client:
pnpm --filter <package-name> add @flipdish/cdk-constructsCreate each client once, at module scope, and give it the agent.
AWS SDK v3 (v3.521.0 or later; older clients take new NodeHttpHandler({ httpsAgent })):
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { idleLimitedHttpsAgentForAwsSdk } from '@flipdish/cdk-constructs/idle-limited-agent';
export const dynamo = new DynamoDBClient({ requestHandler: { httpsAgent: idleLimitedHttpsAgentForAwsSdk() } });Stripe:
import { idleLimitedHttpsAgent } from '@flipdish/cdk-constructs/idle-limited-agent';
import Stripe from 'stripe';
/** One agent for every Stripe client, so they share connections. */
const stripeAgent = idleLimitedHttpsAgent();
const stripe = new Stripe(apiKey, { httpAgent: stripeAgent });Pass { maxIdleMs: 30_000 } only if a server drops idle connections sooner than a minute.
Reference
| Export | What it is |
| ------------------------------------------ | --------------------------------------------------------------------------------------------- |
| new IdleLimitedHttpsAgent(options?) | https.Agent subclass. Keep-alive on by default. Reads maxIdleMs; passes the rest to Node. |
| idleLimitedHttpsAgent(options?) | Same as new IdleLimitedHttpsAgent(options). For Stripe or any client that takes an agent. |
| idleLimitedHttpsAgentForAwsSdk(options?) | Same, with maxSockets: 50 like the agent the AWS SDK makes for itself. |
| IdleLimitedHttpsAgentOptions | https.AgentOptions plus maxIdleMs. |
maxIdleMs: how long, in ms, a pooled connection may sit idle before it is closed instead of reused. Default 60000. Readable as
agent.maxIdleMs. 0 closes a connection once it has been idle for a millisecond. A negative or non-finite value throws a RangeError. The limit
is checked when a request arrives, so it holds across a Lambda freeze.
Development & Testing
This package is maintained in the serverless-app-template repository.
Testing
You can test the constructs by deploying a stack and verifying that the OpenAPI documentation endpoints are available and accurate. For unit testing, use your preferred test runner (e.g., Vitest or Jest) to check that the constructs behave as expected.
Run the unit tests from the repository root. CI runs them before each publish.
pnpm exec vitest run packages/cdk-constructsLicense
MIT
Contributing
This package is maintained in the serverless-app-template repository.
