@fnd-platform/api 1.0.0-alpha.1

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.
Files changed (75) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +204 -0
  3. package/lib/api-project.d.ts +99 -0
  4. package/lib/api-project.d.ts.map +1 -0
  5. package/lib/api-project.js +668 -0
  6. package/lib/api-project.js.map +1 -0
  7. package/lib/handlers/content.d.ts +52 -0
  8. package/lib/handlers/content.d.ts.map +1 -0
  9. package/lib/handlers/content.js +122 -0
  10. package/lib/handlers/content.js.map +1 -0
  11. package/lib/handlers/health.d.ts +43 -0
  12. package/lib/handlers/health.d.ts.map +1 -0
  13. package/lib/handlers/health.js +43 -0
  14. package/lib/handlers/health.js.map +1 -0
  15. package/lib/handlers/media.d.ts +86 -0
  16. package/lib/handlers/media.d.ts.map +1 -0
  17. package/lib/handlers/media.js +287 -0
  18. package/lib/handlers/media.js.map +1 -0
  19. package/lib/index.d.ts +22 -0
  20. package/lib/index.d.ts.map +1 -0
  21. package/lib/index.js +50 -0
  22. package/lib/index.js.map +1 -0
  23. package/lib/lib/errors.d.ts +114 -0
  24. package/lib/lib/errors.d.ts.map +1 -0
  25. package/lib/lib/errors.js +154 -0
  26. package/lib/lib/errors.js.map +1 -0
  27. package/lib/lib/middleware.d.ts +33 -0
  28. package/lib/lib/middleware.d.ts.map +1 -0
  29. package/lib/lib/middleware.js +36 -0
  30. package/lib/lib/middleware.js.map +1 -0
  31. package/lib/lib/request.d.ts +90 -0
  32. package/lib/lib/request.d.ts.map +1 -0
  33. package/lib/lib/request.js +115 -0
  34. package/lib/lib/request.js.map +1 -0
  35. package/lib/lib/response.d.ts +131 -0
  36. package/lib/lib/response.d.ts.map +1 -0
  37. package/lib/lib/response.js +163 -0
  38. package/lib/lib/response.js.map +1 -0
  39. package/lib/middleware/auth.d.ts +57 -0
  40. package/lib/middleware/auth.d.ts.map +1 -0
  41. package/lib/middleware/auth.js +62 -0
  42. package/lib/middleware/auth.js.map +1 -0
  43. package/lib/middleware/cors.d.ts +51 -0
  44. package/lib/middleware/cors.d.ts.map +1 -0
  45. package/lib/middleware/cors.js +62 -0
  46. package/lib/middleware/cors.js.map +1 -0
  47. package/lib/middleware/error-handler.d.ts +38 -0
  48. package/lib/middleware/error-handler.d.ts.map +1 -0
  49. package/lib/middleware/error-handler.js +49 -0
  50. package/lib/middleware/error-handler.js.map +1 -0
  51. package/lib/middleware/index.d.ts +15 -0
  52. package/lib/middleware/index.d.ts.map +1 -0
  53. package/lib/middleware/index.js +49 -0
  54. package/lib/middleware/index.js.map +1 -0
  55. package/lib/middleware/logging.d.ts +48 -0
  56. package/lib/middleware/logging.d.ts.map +1 -0
  57. package/lib/middleware/logging.js +58 -0
  58. package/lib/middleware/logging.js.map +1 -0
  59. package/lib/middleware/validation.d.ts +41 -0
  60. package/lib/middleware/validation.d.ts.map +1 -0
  61. package/lib/middleware/validation.js +76 -0
  62. package/lib/middleware/validation.js.map +1 -0
  63. package/lib/options.d.ts +48 -0
  64. package/lib/options.d.ts.map +1 -0
  65. package/lib/options.js +3 -0
  66. package/lib/options.js.map +1 -0
  67. package/lib/types/api.d.ts +108 -0
  68. package/lib/types/api.d.ts.map +1 -0
  69. package/lib/types/api.js +11 -0
  70. package/lib/types/api.js.map +1 -0
  71. package/lib/types/middleware.d.ts +59 -0
  72. package/lib/types/middleware.d.ts.map +1 -0
  73. package/lib/types/middleware.js +11 -0
  74. package/lib/types/middleware.js.map +1 -0
  75. package/package.json +54 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 fnd-platform contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,204 @@
1
+ # @fnd-platform/api
2
+
3
+ Projen project class and runtime utilities for building serverless Lambda APIs with type-safe handlers, middleware patterns, and DynamoDB integration.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install -D @fnd-platform/api
9
+ # or
10
+ pnpm add -D @fnd-platform/api
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ Add an API package to your monorepo in `.projenrc.ts`:
16
+
17
+ ```typescript
18
+ import { FndMonorepoProject } from '@fnd-platform/core';
19
+ import { FndApiProject } from '@fnd-platform/api';
20
+
21
+ const monorepo = new FndMonorepoProject({
22
+ name: 'my-app',
23
+ defaultReleaseBranch: 'main',
24
+ });
25
+
26
+ const api = new FndApiProject({
27
+ parent: monorepo,
28
+ name: 'api',
29
+ outdir: 'packages/api',
30
+ dynamodb: true,
31
+ cognito: true,
32
+ });
33
+
34
+ monorepo.synth();
35
+ ```
36
+
37
+ ## Configuration Options
38
+
39
+ | Option | Type | Default | Description |
40
+ | ---------- | -------------------- | ----------------- | ------------------------------ |
41
+ | `parent` | `FndMonorepoProject` | **required** | Parent monorepo |
42
+ | `name` | `string` | **required** | Package name |
43
+ | `outdir` | `string` | `packages/{name}` | Output directory |
44
+ | `dynamodb` | `boolean` | `true` | Include DynamoDB utilities |
45
+ | `cognito` | `boolean` | `true` | Include Cognito authentication |
46
+ | `cors` | `boolean` | `true` | Enable CORS support |
47
+
48
+ ## Features
49
+
50
+ - **Lambda Handlers** - Type-safe handler patterns with middleware support
51
+ - **Response Helpers** - Consistent JSON responses with proper status codes
52
+ - **Error Classes** - Structured error handling with automatic response mapping
53
+ - **Middleware Composition** - Composable middleware for auth, validation, CORS, logging
54
+ - **Request Utilities** - Parse body, path params, query params, and user context
55
+
56
+ ## Examples
57
+
58
+ ### Basic Handler
59
+
60
+ ```typescript
61
+ import { APIGatewayProxyHandler } from 'aws-lambda';
62
+ import { success, notFound } from '@fnd-platform/api';
63
+
64
+ export const handler: APIGatewayProxyHandler = async (event) => {
65
+ const data = { message: 'Hello, World!' };
66
+ return success(data);
67
+ };
68
+ ```
69
+
70
+ ### Handler with Middleware
71
+
72
+ ```typescript
73
+ import { compose, withAuth, withErrorHandler, withValidation } from '@fnd-platform/api';
74
+ import { z } from 'zod';
75
+
76
+ const schema = z.object({
77
+ title: z.string().min(1).max(200),
78
+ content: z.string(),
79
+ });
80
+
81
+ export const handler = compose(
82
+ withErrorHandler(),
83
+ withAuth(),
84
+ withValidation(schema)
85
+ )(async (event) => {
86
+ const body = JSON.parse(event.body || '{}');
87
+ // body is validated against schema
88
+ return success({ id: '123', ...body }, 201);
89
+ });
90
+ ```
91
+
92
+ ### Using Request Utilities
93
+
94
+ ```typescript
95
+ import { requirePathParam, getQueryParam, parseBody, getUserId } from '@fnd-platform/api';
96
+
97
+ export const handler = withAuth(async (event) => {
98
+ const id = requirePathParam(event, 'id');
99
+ const page = getQueryParam(event, 'page', '1');
100
+ const body = parseBody(event);
101
+ const userId = getUserId(event);
102
+
103
+ // ... handler logic
104
+ });
105
+ ```
106
+
107
+ ## API Reference
108
+
109
+ See the [full API documentation](../../docs/api/modules/api_src.html) for detailed type definitions and examples.
110
+
111
+ ### Project Class
112
+
113
+ - `FndApiProject` - Projen project class for API packages
114
+
115
+ ### Response Helpers
116
+
117
+ ```typescript
118
+ import {
119
+ success, // 200 OK (or custom status)
120
+ created, // 201 Created
121
+ error, // 500 Internal Server Error (or custom)
122
+ notFound, // 404 Not Found
123
+ unauthorized, // 401 Unauthorized
124
+ forbidden, // 403 Forbidden
125
+ badRequest, // 400 Bad Request
126
+ } from '@fnd-platform/api';
127
+ ```
128
+
129
+ ### Error Classes
130
+
131
+ ```typescript
132
+ import {
133
+ ApiError, // Base error class
134
+ NotFoundError, // 404 errors
135
+ ValidationError, // 400/422 validation errors
136
+ UnauthorizedError, // 401 errors
137
+ ForbiddenError, // 403 errors
138
+ ConflictError, // 409 conflict errors
139
+ } from '@fnd-platform/api';
140
+ ```
141
+
142
+ ### Middleware
143
+
144
+ ```typescript
145
+ import {
146
+ compose, // Compose multiple middleware
147
+ withErrorHandler, // Catch errors and return proper responses
148
+ withAuth, // Validate JWT tokens from Cognito
149
+ withValidation, // Validate request body with Zod
150
+ withCors, // Add CORS headers
151
+ withLogging, // Log requests and responses
152
+ } from '@fnd-platform/api';
153
+ ```
154
+
155
+ ### Request Utilities
156
+
157
+ ```typescript
158
+ import {
159
+ parseBody, // Parse JSON body
160
+ requirePathParam, // Get required path parameter
161
+ getQueryParam, // Get query parameter with default
162
+ getUserId, // Get authenticated user ID
163
+ } from '@fnd-platform/api';
164
+ ```
165
+
166
+ ### Types
167
+
168
+ ```typescript
169
+ import type {
170
+ FndApiProjectOptions,
171
+ Handler,
172
+ AuthenticatedEvent,
173
+ AuthenticatedHandler,
174
+ CognitoClaims,
175
+ PaginatedRequest,
176
+ PaginatedResponse,
177
+ MiddlewareHandler,
178
+ Middleware,
179
+ ValidatedEvent,
180
+ SuccessResponse,
181
+ ErrorResponse,
182
+ ErrorHandlerOptions,
183
+ AuthOptions,
184
+ CorsOptions,
185
+ LoggingOptions,
186
+ } from '@fnd-platform/api';
187
+ ```
188
+
189
+ ## Requirements
190
+
191
+ - Node.js 20+
192
+ - pnpm 8+
193
+ - @fnd-platform/core
194
+
195
+ ## Related
196
+
197
+ - [@fnd-platform/core](../core/README.md) - Core Projen project classes
198
+ - [@fnd-platform/dynamodb](../dynamodb/README.md) - DynamoDB utilities
199
+ - [@fnd-platform/cognito-auth](../cognito-auth/README.md) - Cognito authentication
200
+ - [@fnd-platform/constructs](../constructs/README.md) - CDK constructs for deployment
201
+
202
+ ## License
203
+
204
+ MIT
@@ -0,0 +1,99 @@
1
+ import { typescript } from 'projen';
2
+ import type { FndMonorepoProject } from '@fnd-platform/core';
3
+ import type { FndApiProjectOptions } from './options';
4
+ /**
5
+ * Creates a Lambda API package within an fnd-platform monorepo.
6
+ *
7
+ * This class generates a complete API package scaffold with:
8
+ * - Lambda handler templates
9
+ * - Response helper utilities
10
+ * - Error handling classes
11
+ * - TypeScript types for API interactions
12
+ *
13
+ * Files are generated using Projen's SampleFile, allowing users to modify
14
+ * them after generation while still maintaining the project structure.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { FndMonorepoProject } from '@fnd-platform/core';
19
+ * import { FndApiProject } from '@fnd-platform/api';
20
+ *
21
+ * const monorepo = new FndMonorepoProject({
22
+ * name: 'my-app',
23
+ * defaultReleaseBranch: 'main',
24
+ * });
25
+ *
26
+ * const api = new FndApiProject({
27
+ * parent: monorepo,
28
+ * name: 'api',
29
+ * });
30
+ *
31
+ * monorepo.synth();
32
+ * ```
33
+ */
34
+ export declare class FndApiProject extends typescript.TypeScriptProject {
35
+ /**
36
+ * Reference to the parent monorepo project.
37
+ */
38
+ readonly parentProject: FndMonorepoProject;
39
+ /**
40
+ * Whether DynamoDB integration is enabled.
41
+ */
42
+ readonly dynamodbEnabled: boolean;
43
+ /**
44
+ * Whether Cognito auth integration is enabled.
45
+ */
46
+ readonly cognitoEnabled: boolean;
47
+ /**
48
+ * Whether CORS is enabled.
49
+ */
50
+ readonly corsEnabled: boolean;
51
+ /**
52
+ * Creates a new FndApiProject.
53
+ *
54
+ * @param options - Configuration options for the API project
55
+ * @throws {Error} If required options are missing
56
+ */
57
+ constructor(options: FndApiProjectOptions);
58
+ /**
59
+ * Adds API-specific dependencies to the project.
60
+ */
61
+ private addApiDependencies;
62
+ /**
63
+ * Generates the handlers directory with Lambda handler templates.
64
+ */
65
+ private generateHandlersDirectory;
66
+ /**
67
+ * Generates the lib directory with utility files.
68
+ */
69
+ private generateLibDirectory;
70
+ /**
71
+ * Generates the types directory with TypeScript type definitions.
72
+ */
73
+ private generateTypesDirectory;
74
+ /**
75
+ * Returns the health handler template content.
76
+ */
77
+ private getHealthHandlerTemplate;
78
+ /**
79
+ * Returns the response helpers template content.
80
+ */
81
+ private getResponseTemplate;
82
+ /**
83
+ * Returns the error classes template content.
84
+ */
85
+ private getErrorsTemplate;
86
+ /**
87
+ * Returns the middleware template content.
88
+ */
89
+ private getMiddlewareTemplate;
90
+ /**
91
+ * Returns the API types template content.
92
+ */
93
+ private getApiTypesTemplate;
94
+ /**
95
+ * Returns the entity types template content.
96
+ */
97
+ private getEntityTypesTemplate;
98
+ }
99
+ //# sourceMappingURL=api-project.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api-project.d.ts","sourceRoot":"","sources":["../src/api-project.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAA0B,MAAM,QAAQ,CAAC;AAC5D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAC7D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAItD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,qBAAa,aAAc,SAAQ,UAAU,CAAC,iBAAiB;IAC7D;;OAEG;IACH,SAAgB,aAAa,EAAE,kBAAkB,CAAC;IAElD;;OAEG;IACH,SAAgB,eAAe,EAAE,OAAO,CAAC;IAEzC;;OAEG;IACH,SAAgB,cAAc,EAAE,OAAO,CAAC;IAExC;;OAEG;IACH,SAAgB,WAAW,EAAE,OAAO,CAAC;IAErC;;;;;OAKG;gBACS,OAAO,EAAE,oBAAoB;IA0DzC;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAU1B;;OAEG;IACH,OAAO,CAAC,yBAAyB;IAOjC;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAiB5B;;OAEG;IACH,OAAO,CAAC,sBAAsB;IAY9B;;OAEG;IACH,OAAO,CAAC,wBAAwB;IAoBhC;;OAEG;IACH,OAAO,CAAC,mBAAmB;IA0I3B;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAkGzB;;OAEG;IACH,OAAO,CAAC,qBAAqB;IA8E7B;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAsE3B;;OAEG;IACH,OAAO,CAAC,sBAAsB;CA2F/B"}