@gasket/plugin-fastify 7.4.4 → 7.4.6
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/EXAMPLES.md +144 -0
- package/package.json +8 -7
package/EXAMPLES.md
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# @gasket/plugin-fastify Examples
|
|
2
|
+
|
|
3
|
+
This document provides working examples for all exported interfaces from `@gasket/plugin-fastify`.
|
|
4
|
+
|
|
5
|
+
## Plugin Installation and Configuration
|
|
6
|
+
|
|
7
|
+
### Basic Plugin Setup
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
// gasket.js
|
|
11
|
+
import { makeGasket } from '@gasket/core';
|
|
12
|
+
import pluginFastify from '@gasket/plugin-fastify';
|
|
13
|
+
|
|
14
|
+
export default makeGasket({
|
|
15
|
+
plugins: [
|
|
16
|
+
pluginFastify
|
|
17
|
+
]
|
|
18
|
+
});
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### Plugin Configuration
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
// gasket.js
|
|
25
|
+
export default makeGasket({
|
|
26
|
+
plugins: [
|
|
27
|
+
pluginFastify
|
|
28
|
+
],
|
|
29
|
+
fastify: {
|
|
30
|
+
compression: true,
|
|
31
|
+
trustProxy: true,
|
|
32
|
+
disableRequestLogging: false
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Lifecycle Hooks
|
|
38
|
+
|
|
39
|
+
### `fastify` Lifecycle Hook
|
|
40
|
+
|
|
41
|
+
Access and modify the Fastify instance:
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
// Example plugin or lifecycle file
|
|
45
|
+
export default {
|
|
46
|
+
name: 'my-fastify-plugin',
|
|
47
|
+
hooks: {
|
|
48
|
+
fastify: async function (gasket, app) {
|
|
49
|
+
// Register plugins
|
|
50
|
+
app.register(require('@fastify/cors'), {
|
|
51
|
+
origin: true
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Add hooks
|
|
55
|
+
app.addHook('preHandler', async (request, reply) => {
|
|
56
|
+
console.log('Processing request:', request.url);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Add custom decorators
|
|
60
|
+
app.decorate('customMethod', function() {
|
|
61
|
+
return 'custom value';
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Add routes
|
|
65
|
+
app.get('/hello', async (request, reply) => {
|
|
66
|
+
return { hello: 'world' };
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### `errorMiddleware` Lifecycle Hook
|
|
74
|
+
|
|
75
|
+
Add Express-style error handling middleware:
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
// Example plugin
|
|
79
|
+
export default {
|
|
80
|
+
name: 'my-error-handler',
|
|
81
|
+
hooks: {
|
|
82
|
+
errorMiddleware: function (gasket) {
|
|
83
|
+
return (error, req, res, next) => {
|
|
84
|
+
gasket.logger.error('Request error:', error);
|
|
85
|
+
|
|
86
|
+
// Handle different error types
|
|
87
|
+
if (error.statusCode) {
|
|
88
|
+
res.status(error.statusCode);
|
|
89
|
+
} else {
|
|
90
|
+
res.status(500);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
res.send({
|
|
94
|
+
error: 'Internal Server Error',
|
|
95
|
+
message: error.message
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Configuration Examples
|
|
104
|
+
|
|
105
|
+
### Route Plugin Example
|
|
106
|
+
|
|
107
|
+
```js
|
|
108
|
+
// plugins/routes-plugin.js
|
|
109
|
+
export default {
|
|
110
|
+
name: 'routes-plugin',
|
|
111
|
+
hooks: {
|
|
112
|
+
fastify: async function (gasket, app) {
|
|
113
|
+
// API routes
|
|
114
|
+
app.get('/api/health', async (request, reply) => {
|
|
115
|
+
return {
|
|
116
|
+
status: 'healthy',
|
|
117
|
+
timestamp: new Date().toISOString(),
|
|
118
|
+
version: process.env.npm_package_version
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
app.get('/api/config', async (request, reply) => {
|
|
123
|
+
return {
|
|
124
|
+
environment: gasket.config.env,
|
|
125
|
+
features: gasket.config.features || {}
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// Protected route example
|
|
130
|
+
app.get('/api/protected', {
|
|
131
|
+
preHandler: async (request, reply) => {
|
|
132
|
+
const auth = request.headers.authorization;
|
|
133
|
+
if (!auth || !auth.startsWith('Bearer ')) {
|
|
134
|
+
reply.code(401).send({ error: 'Unauthorized' });
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}, async (request, reply) => {
|
|
139
|
+
return { message: 'Protected data' };
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
```
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gasket/plugin-fastify",
|
|
3
|
-
"version": "7.4.
|
|
3
|
+
"version": "7.4.6",
|
|
4
4
|
"description": "Adds fastify support to your application",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
7
7
|
"files": [
|
|
8
8
|
"generator",
|
|
9
|
-
"lib"
|
|
9
|
+
"lib",
|
|
10
|
+
"EXAMPLES.md"
|
|
10
11
|
],
|
|
11
12
|
"repository": "godaddy/gasket.git",
|
|
12
13
|
"publishConfig": {
|
|
@@ -29,11 +30,11 @@
|
|
|
29
30
|
"@types/connect": "^3.4.38",
|
|
30
31
|
"fastify": "^4.29.1",
|
|
31
32
|
"jest": "^29.7.0",
|
|
32
|
-
"@gasket/core": "^7.6.
|
|
33
|
-
"@gasket/plugin-
|
|
34
|
-
"@gasket/plugin-
|
|
35
|
-
"@gasket/plugin-metadata": "^7.5.
|
|
36
|
-
"create-gasket-app": "^7.4.
|
|
33
|
+
"@gasket/core": "^7.6.3",
|
|
34
|
+
"@gasket/plugin-https": "^7.3.12",
|
|
35
|
+
"@gasket/plugin-logger": "^7.3.8",
|
|
36
|
+
"@gasket/plugin-metadata": "^7.5.4",
|
|
37
|
+
"create-gasket-app": "^7.4.13"
|
|
37
38
|
},
|
|
38
39
|
"peerDependencies": {
|
|
39
40
|
"fastify": "^4.29.1"
|