Skip to content

FastifyInstance.auth0Client causes a type error when using HTTPS or HTTP2 #37

Description

@JesseTG

Description

I'm using @auth0/auth0-fastify to implement authentication in my app. I've started implementing HTTPS support, but noticed some TypeScript issues that prevent it from working. I'm not sure if this indicates a TypeScript issue or a lack of HTTP2 support. Details follow.

When I initialize my app's FastifyInstance like so, then its type signature is parameterized like so:

const fastify = Fastify({
  http2: true,
  https: {
    allowHTTP1: true,
    key: HTTPS_KEY_PATH ? readFileSync(HTTPS_KEY_PATH) : undefined,
    cert: HTTPS_CERT_PATH ? readFileSync(HTTPS_CERT_PATH) : undefined,
  },
});

fastify.get('/', async (request, reply) => {
  const user = await request.server.auth0Client?.getUser({ request, reply });
  // Other stuff...
});

/*
const fastify: Fastify.FastifyInstance<
  Http2SecureServer<
    typeof IncomingMessage,
    typeof ServerResponse,
    typeof Http2ServerRequest,
    typeof Http2ServerResponse
  >,
  Http2ServerRequest,
  Http2ServerResponse<Http2ServerRequest>,
  Fastify.FastifyBaseLogger,
  Fastify.FastifyTypeProviderDefault
> & PromiseLike<TheSameStuff>
*/

The problem is that @auth0/auth0-fastify's declaration-merged FastifyInstance assumes the default parameterization through its use of StoreOptions, which in turn only uses the default parameters of FastifyRequest and FastifyReply.

declare module 'fastify' {
  interface FastifyInstance {
    auth0Client: ServerClient<StoreOptions> | undefined;
  }
}

export interface StoreOptions {
  request: FastifyRequest;
  reply: FastifyReply;
}

End result? These compiler errors:

Type 'FastifyRequest<RouteGenericInterface, Http2SecureServer<typeof IncomingMessage, typeof ServerResponse, typeof Http2ServerRequest, typeof Http2ServerResponse>, ... 5 more ..., ResolveFastifyRequestType<...>>' is not assignable to type 'FastifyRequest<RouteGenericInterface, RawServerDefault, IncomingMessage, FastifySchema, FastifyTypeProviderDefault, unknown, FastifyBaseLogger, ResolveFastifyRequestType<...>>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
  Type 'Http2ServerRequest' is missing the following properties from type 'IncomingMessage': headersDistinct, trailersDistinct

Type 'FastifyReply<RouteGenericInterface, Http2SecureServer<typeof IncomingMessage, typeof ServerResponse, typeof Http2ServerRequest, typeof Http2ServerResponse>, ... 5 more ..., unknown>' is not assignable to type 'FastifyReply<RouteGenericInterface, RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, unknown, FastifySchema, FastifyTypeProviderDefault, unknown>'.
  Type 'Http2ServerRequest' is missing the following properties from type 'IncomingMessage': headersDistinct, trailersDistinct

Reproduction

To reproduce this, try to build the aforementioned code snippets.

Environment

  • @auth0/auth0-fastify@1.2.0
  • fastify@5.6.1
  • typescript@5.9.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    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