@composurecdk/apigateway 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # @composurecdk/apigateway
2
+
3
+ API Gateway builders for [ComposureCDK](../../README.md).
4
+
5
+ This package provides a fluent builder for API Gateway REST APIs with secure, AWS-recommended defaults. It wraps the CDK [RestApi](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.RestApi.html) construct — refer to the CDK documentation for the full set of configurable properties.
6
+
7
+ ## REST API Builder
8
+
9
+ ```ts
10
+ import { createRestApiBuilder } from "@composurecdk/apigateway";
11
+
12
+ const api = createRestApiBuilder()
13
+ .restApiName("My Service")
14
+ .description("Public API")
15
+ .addResource("users", (users) =>
16
+ users
17
+ .addMethod("GET", listUsersIntegration)
18
+ .addResource("{id}", (user) => user.addMethod("GET", getUserIntegration)),
19
+ )
20
+ .build(stack, "MyApi");
21
+ ```
22
+
23
+ Every [RestApiProps](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.RestApiProps.html) property is available as a fluent setter on the builder.
24
+
25
+ ## Secure Defaults
26
+
27
+ `createRestApiBuilder` applies the following defaults. Each can be overridden via the builder's fluent API.
28
+
29
+ | Property | Default | Rationale |
30
+ | ------------------ | ------- | ----------------------------------------------------------------------------------- |
31
+ | `accessLogging` | `true` | Auto-creates a CloudWatch log group for access logging with structured JSON output. |
32
+ | `tracingEnabled` | `true` | Enables X-Ray distributed tracing on the stage. |
33
+ | `loggingLevel` | `INFO` | Enables CloudWatch execution logging for all methods. |
34
+ | `dataTraceEnabled` | `false` | Prevents sensitive request/response bodies from appearing in logs. |
35
+
36
+ These defaults are guided by the [AWS Well-Architected Serverless Applications Lens](https://docs.aws.amazon.com/wellarchitected/latest/serverless-applications-lens/opex-distributed-tracing.html).
37
+
38
+ The defaults are exported as `REST_API_DEFAULTS` for visibility and testing:
39
+
40
+ ```ts
41
+ import { REST_API_DEFAULTS } from "@composurecdk/apigateway";
42
+ ```
43
+
44
+ ### Overriding defaults
45
+
46
+ ```ts
47
+ import { MethodLoggingLevel } from "aws-cdk-lib/aws-apigateway";
48
+
49
+ const api = createRestApiBuilder()
50
+ .restApiName("My Service")
51
+ .accessLogging(false)
52
+ .deployOptions({ tracingEnabled: false, loggingLevel: MethodLoggingLevel.ERROR })
53
+ .build(stack, "MyApi");
54
+ ```
55
+
56
+ ### Access logging
57
+
58
+ By default, the builder creates a CloudWatch log group (using `@composurecdk/logs` with its secure defaults) and configures it as the stage's access log destination. The created log group is returned in the build result:
59
+
60
+ ```ts
61
+ const result = createRestApiBuilder()
62
+ .restApiName("My Service")
63
+ .addMethod("GET", integration, methodResponse)
64
+ .build(stack, "MyApi");
65
+
66
+ result.api; // RestApi
67
+ result.accessLogGroup; // LogGroup | undefined
68
+ ```
69
+
70
+ To provide your own destination instead, set `deployOptions.accessLogDestination` — the auto-created log group is skipped. To disable access logging entirely, set `.accessLogging(false)`.
71
+
72
+ ## Examples
73
+
74
+ - [LambdaApiStack](../examples/src/lambda-api-app.ts) — REST API backed by a Lambda function, wired with `ref`
75
+ - [MockApiStack](../examples/src/mock-api-app.ts) — CRUD REST API with mock integrations
76
+ - [MultiStackApp](../examples/src/multi-stack-app.ts) — REST API split across stacks via `.withStacks()`
77
+ - [StrategyStackApp](../examples/src/strategy-stack-app.ts) — REST API split across stacks via `.withStackStrategy()`
@@ -0,0 +1,8 @@
1
+ import type { RestApiBuilderProps } from "./rest-api-builder.js";
2
+ /**
3
+ * Secure, AWS-recommended defaults applied to every REST API built with
4
+ * {@link createRestApiBuilder}. Each property can be individually overridden
5
+ * via the builder's fluent API.
6
+ */
7
+ export declare const REST_API_DEFAULTS: Partial<RestApiBuilderProps>;
8
+ //# sourceMappingURL=defaults.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAEjE;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,EAAE,OAAO,CAAC,mBAAmB,CA6B1D,CAAC"}
@@ -0,0 +1,34 @@
1
+ import { MethodLoggingLevel } from "aws-cdk-lib/aws-apigateway";
2
+ /**
3
+ * Secure, AWS-recommended defaults applied to every REST API built with
4
+ * {@link createRestApiBuilder}. Each property can be individually overridden
5
+ * via the builder's fluent API.
6
+ */
7
+ export const REST_API_DEFAULTS = {
8
+ /**
9
+ * Automatically create an access log group with structured JSON output.
10
+ * Access logging provides an audit trail of all API calls for security
11
+ * monitoring and troubleshooting.
12
+ * @see https://docs.aws.amazon.com/wellarchitected/latest/serverless-applications-lens/opex-logging.html
13
+ */
14
+ accessLogging: true,
15
+ deployOptions: {
16
+ /**
17
+ * Enable AWS X-Ray tracing on the API Gateway stage.
18
+ * @see https://docs.aws.amazon.com/wellarchitected/latest/serverless-applications-lens/opex-distributed-tracing.html
19
+ */
20
+ tracingEnabled: true,
21
+ /**
22
+ * Enable CloudWatch execution logging for API Gateway methods.
23
+ * @see https://docs.aws.amazon.com/wellarchitected/latest/serverless-applications-lens/opex-logging.html
24
+ */
25
+ loggingLevel: MethodLoggingLevel.INFO,
26
+ /**
27
+ * Disable full request/response body logging to prevent sensitive data
28
+ * from appearing in CloudWatch logs.
29
+ * @see https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-logging.html
30
+ */
31
+ dataTraceEnabled: false,
32
+ },
33
+ };
34
+ //# sourceMappingURL=defaults.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defaults.js","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAGhE;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAiC;IAC7D;;;;;OAKG;IACH,aAAa,EAAE,IAAI;IAEnB,aAAa,EAAE;QACb;;;WAGG;QACH,cAAc,EAAE,IAAI;QAEpB;;;WAGG;QACH,YAAY,EAAE,kBAAkB,CAAC,IAAI;QAErC;;;;WAIG;QACH,gBAAgB,EAAE,KAAK;KACxB;CACF,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { createRestApiBuilder, type RestApiBuilderProps, type RestApiBuilderResult, type IRestApiBuilder, } from "./rest-api-builder.js";
2
+ export { REST_API_DEFAULTS } from "./defaults.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,EACzB,KAAK,eAAe,GACrB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { createRestApiBuilder, } from "./rest-api-builder.js";
2
+ export { REST_API_DEFAULTS } from "./defaults.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,GAIrB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC"}
@@ -0,0 +1,58 @@
1
+ import { type Integration, type IResource, type MethodOptions } from "aws-cdk-lib/aws-apigateway";
2
+ import { type Resolvable } from "@composurecdk/core";
3
+ interface MethodDefinition {
4
+ httpMethod: string;
5
+ integration?: Resolvable<Integration>;
6
+ options?: MethodOptions;
7
+ }
8
+ interface ResourceDefinition {
9
+ methods: MethodDefinition[];
10
+ children: Map<string, ResourceDefinition>;
11
+ }
12
+ /**
13
+ * A declarative builder for defining API Gateway resources and methods.
14
+ *
15
+ * `ResourceBuilder` captures a tree of resource paths and HTTP methods as
16
+ * data. The tree is applied to actual CDK constructs during
17
+ * {@link RestApiBuilder.build}. Users do not construct `ResourceBuilder`
18
+ * directly — instances are provided via the {@link IRestApiBuilder.addResource}
19
+ * callback.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * createRestApiBuilder()
24
+ * .addResource("users", users => users
25
+ * .addMethod("GET", listIntegration)
26
+ * .addResource("{id}", user => user
27
+ * .addMethod("GET", getIntegration)
28
+ * .addMethod("DELETE", deleteIntegration)
29
+ * )
30
+ * );
31
+ * ```
32
+ */
33
+ export declare class ResourceBuilder {
34
+ /** @internal */
35
+ readonly definition: ResourceDefinition;
36
+ /**
37
+ * Adds an HTTP method to this resource.
38
+ *
39
+ * @param httpMethod - The HTTP verb (GET, POST, PUT, DELETE, etc.).
40
+ * @param integration - The backend integration for this method. Accepts a concrete
41
+ * {@link Integration} or a {@link Ref} that resolves to one at build time.
42
+ * @param options - Additional method configuration such as authorization or method responses.
43
+ * @returns This builder for chaining.
44
+ */
45
+ addMethod(httpMethod: string, integration?: Resolvable<Integration>, options?: MethodOptions): this;
46
+ /**
47
+ * Adds a child resource under this resource.
48
+ *
49
+ * @param pathPart - The path segment for the child resource (e.g. "users" or "\{id\}").
50
+ * @param configure - Optional callback to configure the child resource's methods and nested resources.
51
+ * @returns This builder for chaining.
52
+ */
53
+ addResource(pathPart: string, configure?: (resource: ResourceBuilder) => void): this;
54
+ /** @internal */
55
+ applyTo(resource: IResource, context?: Record<string, object>): void;
56
+ }
57
+ export {};
58
+ //# sourceMappingURL=resource-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resource-builder.d.ts","sourceRoot":"","sources":["../src/resource-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAE,KAAK,SAAS,EAAE,KAAK,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAClG,OAAO,EAAW,KAAK,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAE9D,UAAU,gBAAgB;IACxB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;IACtC,OAAO,CAAC,EAAE,aAAa,CAAC;CACzB;AAED,UAAU,kBAAkB;IAC1B,OAAO,EAAE,gBAAgB,EAAE,CAAC;IAC5B,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;CAC3C;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,eAAe;IAC1B,gBAAgB;IAChB,QAAQ,CAAC,UAAU,EAAE,kBAAkB,CAAwC;IAE/E;;;;;;;;OAQG;IACH,SAAS,CACP,UAAU,EAAE,MAAM,EAClB,WAAW,CAAC,EAAE,UAAU,CAAC,WAAW,CAAC,EACrC,OAAO,CAAC,EAAE,aAAa,GACtB,IAAI;IAKP;;;;;;OAMG;IACH,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,CAAC,QAAQ,EAAE,eAAe,KAAK,IAAI,GAAG,IAAI;IASpF,gBAAgB;IAChB,OAAO,CAAC,QAAQ,EAAE,SAAS,EAAE,OAAO,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM,GAAG,IAAI;CAczE"}
@@ -0,0 +1,69 @@
1
+ import { resolve } from "@composurecdk/core";
2
+ /**
3
+ * A declarative builder for defining API Gateway resources and methods.
4
+ *
5
+ * `ResourceBuilder` captures a tree of resource paths and HTTP methods as
6
+ * data. The tree is applied to actual CDK constructs during
7
+ * {@link RestApiBuilder.build}. Users do not construct `ResourceBuilder`
8
+ * directly — instances are provided via the {@link IRestApiBuilder.addResource}
9
+ * callback.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * createRestApiBuilder()
14
+ * .addResource("users", users => users
15
+ * .addMethod("GET", listIntegration)
16
+ * .addResource("{id}", user => user
17
+ * .addMethod("GET", getIntegration)
18
+ * .addMethod("DELETE", deleteIntegration)
19
+ * )
20
+ * );
21
+ * ```
22
+ */
23
+ export class ResourceBuilder {
24
+ /** @internal */
25
+ definition = { methods: [], children: new Map() };
26
+ /**
27
+ * Adds an HTTP method to this resource.
28
+ *
29
+ * @param httpMethod - The HTTP verb (GET, POST, PUT, DELETE, etc.).
30
+ * @param integration - The backend integration for this method. Accepts a concrete
31
+ * {@link Integration} or a {@link Ref} that resolves to one at build time.
32
+ * @param options - Additional method configuration such as authorization or method responses.
33
+ * @returns This builder for chaining.
34
+ */
35
+ addMethod(httpMethod, integration, options) {
36
+ this.definition.methods.push({ httpMethod, integration, options });
37
+ return this;
38
+ }
39
+ /**
40
+ * Adds a child resource under this resource.
41
+ *
42
+ * @param pathPart - The path segment for the child resource (e.g. "users" or "\{id\}").
43
+ * @param configure - Optional callback to configure the child resource's methods and nested resources.
44
+ * @returns This builder for chaining.
45
+ */
46
+ addResource(pathPart, configure) {
47
+ const child = new ResourceBuilder();
48
+ if (configure) {
49
+ configure(child);
50
+ }
51
+ this.definition.children.set(pathPart, child.definition);
52
+ return this;
53
+ }
54
+ /** @internal */
55
+ applyTo(resource, context = {}) {
56
+ for (const method of this.definition.methods) {
57
+ const integration = method.integration !== undefined ? resolve(method.integration, context) : undefined;
58
+ resource.addMethod(method.httpMethod, integration, method.options);
59
+ }
60
+ for (const [pathPart, childDef] of this.definition.children) {
61
+ const childResource = resource.addResource(pathPart);
62
+ const childBuilder = new ResourceBuilder();
63
+ childBuilder.definition.methods = childDef.methods;
64
+ childDef.children.forEach((v, k) => childBuilder.definition.children.set(k, v));
65
+ childBuilder.applyTo(childResource, context);
66
+ }
67
+ }
68
+ }
69
+ //# sourceMappingURL=resource-builder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resource-builder.js","sourceRoot":"","sources":["../src/resource-builder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAmB,MAAM,oBAAoB,CAAC;AAa9D;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,OAAO,eAAe;IAC1B,gBAAgB;IACP,UAAU,GAAuB,EAAE,OAA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AAO,GAAG,QAAQ,CAAC,OAAO,CAAC;YACnD,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,YAAY,CAAC,UAAU,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;YAChF,YAAY,CAAC,OAAO,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;QAC/C,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,126 @@
1
+ import { type Integration, type MethodOptions, RestApi, type RestApiProps } from "aws-cdk-lib/aws-apigateway";
2
+ import { type LogGroup } from "aws-cdk-lib/aws-logs";
3
+ import { type IConstruct } from "constructs";
4
+ import { type IBuilder, type Lifecycle, type Resolvable } from "@composurecdk/core";
5
+ import { ResourceBuilder } from "./resource-builder.js";
6
+ /**
7
+ * Configuration properties for the REST API builder.
8
+ *
9
+ * Extends the CDK {@link RestApiProps} with additional builder-specific options.
10
+ */
11
+ export interface RestApiBuilderProps extends RestApiProps {
12
+ /**
13
+ * Whether to automatically create a CloudWatch log group for access logging.
14
+ *
15
+ * When `true`, the builder creates a log group using
16
+ * {@link createLogGroupBuilder} (with its secure defaults) and configures it
17
+ * as the stage's access log destination with JSON-formatted output. The
18
+ * created log group is returned in the build result as `accessLogGroup`.
19
+ *
20
+ * When `false`, no access log group is created. You can still provide your
21
+ * own destination via `deployOptions.accessLogDestination`.
22
+ *
23
+ * This setting is ignored when `deployOptions.accessLogDestination` is
24
+ * provided — the user-supplied destination takes precedence.
25
+ */
26
+ accessLogging?: boolean;
27
+ }
28
+ /**
29
+ * The build output of a {@link IRestApiBuilder}. Contains the CDK constructs
30
+ * created during {@link Lifecycle.build}, keyed by role.
31
+ */
32
+ export interface RestApiBuilderResult {
33
+ /** The REST API construct created by the builder. */
34
+ api: RestApi;
35
+ /**
36
+ * The CloudWatch log group created for access logging, or `undefined` if
37
+ * access logging was disabled or the user provided their own destination.
38
+ */
39
+ accessLogGroup?: LogGroup;
40
+ }
41
+ /**
42
+ * A fluent builder for configuring and creating an API Gateway REST API.
43
+ *
44
+ * Configuration properties from CDK {@link RestApiProps} are exposed as
45
+ * overloaded getter/setter methods via the builder proxy. The resource tree
46
+ * (paths and HTTP methods) is defined using {@link addResource} and
47
+ * {@link addMethod}, which accept the same arguments as their CDK equivalents.
48
+ *
49
+ * The builder implements {@link Lifecycle}, so it can be used directly as a
50
+ * component in a {@link compose | composed system}. When built, it creates
51
+ * a REST API with the configured properties and resource tree, and returns
52
+ * a {@link RestApiBuilderResult}.
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * const api = createRestApiBuilder()
57
+ * .restApiName("My Service")
58
+ * .description("Public API")
59
+ * .addResource("users", users => users
60
+ * .addMethod("GET", listUsersIntegration)
61
+ * .addResource("{id}", user => user
62
+ * .addMethod("GET", getUserIntegration)
63
+ * )
64
+ * );
65
+ * ```
66
+ */
67
+ export type IRestApiBuilder = IBuilder<RestApiBuilderProps, RestApiBuilder>;
68
+ declare class RestApiBuilder implements Lifecycle<RestApiBuilderResult> {
69
+ props: Partial<RestApiBuilderProps>;
70
+ private readonly root;
71
+ /**
72
+ * Adds an HTTP method to the API root resource (`/`).
73
+ *
74
+ * @param httpMethod - The HTTP verb (GET, POST, PUT, DELETE, etc.).
75
+ * @param integration - The backend integration for this method. Accepts a concrete
76
+ * {@link Integration} or a {@link Ref} that resolves to one at build time.
77
+ * @param options - Additional method configuration such as authorization or method responses.
78
+ * @returns This builder for chaining.
79
+ */
80
+ addMethod(httpMethod: string, integration?: Resolvable<Integration>, options?: MethodOptions): this;
81
+ /**
82
+ * Adds a child resource under the API root resource (`/`).
83
+ *
84
+ * @param pathPart - The path segment for the resource (e.g. "users" or "\{id\}").
85
+ * @param configure - Optional callback to configure the resource's methods and nested resources.
86
+ * @returns This builder for chaining.
87
+ */
88
+ addResource(pathPart: string, configure?: (resource: ResourceBuilder) => void): this;
89
+ build(scope: IConstruct, id: string, context?: Record<string, object>): RestApiBuilderResult;
90
+ }
91
+ /**
92
+ * Creates a new {@link IRestApiBuilder} for configuring an API Gateway REST API.
93
+ *
94
+ * This is the entry point for defining a REST API component. The returned
95
+ * builder exposes every {@link RestApiProps} property as a fluent setter/getter,
96
+ * plus {@link IRestApiBuilder.addResource | addResource} and
97
+ * {@link IRestApiBuilder.addMethod | addMethod} for defining the resource tree.
98
+ * It implements {@link Lifecycle} for use with {@link compose}.
99
+ *
100
+ * @returns A fluent builder for an API Gateway REST API.
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * const api = createRestApiBuilder()
105
+ * .restApiName("My Service")
106
+ * .description("Public API")
107
+ * .addResource("users", users => users
108
+ * .addMethod("GET", listUsersIntegration)
109
+ * .addResource("{id}", user => user
110
+ * .addMethod("GET", getUserIntegration)
111
+ * )
112
+ * );
113
+ *
114
+ * // Use standalone:
115
+ * const result = api.build(stack, "MyApi");
116
+ *
117
+ * // Or compose into a system:
118
+ * const system = compose(
119
+ * { api, handler: createFunctionBuilder() },
120
+ * { api: ["handler"], handler: [] },
121
+ * );
122
+ * ```
123
+ */
124
+ export declare function createRestApiBuilder(): IRestApiBuilder;
125
+ export {};
126
+ //# sourceMappingURL=rest-api-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rest-api-builder.d.ts","sourceRoot":"","sources":["../src/rest-api-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,WAAW,EAEhB,KAAK,aAAa,EAClB,OAAO,EACP,KAAK,YAAY,EAClB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAW,KAAK,QAAQ,EAAE,KAAK,SAAS,EAAE,KAAK,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAE7F,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAGxD;;;;GAIG;AACH,MAAM,WAAW,mBAAoB,SAAQ,YAAY;IACvD;;;;;;;;;;;;;OAaG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED;;;GAGG;AACH,MAAM,WAAW,oBAAoB;IACnC,qDAAqD;IACrD,GAAG,EAAE,OAAO,CAAC;IAEb;;;OAGG;IACH,cAAc,CAAC,EAAE,QAAQ,CAAC;CAC3B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,MAAM,eAAe,GAAG,QAAQ,CAAC,mBAAmB,EAAE,cAAc,CAAC,CAAC;AAE5E,cAAM,cAAe,YAAW,SAAS,CAAC,oBAAoB,CAAC;IAC7D,KAAK,EAAE,OAAO,CAAC,mBAAmB,CAAC,CAAM;IACzC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAyB;IAE9C;;;;;;;;OAQG;IACH,SAAS,CACP,UAAU,EAAE,MAAM,EAClB,WAAW,CAAC,EAAE,UAAU,CAAC,WAAW,CAAC,EACrC,OAAO,CAAC,EAAE,aAAa,GACtB,IAAI;IAKP;;;;;;OAMG;IACH,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,CAAC,QAAQ,EAAE,eAAe,KAAK,IAAI,GAAG,IAAI;IAKpF,KAAK,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,oBAAoB;CA8B7F;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,oBAAoB,IAAI,eAAe,CAEtD"}
@@ -0,0 +1,95 @@
1
+ import { AccessLogFormat, LogGroupLogDestination, RestApi, } from "aws-cdk-lib/aws-apigateway";
2
+ import { Builder } from "@composurecdk/core";
3
+ import { createLogGroupBuilder } from "@composurecdk/logs";
4
+ import { ResourceBuilder } from "./resource-builder.js";
5
+ import { REST_API_DEFAULTS } from "./defaults.js";
6
+ class RestApiBuilder {
7
+ props = {};
8
+ root = new ResourceBuilder();
9
+ /**
10
+ * Adds an HTTP method to the API root resource (`/`).
11
+ *
12
+ * @param httpMethod - The HTTP verb (GET, POST, PUT, DELETE, etc.).
13
+ * @param integration - The backend integration for this method. Accepts a concrete
14
+ * {@link Integration} or a {@link Ref} that resolves to one at build time.
15
+ * @param options - Additional method configuration such as authorization or method responses.
16
+ * @returns This builder for chaining.
17
+ */
18
+ addMethod(httpMethod, integration, options) {
19
+ this.root.addMethod(httpMethod, integration, options);
20
+ return this;
21
+ }
22
+ /**
23
+ * Adds a child resource under the API root resource (`/`).
24
+ *
25
+ * @param pathPart - The path segment for the resource (e.g. "users" or "\{id\}").
26
+ * @param configure - Optional callback to configure the resource's methods and nested resources.
27
+ * @returns This builder for chaining.
28
+ */
29
+ addResource(pathPart, configure) {
30
+ this.root.addResource(pathPart, configure);
31
+ return this;
32
+ }
33
+ build(scope, id, context) {
34
+ const { accessLogging, ...restApiProps } = this.props;
35
+ const userDeployOptions = restApiProps.deployOptions ?? {};
36
+ const autoAccessLog = (accessLogging ?? REST_API_DEFAULTS.accessLogging) && !userDeployOptions.accessLogDestination;
37
+ let accessLogGroup;
38
+ let accessLogProps = {};
39
+ if (autoAccessLog) {
40
+ accessLogGroup = createLogGroupBuilder().build(scope, `${id}AccessLogs`).logGroup;
41
+ accessLogProps = {
42
+ accessLogDestination: new LogGroupLogDestination(accessLogGroup),
43
+ accessLogFormat: AccessLogFormat.jsonWithStandardFields(),
44
+ };
45
+ }
46
+ const mergedProps = {
47
+ ...restApiProps,
48
+ deployOptions: {
49
+ ...REST_API_DEFAULTS.deployOptions,
50
+ ...accessLogProps,
51
+ ...userDeployOptions,
52
+ },
53
+ };
54
+ const api = new RestApi(scope, id, mergedProps);
55
+ this.root.applyTo(api.root, context ?? {});
56
+ return { api, accessLogGroup };
57
+ }
58
+ }
59
+ /**
60
+ * Creates a new {@link IRestApiBuilder} for configuring an API Gateway REST API.
61
+ *
62
+ * This is the entry point for defining a REST API component. The returned
63
+ * builder exposes every {@link RestApiProps} property as a fluent setter/getter,
64
+ * plus {@link IRestApiBuilder.addResource | addResource} and
65
+ * {@link IRestApiBuilder.addMethod | addMethod} for defining the resource tree.
66
+ * It implements {@link Lifecycle} for use with {@link compose}.
67
+ *
68
+ * @returns A fluent builder for an API Gateway REST API.
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * const api = createRestApiBuilder()
73
+ * .restApiName("My Service")
74
+ * .description("Public API")
75
+ * .addResource("users", users => users
76
+ * .addMethod("GET", listUsersIntegration)
77
+ * .addResource("{id}", user => user
78
+ * .addMethod("GET", getUserIntegration)
79
+ * )
80
+ * );
81
+ *
82
+ * // Use standalone:
83
+ * const result = api.build(stack, "MyApi");
84
+ *
85
+ * // Or compose into a system:
86
+ * const system = compose(
87
+ * { api, handler: createFunctionBuilder() },
88
+ * { api: ["handler"], handler: [] },
89
+ * );
90
+ * ```
91
+ */
92
+ export function createRestApiBuilder() {
93
+ return Builder(RestApiBuilder);
94
+ }
95
+ //# sourceMappingURL=rest-api-builder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rest-api-builder.js","sourceRoot":"","sources":["../src/rest-api-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EAEf,sBAAsB,EAEtB,OAAO,GAER,MAAM,4BAA4B,CAAC;AAGpC,OAAO,EAAE,OAAO,EAAkD,MAAM,oBAAoB,CAAC;AAC7F,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAoElD,MAAM,cAAc;IAClB,KAAK,GAAiC,EAAE,CAAC;IACxB,IAAI,GAAG,IAAI,eAAe,EAAE,CAAC;IAE9C;;;;;;;;OAQG;IACH,SAAS,CACP,UAAkB,EAClB,WAAqC,EACrC,OAAuB;QAEvB,IAAI,CAAC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AAC;QAC3C,OAAO,EAAE,GAAG,EAAE,cAAc,EAAE,CAAC;IACjC,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,UAAU,oBAAoB;IAClC,OAAO,OAAO,CAAsC,cAAc,CAAC,CAAC;AACtE,CAAC"}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@composurecdk/apigateway",
3
+ "version": "0.1.0",
4
+ "description": "Composable API Gateway REST API builder with well-architected defaults",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.js",
10
+ "types": "./dist/index.d.ts"
11
+ }
12
+ },
13
+ "files": [
14
+ "dist",
15
+ "README.md",
16
+ "LICENSE"
17
+ ],
18
+ "scripts": {
19
+ "clean": "rm -rf dist",
20
+ "build": "tsc -p tsconfig.build.json",
21
+ "typecheck": "tsc --noEmit",
22
+ "test": "vitest run",
23
+ "test:watch": "vitest"
24
+ },
25
+ "keywords": [],
26
+ "author": "Jason Duffett (https://github.com/laazyj)",
27
+ "license": "MIT",
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "type": "module",
32
+ "peerDependencies": {
33
+ "@composurecdk/core": "^0.1.0",
34
+ "@composurecdk/logs": "^0.1.0",
35
+ "aws-cdk-lib": "^2.0.0",
36
+ "constructs": "^10.0.0"
37
+ },
38
+ "devDependencies": {
39
+ "@types/node": "^25.5.0",
40
+ "aws-cdk-lib": "^2.245.0",
41
+ "constructs": "^10.6.0",
42
+ "typescript": "^6.0.2",
43
+ "vitest": "^4.1.2"
44
+ }
45
+ }