@tsed/platform-fastify 8.12.5 → 8.13.1

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.
@@ -13,6 +13,7 @@ import { PlatformHandlerType } from "@tsed/platform-router";
13
13
  import Fastify from "fastify";
14
14
  import { PlatformFastifyRequest } from "../services/PlatformFastifyRequest.js";
15
15
  import { PlatformFastifyResponse } from "../services/PlatformFastifyResponse.js";
16
+ import { convertPath } from "../utils/convertPath.js";
16
17
  export class PlatformFastify extends PlatformAdapter {
17
18
  constructor() {
18
19
  super(...arguments);
@@ -45,22 +46,24 @@ export class PlatformFastify extends PlatformAdapter {
45
46
  const { app } = this;
46
47
  const rawApp = app.getApp();
47
48
  layers.forEach((layer) => {
49
+ const { path, wildcard } = convertPath(layer.path);
50
+ const handlers = layer.getArgs(false);
48
51
  switch (layer.method) {
49
52
  case "use":
50
53
  if (rawApp.use) {
51
- rawApp.use(...layer.getArgs());
54
+ rawApp.use(path, handlers);
52
55
  }
53
56
  return;
54
57
  case "statics":
55
- this.statics(layer.path, layer.opts);
58
+ this.statics(path, layer.opts);
56
59
  // rawApp.register();
57
60
  return;
58
61
  }
59
62
  try {
60
63
  rawApp.route({
61
64
  method: layer.method.toUpperCase(),
62
- url: layer.path,
63
- handler: this.compose(layer),
65
+ url: path,
66
+ handler: this.compose(layer, wildcard),
64
67
  config: {
65
68
  rawBody: layer.handlers.some((handler) => handler.opts?.paramsTypes?.RAW_BODY)
66
69
  }
@@ -204,8 +207,12 @@ export class PlatformFastify extends PlatformAdapter {
204
207
  this.staticsDecorated = true;
205
208
  return null;
206
209
  }
207
- compose(layer) {
208
- return (req, reply) => {
210
+ compose(layer, wildcard) {
211
+ return (req, _) => {
212
+ const params = req.params;
213
+ if (wildcard && params["*"] && !params[wildcard]) {
214
+ params[wildcard] = params["*"];
215
+ }
209
216
  return runInContext(req.$ctx, async () => {
210
217
  const $ctx = req.$ctx;
211
218
  $ctx.next = null;
package/lib/esm/index.js CHANGED
@@ -6,3 +6,4 @@ export * from "./interfaces/interfaces.js";
6
6
  export * from "./interfaces/PlatformFastifySettings.js";
7
7
  export * from "./services/PlatformFastifyRequest.js";
8
8
  export * from "./services/PlatformFastifyResponse.js";
9
+ export * from "./utils/convertPath.js";
@@ -0,0 +1,37 @@
1
+ import { isString } from "@tsed/core";
2
+ export function convertPath(path) {
3
+ if (isString(path)) {
4
+ const segments = path.split("/");
5
+ const parsed = segments.reduce((options, segment, index) => {
6
+ const isLastSegment = index === segments.length - 1;
7
+ if (isLastSegment && (segment === "*" || segment === "(.*)")) {
8
+ options.wildcard = "*";
9
+ options.path.push("*");
10
+ return options;
11
+ }
12
+ if (isLastSegment && segment.startsWith("*")) {
13
+ options.wildcard = segment.substring(1);
14
+ options.path.push("*");
15
+ return options;
16
+ }
17
+ if (segment.startsWith(":") && segment.endsWith("*")) {
18
+ options.wildcard = segment.substring(1, segment.length - 1);
19
+ options.path.push("*");
20
+ return options;
21
+ }
22
+ if (segment.startsWith("{:") && segment.endsWith("}")) {
23
+ // Handle v5 style parameters like /{param}
24
+ const paramName = segment.substring(2, segment.length - 1);
25
+ options.path.push(`:${paramName}?`);
26
+ return options;
27
+ }
28
+ options.path.push(segment);
29
+ return options;
30
+ }, { path: [], wildcard: undefined });
31
+ return {
32
+ path: parsed.path.join("/"),
33
+ wildcard: parsed.wildcard
34
+ };
35
+ }
36
+ return { path };
37
+ }
@@ -40,6 +40,6 @@ export declare class PlatformFastify extends PlatformAdapter<FastifyInstance> {
40
40
  getServers(): any[];
41
41
  bodyParser(type: string, opts: Record<string, any> | undefined): any;
42
42
  statics(endpoint: string, options: PlatformStaticsOptions): any;
43
- protected compose(layer: PlatformLayer): (req: FastifyRequest, reply: FastifyReply) => Promise<Promise<void>>;
43
+ protected compose(layer: PlatformLayer, wildcard?: string): (req: FastifyRequest, _: FastifyReply) => Promise<Promise<void>>;
44
44
  protected resolvePlugins(): Promise<PlatformFastifyPluginLoadingOptions[]>;
45
45
  }
@@ -6,3 +6,4 @@ export * from "./interfaces/interfaces.js";
6
6
  export * from "./interfaces/PlatformFastifySettings.js";
7
7
  export * from "./services/PlatformFastifyRequest.js";
8
8
  export * from "./services/PlatformFastifyResponse.js";
9
+ export * from "./utils/convertPath.js";
@@ -0,0 +1,6 @@
1
+ interface ConvertPathResult {
2
+ path: string | RegExp;
3
+ wildcard?: string;
4
+ }
5
+ export declare function convertPath(path: string | RegExp): ConvertPathResult;
6
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tsed/platform-fastify",
3
3
  "type": "module",
4
- "version": "8.12.5",
4
+ "version": "8.13.1",
5
5
  "description": "Fastify package for Ts.ED framework",
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/esm/index.js",
@@ -61,17 +61,17 @@
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.12.5",
65
- "@tsed/core": "8.12.5",
66
- "@tsed/di": "8.12.5",
67
- "@tsed/platform-http": "8.12.5",
68
- "@tsed/platform-test-sdk": "8.12.5",
64
+ "@tsed/barrels": "8.13.1",
65
+ "@tsed/core": "8.13.1",
66
+ "@tsed/di": "8.13.1",
67
+ "@tsed/platform-http": "8.13.1",
68
+ "@tsed/platform-test-sdk": "8.13.1",
69
69
  "@types/content-disposition": "^0.5.4",
70
70
  "cross-env": "7.0.3",
71
71
  "fastify": "5.3.2",
72
72
  "fastify-raw-body": "5.0.0",
73
73
  "ts-node": "10.9.2",
74
- "vitest": "3.1.4"
74
+ "vitest": "3.2.4"
75
75
  },
76
76
  "peerDependencies": {
77
77
  "@fastify/accepts": ">=4.3.0",