@a_jackie_z/fastify 1.0.0 → 1.0.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 (2) hide show
  1. package/README.md +71 -34
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -13,7 +13,7 @@ npm install @a_jackie_z/fastify
13
13
  ### Basic Setup
14
14
 
15
15
  ```typescript
16
- import { createFastify } from '@a_jackie_z/fastify'
16
+ import { createFastify, runFastify } from '@a_jackie_z/fastify'
17
17
  import { Type } from '@sinclair/typebox'
18
18
 
19
19
  const app = await createFastify()
@@ -33,26 +33,31 @@ app.route({
33
33
  },
34
34
  })
35
35
 
36
- await app.listen({ port: 3000 })
36
+ await runFastify(app, '0.0.0.0', 3000)
37
37
  ```
38
38
 
39
39
  ## Features
40
40
 
41
41
  - **JWT Authentication** - Built-in JWT support with global and route-level configuration
42
- - **Rate Limiting** - Flexible rate limiting at global and route levels
42
+ - **Rate Limiting** - Global rate limiting for API protection
43
43
  - **Swagger Documentation** - Auto-generated API documentation with TypeBox schemas
44
44
  - **TypeBox Integration** - Type-safe request/response validation
45
- - **Logger Integration** - Uses `@a_jackie_z/logger` for consistent logging
45
+ - **Custom Logger Support** - Integrate any Fastify-compatible logger
46
+ - **Health Check Plugin** - Ready-to-use health check endpoint
47
+ - **Plugin Helper** - Utility for creating reusable Fastify plugins
46
48
 
47
49
  ## Examples
48
50
 
49
51
  ### 1. Complete Setup with All Features
50
52
 
51
53
  ```typescript
52
- import { createFastify } from '@a_jackie_z/fastify'
54
+ import { createFastify, runFastify } from '@a_jackie_z/fastify'
53
55
  import { Type } from '@sinclair/typebox'
54
56
 
55
57
  const app = await createFastify({
58
+ // Optional: Integrate your logger
59
+ // logger: yourFastifyLogger,
60
+
56
61
  // Rate limiting
57
62
  rateLimit: {
58
63
  global: {
@@ -77,7 +82,7 @@ const app = await createFastify({
77
82
  },
78
83
  })
79
84
 
80
- await app.listen({ port: 3000 })
85
+ await runFastify(app, '0.0.0.0', 3000)
81
86
  ```
82
87
 
83
88
  ### 2. Authentication Flow
@@ -259,42 +264,52 @@ app.route({
259
264
  })
260
265
  ```
261
266
 
262
- ### 6. Rate Limiting
263
-
264
- #### Route-Specific Rate Limit
267
+ ### 6. Health Check Plugin
265
268
 
266
269
  ```typescript
267
- app.route({
268
- method: 'GET',
269
- url: '/limited',
270
- config: {
271
- rateLimit: {
272
- max: 5,
273
- timeWindow: '1 minute',
274
- },
275
- },
276
- handler: async () => {
277
- return { message: 'Strict rate limiting applied' }
278
- },
279
- })
270
+ import { createFastify, runFastify, healthPlugin } from '@a_jackie_z/fastify'
271
+
272
+ const app = await createFastify()
273
+
274
+ // Register health check plugin
275
+ await app.register(healthPlugin)
276
+
277
+ // Health endpoint available at: GET /v1/health
278
+ // Response: { status: 200, message: 'ok' }
279
+
280
+ await runFastify(app, '0.0.0.0', 3000)
280
281
  ```
281
282
 
282
- #### Disable Rate Limit for Specific Route
283
+ ### 7. Creating Custom Plugins
283
284
 
284
285
  ```typescript
285
- app.route({
286
- method: 'GET',
287
- url: '/unlimited',
288
- config: {
289
- rateLimit: false,
290
- },
291
- handler: async () => {
292
- return { message: 'No rate limiting' }
293
- },
286
+ import { createFastify, runFastify, createFastifyPlugin } from '@a_jackie_z/fastify'
287
+ import { Type } from '@sinclair/typebox'
288
+
289
+ // Create a reusable plugin
290
+ const myPlugin = createFastifyPlugin((app) => {
291
+ app.get('/plugin-route', {
292
+ schema: {
293
+ response: {
294
+ 200: Type.Object({
295
+ message: Type.String(),
296
+ }),
297
+ },
298
+ },
299
+ handler: async () => {
300
+ return { message: 'From plugin' }
301
+ },
302
+ })
294
303
  })
304
+
305
+ // Use the plugin
306
+ const app = await createFastify()
307
+ await app.register(myPlugin)
308
+
309
+ await runFastify(app, '0.0.0.0', 3000)
295
310
  ```
296
311
 
297
- ### 7. TypeBox Schema Validation
312
+ ### 8. TypeBox Schema Validation
298
313
 
299
314
  ```typescript
300
315
  app.route({
@@ -334,6 +349,7 @@ app.route({
334
349
 
335
350
  ```typescript
336
351
  interface CreateFastifyOptions {
352
+ logger?: FastifyServerOptions['loggerInstance'],
337
353
  rateLimit?: {
338
354
  global?: RateLimitPluginOptions
339
355
  }
@@ -347,7 +363,7 @@ interface CreateFastifyOptions {
347
363
  title: string
348
364
  version: string
349
365
  description: string
350
- routePrefix?: string // Default: '/documentation'
366
+ routePrefix?: string // Default: '/docs/'
351
367
  }
352
368
  }
353
369
  ```
@@ -371,6 +387,27 @@ type JWTAuthorizationHandler = (
371
387
  ) => Promise<boolean> | boolean
372
388
  ```
373
389
 
390
+ ## API Reference
391
+
392
+ ### `createFastify(options?: CreateFastifyOptions): Promise<FastifyServer>`
393
+
394
+ Creates and configures a Fastify server instance with TypeBox support and optional plugins.
395
+
396
+ ### `runFastify(fastify: FastifyServer, host: string, port: number): Promise<void>`
397
+
398
+ Starts the Fastify server. Handles errors and exits the process if the server fails to start.
399
+
400
+ **Parameters:**
401
+ - `fastify` - The Fastify server instance
402
+ - `host` - The host to bind to (e.g., '0.0.0.0' or 'localhost')
403
+ - `port` - The port number to listen on
404
+
405
+ **Example:**
406
+ ```typescript
407
+ const app = await createFastify()
408
+ await runFastify(app, '0.0.0.0', 3000)
409
+ ```
410
+
374
411
  ## Usage Patterns
375
412
 
376
413
  ### 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.0.0",
3
+ "version": "1.0.1",
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>",