Unverified Commit 354615a2 authored by Hong Minhee's avatar Hong Minhee
Browse files

Merge pull request #309 from malkoG/main

parents 7c639ddb 10ed11cd
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -413,6 +413,7 @@ jobs:
          | @fedify/amqp     | ${{ steps.versioning.outputs.version }} | [JSR][jsr:@fedify/amqp]     | [npm][npm:@fedify/amqp]     |
          | @fedify/express  | ${{ steps.versioning.outputs.version }} | [JSR][jsr:@fedify/express]  | [npm][npm:@fedify/express]  |
          | @fedify/h3       | ${{ steps.versioning.outputs.version }} | [JSR][jsr:@fedify/h3]       | [npm][npm:@fedify/h3]       |
          | @fedify/nestjs   | ${{ steps.versioning.outputs.version }} |                             | [npm][npm:@fedify/nestjs]   |
          | @fedify/postgres | ${{ steps.versioning.outputs.version }} | [JSR][jsr:@fedify/postgres] | [npm][npm:@fedify/postgres] |
          | @fedify/redis    | ${{ steps.versioning.outputs.version }} | [JSR][jsr:@fedify/redis]    | [npm][npm:@fedify/redis]    |

@@ -425,6 +426,7 @@ jobs:
          [npm:@fedify/express]: https://www.npmjs.com/package/@fedify/express/v/${{ steps.versioning.outputs.short_version }}
          [jsr:@fedify/h3]: https://jsr.io/@fedify/h3@${{ steps.versioning.outputs.version }}
          [npm:@fedify/h3]: https://www.npmjs.com/package/@fedify/h3/v/${{ steps.versioning.outputs.short_version }}
          [npm:@fedify/nestjs]: https://www.npmjs.com/package/@fedify/nestjs/v/${{ steps.versioning.outputs.short_version }}
          [jsr:@fedify/postgres]: https://jsr.io/@fedify/postgres@${{ steps.versioning.outputs.version }}
          [npm:@fedify/postgres]: https://www.npmjs.com/package/@fedify/postgres/v/${{ steps.versioning.outputs.short_version }}
          [jsr:@fedify/redis]: https://jsr.io/@fedify/redis@${{ steps.versioning.outputs.version }}
+8 −0
Original line number Diff line number Diff line
@@ -80,11 +80,18 @@ To be released.
     render the favicon in terminal emulators that do not support 24-bit
     colors.  [[#168], [#282], [#304] by Hyeonseo Kim]

  -  Supported NestJS integration with the `@fedify/nestjs` package.
     [[#269], [#309] by Jaeyeol Lee]

     -  Added `@fedify/nestjs` package.
     -  Added `FedifyModule` for integrating Fedify into NestJS applications.

[#168]: https://github.com/fedify-dev/fedify/issues/168
[#248]: https://github.com/fedify-dev/fedify/issues/248
[#260]: https://github.com/fedify-dev/fedify/issues/260
[#262]: https://github.com/fedify-dev/fedify/issues/262
[#263]: https://github.com/fedify-dev/fedify/issues/263
[#269]: https://github.com/fedify-dev/fedify/issues/269
[#278]: https://github.com/fedify-dev/fedify/pull/278
[#281]: https://github.com/fedify-dev/fedify/pull/281
[#282]: https://github.com/fedify-dev/fedify/pull/282
@@ -92,6 +99,7 @@ To be released.
[#298]: https://github.com/fedify-dev/fedify/pull/298
[#300]: https://github.com/fedify-dev/fedify/pull/300
[#304]: https://github.com/fedify-dev/fedify/issues/304
[#309]: https://github.com/fedify-dev/fedify/pull/309


Version 1.7.5
+2 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
    "./amqp",
    "./express",
    "./h3",
    "./nestjs",
    "./postgres",
    "./redis",
    "./examples/blog",
@@ -23,6 +24,7 @@
    "@std/yaml": "jsr:@std/yaml@^1.0.8",
    "amqplib": "npm:amqplib@^0.10.8",
    "h3": "npm:h3@^1.15.0",
    "@nestjs/common": "npm:@nestjs/common@^11.0.1",
    "ioredis": "npm:ioredis@^5.6.1",
    "json-preserve-indent": "npm:json-preserve-indent@^1.1.3",
    "postgres": "npm:postgres@^3.4.7",
+2 −0
Original line number Diff line number Diff line
@@ -228,6 +228,8 @@ export default withMermaid(defineConfig({
            moduleResolution: ModuleResolutionKind.Bundler,
            module: ModuleKind.ESNext,
            target: ScriptTarget.ESNext,
            experimentalDecorators: true, // For @fedify/nestjs
            emitDecoratorMetadata: true, // For @fedify/nestjs
            lib: ["dom", "dom.iterable", "esnext"],
            types: [
              "dom",
+163 −0
Original line number Diff line number Diff line
@@ -219,6 +219,169 @@ export const handle = fedifyHook(federation, (req) => "context data");
[Svelte]: https://svelte.dev/


NestJS
------

*This API is available since Fedify 1.8.0.*

> [!IMPORTANT]
> In a CommonJS-based NestJS project, this ESM-only module requires setting
> `NODE_OPTIONS=--experimental-require-module` at runtime.

[NestJS] is a modular, versatile, and scalable framework for building efficient,
reliable, and scalable server-side applications with Node.js and TypeScript.
The [@fedify/nestjs] package provides a middleware to integrate Fedify with
NestJS:

~~~~ typescript [modules/federation/federation.service.ts] twoslash
import { Injectable, Inject, OnModuleInit } from '@nestjs/common';
import {
  FEDIFY_FEDERATION,
} from '@fedify/nestjs';
import { Federation, parseSemVer } from '@fedify/fedify';

@Injectable()
export class FederationService implements OnModuleInit {
  private initialized = false;

  constructor(
    @Inject(FEDIFY_FEDERATION) private federation: Federation<unknown>,
  ) { }

  async onModuleInit() {
    if (!this.initialized) {
      await this.initialize();
      this.initialized = true;
    }
  }

  async initialize() {
    this.federation.setNodeInfoDispatcher("/nodeinfo/2.1", async (context) => {
      return {
        software: {
          name: "Fedify NestJS sample",
          version: parseSemVer("0.0.1")
        },
        protocols: ["activitypub"],
        usage: {
          users: {
            total: 0,
            activeHalfyear: 0,
            activeMonth: 0,
            activeDay: 0,
          },
          localPosts: 0,
          localComments: 0,
        },
      }
    });
  }
}
~~~~

~~~~ typescript [modules/federation/federation.module.ts] twoslash
// @noErrors: 2395 2307
import { Injectable, Inject, OnModuleInit } from '@nestjs/common';
import {
  FEDIFY_FEDERATION,
} from '@fedify/nestjs';
import { Federation } from '@fedify/fedify';

@Injectable()
export class FederationService implements OnModuleInit {
  private initialized = false;

  constructor(
    @Inject(FEDIFY_FEDERATION) private federation: Federation<unknown>,
  ) { }

  async onModuleInit() {
    if (!this.initialized) {
      await this.initialize();
      this.initialized = true;
    }
  }

  async initialize() {
  }
}
// ---cut-before---
import { Module } from '@nestjs/common';
import { FederationService } from './federation.service';

@Module({
  providers: [FederationService],
  exports: [FederationService],
})
export class FederationModule {}
~~~~

~~~~ typescript [app.module.ts] twoslash
// @noErrors: 2307
// ---cut-before---
import {
  Inject,
  MiddlewareConsumer,
  Module,
  NestModule,
  RequestMethod,
} from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { DatabaseModule } from './database/database.module';
import { FederationModule } from './modules/federation/federation.module';
import { InProcessMessageQueue, MemoryKvStore, Federation } from '@fedify/fedify';
import process from 'node:process';

import {
  FEDIFY_FEDERATION,
  FedifyModule,
  integrateFederation,
} from '@fedify/nestjs';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    DatabaseModule,
    FedifyModule.forRoot({
      kv: new MemoryKvStore(),
      queue: new InProcessMessageQueue(),
      origin: process.env.FEDERATION_ORIGIN || 'http://localhost:3000',
    }),
    FederationModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule implements NestModule {
  constructor(
    @Inject(FEDIFY_FEDERATION) private federation: Federation<unknown>,
  ) { }

  configure(consumer: MiddlewareConsumer) {
    const fedifyMiddleware = integrateFederation(
      this.federation,
      async (req, res) => {
        return {
          request: req,
          response: res,
          url: new URL(req.url, process.env.FEDERATION_ORIGIN),
        };
      },
    );

    // Apply middleware to all routes except auth endpoints
    consumer.apply(fedifyMiddleware).forRoutes({ path: '*', method: RequestMethod.ALL });
  }
}
~~~~

[NestJS]: https://nestjs.com/


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

Loading