Skip to content

Extend FastifyAuthFunction type to accept async functions #258

@FerrielMelarpis

Description

@FerrielMelarpis

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

Context

The FastifyAuthFunction type provided only covers the callback-style function.
This library supports both callback-style and promise-based functions.

[!NOTE] Not sure if this should be filed as bug. The feature is implemented for promise-based functions, it's just missing the types I need. Feel free to move this to appropriate place. 🙂

Proposal

Extend the FastifyAuthFunction type to be a union of the callback-style functions and promise-based functions.

Something like:

import type { FastifyInstance, FastifyRequest, FastifyReply, HookHandlerDoneFunction } from "fastify";

...

export FastifyAuthFunction =
  | ((this: FastifyInstance, request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) => void)
  | ((this: FastifyInstance, request: FastifyRequest, reply: FastifyReply) => Promise<void>);

Motivation

When creating several authentication strategies, I want their functions to have consistent signature. I started using the promise-based APIs in fastify and when I used this library, I found that typescript complains about a 4th argument when I have some tests calling the strategy function like this:

await strategyFn.call(mockFastifyInstance, mockRequest, mockReply); // Expects a 4th argument

This matches the current type definition wherein we should provide a done handler.
However, that's not required for async functions.

Example

await strategyFn.call(mockFastifyInstance, mockRequest, mockReply); // valid in TS

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions