Skip to content

Commit 34384ee

Browse files
committed
feat(fastify-autoload): add fastify-autoload
update build script to compile & bundle all ts files
1 parent 4d0f9f4 commit 34384ee

File tree

9 files changed

+201
-11
lines changed

9 files changed

+201
-11
lines changed

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"scripts": {
55
"prepare": "husky install",
66
"dev": "nodemon",
7-
"build": "esbuild src/index.ts --bundle --minify --platform=node --outdir=build",
7+
"build": "rm -rf build && esbuild `find src \\( -name '*.ts' \\)` --platform=node --outdir=build --resolve-extensions=.js --bundle --minify",
88
"format": "prettier --write 'src/**/*.{js,ts,json,md}'",
99
"lint": "prettier --check 'src/**/*.{js,ts,json,md}' && eslint --ignore-path .gitignore .",
1010
"cz": "cz",
@@ -34,6 +34,9 @@
3434
"typescript": "^4.6.3"
3535
},
3636
"dependencies": {
37-
"fastify": "^3.28.0"
37+
"fastify": "^3.28.0",
38+
"fastify-autoload": "^3.12.0",
39+
"fastify-plugin": "^3.0.1",
40+
"fastify-sensible": "^3.1.2"
3841
}
3942
}

pnpm-lock.yaml

+96-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
import { join } from 'path'
12
import Fastify from 'fastify'
3+
import autoLoad from 'fastify-autoload'
24

35
const fastify = Fastify({
46
logger: true
57
})
68

7-
fastify.get('/', async () => {
8-
return { hello: 'world' }
9+
fastify.register(autoLoad, {
10+
dir: join(__dirname, 'plugins')
11+
})
12+
13+
fastify.register(autoLoad, {
14+
dir: join(__dirname, 'routes')
915
})
1016

1117
const start = async () => {

src/plugins/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Plugins Folder
2+
3+
Plugins define behavior that is common to all the routes in your
4+
application. Authentication, caching, templates, and all the other cross
5+
cutting concerns should be handled by plugins placed in this folder.
6+
7+
Files in this folder are typically defined through the
8+
[`fastify-plugin`](https://github.com/fastify/fastify-plugin) module,
9+
making them non-encapsulated. They can define decorators and set hooks
10+
that will then be used in the rest of your application.
11+
12+
Check out:
13+
14+
- [The hitchhiker's guide to plugins](https://www.fastify.io/docs/latest/Plugins-Guide/)
15+
- [Fastify decorators](https://www.fastify.io/docs/latest/Decorators/).
16+
- [Fastify lifecycle](https://www.fastify.io/docs/latest/Lifecycle/).

src/plugins/sensible.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import fp from 'fastify-plugin'
2+
import sensible, { SensibleOptions } from 'fastify-sensible'
3+
4+
/**
5+
* This plugins adds some utilities to handle http errors
6+
*
7+
* @see https://github.com/fastify/fastify-sensible
8+
*/
9+
export default fp<SensibleOptions>(async (fastify, opts) => {
10+
fastify.register(sensible, {
11+
errorHandler: false
12+
})
13+
})

src/plugins/support.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import fp from 'fastify-plugin'
2+
3+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
4+
export interface SupportPluginOptions {
5+
// Specify Support plugin options here
6+
}
7+
8+
// The use of fastify-plugin is required to be able
9+
// to export the decorators to the outer scope
10+
export default fp<SupportPluginOptions>(async (fastify, opts) => {
11+
fastify.decorate('someSupport', function () {
12+
return 'hugs'
13+
})
14+
})
15+
16+
// When using .decorate you have to specify added properties for Typescript
17+
declare module 'fastify' {
18+
export interface FastifyInstance {
19+
someSupport(): string
20+
}
21+
}

src/routes/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Routes Folder
2+
3+
Routes define endpoints within your application. Fastify provides an
4+
easy path to a microservice architecture, in the future you might want
5+
to independently deploy some of those.
6+
7+
In this folder you should define all the routes that define the endpoints
8+
of your web application.
9+
Each service is a [Fastify
10+
plugin](https://www.fastify.io/docs/latest/Plugins/), it is
11+
encapsulated (it can have its own independent plugins) and it is
12+
typically stored in a file; be careful to group your routes logically,
13+
e.g. all `/users` routes in a `users.js` file. We have added
14+
a `root.js` file for you with a '/' root added.
15+
16+
If a single file become too large, create a folder and add a `index.js` file there:
17+
this file must be a Fastify plugin, and it will be loaded automatically
18+
by the application. You can now add as many files as you want inside that folder.
19+
In this way you can create complex routes within a single monolith,
20+
and eventually extract them.
21+
22+
If you need to share functionality between routes, place that
23+
functionality into the `plugins` folder, and share it via
24+
[decorators](https://www.fastify.io/docs/latest/Decorators/).

src/routes/examples/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { FastifyPluginAsync } from 'fastify'
2+
3+
const example: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
4+
fastify.get('/', async function (request, reply) {
5+
return 'this is an example'
6+
})
7+
}
8+
9+
export default example

src/routes/root.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { FastifyPluginAsync } from 'fastify'
2+
3+
const root: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
4+
fastify.get('/', async function (request, reply) {
5+
return { root: true }
6+
})
7+
}
8+
9+
export default root

0 commit comments

Comments
 (0)