@tsed/platform-fastify 8.24.0 → 8.24.2

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.
@@ -6,7 +6,7 @@ import fastifyStatics from "@fastify/static";
6
6
  import { isFunction, isString } from "@tsed/core";
7
7
  import { constant, inject, logger, runInContext } from "@tsed/di";
8
8
  import { NotFound } from "@tsed/exceptions";
9
- import { $alter } from "@tsed/hooks";
9
+ import { $alter, $asyncEmit } from "@tsed/hooks";
10
10
  import { PlatformExceptions } from "@tsed/platform-exceptions";
11
11
  import { adapter, createContext, createServer, PlatformAdapter, PlatformBuilder, PlatformRequest, PlatformResponse } from "@tsed/platform-http";
12
12
  import { PlatformHandlerType } from "@tsed/platform-router";
@@ -14,6 +14,7 @@ import Fastify from "fastify";
14
14
  import { PlatformFastifyRequest } from "../services/PlatformFastifyRequest.js";
15
15
  import { PlatformFastifyResponse } from "../services/PlatformFastifyResponse.js";
16
16
  import { convertPath } from "../utils/convertPath.js";
17
+ import { toPrefix } from "../utils/toPrefix.js";
17
18
  function callNext(next, metadata, $ctx) {
18
19
  if (metadata.type !== PlatformHandlerType.RESPONSE_FN) {
19
20
  return next && $ctx.error && !$ctx.isDone() ? next($ctx.error) : next();
@@ -47,10 +48,10 @@ export class PlatformFastify extends PlatformAdapter {
47
48
  adapter: PlatformFastify
48
49
  });
49
50
  }
50
- mapLayers(layers) {
51
+ async mapLayers(layers) {
51
52
  const { app } = this;
52
53
  const rawApp = app.getApp();
53
- layers.forEach((layer) => {
54
+ for (const layer of layers) {
54
55
  const { path, wildcard } = convertPath(layer.path);
55
56
  const handlers = layer.getArgs(false);
56
57
  switch (layer.method) {
@@ -58,10 +59,10 @@ export class PlatformFastify extends PlatformAdapter {
58
59
  if (rawApp.use) {
59
60
  rawApp.use(path, handlers);
60
61
  }
61
- return;
62
+ continue;
62
63
  case "statics":
63
- this.statics(path, layer.opts);
64
- return;
64
+ await this.statics(path, layer.opts);
65
+ continue;
65
66
  }
66
67
  try {
67
68
  rawApp.route({
@@ -79,7 +80,7 @@ export class PlatformFastify extends PlatformAdapter {
79
80
  error_message: er.message
80
81
  });
81
82
  }
82
- });
83
+ }
83
84
  }
84
85
  mapHandler(handler, metadata) {
85
86
  if (metadata.isRawMiddleware()) {
@@ -204,14 +205,15 @@ export class PlatformFastify extends PlatformAdapter {
204
205
  bodyParser(type, opts) {
205
206
  return null;
206
207
  }
207
- statics(endpoint, options) {
208
- this.app.getApp().register(fastifyStatics, {
209
- root: options.root,
210
- prefix: endpoint,
208
+ async statics(endpoint, options) {
209
+ options = {
210
+ prefix: toPrefix(endpoint),
211
+ ...options,
211
212
  decorateReply: !this.staticsDecorated
212
- });
213
+ };
214
+ await this.app.getApp().register(fastifyStatics, options);
213
215
  this.staticsDecorated = true;
214
- return null;
216
+ await $asyncEmit("$staticsMounted", [endpoint, options]);
215
217
  }
216
218
  compose(layer, wildcard) {
217
219
  return (req, _) => {
package/lib/esm/index.js CHANGED
@@ -7,3 +7,4 @@ export * from "./interfaces/PlatformFastifySettings.js";
7
7
  export * from "./services/PlatformFastifyRequest.js";
8
8
  export * from "./services/PlatformFastifyResponse.js";
9
9
  export * from "./utils/convertPath.js";
10
+ export * from "./utils/toPrefix.js";
@@ -0,0 +1,3 @@
1
+ export function toPrefix(endpoint) {
2
+ return endpoint === "/" ? "/" : endpoint.endsWith("/") ? endpoint : `${endpoint}/`;
3
+ }
@@ -1,6 +1,7 @@
1
1
  import "@tsed/platform-multer/fastify";
2
2
  import * as Http from "node:http";
3
3
  import { IncomingMessage, ServerResponse } from "node:http";
4
+ import { type FastifyStaticOptions } from "@fastify/static";
4
5
  import { Type } from "@tsed/core";
5
6
  import { PlatformAdapter, PlatformBuilder, PlatformStaticsOptions } from "@tsed/platform-http";
6
7
  import { PlatformHandlerMetadata, PlatformLayer } from "@tsed/platform-router";
@@ -28,7 +29,7 @@ export declare class PlatformFastify extends PlatformAdapter<FastifyInstance> {
28
29
  * @param settings
29
30
  */
30
31
  static bootstrap(module: Type<any>, settings?: Partial<TsED.Configuration>): Promise<PlatformBuilder<Fastify.FastifyInstance<Fastify.RawServerDefault, Http.IncomingMessage, Http.ServerResponse<Http.IncomingMessage>, Fastify.FastifyBaseLogger, Fastify.FastifyTypeProviderDefault>>>;
31
- mapLayers(layers: PlatformLayer[]): void;
32
+ mapLayers(layers: PlatformLayer[]): Promise<void>;
32
33
  mapHandler(handler: (...args: any[]) => any, metadata: PlatformHandlerMetadata): (...args: any[]) => any;
33
34
  useContext(): Promise<void>;
34
35
  createApp(): {
@@ -38,7 +39,7 @@ export declare class PlatformFastify extends PlatformAdapter<FastifyInstance> {
38
39
  afterLoadRoutes(): Promise<void>;
39
40
  getServers(): any[];
40
41
  bodyParser(type: string, opts: Record<string, any> | undefined): any;
41
- statics(endpoint: string, options: PlatformStaticsOptions): any;
42
+ statics(endpoint: string, options: PlatformStaticsOptions & FastifyStaticOptions): Promise<void>;
42
43
  protected compose(layer: PlatformLayer, wildcard?: string): (req: FastifyRequest, _: FastifyReply) => Promise<Promise<any>>;
43
44
  protected resolvePlugins(): Promise<PlatformFastifyPluginLoadingOptions[]>;
44
45
  }
@@ -7,3 +7,4 @@ export * from "./interfaces/PlatformFastifySettings.js";
7
7
  export * from "./services/PlatformFastifyRequest.js";
8
8
  export * from "./services/PlatformFastifyResponse.js";
9
9
  export * from "./utils/convertPath.js";
10
+ export * from "./utils/toPrefix.js";
@@ -1,4 +1,4 @@
1
- import type { FastifyInstance } from "fastify";
2
- export interface PlatformFastifySettings {
1
+ import type { FastifyHttpOptions, FastifyInstance } from "fastify";
2
+ export interface PlatformFastifySettings extends FastifyHttpOptions<any, any> {
3
3
  app?: FastifyInstance;
4
4
  }
@@ -0,0 +1 @@
1
+ export declare function toPrefix(endpoint: string): string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tsed/platform-fastify",
3
3
  "type": "module",
4
- "version": "8.24.0",
4
+ "version": "8.24.2",
5
5
  "description": "Fastify package for Ts.ED framework",
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/esm/index.js",
@@ -61,11 +61,11 @@
61
61
  "@fastify/cookie": "11.0.1",
62
62
  "@fastify/formbody": "8.0.1",
63
63
  "@fastify/session": "11.0.1",
64
- "@tsed/barrels": "8.24.0",
65
- "@tsed/core": "8.24.0",
66
- "@tsed/di": "8.24.0",
67
- "@tsed/platform-http": "8.24.0",
68
- "@tsed/platform-test-sdk": "8.24.0",
64
+ "@tsed/barrels": "8.24.2",
65
+ "@tsed/core": "8.24.2",
66
+ "@tsed/di": "8.24.2",
67
+ "@tsed/platform-http": "8.24.2",
68
+ "@tsed/platform-test-sdk": "8.24.2",
69
69
  "@types/content-disposition": "^0.5.4",
70
70
  "cross-env": "7.0.3",
71
71
  "fastify": "5.3.2",