Unverified Commit a6eb8599 authored by Hong Minhee's avatar Hong Minhee
Browse files

Adjust style and changelog, and add docs

parent 5cb637f0
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -61,6 +61,11 @@ To be released.

     -  Added `CreateFederationOptions.tracerProvider` option.

 -  Added `@fedify/fedify/x/sveltekit` module for integrating with [SvelteKit]
    hook.  [[#171], [#183] by Jiyu Park]

     -  Added `fedifyHook()` function.

 -  The scaffold project generated by `fedify init` command now enables
    tracing data into log messages.

@@ -68,10 +73,14 @@ To be released.
    [[#173], [#186] by PGD]


[SvelteKit]: https://kit.svelte.dev/
[#162]: https://github.com/dahlia/fedify/issues/162
[#171]: https://github.com/dahlia/fedify/issues/171
[#173]: https://github.com/dahlia/fedify/issues/173
[#183]: https://github.com/dahlia/fedify/pull/183
[#186]: https://github.com/dahlia/fedify/pull/186


Version 1.2.8
-------------

@@ -87,7 +96,6 @@ Released on November 23, 2024.
[#177]: https://github.com/dahlia/fedify/issues/177
[#181]: https://github.com/dahlia/fedify/pull/181

 -  Support SvelteKit integrations. [[#183]]

Version 1.2.7
-------------
@@ -2557,4 +2565,4 @@ Version 0.1.0

Initial release.  Released on March 8, 2024.

<!-- cSpell: ignore Dogeon Fabien Wressell Emelia Hana Heesun Kyunghee -->
<!-- cSpell: ignore Dogeon Fabien Wressell Emelia Hana Heesun Kyunghee Jiyu -->
+26 −0
Original line number Diff line number Diff line
@@ -191,6 +191,32 @@ export const handler = integrateHandler(
[Fresh]: https://fresh.deno.dev/


SvelteKit
---------

*This API is available since Fedify 1.3.0.*

[SvelteKit] is a framework for building web applications with [Svelte].  Fedify
has the `@fedify/fedify/x/sveltekit` module that provides a hook handler to
integrate Fedify with SvelteKit.  Put the following code in your
*hooks.server.ts* file:

~~~~ typescript
import { createFederation } from "@fedify/fedify";
import { fedifyHook } from "@fedify/fedify/x/sveltekit";

const federation = createFederation<string>({
  // Omitted for brevity; see the related section for details.
});

// This is the entry point to the Fedify hook from the SvelteKit framework:
export const handle = fedifyHook(federation, (req) => "context data");
~~~~

[SvelteKit]: https://kit.svelte.dev/
[Svelte]: https://svelte.dev/


Custom middleware
-----------------

+37 −36
Original line number Diff line number Diff line
/**
 * Fedify with SvelteKit
 * ================
 * =====================
 *
 * This module provides a [SvelteKit] middleware to integrate with the Fedify.
 * This module provides a [SvelteKit] hook to integrate with the Fedify.
 *
 * [SvelteKit]: https://svelte.dev/
 * [SvelteKit]: https://kit.svelte.dev/
 *
 * @module
 * @since 1.3.0
 */

import type {
@@ -24,11 +25,12 @@ type HookParams = {
};

/**
 * Create a SvelteKit hook handler to integrate with the {@link Federation} object.
 * Create a SvelteKit hook handler to integrate with the {@link Federation}
 * object.
 *
 * @example hooks.server.ts
 * ``` typescript
 * import { federation } from "federation.ts"; // Import the `Federation` object
 * import { federation } from "./federation"; // Import the `Federation` object
 *
 * export const handle = fedifyHook(federation, () => undefined);
 * ```
@@ -39,28 +41,26 @@ type HookParams = {
 * @param createContextData A function to create a context data for the
 *                          {@link Federation} object.
 * @returns A SvelteKit hook handler.
 * @since 1.3.0
 */
export const fedifyHook = <TContextData>(
export function fedifyHook<TContextData>(
  federation: Federation<TContextData>,
  createContextData: (
    event: RequestEvent,
  ) => TContextData | Promise<TContextData>,
) => {
): (params: HookParams) => Promise<Response> {
  return async ({ event, resolve }: HookParams) => {
    return await federation.fetch(event.request, {
      contextData: await createContextData(event),
      ...integrateFetchOptions({ event, resolve }),
    });
  };
};
}

const integrateFetchOptions = (
function integrateFetchOptions(
  { event, resolve }: HookParams,
): Omit<FederationFetchOptions<void>, "contextData"> => ({
  // If the `federation` object finds a request not responsible for it
  // (i.e., not a federation-related request), it will call the `resolve`
  // provided by the SvelteKit framework to continue the request handling
  // by the SvelteKit:
): Omit<FederationFetchOptions<void>, "contextData"> {
  return {
    async onNotFound(): Promise<Response> {
      return await resolve(event);
    },
@@ -83,4 +83,5 @@ const integrateFetchOptions = (
        },
      });
    },
});
  };
}