Skip to content

Commit

Permalink
Merge branch 'main' into tap-18-test
Browse files Browse the repository at this point in the history
  • Loading branch information
Eomm committed Aug 26, 2023
2 parents 888dfdf + 8974714 commit 0b4a245
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 40 deletions.
15 changes: 15 additions & 0 deletions .github/dependabot.yml
Expand Up @@ -17,3 +17,18 @@ updates:
schedule:
interval: "weekly"
open-pull-requests-limit: 10
groups:
# Production dependencies without breaking changes
dependencies:
dependency-type: "production"
update-types:
- "minor"
- "patch"
# Production dependencies with breaking changes
dependencies-major:
dependency-type: "production"
update-types:
- "major"
# Development dependencies
dev-dependencies:
dependency-type: "development"
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yml
Expand Up @@ -91,7 +91,7 @@ jobs:
**MAIN**: ${{ needs.benchmark.outputs.MAIN-BENCH-20 }}
remove-label:
if: "always()"
if: ${{ github.event.label.name == 'benchmark' }}
needs:
- benchmark
- output-benchmark
Expand Down
25 changes: 0 additions & 25 deletions .github/workflows/sync-next.yml

This file was deleted.

2 changes: 1 addition & 1 deletion docs/Guides/Getting-Started.md
Expand Up @@ -277,7 +277,7 @@ async function dbConnector (fastify, options) {

// Wrapping a plugin function with fastify-plugin exposes the decorators
// and hooks, declared inside the plugin to the parent scope.
module.exports = fastifyPlugin(dbConnector)
export default fastifyPlugin(dbConnector)

```

Expand Down
15 changes: 9 additions & 6 deletions lib/pluginUtils.js
Expand Up @@ -25,12 +25,15 @@ function getPluginName (func) {
// let's see if this is a file, and in that case use that
// this is common for plugins
const cache = require.cache
const keys = Object.keys(cache)

for (let i = 0; i < keys.length; i++) {
const key = keys[i]
if (cache[key].exports === func) {
return key
// cache is undefined inside SEA
if (cache) {
const keys = Object.keys(cache)

for (let i = 0; i < keys.length; i++) {
const key = keys[i]
if (cache[key].exports === func) {
return key
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion test/custom-http-server.test.js
Expand Up @@ -10,8 +10,9 @@ const dns = require('dns').promises

test('Should support a custom http server', async t => {
const localAddresses = await dns.lookup('localhost', { all: true })
const minPlan = localAddresses.length - 1 || 1

t.plan((localAddresses.length - 1) + 3)
t.plan(minPlan + 3)

const serverFactory = (handler, opts) => {
t.ok(opts.serverFactory, 'it is called once for localhost')
Expand Down
3 changes: 2 additions & 1 deletion test/https/custom-https-server.test.js
Expand Up @@ -12,8 +12,9 @@ t.before(buildCertificate)

test('Should support a custom https server', async t => {
const localAddresses = await dns.lookup('localhost', { all: true })
const minPlan = localAddresses.length - 1 || 1

t.plan((localAddresses.length - 1) + 3)
t.plan(minPlan + 3)

const serverFactory = (handler, opts) => {
t.ok(opts.serverFactory, 'it is called once for localhost')
Expand Down
15 changes: 15 additions & 0 deletions test/internals/plugin.test.js
Expand Up @@ -29,6 +29,21 @@ test('getPluginName should return plugin name if the file is cached', t => {
t.equal(pluginName, expectedPluginName)
})

test('getPluginName should not throw when require.cache is undefined', t => {
t.plan(1)
function example () {
console.log('is just an example')
}
const cache = require.cache
require.cache = undefined
t.teardown(() => {
require.cache = cache
})
const pluginName = pluginUtilsPublic.getPluginName(example)

t.equal(pluginName, 'example')
})

test("getMeta should return the object stored with the 'plugin-meta' symbol", t => {
t.plan(1)

Expand Down
9 changes: 7 additions & 2 deletions test/types/instance.test-d.ts
Expand Up @@ -6,7 +6,8 @@ import fastify, {
FastifyInstance,
RawReplyDefaultExpression,
RawRequestDefaultExpression,
RawServerDefault
RawServerDefault,
RouteGenericInterface
} from '../../fastify'
import { HookHandlerDoneFunction } from '../../types/hooks'
import { FastifyReply } from '../../types/reply'
Expand Down Expand Up @@ -257,9 +258,13 @@ expectNotDeprecated(server.listen({ port: 3000, host: '::/0', ipv6Only: true },

expectAssignable<void>(server.routing({} as RawRequestDefaultExpression, {} as RawReplyDefaultExpression))

expectType<FastifyInstance>(fastify().get('/', {
expectType<FastifyInstance>(fastify().get<RouteGenericInterface, { contextKey: string }>('/', {
handler: () => {},
errorHandler: (error, request, reply) => {
expectAssignable<FastifyError>(error)
expectAssignable<FastifyRequest>(request)
expectAssignable<{ contextKey: string }>(request.routeConfig)
expectAssignable<FastifyReply>(reply)
expectAssignable<void>(server.errorHandler(error, request, reply))
}
}))
Expand Down
27 changes: 26 additions & 1 deletion test/types/type-provider.test-d.ts
Expand Up @@ -3,7 +3,8 @@ import fastify, {
HookHandlerDoneFunction,
FastifyRequest,
FastifyReply,
FastifyInstance
FastifyInstance,
FastifyError
} from '../../fastify'
import { expectAssignable, expectError, expectType } from 'tsd'
import { IncomingHttpHeaders } from 'http'
Expand Down Expand Up @@ -79,6 +80,14 @@ expectAssignable(server.withTypeProvider<TypeBoxProvider>().get(
y: Type.Number(),
z: Type.Number()
})
},
errorHandler: (error, request, reply) => {
expectType<FastifyError>(error)
expectAssignable<FastifyRequest>(request)
expectType<number>(request.body.x)
expectType<number>(request.body.y)
expectType<number>(request.body.z)
expectAssignable<FastifyReply>(reply)
}
},
(req) => {
Expand Down Expand Up @@ -108,6 +117,14 @@ expectAssignable(server.withTypeProvider<JsonSchemaToTsProvider>().get(
z: { type: 'boolean' }
}
} as const
},
errorHandler: (error, request, reply) => {
expectType<FastifyError>(error)
expectAssignable<FastifyRequest>(request)
expectType<number | undefined>(request.body.x)
expectType<string | undefined>(request.body.y)
expectType<boolean | undefined>(request.body.z)
expectAssignable<FastifyReply>(reply)
}
},
(req) => {
Expand Down Expand Up @@ -135,6 +152,14 @@ expectAssignable(server.withTypeProvider<TypeBoxProvider>().withTypeProvider<Jso
z: { type: 'boolean' }
}
} as const
},
errorHandler: (error, request, reply) => {
expectType<FastifyError>(error)
expectAssignable<FastifyRequest>(request)
expectType<number | undefined>(request.body.x)
expectType<string | undefined>(request.body.y)
expectType<boolean | undefined>(request.body.z)
expectAssignable<FastifyReply>(reply)
}
},
(req) => {
Expand Down
4 changes: 3 additions & 1 deletion types/hooks.d.ts
Expand Up @@ -630,7 +630,9 @@ export type ApplicationHookLookup<K extends ApplicationHook> = K extends 'onRegi
? onCloseHookHandler
: K extends 'preClose'
? preCloseHookHandler
: never
: K extends 'onRoute'
? onRouteHookHandler
: never

export type ApplicationHookAsyncLookup<K extends ApplicationHook> = K extends 'onRegister'
? onRegisterHookHandler
Expand Down
7 changes: 6 additions & 1 deletion types/route.d.ts
Expand Up @@ -45,7 +45,12 @@ export interface RouteShorthandOptions<
version?: string;
constraints?: { [name: string]: any },
prefixTrailingSlash?: 'slash'|'no-slash'|'both';
errorHandler?: (this: FastifyInstance, error: FastifyError, request: FastifyRequest, reply: FastifyReply) => void;
errorHandler?: (
this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
error: FastifyError,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
) => void;
childLoggerFactory?: FastifyChildLoggerFactory<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
schemaErrorFormatter?: SchemaErrorFormatter;

Expand Down

0 comments on commit 0b4a245

Please sign in to comment.