feat: updates

This commit is contained in:
qier222 2023-01-07 14:39:03 +08:00
parent 884f3df41a
commit c6c59b2cd9
No known key found for this signature in database
84 changed files with 3531 additions and 2616 deletions

View file

@ -1,4 +1,5 @@
# Getting Started with [Fastify-CLI](https://www.npmjs.com/package/fastify-cli)
This project was bootstrapped with Fastify-CLI.
## Available Scripts

View file

@ -1,14 +1,14 @@
import { join } from 'path';
import AutoLoad, {AutoloadPluginOptions} from '@fastify/autoload';
import { FastifyPluginAsync } from 'fastify';
import { join } from 'path'
import AutoLoad, { AutoloadPluginOptions } from '@fastify/autoload'
import { FastifyPluginAsync } from 'fastify'
export type AppOptions = {
// Place your custom options for app below here.
} & Partial<AutoloadPluginOptions>;
} & Partial<AutoloadPluginOptions>
const app: FastifyPluginAsync<AppOptions> = async (
fastify,
opts
fastify,
opts
): Promise<void> => {
// Place here your custom code!
@ -19,17 +19,16 @@ const app: FastifyPluginAsync<AppOptions> = async (
// through your application
void fastify.register(AutoLoad, {
dir: join(__dirname, 'plugins'),
options: opts
options: opts,
})
// This loads all plugins defined in routes
// define your routes in one of these
void fastify.register(AutoLoad, {
dir: join(__dirname, 'routes'),
options: opts
options: opts,
})
}
};
export default app;
export default app
export { app }

View file

@ -11,6 +11,6 @@ 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/Guides/Plugins-Guide/)
* [Fastify decorators](https://www.fastify.io/docs/latest/Reference/Decorators/).
* [Fastify lifecycle](https://www.fastify.io/docs/latest/Reference/Lifecycle/).
- [The hitchhiker's guide to plugins](https://www.fastify.io/docs/latest/Guides/Plugins-Guide/)
- [Fastify decorators](https://www.fastify.io/docs/latest/Reference/Decorators/).
- [Fastify lifecycle](https://www.fastify.io/docs/latest/Reference/Lifecycle/).

View file

@ -8,6 +8,6 @@ import sensible, { SensibleOptions } from '@fastify/sensible'
*/
export default fp<SensibleOptions>(async (fastify, opts) => {
fastify.register(sensible, {
errorHandler: false
errorHandler: false,
})
})

View file

@ -15,6 +15,6 @@ export default fp<SupportPluginOptions>(async (fastify, opts) => {
// When using .decorate you have to specify added properties for Typescript
declare module 'fastify' {
export interface FastifyInstance {
someSupport(): string;
someSupport(): string
}
}

View file

@ -6,4 +6,4 @@ const root: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
})
}
export default root;
export default root

View file

@ -2,18 +2,18 @@
import Fastify from 'fastify'
import fp from 'fastify-plugin'
import App from '../src/app'
import * as tap from 'tap';
import * as tap from 'tap'
export type Test = typeof tap['Test']['prototype'];
export type Test = typeof tap['Test']['prototype']
// Fill in this config with all the configurations
// needed for testing the application
async function config () {
async function config() {
return {}
}
// Automatically build and tear down our instance
async function build (t: Test) {
async function build(t: Test) {
const app = Fastify()
// fastify-plugin ensures that all decorators
@ -21,7 +21,7 @@ async function build (t: Test) {
// different from the production setup
void app.register(fp(App), await config())
await app.ready();
await app.ready()
// Tear down our app after we are done
t.teardown(() => void app.close())
@ -29,7 +29,4 @@ async function build (t: Test) {
return app
}
export {
config,
build
}
export { config, build }

View file

@ -2,7 +2,7 @@ import { test } from 'tap'
import Fastify from 'fastify'
import Support from '../../src/plugins/support'
test('support works standalone', async (t) => {
test('support works standalone', async t => {
const fastify = Fastify()
void fastify.register(Support)
await fastify.ready()

View file

@ -1,11 +1,11 @@
import { test } from 'tap'
import { build } from '../helper'
test('example is loaded', async (t) => {
test('example is loaded', async t => {
const app = await build(t)
const res = await app.inject({
url: '/example'
url: '/example',
})
t.equal(res.payload, 'this is an example')

View file

@ -1,11 +1,11 @@
import { test } from 'tap'
import { build } from '../helper'
test('default root route', async (t) => {
test('default root route', async t => {
const app = await build(t)
const res = await app.inject({
url: '/'
url: '/',
})
t.same(JSON.parse(res.payload), { root: true })
})