@fedify/nestjs 1.8.0-pr.309.1062

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,133 @@
1
+ <!-- deno-fmt-ignore-file -->
2
+
3
+ @fedify/nestjs: Integrate Fedify with NestJS
4
+ ==============================================
5
+
6
+ [![npm][npm badge]][npm]
7
+ [![Matrix][Matrix badge]][Matrix]
8
+ [![Follow @fedify@hollo.social][@fedify@hollo.social badge]][@fedify@hollo.social]
9
+
10
+ This package provides a simple way to integrate [Fedify] with [NestJS].
11
+
12
+ The integration code looks like this:
13
+
14
+ ~~~~ typescript
15
+ // --- modules/federation/federation.service ---
16
+
17
+ import { Injectable, Inject, OnModuleInit } from '@nestjs/common';
18
+ import {
19
+ FEDIFY_FEDERATION,
20
+ } from '@fedify/nestjs';
21
+ import { Federation, parseSemVer } from '@fedify/fedify';
22
+
23
+ @Injectable()
24
+ export class FederationService implements OnModuleInit {
25
+ private initialized = false;
26
+
27
+ constructor(
28
+ @Inject(FEDIFY_FEDERATION) private federation: Federation<unknown>,
29
+ ) { }
30
+
31
+ async onModuleInit() {
32
+ if (!this.initialized) {
33
+ await this.initialize();
34
+ this.initialized = true;
35
+ }
36
+ }
37
+
38
+ async initialize() {
39
+ this.federation.setNodeInfoDispatcher(async (context) => {
40
+ return {
41
+ software: {
42
+ name: "Fedify NestJS sample",
43
+ version: parseSemVer("0.0.1")
44
+ }
45
+ }
46
+ });
47
+ }
48
+ }
49
+
50
+
51
+ // --- modules/federation/federation.module.ts ---
52
+
53
+ import { Module } from '@nestjs/common';
54
+ import { FederationService } from './federation.service';
55
+
56
+ @Module({
57
+ providers: [FederationService],
58
+ exports: [FederationService],
59
+ })
60
+ export class FederationModule {}
61
+
62
+
63
+ // --- main.module.ts ---
64
+ import {
65
+ Inject,
66
+ MiddlewareConsumer,
67
+ Module,
68
+ NestModule,
69
+ RequestMethod,
70
+ } from '@nestjs/common';
71
+ import { AppController } from './app.controller';
72
+ import { AppService } from './app.service';
73
+ import { DatabaseModule } from './database/database.module';
74
+ import { FederationModule } from './modules/federation/federation.module';
75
+ import { InProcessMessageQueue, MemoryKvStore, Federation } from '@fedify/fedify';
76
+
77
+ import {
78
+ FEDIFY_FEDERATION,
79
+ FedifyModule,
80
+ integrateFederation,
81
+ } from '@fedify/nestjs';
82
+
83
+ @Module({
84
+ imports: [
85
+ ConfigModule.forRoot({
86
+ isGlobal: true,
87
+ }),
88
+ DatabaseModule,
89
+ FedifyModule.forRoot({
90
+ // Allow localhost URLs in development
91
+ kv: new MemoryKvStore(),
92
+ queue: new InProcessMessageQueue(),
93
+ origin: process.env.FEDERATION_ORIGIN || 'http://localhost:3000',
94
+ }),
95
+ FederationModule,
96
+ ],
97
+ controllers: [AppController],
98
+ providers: [AppService],
99
+ })
100
+
101
+ export class AppModule implements NestModule {
102
+ constructor(
103
+ @Inject(FEDIFY_FEDERATION) private federation: Federation<unknown>,
104
+ ) { }
105
+
106
+ configure(consumer: MiddlewareConsumer) {
107
+ const fedifyMiddleware = integrateFederation(
108
+ this.federation,
109
+ async (req, res) => {
110
+ // Create rich context with database access and request info
111
+ return {
112
+ request: req,
113
+ response: res,
114
+ url: new URL(req.url, `${req.protocol}://${req.get('host')}`),
115
+ };
116
+ },
117
+ );
118
+
119
+ // Apply middleware to all routes except auth endpoints
120
+ consumer.apply(fedifyMiddleware)
121
+ }
122
+ }
123
+
124
+ ~~~~
125
+
126
+ [npm]: https://www.npmjs.com/package/@fedify/express
127
+ [npm badge]: https://img.shields.io/npm/v/@fedify/express?logo=npm
128
+ [Matrix]: https://matrix.to/#/#fedify:matrix.org
129
+ [Matrix badge]: https://img.shields.io/matrix/fedify%3Amatrix.org
130
+ [@fedify@hollo.social badge]: https://fedi-badge.deno.dev/@fedify@hollo.social/followers.svg
131
+ [@fedify@hollo.social]: https://hollo.social/@fedify
132
+ [Fedify]: https://fedify.dev/
133
+ [Express]: https://expressjs.com/
@@ -0,0 +1,17 @@
1
+ import { DynamicModule, NestMiddleware, Type } from "@nestjs/common";
2
+ import { Federation, FederationOptions } from "@fedify/fedify";
3
+ import { Request, Response } from "express";
4
+
5
+ //#region fedify.module.d.ts
6
+ declare class FedifyModule {
7
+ static forRoot(options: FederationOptions<unknown>): DynamicModule;
8
+ }
9
+ //#endregion
10
+ //#region fedify.constants.d.ts
11
+ declare const FEDIFY_FEDERATION = "FEDIFY_FEDERATION";
12
+ //#endregion
13
+ //#region fedify.middleware.d.ts
14
+ type ContextDataFactory<TContextData> = (req: Request, res: Response) => TContextData | Promise<TContextData>;
15
+ declare function integrateFederation<TContextData>(federation: Federation<unknown>, contextDataFactory: ContextDataFactory<TContextData>): Type<NestMiddleware>;
16
+ //#endregion
17
+ export { ContextDataFactory, FEDIFY_FEDERATION, FedifyModule, integrateFederation };
package/dist/index.js ADDED
@@ -0,0 +1,65 @@
1
+ import { Injectable, Module } from "@nestjs/common";
2
+ import { MemoryKvStore, createFederation } from "@fedify/fedify";
3
+ import { Request } from "express";
4
+
5
+ //#region fedify.constants.ts
6
+ const FEDIFY_FEDERATION = "FEDIFY_FEDERATION";
7
+
8
+ //#endregion
9
+ //#region fedify.module.ts
10
+ var FedifyModule = @Module({}) class FedifyModule {
11
+ static forRoot(options) {
12
+ const providers = [{
13
+ provide: FEDIFY_FEDERATION,
14
+ useFactory: () => {
15
+ const federationOptions = { ...options };
16
+ federationOptions.kv = options.kv || new MemoryKvStore();
17
+ const federation = createFederation(federationOptions);
18
+ return federation;
19
+ }
20
+ }];
21
+ return {
22
+ module: FedifyModule,
23
+ providers,
24
+ exports: [FEDIFY_FEDERATION],
25
+ global: true
26
+ };
27
+ }
28
+ };
29
+
30
+ //#endregion
31
+ //#region fedify.middleware.ts
32
+ function integrateFederation(federation, contextDataFactory) {
33
+ @Injectable() class FedifyIntegrationMiddleware {
34
+ async use(req, res, next) {
35
+ try {
36
+ const contextData = await contextDataFactory(req, res);
37
+ const url = new URL(req.url, `${req.protocol}://${req.get("host")}`);
38
+ const headers = new Headers();
39
+ Object.entries(req.headers).forEach(([key, value]) => {
40
+ if (value) headers.set(key, Array.isArray(value) ? value.join(", ") : value.toString());
41
+ });
42
+ const webRequest = new Request(url.toString(), {
43
+ method: req.method,
44
+ headers,
45
+ body: ["GET", "HEAD"].includes(req.method) ? void 0 : JSON.stringify(req.body)
46
+ });
47
+ const response = await federation.fetch(webRequest, { contextData });
48
+ if (response) {
49
+ response.headers.forEach((value, key) => {
50
+ res.setHeader(key, value);
51
+ });
52
+ res.status(response.status);
53
+ const body = await response.text();
54
+ res.send(body);
55
+ } else next();
56
+ } catch (error) {
57
+ next(error);
58
+ }
59
+ }
60
+ }
61
+ return FedifyIntegrationMiddleware;
62
+ }
63
+
64
+ //#endregion
65
+ export { FEDIFY_FEDERATION, FedifyModule, integrateFederation };
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@fedify/nestjs",
3
+ "version": "1.8.0-pr.309.1062+7bc9cc4d",
4
+ "description": "Integrate Fedify with Nest.js",
5
+ "keywords": [
6
+ "Fedify",
7
+ "Nest",
8
+ "Nest.js"
9
+ ],
10
+ "author": {
11
+ "name": "Jaeyeol Lee",
12
+ "email": "jaeyeol.lee@hey.com",
13
+ "url": "https://kodingwarrior.github.io/"
14
+ },
15
+ "homepage": "https://fedify.dev/",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/fedify-dev/fedify.git",
19
+ "directory": "nestjs"
20
+ },
21
+ "license": "MIT",
22
+ "bugs": {
23
+ "url": "https://github.com/fedify-dev/fedify/issues"
24
+ },
25
+ "funding": [
26
+ "https://opencollective.com/fedify",
27
+ "https://github.com/sponsors/dahlia"
28
+ ],
29
+ "type": "module",
30
+ "module": "./dist/index.js",
31
+ "types": "./dist/index.d.ts",
32
+ "exports": {
33
+ ".": {
34
+ "import": {
35
+ "types": "./dist/index.d.ts",
36
+ "import": "./dist/index.js",
37
+ "default": "./dist/index.js"
38
+ }
39
+ },
40
+ "./package.json": "./package.json"
41
+ },
42
+ "files": [
43
+ "dist/",
44
+ "package.json"
45
+ ],
46
+ "peerDependencies": {
47
+ "@nestjs/common": "^11.0.1",
48
+ "express": "^4.0.0",
49
+ "@fedify/fedify": "1.8.0-pr.309.1062+7bc9cc4d"
50
+ },
51
+ "devDependencies": {
52
+ "@types/express": "^4.0.0",
53
+ "@types/node": "^22.16.0",
54
+ "tsdown": "^0.12.9",
55
+ "typescript": "^5.8.3"
56
+ },
57
+ "scripts": {
58
+ "build": "tsdown",
59
+ "prepublish": "tsdown"
60
+ }
61
+ }