amplify-astro-adapter 0.1.2 → 0.1.3

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 (2) hide show
  1. package/README.md +70 -301
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,36 +1,26 @@
1
1
  # amplify-astro-adapter
2
2
 
3
- AWS Amplify adapter for Astro. Extends `@astrojs/node` with Amplify Hosting deployment configuration.
3
+ AWS Amplify adapter for Astro with built-in cookie-based sessions.
4
4
 
5
- ## Version 0.1.0
6
-
7
- This adapter is based on `@astrojs/node` architecture, adding AWS Amplify-specific deployment features and cookie-based session support.
8
-
9
- ### Features
5
+ ## Features
10
6
 
11
7
  - Astro 4.x and 5.x support
8
+ - **Cookie-based sessions** - Zero config, works with serverless
9
+ - **Auto-generates `amplify.yml`** - Detects your package manager
12
10
  - **envGetSecret** support for type-safe environment variables
13
- - **Cookie-based sessions** (built-in, zero setup)
14
- - **Auto-generates `amplify.yml`** - detects your package manager and creates the build spec for you
15
11
  - Configurable Node.js runtime (nodejs20.x, nodejs22.x)
16
- - Generates proper `deploy-manifest.json` for Amplify Hosting
17
12
 
18
13
  ## Installation
19
14
 
20
15
  ```bash
21
- # Using npm
22
16
  npm install amplify-astro-adapter
23
-
24
- # Using yarn
25
- yarn add amplify-astro-adapter
26
-
27
- # Using pnpm
17
+ # or
28
18
  pnpm add amplify-astro-adapter
19
+ # or
20
+ yarn add amplify-astro-adapter
29
21
  ```
30
22
 
31
- ## Configuration
32
-
33
- Add the adapter to your Astro config:
23
+ ## Quick Start
34
24
 
35
25
  ```js
36
26
  // astro.config.mjs
@@ -38,366 +28,149 @@ import { defineConfig } from 'astro/config';
38
28
  import amplify from 'amplify-astro-adapter';
39
29
 
40
30
  export default defineConfig({
41
- output: 'server', // or 'hybrid'
42
- adapter: amplify({
43
- runtime: 'nodejs20.x' // or 'nodejs22.x'
44
- })
31
+ output: 'server',
32
+ adapter: amplify()
45
33
  });
46
34
  ```
47
35
 
48
- ### Configuration Options
36
+ That's it! Sessions and middleware are configured automatically.
49
37
 
50
- | Option | Type | Default | Description |
51
- |--------|------|---------|-------------|
52
- | `runtime` | `'nodejs20.x' \| 'nodejs22.x'` | `'nodejs20.x'` | Node.js runtime for AWS Lambda |
53
- | `experimentalDisableStreaming` | `boolean` | `false` | Disable HTML streaming (useful for Lambda response constraints) |
54
- | `experimentalStaticHeaders` | `boolean` | `false` | Enable static header file processing (`_headers.json`) |
55
- | `experimentalErrorPageHost` | `string \| URL` | - | Custom host for fetching prerendered error pages |
38
+ ## Configuration Options
56
39
 
57
40
  ```js
58
- // Advanced configuration example
59
41
  export default defineConfig({
60
42
  output: 'server',
61
43
  adapter: amplify({
62
- runtime: 'nodejs22.x',
63
- experimentalDisableStreaming: true,
64
- experimentalStaticHeaders: false,
65
- experimentalErrorPageHost: 'https://errors.example.com',
44
+ runtime: 'nodejs20.x' // or 'nodejs22.x'
66
45
  })
67
46
  });
68
47
  ```
69
48
 
70
- ### Server Modes
71
-
72
- The adapter supports two server modes (inherited from `@astrojs/node`):
73
-
74
- - **`standalone`** (default) - Auto-starts an HTTP server, serves static files, handles all requests. Used by AWS Amplify Lambda.
75
- - **`middleware`** - Exports a handler for integration with Express, Fastify, or other Node.js frameworks.
49
+ | Option | Type | Default | Description |
50
+ |--------|------|---------|-------------|
51
+ | `runtime` | `'nodejs20.x' \| 'nodejs22.x'` | `'nodejs20.x'` | Node.js runtime for AWS Lambda |
76
52
 
77
53
  ## Sessions
78
54
 
79
- This adapter includes built-in cookie-based session support as the **default Astro session driver**. No external session storage (Redis, DynamoDB, etc.) is needed - sessions are stored in HTTP-only cookies and work immediately with AWS Lambda.
80
-
81
- ### Key Features
82
-
83
- - **Zero configuration** - Cookie sessions are enabled by default
84
- - **Encrypted with iron-session** - Session data is securely encrypted
85
- - **Automatic chunking** - Large sessions are automatically split across multiple cookies
86
- - **No external dependencies** - No Redis, DynamoDB, or other storage required
55
+ Sessions are **enabled by default** with zero configuration. Data is stored in encrypted HTTP-only cookies.
87
56
 
88
57
  ### Environment Variable
89
58
 
90
- Set a session secret for encryption (32+ characters recommended):
59
+ Set a session secret for encryption (32+ characters):
91
60
 
92
61
  ```bash
93
- SESSION_SECRET=your-32-character-or-longer-secret
94
- ```
95
-
96
- > **Note:** If `SESSION_SECRET` is not set, a default secret is used in development. Always set a secure secret in production.
97
-
98
- ### Auto-Configuration
99
-
100
- Sessions are automatically enabled when you use this adapter (no configuration needed):
101
-
102
- ```js
103
- // astro.config.mjs
104
- import { defineConfig } from 'astro/config';
105
- import amplify from 'amplify-astro-adapter';
106
-
107
- export default defineConfig({
108
- output: 'server',
109
- adapter: amplify(),
110
- session: {
111
- // Cookie session is the default - no driver config needed!
112
- // Or explicitly:
113
- // driver: 'amplify-astro-adapter/session',
114
- },
115
- });
62
+ AMPLIFY_SESSION_PASSWORD=your-32-character-or-longer-secret
116
63
  ```
117
64
 
118
- ### Custom Session Options
119
-
120
- If you want to customize the session behavior, you can manually configure it:
65
+ > In development, a secret is auto-generated. **Always set this in production.**
121
66
 
122
- ```js
123
- import { defineConfig } from 'astro/config';
124
- import amplify from 'amplify-astro-adapter';
125
- import { createSessionStorage } from 'amplify-astro-adapter/session';
126
-
127
- export default defineConfig({
128
- adapter: amplify({ runtime: 'nodejs20.x' }),
129
- session: createSessionStorage({
130
- prefix: 'myapp_session',
131
- cookie: {
132
- maxAge: 60 * 60 * 24 * 30, // 30 days
133
- sameSite: 'lax',
134
- httpOnly: true,
135
- secure: true,
136
- }
137
- })
138
- });
139
- ```
140
-
141
- ### Using Sessions in Pages
142
-
143
- Access sessions directly in `.astro` files:
67
+ ### Using Sessions
144
68
 
145
69
  ```astro
146
70
  ---
147
71
  // src/pages/dashboard.astro
148
- const session = await Astro.session;
149
- const user = await session?.get('user');
150
- await session?.set('lastVisit', new Date().toISOString());
72
+ const username = await Astro.session?.get('username');
151
73
  ---
152
74
 
153
- <html>
154
- <body>
155
- {user ? (
156
- <h1>Welcome back, {user.name}!</h1>
157
- ) : (
158
- <a href="/login">Please log in</a>
159
- )}
160
- </body>
161
- </html>
75
+ <h1>Welcome {username}!</h1>
162
76
  ```
163
77
 
164
- ### Using Sessions in API Routes
78
+ ### API Routes
165
79
 
166
80
  ```ts
167
- // src/pages/api/session.ts
81
+ // src/pages/api/login.ts
168
82
  import type { APIRoute } from 'astro';
169
83
 
170
- export const POST: APIRoute = async ({ session }) => {
171
- // Set session data
172
- await session?.set('userId', '123');
173
- await session?.set('cart', ['item1', 'item2']);
84
+ export const POST: APIRoute = async ({ session, request, redirect }) => {
85
+ const formData = await request.formData();
86
+ const username = formData.get('username') as string;
174
87
 
175
- return Response.json({ success: true });
176
- };
177
-
178
- export const GET: APIRoute = async ({ session }) => {
179
- // Get session data
180
- const userId = await session?.get('userId');
181
- const cart = await session?.get('cart') ?? [];
182
-
183
- return Response.json({ userId, cart });
184
- };
185
-
186
- export const DELETE: APIRoute = async ({ session }) => {
187
- // Destroy session
188
- await session?.delete('userId');
189
- // or destroy entire session:
190
- await session?.destroy();
88
+ session?.set('username', username);
191
89
 
192
- return Response.json({ success: true });
90
+ return redirect('/');
193
91
  };
194
92
  ```
195
93
 
196
- ### Direct Cookie Helpers (Bypass Astro Sessions)
197
-
198
- For simple cases where you don't need the full Astro session API, you can use the helper functions directly:
199
-
200
94
  ```ts
95
+ // src/pages/api/logout.ts
201
96
  import type { APIRoute } from 'astro';
202
- import { getSession, setSession, destroySession } from 'amplify-astro-adapter/session';
203
-
204
- export const POST: APIRoute = async ({ cookies }) => {
205
- await setSession(cookies, { userId: '123', cart: ['item1', 'item2'] });
206
- return Response.json({ success: true });
207
- };
208
97
 
209
- export const GET: APIRoute = async ({ cookies }) => {
210
- const session = await getSession(cookies);
211
- return Response.json({ session });
212
- };
213
-
214
- export const DELETE: APIRoute = async ({ cookies }) => {
215
- await destroySession(cookies);
216
- return Response.json({ success: true });
98
+ export const POST: APIRoute = async ({ session, redirect }) => {
99
+ await session?.destroy();
100
+ return redirect('/');
217
101
  };
218
102
  ```
219
103
 
220
- ### Middleware Integration
104
+ ### Custom Session Configuration
221
105
 
222
- To make sessions available automatically across your app, add this to your `src/env.d.ts`:
223
-
224
- ```typescript
225
- /// <reference types="astro/client" />
106
+ ```js
107
+ import { defineConfig } from 'astro/config';
108
+ import amplify from 'amplify-astro-adapter';
109
+ import { createCookieSessionDriver } from 'amplify-astro-adapter/session';
226
110
 
227
- declare namespace App {
228
- interface Locals {
229
- /**
230
- * Session data automatically available in middleware and pages
231
- */
232
- session?: import('amplify-astro-adapter/session').SessionData;
111
+ export default defineConfig({
112
+ output: 'server',
113
+ adapter: amplify(),
114
+ session: {
115
+ driver: createCookieSessionDriver({
116
+ password: process.env.AMPLIFY_SESSION_PASSWORD,
117
+ ttl: 86400, // 1 day in seconds
118
+ cookieOptions: {
119
+ httpOnly: true,
120
+ secure: true,
121
+ sameSite: 'lax',
122
+ path: '/',
123
+ }
124
+ })
233
125
  }
234
- }
235
- ```
236
-
237
- Then create `src/middleware.ts`:
238
-
239
- ```ts
240
- import { defineMiddleware } from 'astro:middleware';
241
- import { getSession } from 'amplify-astro-adapter/session';
242
-
243
- export const onRequest = defineMiddleware(async (context) => {
244
- // Load session into context.locals
245
- context.locals.session = await getSession(context.cookies);
246
-
247
- // Session is now automatically available in all pages
248
- return next();
249
126
  });
250
127
  ```
251
128
 
252
- Access the session in components:
253
-
254
- ```astro
255
- ---
256
- // Session is automatically available via Astro.locals
257
- const userId = Astro.locals.session?.userId;
258
- ---
259
- <h1>Welcome {userId}</h1>
260
- ```
261
-
262
129
  ### Session Limitations
263
130
 
264
- - **4KB size limit** per cookie (browsers support ~50+ cookies = ~200KB total)
265
- - **Client-stored** - data can be viewed by the user (use httpOnly for XSS protection)
266
- - **Cannot invalidate** before expiration (delete the cookie to invalidate)
267
-
268
- **What fits in 4KB:**
269
- - User IDs, auth tokens
270
- - Shopping cart contents
271
- - User preferences
272
- - Session metadata
273
-
274
- **What doesn't fit:**
275
- - Large file uploads
276
- - Extensive logging data
277
- - Large datasets
278
-
279
- ## envGetSecret Support
280
-
281
- Use type-safe environment variables with Astro's `astro:env/server`:
282
-
283
- ```ts
284
- import { getSecret } from 'astro:env/server';
285
-
286
- const apiKey = await getSecret('API_KEY');
287
- ```
131
+ - **4KB per cookie** - Large sessions auto-chunk across multiple cookies
132
+ - **Client-stored** - Encrypted, but don't store sensitive server-only data
288
133
 
289
134
  ## AWS Amplify Hosting
290
135
 
291
136
  ### amplify.yml Auto-Generation
292
137
 
293
- When you run `astro build` for the first time, this adapter automatically generates an `amplify.yml` file in your project root. It detects your package manager (npm, pnpm, yarn, or bun) and creates the appropriate build specification.
294
-
295
- Just commit the generated `amplify.yml` to your repository and deploy!
138
+ On first build, the adapter generates `amplify.yml` for your package manager. Just commit it and deploy!
296
139
 
297
140
  ### Build Settings
298
141
 
299
- Set the custom image environment variable:
142
+ Set the custom image environment variable in Amplify Console:
300
143
 
301
144
  ```
302
145
  _CUSTOM_IMAGE=amplify:al2023
303
146
  ```
304
147
 
305
- ### Build Specifications (Reference)
306
-
307
- The adapter generates these for you, but for reference:
308
-
309
- **npm:**
310
- ```yaml
311
- version: 1
312
- frontend:
313
- phases:
314
- preBuild:
315
- commands:
316
- - npm ci
317
- build:
318
- commands:
319
- - npm run build
320
- - mv node_modules ./.amplify-hosting/compute/default
321
- artifacts:
322
- baseDirectory: .amplify-hosting
323
- files:
324
- - '**/*'
325
- cache:
326
- paths:
327
- - node_modules/**/*
328
- ```
148
+ ## envGetSecret Support
329
149
 
330
- **pnpm:**
331
- ```yaml
332
- version: 1
333
- frontend:
334
- phases:
335
- preBuild:
336
- commands:
337
- - npm i -g pnpm
338
- - pnpm config set store-dir .pnpm-store
339
- - pnpm i
340
- build:
341
- commands:
342
- - pnpm run build
343
- artifacts:
344
- baseDirectory: .amplify-hosting
345
- files:
346
- - '**/*'
347
- cache:
348
- paths:
349
- - .pnpm-store/**/*
350
- ```
150
+ ```ts
151
+ import { getSecret } from 'astro:env/server';
351
152
 
352
- **yarn:**
353
- ```yaml
354
- version: 1
355
- frontend:
356
- phases:
357
- preBuild:
358
- commands:
359
- - yarn install
360
- build:
361
- commands:
362
- - yarn run build
363
- - mv node_modules ./.amplify-hosting/compute/default
364
- artifacts:
365
- baseDirectory: .amplify-hosting
366
- files:
367
- - '**/*'
368
- cache:
369
- paths:
370
- - node_modules/**/*
153
+ const apiKey = await getSecret('API_KEY');
371
154
  ```
372
155
 
373
156
  ## Migration from astro-aws-amplify
374
157
 
375
- If you're migrating from the old `astro-aws-amplify` package:
376
-
377
- 1. Update your dependencies:
378
- ```bash
379
- npm uninstall astro-aws-amplify
380
- npm install amplify-astro-adapter
381
- ```
382
-
383
- 2. Update your import:
384
- ```diff
385
- - import awsAmplify from 'astro-aws-amplify';
386
- + import amplify from 'amplify-astro-adapter';
387
- ```
158
+ ```bash
159
+ npm uninstall astro-aws-amplify
160
+ npm install amplify-astro-adapter
161
+ ```
388
162
 
389
- 3. No other changes needed - configuration is the same!
163
+ ```diff
164
+ - import awsAmplify from 'astro-aws-amplify';
165
+ + import amplify from 'amplify-astro-adapter';
166
+ ```
390
167
 
391
168
  ## Package Exports
392
169
 
393
- The adapter provides multiple entry points:
394
-
395
170
  | Export | Description |
396
171
  |--------|-------------|
397
172
  | `amplify-astro-adapter` | Main adapter function |
398
- | `amplify-astro-adapter/server` | Server runtime (used internally by Astro) |
399
- | `amplify-astro-adapter/session` | Session driver and helpers |
400
- | `amplify-astro-adapter/middleware` | Cookie context middleware (used internally) |
173
+ | `amplify-astro-adapter/session` | Session driver for custom configuration |
401
174
 
402
175
  ## License
403
176
 
@@ -406,7 +179,3 @@ MIT
406
179
  ## Author
407
180
 
408
181
  Zach Handley <zachhandley@gmail.com>
409
-
410
- ## Acknowledgements
411
-
412
- Extends the official [`@astrojs/node`](https://github.com/withastro/astro/tree/main/packages/integrations/node) adapter with AWS Amplify Hosting deployment configuration.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "amplify-astro-adapter",
3
- "version": "0.1.02",
3
+ "version": "0.1.03",
4
4
  "description": "Deploy Astro to AWS Amplify (SSR) - extending @astrojs/node",
5
5
  "type": "module",
6
6
  "license": "MIT",