-
-
Notifications
You must be signed in to change notification settings - Fork 58
types: extended FastifyAuthFunction type to accept async functions #260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,13 +9,21 @@ const app = fastify() | |||||
type Done = (error?: Error) => void | ||||||
|
||||||
app.register(fastifyAuth).after((_err) => { | ||||||
// Callback tests | ||||||
app.auth([ | ||||||
(request, reply, done) => { | ||||||
expectType<FastifyRequest>(request) | ||||||
expectType<FastifyReply>(reply) | ||||||
expectType<Done>(done) | ||||||
}, | ||||||
], { relation: 'or' }) | ||||||
app.auth([ | ||||||
(request, reply, done) => { | ||||||
expectType<FastifyRequest>(request) | ||||||
expectType<FastifyReply>(reply) | ||||||
expectType<Done>(done) | ||||||
}, | ||||||
], { relation: 'and' }) | ||||||
app.auth([ | ||||||
(request, reply, done) => { | ||||||
expectType<FastifyRequest>(request) | ||||||
|
@@ -29,12 +37,123 @@ app.register(fastifyAuth).after((_err) => { | |||||
expectType<FastifyReply>(reply) | ||||||
expectType<Done>(done) | ||||||
}, | ||||||
], { relation: 'or', run: 'all' }) | ||||||
app.auth([ | ||||||
(request, reply, done) => { | ||||||
expectType<FastifyRequest>(request) | ||||||
expectType<FastifyReply>(reply) | ||||||
expectType<Done>(done) | ||||||
}, | ||||||
], { relation: 'and', run: 'all' }) | ||||||
|
||||||
// Async function tests | ||||||
app.auth([ | ||||||
async (request: FastifyRequest, reply: FastifyReply) => { | ||||||
expectType<FastifyRequest>(request) | ||||||
expectType<FastifyReply>(reply) | ||||||
await Promise.resolve() | ||||||
}, | ||||||
]) | ||||||
app.auth([ | ||||||
async (request: FastifyRequest, reply: FastifyReply) => { | ||||||
expectType<FastifyRequest>(request) | ||||||
expectType<FastifyReply>(reply) | ||||||
await Promise.resolve() | ||||||
}, | ||||||
], { relation: 'or' }) | ||||||
app.auth([ | ||||||
async (request: FastifyRequest, reply: FastifyReply) => { | ||||||
expectType<FastifyRequest>(request) | ||||||
expectType<FastifyReply>(reply) | ||||||
await Promise.resolve() | ||||||
}, | ||||||
], { relation: 'and' }) | ||||||
app.auth([ | ||||||
async (request: FastifyRequest, reply: FastifyReply) => { | ||||||
expectType<FastifyRequest>(request) | ||||||
expectType<FastifyReply>(reply) | ||||||
await Promise.resolve() | ||||||
}, | ||||||
], { run: 'all' }) | ||||||
app.auth([ | ||||||
async (request: FastifyRequest, reply: FastifyReply) => { | ||||||
expectType<FastifyRequest>(request) | ||||||
expectType<FastifyReply>(reply) | ||||||
await Promise.resolve() | ||||||
}, | ||||||
], { relation: 'or', run: 'all' }) | ||||||
app.auth([ | ||||||
async (request: FastifyRequest, reply: FastifyReply) => { | ||||||
expectType<FastifyRequest>(request) | ||||||
expectType<FastifyReply>(reply) | ||||||
await Promise.resolve() | ||||||
}, | ||||||
], { relation: 'and', run: 'all' }) | ||||||
|
||||||
// Promise-based function tests | ||||||
app.auth([ | ||||||
(request: FastifyRequest, reply: FastifyReply) => { | ||||||
expectType<FastifyRequest>(request) | ||||||
expectType<FastifyReply>(reply) | ||||||
return Promise.resolve() | ||||||
}, | ||||||
]) | ||||||
app.auth([ | ||||||
(request: FastifyRequest, reply: FastifyReply) => { | ||||||
expectType<FastifyRequest>(request) | ||||||
expectType<FastifyReply>(reply) | ||||||
return Promise.resolve() | ||||||
}, | ||||||
], { relation: 'or' }) | ||||||
app.auth([ | ||||||
(request: FastifyRequest, reply: FastifyReply) => { | ||||||
expectType<FastifyRequest>(request) | ||||||
expectType<FastifyReply>(reply) | ||||||
return Promise.resolve() | ||||||
}, | ||||||
], { relation: 'and' }) | ||||||
app.auth([ | ||||||
(request: FastifyRequest, reply: FastifyReply) => { | ||||||
expectType<FastifyRequest>(request) | ||||||
expectType<FastifyReply>(reply) | ||||||
return Promise.resolve() | ||||||
}, | ||||||
], { run: 'all' }) | ||||||
app.auth([ | ||||||
(request: FastifyRequest, reply: FastifyReply) => { | ||||||
expectType<FastifyRequest>(request) | ||||||
expectType<FastifyReply>(reply) | ||||||
return Promise.resolve() | ||||||
}, | ||||||
], { relation: 'or', run: 'all' }) | ||||||
app.auth([ | ||||||
(request: FastifyRequest, reply: FastifyReply) => { | ||||||
expectType<FastifyRequest>(request) | ||||||
expectType<FastifyReply>(reply) | ||||||
return Promise.resolve() | ||||||
}, | ||||||
], { relation: 'and', run: 'all' }) | ||||||
|
||||||
// this context tests | ||||||
app.auth([ | ||||||
async function () { | ||||||
expectType<FastifyInstance>(this) | ||||||
await Promise.resolve() | ||||||
}, | ||||||
]) | ||||||
app.auth([ | ||||||
function () { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function is missing the explicit 'this: FastifyInstance' type annotation that was mentioned as necessary in the PR description. This should be 'function (this: FastifyInstance) {' to match the requirement for TypeScript's inability to infer 'this' context in union types.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
expectType<FastifyInstance>(this) | ||||||
return Promise.resolve() | ||||||
}, | ||||||
]) | ||||||
app.auth([ | ||||||
async function () { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The async function is missing the explicit 'this: FastifyInstance' type annotation that was mentioned as necessary in the PR description. This should be 'async function (this: FastifyInstance) {' to match the requirement for TypeScript's inability to infer 'this' context in union types.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
expectType<FastifyInstance>(this) | ||||||
await Promise.resolve() | ||||||
}, | ||||||
]) | ||||||
|
||||||
const auth = app.auth([() => {}]) | ||||||
expectType<preHandlerHookHandler>(auth) | ||||||
app.get('/secret', { preHandler: auth }, () => {}) | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The async function is missing the explicit 'this: FastifyInstance' type annotation that was mentioned as necessary in the PR description. This should be 'async function (this: FastifyInstance) {' to match the requirement for TypeScript's inability to infer 'this' context in union types.
Copilot uses AI. Check for mistakes.