@a_jackie_z/fastify 1.1.3 → 1.1.4

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 +64 -0
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -398,6 +398,7 @@ Creates and configures a Fastify server instance with Zod support and optional p
398
398
  Starts the Fastify server. Handles errors and exits the process if the server fails to start.
399
399
 
400
400
  **Parameters:**
401
+
401
402
  - `fastify` - The Fastify server instance
402
403
  - `host` - The host to bind to (e.g., '0.0.0.0' or 'localhost')
403
404
  - `port` - The port number to listen on
@@ -408,6 +409,69 @@ const app = await createFastify()
408
409
  await runFastify(app, '0.0.0.0', 3000)
409
410
  ```
410
411
 
412
+ ## Fastify JWT Service
413
+
414
+ A typed helper for issuing and verifying access/refresh tokens alongside the Fastify JWT plugin.
415
+
416
+ **Options**
417
+ - `accessTokenExpiry` (string) — required access token lifetime (e.g., `15m`)
418
+ - `refreshTokenExpiry` (string) — required refresh token lifetime (e.g., `7d`)
419
+ - `renewalThreshold` (string) — renew refresh tokens when `exp` is within this window
420
+ - `accessTokenSchema` (ZodSchema) — optional, extends `baseAccessTokenPayloadSchema`
421
+ - `refreshTokenSchema` (ZodSchema) — optional, extends `baseRefreshTokenPayloadSchema`
422
+
423
+ **Basic usage**
424
+ ```typescript
425
+ import {
426
+ createFastify,
427
+ FastifyJwtService,
428
+ baseAccessTokenPayloadSchema,
429
+ baseRefreshTokenPayloadSchema,
430
+ } from '@a_jackie_z/fastify'
431
+ import { z } from 'zod'
432
+
433
+ const app = await createFastify({
434
+ jwt: { secret: 'your-secret', global: false },
435
+ })
436
+
437
+ const jwtService = new FastifyJwtService(app, {
438
+ accessTokenExpiry: '15m',
439
+ refreshTokenExpiry: '7d',
440
+ renewalThreshold: '1d',
441
+ })
442
+
443
+ const { accessToken, refreshToken } = jwtService.generateTokenPair('identity-123')
444
+
445
+ const accessPayload = jwtService.verifyAccessToken(accessToken)
446
+ const refreshPayload = jwtService.verifyRefreshToken(refreshToken)
447
+
448
+ if (jwtService.shouldRenewRefreshToken(refreshPayload.exp)) {
449
+ const renewedRefreshToken = jwtService.renewRefreshToken(refreshPayload.identityId)
450
+ }
451
+ ```
452
+
453
+ **Custom payloads**
454
+ ```typescript
455
+ const accessTokenSchema = baseAccessTokenPayloadSchema.extend({
456
+ roles: z.array(z.string()),
457
+ })
458
+
459
+ const refreshTokenSchema = baseRefreshTokenPayloadSchema.extend({
460
+ deviceId: z.string().optional(),
461
+ })
462
+
463
+ const jwtService = new FastifyJwtService<
464
+ z.infer<typeof accessTokenSchema>,
465
+ z.infer<typeof refreshTokenSchema>
466
+ >(app, {
467
+ accessTokenExpiry: '15m',
468
+ refreshTokenExpiry: '7d',
469
+ renewalThreshold: '1d',
470
+ accessTokenSchema,
471
+ refreshTokenSchema,
472
+ })
473
+ ```
474
+
411
475
  ## Usage Patterns
412
476
 
413
477
  ### Pattern 1: Global JWT with Selective Public Routes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@a_jackie_z/fastify",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "A collection of Fastify plugins and utilities for building robust web applications.",
5
5
  "license": "MIT",
6
6
  "author": "Sang Lu <connect.with.sang@gmail.com>",