Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ To be released.
- Created SvelteKit integration as the *@fedify/sveltekit* package.
Separated from `@fedify/fedify/x/sveltekit` to improve modularity and
reduce bundle size. [[#375] by Chanhaeng Lee]
- Fixed SvelteKit integration hook types to correctly infer the request
and response types in hooks. [[#271], [#394] by Chanhaeng Lee]

[#271]: https://github.com/fedify-dev/fedify/pull/271
[#394]: https://github.com/fedify-dev/fedify/pull/394


Version 1.8.8
Expand Down
8 changes: 3 additions & 5 deletions packages/sveltekit/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
"version": "1.9.0",
"license": "MIT",
"imports": {
"@std/assert": "jsr:@std/assert@^1.0.13"
"@std/assert": "jsr:@std/assert@^1.0.13",
"@sveltejs/kit": "npm:@sveltejs/kit@^2.0.0"
},
"exports": {
".": "./src/mod.ts"
},
"exclude": [
"dist",
"node_modules"
],
"exclude": ["dist", "node_modules"],
"tasks": {
"check": "deno fmt --check && deno lint && deno check src/*.ts",
"test": "deno test --allow-net --allow-env"
Expand Down
26 changes: 8 additions & 18 deletions packages/sveltekit/src/mod.test.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,27 @@
import { strictEqual } from "node:assert/strict";
import { describe, test } from "node:test";
import { fedifyHook } from "./mod.ts";
import type { RequestEvent } from "@sveltejs/kit";

interface MockRequestEvent {
request: Request;
}

interface MockHookParams {
event: MockRequestEvent;
resolve: (event: MockRequestEvent) => Promise<Response>;
}

interface MockFederation<T> {
interface MockFederation {
fetch(request: Request, options: unknown): Promise<Response>;
}

describe("fedifyHook", () => {
test("creates hook handler function", () => {
const mockFederation: MockFederation<undefined> = {
const mockFederation = {
fetch: () => Promise.resolve(new Response("OK")),
};

const createContextData = () => undefined;

const hookHandler = fedifyHook(mockFederation as never, createContextData);
const hookHandler = fedifyHook(mockFederation as never);
strictEqual(typeof hookHandler, "function");
});

test("calls federation.fetch with correct parameters", async () => {
let capturedRequest: Request | undefined;
let capturedOptions: unknown;

const mockFederation: MockFederation<string> = {
const mockFederation: MockFederation = {
fetch: (request, options) => {
capturedRequest = request;
capturedOptions = options;
Expand All @@ -44,7 +34,7 @@ describe("fedifyHook", () => {
const hookHandler = fedifyHook(mockFederation as never, createContextData);

const mockRequest = new Request("https://example.com/test");
const mockEvent: MockRequestEvent = { request: mockRequest };
const mockEvent: RequestEvent = { request: mockRequest } as RequestEvent;
const mockResolve = () =>
Promise.resolve(new Response("SvelteKit response"));

Expand All @@ -64,7 +54,7 @@ describe("fedifyHook", () => {
test("handles async context data creation", async () => {
let capturedContextData: unknown;

const mockFederation: MockFederation<string> = {
const mockFederation: MockFederation = {
fetch: (_request, options) => {
capturedContextData = (options as { contextData: string }).contextData;
return Promise.resolve(new Response("OK"));
Expand All @@ -79,7 +69,7 @@ describe("fedifyHook", () => {
const hookHandler = fedifyHook(mockFederation as never, createContextData);

const mockRequest = new Request("https://example.com/test");
const mockEvent: MockRequestEvent = { request: mockRequest };
const mockEvent: RequestEvent = { request: mockRequest } as RequestEvent;
const mockResolve = () =>
Promise.resolve(new Response("SvelteKit response"));

Expand Down
20 changes: 6 additions & 14 deletions packages/sveltekit/src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@ import type {
Federation,
FederationFetchOptions,
} from "@fedify/fedify/federation";

type RequestEvent = {
request: Request;
};

type HookParams = {
event: RequestEvent;
resolve: (event: RequestEvent) => Promise<Response>;
};
import type { Handle, RequestEvent } from "@sveltejs/kit";

/**
* Create a SvelteKit hook handler to integrate with the {@link Federation}
Expand All @@ -32,7 +24,7 @@ type HookParams = {
* ``` typescript
* import { federation } from "./federation"; // Import the `Federation` object
*
* export const handle = fedifyHook(federation, () => undefined);
* export const handle = fedifyHook(federation);
* ```
*
* @template TContextData A type of the context data for the {@link Federation}
Expand All @@ -47,9 +39,9 @@ export function fedifyHook<TContextData>(
federation: Federation<TContextData>,
createContextData: (
event: RequestEvent,
) => TContextData | Promise<TContextData>,
): (params: HookParams) => Promise<Response> {
return async ({ event, resolve }: HookParams) => {
) => TContextData | Promise<TContextData> = () => undefined as TContextData,
): Handle {
return async ({ event, resolve }) => {
return await federation.fetch(event.request, {
contextData: await createContextData(event),
...integrateFetchOptions({ event, resolve }),
Expand All @@ -58,7 +50,7 @@ export function fedifyHook<TContextData>(
}

function integrateFetchOptions(
{ event, resolve }: HookParams,
{ event, resolve }: Parameters<Handle>[0],
): Omit<FederationFetchOptions<void>, "contextData"> {
return {
async onNotFound(): Promise<Response> {
Expand Down
Loading