From 704ce1bc9c32fbcd72c0bcb3fb44c24ff0a3e72d Mon Sep 17 00:00:00 2001 From: Thijs Daniels Date: Fri, 15 Sep 2023 14:17:23 +0200 Subject: [PATCH] refactor(cdk-rpc-api): rename to RpcApi --- .changeset/hungry-tomatoes-remember.md | 5 ++ apps/infrastructure/stacks/Storybook.ts | 3 +- apps/infrastructure/stacks/Website.ts | 3 +- package-lock.json | 26 ++++++++- packages/cdk-api/README.md | 1 - packages/cdk-api/index.ts | 1 - packages/{cdk-api => cdk-rpc-api}/LICENSE.md | 0 packages/cdk-rpc-api/README.md | 1 + .../constructs/RpcApi.ts} | 57 ++++++++++++++----- packages/cdk-rpc-api/index.ts | 1 + .../{cdk-api => cdk-rpc-api}/package.json | 2 +- .../{cdk-api => cdk-rpc-api}/tsconfig.json | 0 12 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 .changeset/hungry-tomatoes-remember.md delete mode 100644 packages/cdk-api/README.md delete mode 100644 packages/cdk-api/index.ts rename packages/{cdk-api => cdk-rpc-api}/LICENSE.md (100%) create mode 100644 packages/cdk-rpc-api/README.md rename packages/{cdk-api/constructs/RestApi.ts => cdk-rpc-api/constructs/RpcApi.ts} (64%) create mode 100644 packages/cdk-rpc-api/index.ts rename packages/{cdk-api => cdk-rpc-api}/package.json (93%) rename packages/{cdk-api => cdk-rpc-api}/tsconfig.json (100%) diff --git a/.changeset/hungry-tomatoes-remember.md b/.changeset/hungry-tomatoes-remember.md new file mode 100644 index 00000000..c7be3be9 --- /dev/null +++ b/.changeset/hungry-tomatoes-remember.md @@ -0,0 +1,5 @@ +--- +"@codedazur/cdk-rpc-api": major +--- + +stable release diff --git a/apps/infrastructure/stacks/Storybook.ts b/apps/infrastructure/stacks/Storybook.ts index 43e3536b..9a86b5af 100644 --- a/apps/infrastructure/stacks/Storybook.ts +++ b/apps/infrastructure/stacks/Storybook.ts @@ -1,6 +1,5 @@ -import { StaticSite } from "@codedazur/cdk-static-site"; -import { App, Stack, StackProps } from "aws-cdk-lib"; import { env } from "@codedazur/essentials"; +import { App, Stack, StackProps } from "aws-cdk-lib"; import { ToolkitSite } from "../constructs/ToolkitSite"; export class Storybook extends Stack { diff --git a/apps/infrastructure/stacks/Website.ts b/apps/infrastructure/stacks/Website.ts index 1614854b..81533319 100644 --- a/apps/infrastructure/stacks/Website.ts +++ b/apps/infrastructure/stacks/Website.ts @@ -1,6 +1,5 @@ -import { StaticSite } from "@codedazur/cdk-static-site"; -import { App, Stack, StackProps } from "aws-cdk-lib"; import { env } from "@codedazur/essentials"; +import { App, Stack, StackProps } from "aws-cdk-lib"; import { ToolkitSite } from "../constructs/ToolkitSite"; export class Website extends Stack { diff --git a/package-lock.json b/package-lock.json index 77e8ade6..3942d2cb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2602,6 +2602,10 @@ "resolved": "packages/cdk-cache-invalidator", "link": true }, + "node_modules/@codedazur/cdk-rpc-api": { + "resolved": "packages/cdk-rpc-api", + "link": true + }, "node_modules/@codedazur/cdk-static-site": { "resolved": "packages/cdk-static-site", "link": true @@ -24996,6 +25000,23 @@ "constructs": ">=10" } }, + "packages/cdk-rpc-api": { + "version": "0.0.0", + "license": "MIT", + "devDependencies": { + "@codedazur/eslint-config": "*", + "@codedazur/tsconfig": "*", + "@types/node": "^20.4.5", + "aws-cdk-lib": "^2.89.0", + "constructs": "^10.2.69", + "eslint": "^8.46.0", + "typescript": "^5.1.6" + }, + "peerDependencies": { + "aws-cdk-lib": ">=2", + "constructs": ">=10" + } + }, "packages/cdk-static-site": { "name": "@codedazur/cdk-static-site", "version": "1.0.1", @@ -25035,7 +25056,7 @@ }, "packages/essentials": { "name": "@codedazur/essentials", - "version": "1.1.3", + "version": "1.2.0", "license": "MIT", "devDependencies": { "@codedazur/eslint-config": "*", @@ -25132,6 +25153,7 @@ } }, "packages/react-media": { + "name": "@codedazur/react-media", "version": "0.0.2", "license": "MIT", "dependencies": { @@ -25174,7 +25196,7 @@ }, "packages/react-pagination": { "name": "@codedazur/react-pagination", - "version": "0.0.1", + "version": "1.0.0", "license": "MIT", "dependencies": { "@codedazur/essentials": "*" diff --git a/packages/cdk-api/README.md b/packages/cdk-api/README.md deleted file mode 100644 index dc4f52da..00000000 --- a/packages/cdk-api/README.md +++ /dev/null @@ -1 +0,0 @@ -# @codedazur/cdk-api diff --git a/packages/cdk-api/index.ts b/packages/cdk-api/index.ts deleted file mode 100644 index c78af47c..00000000 --- a/packages/cdk-api/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./constructs/RestApi"; diff --git a/packages/cdk-api/LICENSE.md b/packages/cdk-rpc-api/LICENSE.md similarity index 100% rename from packages/cdk-api/LICENSE.md rename to packages/cdk-rpc-api/LICENSE.md diff --git a/packages/cdk-rpc-api/README.md b/packages/cdk-rpc-api/README.md new file mode 100644 index 00000000..adf35dda --- /dev/null +++ b/packages/cdk-rpc-api/README.md @@ -0,0 +1 @@ +# @codedazur/cdk-rpc-api diff --git a/packages/cdk-api/constructs/RestApi.ts b/packages/cdk-rpc-api/constructs/RpcApi.ts similarity index 64% rename from packages/cdk-api/constructs/RestApi.ts rename to packages/cdk-rpc-api/constructs/RpcApi.ts index e9b763a9..484ecda5 100644 --- a/packages/cdk-api/constructs/RestApi.ts +++ b/packages/cdk-rpc-api/constructs/RpcApi.ts @@ -17,8 +17,8 @@ import { import { ApiGateway } from "aws-cdk-lib/aws-route53-targets"; import { Construct } from "constructs"; -export interface ApiProps extends RestApiProps { - endpoints: Record; +export interface RpcApiProps extends RestApiProps { + procedures: Record; domain?: DomainProps; } @@ -27,33 +27,64 @@ interface DomainProps { subdomain?: string; } -interface ResourceProps { +interface ProcedureProps { method?: HttpMethod; handler?: IFunction; - endpoints?: Record; + procedures?: Record; } -export class Api extends RestApi { - constructor(scope: Construct, id: string, { endpoints, ...props }: ApiProps) { +/** + * A construct to create a Remote Procedure Call API on API Gateway. + * + * @example + * new RpcApi(this, "MyRpcApi", { + * procedures: { + * helloWorld: { + * method: HttpMethod.GET, + * handler: helloWorldHandler, + * }, + * search: { + * method: HttpMethod.GET, + * handler: searchHandler, + * procedures: { + * createIndex: { + * method: HttpMethod.POST, + * handler: createIndexHandler, + * }, + * flushIndex: { + * method: HttpMethod.DELETE, + * handler: flushIndexHandler, + * }, + * }, + * }, + * }, + * }); + */ +export class RpcApi extends RestApi { + constructor( + scope: Construct, + id: string, + { procedures: endpoints, ...props }: RpcApiProps, + ) { super(scope, id, props); - this.createEndpoints(this.root, endpoints); + this.createProcedures(this.root, endpoints); this.createDomain(props.domain); } - protected createEndpoints( + protected createProcedures( parent: IResource, - children: Record, + children: Record, ) { Object.entries(children).forEach(([slug, child]) => - this.createEndpoint(parent, slug, child), + this.createProcedure(parent, slug, child), ); } - protected createEndpoint( + protected createProcedure( parent: IResource, slug: string, - { method, handler, endpoints }: ResourceProps, + { method, handler, procedures: endpoints }: ProcedureProps, ) { const resource = parent.addResource(slug, { defaultIntegration: @@ -65,7 +96,7 @@ export class Api extends RestApi { } if (endpoints) { - this.createEndpoints(resource, endpoints); + this.createProcedures(resource, endpoints); } } diff --git a/packages/cdk-rpc-api/index.ts b/packages/cdk-rpc-api/index.ts new file mode 100644 index 00000000..086276cf --- /dev/null +++ b/packages/cdk-rpc-api/index.ts @@ -0,0 +1 @@ +export * from "./constructs/RpcApi"; diff --git a/packages/cdk-api/package.json b/packages/cdk-rpc-api/package.json similarity index 93% rename from packages/cdk-api/package.json rename to packages/cdk-rpc-api/package.json index f0445e21..7fa408a5 100644 --- a/packages/cdk-api/package.json +++ b/packages/cdk-rpc-api/package.json @@ -1,5 +1,5 @@ { - "name": "@codedazur/cdk-rest-api", + "name": "@codedazur/cdk-rpc-api", "version": "0.0.0", "main": "./index.ts", "types": "./index.ts", diff --git a/packages/cdk-api/tsconfig.json b/packages/cdk-rpc-api/tsconfig.json similarity index 100% rename from packages/cdk-api/tsconfig.json rename to packages/cdk-rpc-api/tsconfig.json