Skip to content

Commit 943d447

Browse files
authored
feat: snippet for typescript (#17)
1 parent 5c669c8 commit 943d447

File tree

1 file changed

+250
-1
lines changed

1 file changed

+250
-1
lines changed

snippets/snippets-ts.json

Lines changed: 250 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,250 @@
1-
{}
1+
{
2+
"fastify hello server": {
3+
"scope": "typescript",
4+
"prefix": "ffhello",
5+
"body": [
6+
"import fastify from 'fastify'",
7+
"const app = fastify({ logger: true })",
8+
"app.get('/', async (request, reply) => {",
9+
" return { hello: 'world' }",
10+
"})",
11+
"app.post('/', async (request, reply) => {",
12+
" return request.body",
13+
"})",
14+
"app.listen({ port: 8080 })"
15+
],
16+
"description": "Create an hello world server"
17+
},
18+
"fastify register": {
19+
"scope": "typescript",
20+
"prefix": ["ffregister", "ffplugin"],
21+
"body": [
22+
"app.register(function plugin (app, opts, next) {",
23+
" next()",
24+
"})"
25+
],
26+
"description": "Add an empty plugin"
27+
},
28+
"fastify print routes": {
29+
"scope": "typescript",
30+
"prefix": "ffprintroutes",
31+
"body": ["app.ready(() => {", " console.log(app.printRoutes())", "})"],
32+
"description": "Print to stdout the routes tree"
33+
},
34+
"fastify error handler": {
35+
"scope": "typescript",
36+
"prefix": ["fferrorhandler"],
37+
"body": [
38+
"app.setErrorHandler(function (error, request, reply) {",
39+
" this.log.error(error)",
40+
" reply.status(500).send({ ok: false })",
41+
"})"
42+
],
43+
"description": "Add a custom error handler"
44+
},
45+
"fastify decorate server": {
46+
"scope": "typescript",
47+
"prefix": ["ffdecorateserver"],
48+
"body": [
49+
"app.decorate('utility', function () {",
50+
" ${TM_SELECTED_TEXT:${0}}",
51+
"})"
52+
],
53+
"description": "Add a server decorator"
54+
},
55+
"fastify decorate request": {
56+
"scope": "typescript",
57+
"prefix": ["ffdecoraterequest"],
58+
"body": [
59+
"app.decorateRequest('utility', function () {",
60+
" ${TM_SELECTED_TEXT:${0}}",
61+
"})"
62+
],
63+
"description": "Add a request decorator"
64+
},
65+
"fastify decorate reply": {
66+
"scope": "typescript",
67+
"prefix": ["ffdecoratereply"],
68+
"body": [
69+
"app.decorateReply('utility', function () {",
70+
" ${TM_SELECTED_TEXT:${0}}",
71+
"})"
72+
],
73+
"description": "Add a reply decorator"
74+
},
75+
"fastify hook onRequest": {
76+
"scope": "typescript",
77+
"prefix": ["ffonrequest", "hookonrequest"],
78+
"body": [
79+
"app.addHook('onRequest', async function hook (request, reply) {",
80+
" ${TM_SELECTED_TEXT:${0}}",
81+
"})"
82+
],
83+
"description": "Add an instance fastify onRequest hook"
84+
},
85+
"fastify hook preParsing": {
86+
"scope": "typescript",
87+
"prefix": ["ffpreparsing", "hookpreparsing"],
88+
"body": [
89+
"app.addHook('preParsing', async function hook (request, reply, payload) {",
90+
" const newPayload = $0",
91+
" return newPayload",
92+
"})"
93+
],
94+
"description": "Add an instance fastify preParsing hook"
95+
},
96+
"fastify hook preValidation": {
97+
"scope": "typescript",
98+
"prefix": ["ffprevalidation", "hookprevalidation"],
99+
"body": [
100+
"app.addHook('preValidation', async function hook (request, reply) {",
101+
" // request's fields are not been validated yet by the JSON Schema",
102+
"})"
103+
],
104+
"description": "Add an instance fastify preValidation hook"
105+
},
106+
"fastify hook preHandler": {
107+
"scope": "typescript",
108+
"prefix": ["ffprehandler", "hookprehandler"],
109+
"body": [
110+
"app.addHook('preHandler', async function hook (request, reply) {",
111+
" $0",
112+
"})"
113+
],
114+
"description": "Add an instance fastify preHandler hook"
115+
},
116+
"fastify hook preSerialization": {
117+
"scope": "typescript",
118+
"prefix": ["ffpreserialization", "hookpreserialization"],
119+
"body": [
120+
"app.addHook('preSerialization', async function hook (request, reply, payload) {",
121+
" // the hook is NOT called if the payload is a string, a Buffer, a stream or null.",
122+
" const newPayload = $0",
123+
" return newPayload",
124+
"})"
125+
],
126+
"description": "Add an instance fastify preSerialization hook"
127+
},
128+
"fastify hook onError": {
129+
"scope": "typescript",
130+
"prefix": ["ffonerror", "hookonerror"],
131+
"body": [
132+
"app.addHook('onError', async function hook (request, reply, error) {",
133+
" // You should not use this hook to update the error",
134+
"})"
135+
],
136+
"description": "Add an instance fastify onError hook"
137+
},
138+
"fastify hook onRequestAbort": {
139+
"scope": "typescript",
140+
"prefix": ["ffonrequestabort", "hookonrequestabort"],
141+
"body": [
142+
"app.addHook('onRequestAbort', async function hook (request, reply) {",
143+
" // Notice: run when a client closes the connection before. Therefore, you will not be able to send data to the client.",
144+
"})"
145+
],
146+
"description": "Add an instance fastify onRequestAbort hook"
147+
},
148+
"fastify hook onSend": {
149+
"scope": "typescript",
150+
"prefix": ["ffonsend", "hookonsend"],
151+
"body": [
152+
"app.addHook('onSend', async function hook (request, reply, payload) {",
153+
" // If you change the payload, you may only change it to a string, a Buffer, a stream, or null.",
154+
" const newPayload = $0",
155+
" return newPayload",
156+
"})"
157+
],
158+
"description": "Add an instance fastify onSend hook"
159+
},
160+
"fastify hook onResponse": {
161+
"scope": "typescript",
162+
"prefix": ["ffonresponse", "hookonresponse"],
163+
"body": [
164+
"app.addHook('onResponse', async function hook (request, reply) {",
165+
" // The onResponse hook is executed when a response has been sent",
166+
"})"
167+
],
168+
"description": "Add an instance fastify onResponse hook"
169+
},
170+
"fastify hook onTimeout": {
171+
"scope": "typescript",
172+
"prefix": ["ffontimeout", "hookontimeout"],
173+
"body": [
174+
"app.addHook('onTimeout', async function hook (request, reply) {",
175+
" // The onTimeout hook is executed when a request is timed out and the http socket has been hanged up",
176+
"})"
177+
],
178+
"description": "Add an instance fastify onTimeout hook"
179+
},
180+
"fastify application hook onReady": {
181+
"scope": "typescript",
182+
"prefix": ["ffonready", "hookonready"],
183+
"body": ["app.addHook('onReady', async function hook () {", "", "})"],
184+
"description": "Add the fastify onReady application hook"
185+
},
186+
"fastify application hook preClose": {
187+
"scope": "typescript",
188+
"prefix": ["ffpreclose", "hookpreclose"],
189+
"body": ["app.addHook('preClose', async function hook () {", "", "})"],
190+
"description": "Add the fastify preClose application hook"
191+
},
192+
"fastify application hook onClose": {
193+
"scope": "typescript",
194+
"prefix": ["ffonclose", "hookonclose"],
195+
"body": ["app.addHook('onClose', async function hook (app) {", "", "})"],
196+
"description": "Add the fastify onClose application hook"
197+
},
198+
"fastify application hook onRoute": {
199+
"scope": "typescript",
200+
"prefix": ["ffonroute", "hookonroute"],
201+
"body": [
202+
"app.addHook('onRoute', function hook (routeOptions) {",
203+
" // routeOptions.method",
204+
" // routeOptions.schema",
205+
" // routeOptions.url",
206+
" // routeOptions.path",
207+
" // routeOptions.routePath",
208+
" // routeOptions.bodyLimit",
209+
" // routeOptions.logLevel",
210+
" // routeOptions.logSerializers",
211+
" // routeOptions.prefix",
212+
" // routeOptions[hooksName] to get the hooks array",
213+
"})"
214+
],
215+
"description": "Add the fastify onRoute application hook"
216+
},
217+
"fastify application hook onRegister": {
218+
"scope": "typescript",
219+
"prefix": ["ffonregister", "hookonregister"],
220+
"body": [
221+
"app.addHook('onRegister', function hook (app, opts) {",
222+
" // The hook will be executed before the registered code",
223+
"})"
224+
],
225+
"description": "Add the fastify onRegister application hook"
226+
},
227+
"fastify add schema": {
228+
"scope": "typescript",
229+
"prefix": ["ffaddschema", "ffsharedschema", "ffjsonschema", "ffschema"],
230+
"body": ["app.addSchema($0)"],
231+
"description": "Add a shared JSON schema to fastify"
232+
},
233+
"fastify not found handler": {
234+
"scope": "typescript",
235+
"prefix": ["ffnotfound", "ff404"],
236+
"body": [
237+
"app.setNotFoundHandler(function basic404(request, reply) {",
238+
" const { url, method } = request.raw",
239+
" const message = `Route ${method}:${url} not found`",
240+
" request.log.info(message)",
241+
" reply.code(404).send({",
242+
" message,",
243+
" error: 'Not Found',",
244+
" statusCode: 404",
245+
" })",
246+
"})"
247+
],
248+
"description": "Add the default 404 not found handler"
249+
}
250+
}

0 commit comments

Comments
 (0)