Skip to content

Commit 7ec8a6d

Browse files
authored
Added Error to return type of async validator (#89)
* Added Error to return type of async validator * Split the promise validator tsd test
1 parent 346d107 commit 7ec8a6d

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface FastifyBasicAuthOptions {
2525
req: FastifyRequest,
2626
reply: FastifyReply,
2727
done: (err?: Error) => void
28-
): void | Promise<void>;
28+
): void | Promise<void|Error>;
2929
authenticate?: boolean | { realm: string };
3030
header?: string;
3131
}

index.test-d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import fastifyBasicAuth from '.'
1212

1313
const app = fastify()
1414

15+
//validation ok
1516
app.register(fastifyBasicAuth, {
1617
validate: async function validatePromise (username, password, req, reply) {
1718
expectType<string>(username)
@@ -23,6 +24,21 @@ app.register(fastifyBasicAuth, {
2324
header: 'x-forwarded-authorization'
2425
})
2526

27+
//validation failure
28+
app.register(fastifyBasicAuth, {
29+
validate: async function validatePromise (username, password, req, reply) {
30+
expectType<string>(username)
31+
expectType<string>(password)
32+
expectType<FastifyRequest>(req)
33+
expectType<FastifyReply>(reply)
34+
expectType<FastifyInstance>(this)
35+
return new Error("unauthorized")
36+
},
37+
header: 'x-forwarded-authorization'
38+
})
39+
40+
41+
2642
app.register(fastifyBasicAuth, {
2743
validate: function validateCallback (username, password, req, reply, done) {
2844
expectType<string>(username)

0 commit comments

Comments
 (0)