Skip to content

SvelteKit integration hook has the wrong type #271

@re-xyr

Description

@re-xyr

The SvelteKit hook in @fedify/fedify/x/sveltekit has the type:

type RequestEvent = {
  request: Request
}

type HookParams = {
  event: RequestEvent 
  //     ^ covariant
  resolve: (event: RequestEvent) => Promise<Response>
  //               ^ contravariant
}

fedifyHook(...): (params: HookParams) => Promise<Response>

Notice two points here:

Indeed, TypeScript complains if you try to mark it as a Handle:

import { fedifyHook } from '@fedify/fedify/x/svelte'
import type { Handle } from '@sveltejs/kit'

const handleActivityPub = fedifyHook(federation, makeContextData) as Handle
//                                                                ^^^^^^^^^
// TS2352: Conversion of type '(params: HookParams) => Promise<Response>' to type 
// 'Handle' may be a mistake because neither type sufficiently overlaps with the other. 
// If this was intentional, convert the expression to 'unknown' first.

I suppose the most straightforward way to solve this is to add @sveltejs/kit to devDependencies and then use the actual Handle type from there, something like:

import type { Handle, RequestEvent } from '@sveltejs/kit'

export function fedifyHook<TContextData>(
  federation: Federation<TContextData>,
  createContextData: (
    event: RequestEvent,
  ) => TContextData | Promise<TContextData>,
): Handle {
  return async ({ event, resolve }) => {
    // -- snip --
  };
}

PS. I see the AI labeler adding more and more labels to my issue every time I edit it. You may want to fix that too!

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions