Skip to content

Commit

Permalink
v3 boilerplate (fastify#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Mark Clements authored Jun 25, 2020
1 parent aeb6a7e commit 850b58a
Show file tree
Hide file tree
Showing 18 changed files with 27 additions and 58 deletions.
8 changes: 3 additions & 5 deletions templates/app-ts/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import AutoLoad from 'fastify-autoload';
import * as path from 'path';
import { FastifyInstance } from 'fastify';

export default function (fastify: FastifyInstance, opts: any, next: Function) {
export default async function (fastify: FastifyInstance, opts: any) {
// Place here your custom code!

// Do not touch the following lines
Expand All @@ -15,13 +15,11 @@ export default function (fastify: FastifyInstance, opts: any, next: Function) {
options: opts
})

// This loads all plugins defined in services
// This loads all plugins defined in routes
// define your routes in one of these
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'services'),
dir: path.join(__dirname, 'routes'),
options: opts
})

// Make sure to call next when done
next()
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ test('example is loaded', async (t) => {
const res = await app.inject({
url: '/example'
})

t.equal(res.payload, 'this is an example')
})
File renamed without changes.
2 changes: 1 addition & 1 deletion templates/app-ts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
},
"include": [
"services/**/*.ts",
"routes/**/*.ts",
"plugins/**/*.ts",
"app.ts"
]
Expand Down
9 changes: 3 additions & 6 deletions templates/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const path = require('path')
const AutoLoad = require('fastify-autoload')

module.exports = function (fastify, opts, next) {
module.exports = async function (fastify, opts, next) {
// Place here your custom code!

// Do not touch the following lines
Expand All @@ -16,13 +16,10 @@ module.exports = function (fastify, opts, next) {
options: Object.assign({}, opts)
})

// This loads all plugins defined in services
// This loads all plugins defined in routes
// define your routes in one of these
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'services'),
dir: path.join(__dirname, 'routes'),
options: Object.assign({}, opts)
})

// Make sure to call next when done
next()
}
9 changes: 0 additions & 9 deletions templates/app/plugins/support.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,3 @@ module.exports = fp(async function (fastify, opts) {
return 'hugs'
})
})

// You can also use plugin with opts in fastify v2
//
// module.exports = fp(function (fastify, opts, next) {
// fastify.decorate('someSupport', function () {
// return 'hugs'
// })
// next()
// })
File renamed without changes.
7 changes: 7 additions & 0 deletions templates/app/routes/example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = async function (fastify, opts) {
fastify.get('/', async function (request, reply) {
return 'this is an example'
})
}
7 changes: 7 additions & 0 deletions templates/app/routes/root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = async function (fastify, opts) {
fastify.get('/', async function (request, reply) {
return { root: true }
})
}
16 changes: 0 additions & 16 deletions templates/app/services/example/index.js

This file was deleted.

16 changes: 0 additions & 16 deletions templates/app/services/root.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test('example is loaded', async (t) => {
t.equal(res.payload, 'this is an example')
})

// You can also use plugin with opts in fastify v2
// inject callback style:
//
// test('example is loaded', (t) => {
// t.plan(2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test('default root route', async (t) => {
t.deepEqual(JSON.parse(res.payload), { root: true })
})

// You can also use plugins with opts in fastify v2
// inject callback style:
//
// test('default root route', (t) => {
// t.plan(2)
Expand Down
4 changes: 2 additions & 2 deletions test/generate-typescript.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function define (t) {

const testGlob = pkg.scripts.test.split(' ')[1]

t.equal(minimatch.match(['test/services/plugins/more/test/here/ok.test.ts'], testGlob).length, 1)
t.equal(minimatch.match(['test/routes/plugins/more/test/here/ok.test.ts'], testGlob).length, 1)
resolve()
})
})
Expand All @@ -167,7 +167,7 @@ function define (t) {
t.equal(tsConfig.compilerOptions.baseUrl, '.')
t.deepEqual(tsConfig.compilerOptions.paths['*'], ['node_modules/*'])
t.deepEqual(tsConfig.include, [
'services/**/*.ts',
'routes/**/*.ts',
'plugins/**/*.ts',
'app.ts'
])
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
},
"include": [
"services/**/*.ts",
"routes/**/*.ts",
"plugins/**/*.ts",
"test/**/*.ts",
"app.ts"
Expand Down

0 comments on commit 850b58a

Please sign in to comment.