@fedify/sveltekit 1.9.0-dev.1461

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright 2024–2025 Hong Minhee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,66 @@
1
+ <!-- deno-fmt-ignore-file -->
2
+
3
+ @fedify/sveltekit: Integrate Fedify with SvelteKit
4
+ ==================================================
5
+
6
+ [![JSR][JSR badge]][JSR]
7
+ [![npm][npm badge]][npm]
8
+ [![Follow @fedify@hollo.social][@fedify@hollo.social badge]][@fedify@hollo.social]
9
+
10
+ *This package is available since Fedify 1.9.0.*
11
+
12
+ This package provides a simple way to integrate [Fedify] with [SvelteKit].
13
+
14
+ The integration code looks like this:
15
+
16
+ ~~~~ typescript
17
+ import { createFederation } from "@fedify/fedify";
18
+ import { fedifyHook } from "@fedify/sveltekit";
19
+
20
+ const federation = createFederation<string>({
21
+ // Omitted for brevity; see the related section for details.
22
+ });
23
+
24
+ // This is the entry point to the Fedify hook from the SvelteKit framework:
25
+ export const handle = fedifyHook(federation, (req) => "context data");
26
+ ~~~~
27
+
28
+ Put the above code in your *hooks.server.ts* file.
29
+
30
+ How it works
31
+ ------------
32
+
33
+ Fedify behaves as a hook handler that wraps around the SvelteKit request handler.
34
+ The hook intercepts the incoming HTTP requests and dispatches them to
35
+ the appropriate handler based on the request path and the `Accept` header
36
+ (i.e., content negotiation). This architecture allows Fedify and your SvelteKit
37
+ application to coexist in the same domain and port.
38
+
39
+ For example, if you make a request to */.well-known/webfinger* Fedify will
40
+ handle the request by itself, but if you make a request to */users/alice*
41
+ (assuming your SvelteKit app has a handler for `/users/[handle]`) with `Accept:
42
+ text/html` header, Fedify will dispatch the request to the SvelteKit app's
43
+ appropriate handler for `/users/[handle]`. Or if you define an actor dispatcher
44
+ for `/users/{handle}` in Fedify, and the request is made with `Accept:
45
+ application/activity+json` header, Fedify will dispatch the request to the
46
+ appropriate actor dispatcher.
47
+
48
+ Installation
49
+ ------------
50
+
51
+ ~~~~ sh
52
+ deno add jsr:@fedify/sveltekit # Deno
53
+ npm add @fedify/sveltekit # npm
54
+ pnpm add @fedify/sveltekit # pnpm
55
+ yarn add @fedify/sveltekit # Yarn
56
+ bun add @fedify/sveltekit # Bun
57
+ ~~~~
58
+
59
+ [JSR]: https://jsr.io/@fedify/sveltekit
60
+ [JSR badge]: https://jsr.io/badges/@fedify/sveltekit
61
+ [npm]: https://www.npmjs.com/package/@fedify/sveltekit
62
+ [npm badge]: https://img.shields.io/npm/v/@fedify/sveltekit?logo=npm
63
+ [@fedify@hollo.social badge]: https://fedi-badge.deno.dev/@fedify@hollo.social/followers.svg
64
+ [@fedify@hollo.social]: https://hollo.social/@fedify
65
+ [Fedify]: https://fedify.dev/
66
+ [SvelteKit]: https://kit.svelte.dev/
package/dist/mod.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ import { Federation } from "@fedify/fedify/federation";
2
+
3
+ //#region src/mod.d.ts
4
+
5
+ type RequestEvent = {
6
+ request: Request;
7
+ };
8
+ type HookParams = {
9
+ event: RequestEvent;
10
+ resolve: (event: RequestEvent) => Promise<Response>;
11
+ };
12
+ /**
13
+ * Create a SvelteKit hook handler to integrate with the {@link Federation}
14
+ * object.
15
+ *
16
+ * @example hooks.server.ts
17
+ * ``` typescript
18
+ * import { federation } from "./federation"; // Import the `Federation` object
19
+ *
20
+ * export const handle = fedifyHook(federation, () => undefined);
21
+ * ```
22
+ *
23
+ * @template TContextData A type of the context data for the {@link Federation}
24
+ * object.
25
+ * @param federation A {@link Federation} object to integrate with SvelteKit.
26
+ * @param createContextData A function to create a context data for the
27
+ * {@link Federation} object.
28
+ * @returns A SvelteKit hook handler.
29
+ * @since 1.9.0
30
+ */
31
+ declare function fedifyHook<TContextData>(federation: Federation<TContextData>, createContextData: (event: RequestEvent) => TContextData | Promise<TContextData>): (params: HookParams) => Promise<Response>;
32
+ //#endregion
33
+ export { fedifyHook };
package/dist/mod.js ADDED
@@ -0,0 +1,52 @@
1
+ //#region src/mod.ts
2
+ /**
3
+ * Create a SvelteKit hook handler to integrate with the {@link Federation}
4
+ * object.
5
+ *
6
+ * @example hooks.server.ts
7
+ * ``` typescript
8
+ * import { federation } from "./federation"; // Import the `Federation` object
9
+ *
10
+ * export const handle = fedifyHook(federation, () => undefined);
11
+ * ```
12
+ *
13
+ * @template TContextData A type of the context data for the {@link Federation}
14
+ * object.
15
+ * @param federation A {@link Federation} object to integrate with SvelteKit.
16
+ * @param createContextData A function to create a context data for the
17
+ * {@link Federation} object.
18
+ * @returns A SvelteKit hook handler.
19
+ * @since 1.9.0
20
+ */
21
+ function fedifyHook(federation, createContextData) {
22
+ return async ({ event, resolve }) => {
23
+ return await federation.fetch(event.request, {
24
+ contextData: await createContextData(event),
25
+ ...integrateFetchOptions({
26
+ event,
27
+ resolve
28
+ })
29
+ });
30
+ };
31
+ }
32
+ function integrateFetchOptions({ event, resolve }) {
33
+ return {
34
+ async onNotFound() {
35
+ return await resolve(event);
36
+ },
37
+ async onNotAcceptable() {
38
+ const res = await resolve(event);
39
+ if (res.status !== 404) return res;
40
+ return new Response("Not acceptable", {
41
+ status: 406,
42
+ headers: {
43
+ "Content-Type": "text/plain",
44
+ Vary: "Accept"
45
+ }
46
+ });
47
+ }
48
+ };
49
+ }
50
+
51
+ //#endregion
52
+ export { fedifyHook };
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@fedify/sveltekit",
3
+ "version": "1.9.0-dev.1461+7957aafe",
4
+ "description": "Integrate Fedify with SvelteKit",
5
+ "keywords": [
6
+ "Fedify",
7
+ "ActivityPub",
8
+ "Fediverse",
9
+ "SvelteKit",
10
+ "Svelte"
11
+ ],
12
+ "author": {
13
+ "name": "Hong Minhee",
14
+ "email": "hong@minhee.org",
15
+ "url": "https://hongminhee.org/"
16
+ },
17
+ "homepage": "https://fedify.dev/",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/fedify-dev/fedify.git",
21
+ "directory": "packages/sveltekit"
22
+ },
23
+ "license": "MIT",
24
+ "bugs": {
25
+ "url": "https://github.com/fedify-dev/fedify/issues"
26
+ },
27
+ "funding": [
28
+ "https://opencollective.com/fedify",
29
+ "https://github.com/sponsors/dahlia"
30
+ ],
31
+ "type": "module",
32
+ "main": "./dist/mod.js",
33
+ "module": "./dist/mod.js",
34
+ "types": "./dist/mod.d.ts",
35
+ "exports": {
36
+ ".": {
37
+ "require": {
38
+ "types": "./dist/mod.d.ts",
39
+ "import": "./dist/mod.js",
40
+ "default": "./dist/mod.js"
41
+ },
42
+ "import": {
43
+ "types": "./dist/mod.d.ts",
44
+ "import": "./dist/mod.js",
45
+ "default": "./dist/mod.js"
46
+ }
47
+ },
48
+ "./package.json": "./package.json"
49
+ },
50
+ "files": [
51
+ "dist/",
52
+ "package.json"
53
+ ],
54
+ "peerDependencies": {
55
+ "@sveltejs/kit": "^2.0.0",
56
+ "@fedify/fedify": "1.9.0-dev.1461+7957aafe"
57
+ },
58
+ "devDependencies": {
59
+ "tsdown": "^0.12.9",
60
+ "typescript": "^5.9.2"
61
+ },
62
+ "scripts": {
63
+ "build": "tsdown",
64
+ "prepublish": "tsdown",
65
+ "test": "deno task codegen && tsdown && cd dist/ && node --test"
66
+ }
67
+ }