Skip to content

Commit

Permalink
feat(fastify-autoload): add fastify-autoload
Browse files Browse the repository at this point in the history
update build script to compile & bundle all ts files
  • Loading branch information
wd-David committed Apr 25, 2022
1 parent 4d0f9f4 commit 34384ee
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 11 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"scripts": {
"prepare": "husky install",
"dev": "nodemon",
"build": "esbuild src/index.ts --bundle --minify --platform=node --outdir=build",
"build": "rm -rf build && esbuild `find src \\( -name '*.ts' \\)` --platform=node --outdir=build --resolve-extensions=.js --bundle --minify",
"format": "prettier --write 'src/**/*.{js,ts,json,md}'",
"lint": "prettier --check 'src/**/*.{js,ts,json,md}' && eslint --ignore-path .gitignore .",
"cz": "cz",
Expand Down Expand Up @@ -34,6 +34,9 @@
"typescript": "^4.6.3"
},
"dependencies": {
"fastify": "^3.28.0"
"fastify": "^3.28.0",
"fastify-autoload": "^3.12.0",
"fastify-plugin": "^3.0.1",
"fastify-sensible": "^3.1.2"
}
}
103 changes: 96 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { join } from 'path'
import Fastify from 'fastify'
import autoLoad from 'fastify-autoload'

const fastify = Fastify({
logger: true
})

fastify.get('/', async () => {
return { hello: 'world' }
fastify.register(autoLoad, {
dir: join(__dirname, 'plugins')
})

fastify.register(autoLoad, {
dir: join(__dirname, 'routes')
})

const start = async () => {
Expand Down
16 changes: 16 additions & 0 deletions src/plugins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Plugins Folder

Plugins define behavior that is common to all the routes in your
application. Authentication, caching, templates, and all the other cross
cutting concerns should be handled by plugins placed in this folder.

Files in this folder are typically defined through the
[`fastify-plugin`](https://github.com/fastify/fastify-plugin) module,
making them non-encapsulated. They can define decorators and set hooks
that will then be used in the rest of your application.

Check out:

- [The hitchhiker's guide to plugins](https://www.fastify.io/docs/latest/Plugins-Guide/)
- [Fastify decorators](https://www.fastify.io/docs/latest/Decorators/).
- [Fastify lifecycle](https://www.fastify.io/docs/latest/Lifecycle/).
13 changes: 13 additions & 0 deletions src/plugins/sensible.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import fp from 'fastify-plugin'
import sensible, { SensibleOptions } from 'fastify-sensible'

/**
* This plugins adds some utilities to handle http errors
*
* @see https://github.com/fastify/fastify-sensible
*/
export default fp<SensibleOptions>(async (fastify, opts) => {
fastify.register(sensible, {
errorHandler: false
})
})
21 changes: 21 additions & 0 deletions src/plugins/support.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import fp from 'fastify-plugin'

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface SupportPluginOptions {
// Specify Support plugin options here
}

// The use of fastify-plugin is required to be able
// to export the decorators to the outer scope
export default fp<SupportPluginOptions>(async (fastify, opts) => {
fastify.decorate('someSupport', function () {
return 'hugs'
})
})

// When using .decorate you have to specify added properties for Typescript
declare module 'fastify' {
export interface FastifyInstance {
someSupport(): string
}
}
24 changes: 24 additions & 0 deletions src/routes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Routes Folder

Routes define endpoints within your application. Fastify provides an
easy path to a microservice architecture, in the future you might want
to independently deploy some of those.

In this folder you should define all the routes that define the endpoints
of your web application.
Each service is a [Fastify
plugin](https://www.fastify.io/docs/latest/Plugins/), it is
encapsulated (it can have its own independent plugins) and it is
typically stored in a file; be careful to group your routes logically,
e.g. all `/users` routes in a `users.js` file. We have added
a `root.js` file for you with a '/' root added.

If a single file become too large, create a folder and add a `index.js` file there:
this file must be a Fastify plugin, and it will be loaded automatically
by the application. You can now add as many files as you want inside that folder.
In this way you can create complex routes within a single monolith,
and eventually extract them.

If you need to share functionality between routes, place that
functionality into the `plugins` folder, and share it via
[decorators](https://www.fastify.io/docs/latest/Decorators/).
9 changes: 9 additions & 0 deletions src/routes/examples/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { FastifyPluginAsync } from 'fastify'

const example: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
fastify.get('/', async function (request, reply) {
return 'this is an example'
})
}

export default example
9 changes: 9 additions & 0 deletions src/routes/root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { FastifyPluginAsync } from 'fastify'

const root: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
fastify.get('/', async function (request, reply) {
return { root: true }
})
}

export default root

0 comments on commit 34384ee

Please sign in to comment.