Skip to content

Commit

Permalink
refactor(cdk-rpc-api): rename to RpcApi
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsdaniels committed Sep 15, 2023
1 parent 936734a commit 704ce1b
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-tomatoes-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codedazur/cdk-rpc-api": major
---

stable release
3 changes: 1 addition & 2 deletions apps/infrastructure/stacks/Storybook.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions apps/infrastructure/stacks/Website.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
26 changes: 24 additions & 2 deletions package-lock.json

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

1 change: 0 additions & 1 deletion packages/cdk-api/README.md

This file was deleted.

1 change: 0 additions & 1 deletion packages/cdk-api/index.ts

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions packages/cdk-rpc-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @codedazur/cdk-rpc-api
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ResourceProps>;
export interface RpcApiProps extends RestApiProps {
procedures: Record<string, ProcedureProps>;
domain?: DomainProps;
}

Expand All @@ -27,33 +27,64 @@ interface DomainProps {
subdomain?: string;
}

interface ResourceProps {
interface ProcedureProps {
method?: HttpMethod;
handler?: IFunction;
endpoints?: Record<string, ResourceProps>;
procedures?: Record<string, ProcedureProps>;
}

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<string, ResourceProps>,
children: Record<string, ProcedureProps>,
) {
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:
Expand All @@ -65,7 +96,7 @@ export class Api extends RestApi {
}

if (endpoints) {
this.createEndpoints(resource, endpoints);
this.createProcedures(resource, endpoints);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/cdk-rpc-api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./constructs/RpcApi";
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@codedazur/cdk-rest-api",
"name": "@codedazur/cdk-rpc-api",
"version": "0.0.0",
"main": "./index.ts",
"types": "./index.ts",
Expand Down
File renamed without changes.

0 comments on commit 704ce1b

Please sign in to comment.