@tsed/platform-http 8.6.1 → 8.7.0

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.
Files changed (45) hide show
  1. package/lib/esm/common/decorators/multer/multerFileSize.js +4 -5
  2. package/lib/esm/common/decorators/multer/multerFileSize.js.map +1 -1
  3. package/lib/esm/common/decorators/multer/multerOptions.js +4 -9
  4. package/lib/esm/common/decorators/multer/multerOptions.js.map +1 -1
  5. package/lib/esm/common/decorators/multer/multipartFile.js +4 -30
  6. package/lib/esm/common/decorators/multer/multipartFile.js.map +1 -1
  7. package/lib/esm/common/index.js +0 -1
  8. package/lib/esm/common/index.js.map +1 -1
  9. package/lib/esm/common/middlewares/PlatformAcceptMimesMiddleware.js +7 -0
  10. package/lib/esm/common/middlewares/PlatformAcceptMimesMiddleware.js.map +1 -1
  11. package/lib/esm/common/middlewares/PlatformMulterMiddleware.js +3 -50
  12. package/lib/esm/common/middlewares/PlatformMulterMiddleware.js.map +1 -1
  13. package/lib/esm/common/services/PlatformAdapter.js +8 -0
  14. package/lib/esm/common/services/PlatformAdapter.js.map +1 -1
  15. package/lib/esm/common/services/PlatformHandler.js +2 -3
  16. package/lib/esm/common/services/PlatformHandler.js.map +1 -1
  17. package/lib/types/common/config/interfaces/PlatformMulterSettings.d.ts +17 -14
  18. package/lib/types/common/decorators/multer/multerFileSize.d.ts +4 -2
  19. package/lib/types/common/decorators/multer/multerOptions.d.ts +4 -3
  20. package/lib/types/common/decorators/multer/multipartFile.d.ts +6 -4
  21. package/lib/types/common/index.d.ts +0 -1
  22. package/lib/types/common/middlewares/PlatformAcceptMimesMiddleware.d.ts +8 -1
  23. package/lib/types/common/middlewares/PlatformMulterMiddleware.d.ts +3 -19
  24. package/lib/types/common/services/PlatformAdapter.d.ts +3 -2
  25. package/lib/types/common/services/PlatformHandler.d.ts +0 -2
  26. package/package.json +19 -18
  27. package/src/common/config/interfaces/PlatformMulterSettings.ts +22 -21
  28. package/src/common/decorators/multer/multerFileSize.ts +4 -5
  29. package/src/common/decorators/multer/multerOptions.ts +4 -12
  30. package/src/common/decorators/multer/multipartFile.ts +6 -43
  31. package/src/common/index.ts +0 -1
  32. package/src/common/middlewares/PlatformAcceptMimesMiddleware.ts +11 -1
  33. package/src/common/middlewares/PlatformMulterMiddleware.ts +3 -59
  34. package/src/common/services/PlatformAdapter.ts +5 -2
  35. package/src/common/services/PlatformHandler.ts +2 -3
  36. package/vitest.config.mts +4 -4
  37. package/lib/esm/common/services/PlatformMiddlewaresChain.js +0 -43
  38. package/lib/esm/common/services/PlatformMiddlewaresChain.js.map +0 -1
  39. package/lib/types/common/services/PlatformMiddlewaresChain.d.ts +0 -13
  40. package/src/common/decorators/multer/multerFileSize.spec.ts +0 -22
  41. package/src/common/decorators/multer/multerOptions.spec.ts +0 -21
  42. package/src/common/decorators/multer/multipartFile.spec.ts +0 -259
  43. package/src/common/middlewares/PlatformMulterMiddleware.spec.ts +0 -95
  44. package/src/common/services/PlatformMiddlewaresChain.spec.ts +0 -96
  45. package/src/common/services/PlatformMiddlewaresChain.ts +0 -53
@@ -1,11 +1,11 @@
1
- import {MulterOptions} from "./multerOptions.js";
1
+ import {MulterFileSize as M} from "@tsed/platform-multer";
2
2
 
3
3
  /**
4
4
  * Define file size limit.
5
5
  *
6
6
  * ```typescript
7
7
  * import {Post} from "@tsed/schema";
8
- * import {MulterOptions, MultipartFile} from "@tsed/platform-http";
8
+ * import {MulterOptions, MultipartFile} from "@tsed/platform-multer";
9
9
  * import {Controller} from "@tsed/di";
10
10
  * import {Multer} from "@types/multer";
11
11
  *
@@ -25,8 +25,7 @@ import {MulterOptions} from "./multerOptions.js";
25
25
  * @param fileSize
26
26
  * @returns {(target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor}
27
27
  * @decorator
28
+ * @deprecated use MulterFileSize from @tsed/platform-multer
28
29
  * @multer
29
30
  */
30
- export function MulterFileSize(fileSize: number): MethodDecorator {
31
- return MulterOptions({limits: {fileSize}});
32
- }
31
+ export const MulterFileSize: typeof M = M;
@@ -1,13 +1,10 @@
1
- import {StoreMerge} from "@tsed/core";
2
-
3
- import {PlatformMulterSettings} from "../../config/interfaces/PlatformMulterSettings.js";
4
- import {PlatformMulterMiddleware} from "../../middlewares/PlatformMulterMiddleware.js";
1
+ import {MulterOptions as M} from "@tsed/platform-multer";
5
2
 
6
3
  /**
7
4
  * Define multer option for all MultipartFile
8
5
  *
9
6
  * ```typescript
10
- * import {PlatformMulterFile, MultipartFile, MulterOptions} from "@tsed/platform-http";
7
+ * import {PlatformMulterFile, MultipartFile, MulterOptions} from "@tsed/platform-multer";
11
8
  * import {Controller} from "@tsed/di";
12
9
  * import {Post} from "@tsed/schema";
13
10
  *
@@ -43,11 +40,6 @@ import {PlatformMulterMiddleware} from "../../middlewares/PlatformMulterMiddlewa
43
40
  * @returns {(target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor}
44
41
  * @decorator
45
42
  * @multer
43
+ * @deprecated use MulterOptions from @tsed/platform-multer
46
44
  */
47
- export function MulterOptions(options: PlatformMulterSettings): MethodDecorator {
48
- return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
49
- StoreMerge(PlatformMulterMiddleware, {options})(target, propertyKey, descriptor);
50
-
51
- return descriptor;
52
- };
53
- }
45
+ export const MulterOptions: typeof M = M;
@@ -1,27 +1,12 @@
1
- import {DecoratorParameters, Metadata, StoreMerge, useDecorators, useMethodDecorators} from "@tsed/core";
2
- import {ParamTypes, UseParam} from "@tsed/platform-params";
3
- import {InFile} from "@tsed/schema";
4
-
5
- import {PlatformMulterFile} from "../../config/interfaces/PlatformMulterSettings.js";
6
- import {MulterInputOptions, PlatformMulterMiddleware} from "../../middlewares/PlatformMulterMiddleware.js";
7
-
8
- function mapOptions(name: string, maxCount: number | undefined): MulterInputOptions {
9
- return {
10
- fields: [
11
- {
12
- name,
13
- maxCount
14
- }
15
- ]
16
- };
17
- }
1
+ import type {MultipartFile as MType} from "@tsed/platform-multer";
2
+ import {MultipartFile as M} from "@tsed/platform-multer";
18
3
 
19
4
  /**
20
5
  * Define a parameter as Multipart file.
21
6
  *
22
7
  * ```typescript
23
8
  * import {Post} from "@tsed/schema";
24
- * import {Controller, MulterOptions, MultipartFile} from "@tsed/platform-http";
9
+ * import {Controller, MulterOptions, MultipartFile} from "@tsed/platform-multer";
25
10
  * import {Controller} from "@tsed/di";
26
11
  * import {Multer} from "@types/multer";
27
12
  *
@@ -60,29 +45,7 @@ function mapOptions(name: string, maxCount: number | undefined): MulterInputOpti
60
45
  * @returns Function
61
46
  * @decorator
62
47
  * @input
48
+ * @deprecated use MultipartFile from @tsed/platform-multer
63
49
  */
64
- export function MultipartFile(name: string, maxCount?: number): ParameterDecorator {
65
- return (...args: DecoratorParameters): void => {
66
- const [target, propertyKey, index] = args;
67
- const multiple = Metadata.getParamTypes(target, propertyKey)[index as number] === Array;
68
-
69
- name = (typeof name === "object" ? undefined : name)!;
70
-
71
- const expression = [name, !multiple && "0"].filter(Boolean).join(".");
72
-
73
- const decorators = useDecorators(
74
- InFile(name),
75
- useMethodDecorators(StoreMerge(PlatformMulterMiddleware, mapOptions(name, maxCount))),
76
- UseParam({
77
- paramType: ParamTypes.FILES,
78
- dataPath: "$ctx.request.files",
79
- expression,
80
- useValidation: true
81
- })
82
- );
83
-
84
- decorators(...args);
85
- };
86
- }
87
-
88
- export type MultipartFile = PlatformMulterFile;
50
+ export const MultipartFile: typeof M = M;
51
+ export type MultipartFile = MType;
@@ -44,7 +44,6 @@ export * from "./services/Platform.js";
44
44
  export * from "./services/PlatformAdapter.js";
45
45
  export * from "./services/PlatformApplication.js";
46
46
  export * from "./services/PlatformHandler.js";
47
- export * from "./services/PlatformMiddlewaresChain.js";
48
47
  export * from "./services/PlatformRequest.js";
49
48
  export * from "./services/PlatformResponse.js";
50
49
  export * from "./utils/closeServer.js";
@@ -3,13 +3,23 @@ import {constant, injectable, ProviderType} from "@tsed/di";
3
3
  import {NotAcceptable} from "@tsed/exceptions";
4
4
  import {MiddlewareMethods} from "@tsed/platform-middlewares";
5
5
  import {Context} from "@tsed/platform-params";
6
+ import type {AlterEndpointHandlersArg} from "@tsed/platform-router";
7
+ import type {JsonOperationRoute} from "@tsed/schema";
6
8
 
7
9
  /**
8
10
  * @middleware
9
11
  * @platform
10
12
  */
11
13
  export class PlatformAcceptMimesMiddleware implements MiddlewareMethods {
12
- acceptMimes = constant<string[]>("acceptMimes", []);
14
+ protected acceptMimes = constant<string[]>("acceptMimes", []);
15
+
16
+ public $alterEndpointHandlers(handlers: AlterEndpointHandlersArg, operationRoute: JsonOperationRoute) {
17
+ const hasAcceptMimes = operationRoute.endpoint.acceptMimes.length || this.acceptMimes.length;
18
+ return {
19
+ ...handlers,
20
+ before: [hasAcceptMimes && PlatformAcceptMimesMiddleware, ...handlers.before].filter(Boolean) as any[]
21
+ };
22
+ }
13
23
 
14
24
  public use(@Context() ctx: Context): void {
15
25
  const {endpoint, request} = ctx;
@@ -1,62 +1,6 @@
1
- import {constant, inject, injectable, ProviderType} from "@tsed/di";
2
- import {BadRequest} from "@tsed/exceptions";
3
- import {MiddlewareMethods} from "@tsed/platform-middlewares";
4
- import {Context} from "@tsed/platform-params";
5
- import type {MulterError} from "multer";
6
-
7
- import {PlatformMulterField, PlatformMulterSettings} from "../config/interfaces/PlatformMulterSettings.js";
8
- import {PlatformContext} from "../domain/PlatformContext.js";
9
- import {PlatformAdapter} from "../services/PlatformAdapter.js";
10
- import {PlatformApplication} from "../services/PlatformApplication.js";
11
-
12
- export interface MulterInputOptions {
13
- fields: PlatformMulterField[];
14
- }
15
-
16
- export class MulterException extends BadRequest {
17
- constructor(er: MulterError) {
18
- super(er.message);
19
- this.origin = er;
20
- this.name = er.code;
21
- }
22
- }
1
+ import {PlatformMulterMiddleware as M} from "@tsed/platform-multer";
23
2
 
24
3
  /**
25
- * @middleware
4
+ * @deprecated Since version 8.5. Use PlatformMulterMiddleware from @tsed/platform-multer package
26
5
  */
27
- export class PlatformMulterMiddleware implements MiddlewareMethods {
28
- async use(@Context() ctx: PlatformContext) {
29
- try {
30
- const {fields, options = {}} = ctx.endpoint.get(PlatformMulterMiddleware);
31
- const settings: PlatformMulterSettings = {
32
- ...constant("multer", {}),
33
- ...options
34
- };
35
-
36
- /* istanbul ignore next */
37
- if (settings.storage) {
38
- delete settings.dest;
39
- }
40
-
41
- const multer = inject(PlatformAdapter).multipart(settings);
42
-
43
- if (multer) {
44
- const middleware: any = multer.fields(this.getFields({fields}));
45
-
46
- return await middleware(ctx.getRequest(), ctx.getResponse());
47
- }
48
- } catch (er) {
49
- if (er.code) {
50
- throw new MulterException(er);
51
- }
52
-
53
- throw er;
54
- }
55
- }
56
-
57
- protected getFields(conf: MulterInputOptions) {
58
- return conf.fields.map(({name, maxCount}) => ({name, maxCount}));
59
- }
60
- }
61
-
62
- injectable(PlatformMulterMiddleware).type(ProviderType.MIDDLEWARE).priority(-10);
6
+ export const PlatformMulterMiddleware: typeof M = M;
@@ -3,9 +3,9 @@ import {IncomingMessage, ServerResponse} from "node:http";
3
3
  import {Type} from "@tsed/core";
4
4
  import {configuration, constant, inject, injectable} from "@tsed/di";
5
5
  import {$on} from "@tsed/hooks";
6
+ import type {PlatformMulter, PlatformMulterSettings} from "@tsed/platform-multer";
6
7
  import {PlatformHandlerMetadata, PlatformLayer} from "@tsed/platform-router";
7
8
 
8
- import {PlatformMulter, PlatformMulterSettings} from "../config/interfaces/PlatformMulterSettings.js";
9
9
  import {PlatformStaticsOptions} from "../config/interfaces/PlatformStaticsSettings.js";
10
10
  import {application} from "../fn/application.js";
11
11
  import {createHttpServer} from "../utils/createHttpServer.js";
@@ -81,8 +81,11 @@ export abstract class PlatformAdapter<App = TsED.Application> {
81
81
  /**
82
82
  * Return the multipart middleware
83
83
  * @param options
84
+ * @deprecated use inject("MULTER_MODULE")?.get(options) instead
84
85
  */
85
- abstract multipart(options: PlatformMulterSettings): PlatformMulter;
86
+ multipart(options: PlatformMulterSettings): PlatformMulter {
87
+ return inject<any>(Symbol.for("MULTER_MODULE"))?.get(options);
88
+ }
86
89
 
87
90
  /**
88
91
  * Return the body parser for the given
@@ -1,5 +1,6 @@
1
1
  import {AnyPromiseResult, AnyToPromiseStatus, catchAsyncError} from "@tsed/core";
2
2
  import {inject, injectable, Provider, ProviderScope} from "@tsed/di";
3
+ import {$alter} from "@tsed/hooks";
3
4
  import {PlatformExceptions} from "@tsed/platform-exceptions";
4
5
  import {PlatformParams, PlatformParamsCallback} from "@tsed/platform-params";
5
6
  import {PlatformResponseFilter} from "@tsed/platform-response-filter";
@@ -16,7 +17,6 @@ import {AnyToPromiseWithCtx} from "../domain/AnyToPromiseWithCtx.js";
16
17
  import {PlatformContext} from "../domain/PlatformContext.js";
17
18
  import {setResponseHeaders} from "../utils/setResponseHeaders.js";
18
19
  import {PlatformAdapter} from "./PlatformAdapter.js";
19
- import {PlatformMiddlewaresChain} from "./PlatformMiddlewaresChain.js";
20
20
 
21
21
  /**
22
22
  * Platform Handler abstraction layer. Wrap original class method to a pure platform handler (Express, Koa, etc...).
@@ -26,14 +26,13 @@ export class PlatformHandler {
26
26
  protected responseFilter = inject(PlatformResponseFilter);
27
27
  protected platformParams = inject(PlatformParams);
28
28
  protected platformExceptions = inject(PlatformExceptions);
29
- protected platformMiddlewaresChain = inject(PlatformMiddlewaresChain);
30
29
  protected platformRouters = inject(PlatformRouters);
31
30
 
32
31
  constructor() {
33
32
  // configure the router module
34
33
  this.platformRouters.hooks
35
34
  .on("alterEndpointHandlers", (handlers: AlterEndpointHandlersArg, operationRoute: JsonOperationRoute) => {
36
- handlers = this.platformMiddlewaresChain.get(handlers, operationRoute);
35
+ handlers = $alter("$alterEndpointHandlers", handlers, [operationRoute]);
37
36
 
38
37
  handlers.after.push(useResponseHandler(this.flush.bind(this)));
39
38
 
package/vitest.config.mts CHANGED
@@ -14,10 +14,10 @@ export default defineConfig(
14
14
  "**/Fake*"
15
15
  ],
16
16
  thresholds: {
17
- statements: 96.88,
18
- branches: 95.5,
19
- functions: 94.11,
20
- lines: 96.88
17
+ statements: 0,
18
+ branches: 0,
19
+ functions: 0,
20
+ lines: 0
21
21
  }
22
22
  }
23
23
  }
@@ -1,43 +0,0 @@
1
- import { isClass } from "@tsed/core";
2
- import { constant, injectable } from "@tsed/di";
3
- import { ParamTypes } from "@tsed/platform-params";
4
- import { JsonEntityStore } from "@tsed/schema";
5
- import { PlatformAcceptMimesMiddleware } from "../middlewares/PlatformAcceptMimesMiddleware.js";
6
- import { PlatformMulterMiddleware } from "../middlewares/PlatformMulterMiddleware.js";
7
- export class PlatformMiddlewaresChain {
8
- constructor() {
9
- this.acceptMimes = constant("acceptMimes", []);
10
- }
11
- get(handlers, operationRoute) {
12
- const { ACCEPT_MIMES, FILE } = this.getParamTypes(handlers, operationRoute);
13
- return {
14
- ...handlers,
15
- before: [ACCEPT_MIMES && PlatformAcceptMimesMiddleware, ...handlers.before, FILE && PlatformMulterMiddleware].filter(Boolean)
16
- };
17
- }
18
- hasAcceptMimes(operationRoute) {
19
- return operationRoute.endpoint.acceptMimes.length || this.acceptMimes.length;
20
- }
21
- getParamTypes(middlewares, operationRoute) {
22
- return middlewares.before
23
- .concat(middlewares.after)
24
- .filter(isClass)
25
- .reduce((paramTypes, token) => {
26
- const entity = JsonEntityStore.fromMethod(token, "use");
27
- if (entity.decoratorType === "method") {
28
- const { FILE, RAW_BODY, BODY } = entity.getParamTypes();
29
- paramTypes.FILE = paramTypes.FILE || FILE;
30
- paramTypes.RAW_BODY = paramTypes.RAW_BODY || RAW_BODY;
31
- paramTypes.BODY = paramTypes.BODY || BODY;
32
- }
33
- return paramTypes;
34
- }, {
35
- ACCEPT_MIMES: this.hasAcceptMimes(operationRoute),
36
- FILE: operationRoute.has(ParamTypes.FILES),
37
- RAW_BODY: operationRoute.has(ParamTypes.RAW_BODY),
38
- BODY: operationRoute.has(ParamTypes.BODY) || operationRoute.method === "ALL"
39
- });
40
- }
41
- }
42
- injectable(PlatformMiddlewaresChain);
43
- //# sourceMappingURL=PlatformMiddlewaresChain.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"PlatformMiddlewaresChain.js","sourceRoot":"","sources":["../../../../src/common/services/PlatformMiddlewaresChain.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAC,MAAM,YAAY,CAAC;AACnC,OAAO,EAAC,QAAQ,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC;AAC9C,OAAO,EAAC,UAAU,EAAC,MAAM,uBAAuB,CAAC;AAEjD,OAAO,EAAC,eAAe,EAAqB,MAAM,cAAc,CAAC;AAEjE,OAAO,EAAC,6BAA6B,EAAC,MAAM,iDAAiD,CAAC;AAC9F,OAAO,EAAC,wBAAwB,EAAC,MAAM,4CAA4C,CAAC;AAEpF,MAAM,OAAO,wBAAwB;IAArC;QACY,gBAAW,GAAG,QAAQ,CAAW,aAAa,EAAE,EAAE,CAAC,CAAC;IAwChE,CAAC;IAtCC,GAAG,CAAC,QAAkC,EAAE,cAAkC;QACxE,MAAM,EAAC,YAAY,EAAE,IAAI,EAAC,GAAG,IAAI,CAAC,aAAa,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC;QAE1E,OAAO;YACL,GAAG,QAAQ;YACX,MAAM,EAAE,CAAC,YAAY,IAAI,6BAA6B,EAAE,GAAG,QAAQ,CAAC,MAAM,EAAE,IAAI,IAAI,wBAAwB,CAAC,CAAC,MAAM,CAAC,OAAO,CAAU;SACvI,CAAC;IACJ,CAAC;IAES,cAAc,CAAC,cAAkC;QACzD,OAAO,cAAc,CAAC,QAAQ,CAAC,WAAW,CAAC,MAAM,IAAI,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC;IAC/E,CAAC;IAES,aAAa,CAAC,WAAqC,EAAE,cAAkC;QAC/F,OAAO,WAAW,CAAC,MAAM;aACtB,MAAM,CAAC,WAAW,CAAC,KAAK,CAAC;aACzB,MAAM,CAAC,OAAO,CAAC;aACf,MAAM,CACL,CAAC,UAAU,EAAE,KAAK,EAAE,EAAE;YACpB,MAAM,MAAM,GAAG,eAAe,CAAC,UAAU,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;YAExD,IAAI,MAAM,CAAC,aAAa,KAAK,QAAQ,EAAE,CAAC;gBACtC,MAAM,EAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAC,GAAG,MAAM,CAAC,aAAa,EAAE,CAAC;gBACtD,UAAU,CAAC,IAAI,GAAG,UAAU,CAAC,IAAI,IAAI,IAAI,CAAC;gBAC1C,UAAU,CAAC,QAAQ,GAAG,UAAU,CAAC,QAAQ,IAAI,QAAQ,CAAC;gBACtD,UAAU,CAAC,IAAI,GAAG,UAAU,CAAC,IAAI,IAAI,IAAI,CAAC;YAC5C,CAAC;YAED,OAAO,UAAU,CAAC;QACpB,CAAC,EACD;YACE,YAAY,EAAE,IAAI,CAAC,cAAc,CAAC,cAAc,CAAC;YACjD,IAAI,EAAE,cAAc,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC;YAC1C,QAAQ,EAAE,cAAc,CAAC,GAAG,CAAC,UAAU,CAAC,QAAQ,CAAC;YACjD,IAAI,EAAE,cAAc,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,cAAc,CAAC,MAAM,KAAK,KAAK;SAC7E,CACF,CAAC;IACN,CAAC;CACF;AAED,UAAU,CAAC,wBAAwB,CAAC,CAAC"}
@@ -1,13 +0,0 @@
1
- import { AlterEndpointHandlersArg } from "@tsed/platform-router";
2
- import { JsonOperationRoute } from "@tsed/schema";
3
- export declare class PlatformMiddlewaresChain {
4
- protected acceptMimes: string[];
5
- get(handlers: AlterEndpointHandlersArg, operationRoute: JsonOperationRoute): AlterEndpointHandlersArg;
6
- protected hasAcceptMimes(operationRoute: JsonOperationRoute): number;
7
- protected getParamTypes(middlewares: AlterEndpointHandlersArg, operationRoute: JsonOperationRoute): {
8
- ACCEPT_MIMES: number;
9
- FILE: boolean;
10
- RAW_BODY: boolean;
11
- BODY: boolean;
12
- };
13
- }
@@ -1,22 +0,0 @@
1
- import {Store} from "@tsed/core";
2
-
3
- import {PlatformMulterMiddleware} from "../../middlewares/PlatformMulterMiddleware.js";
4
- import {MulterFileSize} from "./multerFileSize.js";
5
-
6
- describe("MulterFileSize", () => {
7
- it("should set the file size", () => {
8
- class Test {
9
- @MulterFileSize(100)
10
- file() {}
11
- }
12
-
13
- const store = Store.fromMethod(Test, "file");
14
- expect(store.get(PlatformMulterMiddleware)).toEqual({
15
- options: {
16
- limits: {
17
- fileSize: 100
18
- }
19
- }
20
- });
21
- });
22
- });
@@ -1,21 +0,0 @@
1
- import {Store} from "@tsed/core";
2
-
3
- import {PlatformMulterMiddleware} from "../../middlewares/PlatformMulterMiddleware.js";
4
- import {MulterOptions} from "./multerOptions.js";
5
-
6
- describe("@MulterOptions()", () => {
7
- it("should store metadata", () => {
8
- class Test {
9
- @MulterOptions({dest: "/"})
10
- test() {}
11
- }
12
-
13
- const store = Store.fromMethod(Test.prototype, "test");
14
-
15
- expect(store.get(PlatformMulterMiddleware)).toEqual({
16
- options: {
17
- dest: "/"
18
- }
19
- });
20
- });
21
- });
@@ -1,259 +0,0 @@
1
- import "../../index.js";
2
-
3
- import {descriptorOf, Metadata, Store} from "@tsed/core";
4
- import {ParamTypes} from "@tsed/platform-params";
5
- import {getSpec, JsonParameterStore, Post, SpecTypes} from "@tsed/schema";
6
-
7
- import {PlatformMulterMiddleware} from "../../middlewares/PlatformMulterMiddleware.js";
8
- import {MultipartFile} from "./multipartFile.js";
9
-
10
- class Test {
11
- test() {}
12
- }
13
-
14
- describe("@MultipartFile()", () => {
15
- describe("one file", () => {
16
- // WHEN
17
- class TestController {
18
- @Post("/")
19
- test(@MultipartFile("file1", 1) file: any) {}
20
- }
21
-
22
- // THEN
23
- const store = Store.fromMethod(TestController, "test");
24
- const param = JsonParameterStore.get(TestController, "test", 0);
25
-
26
- it("should set params properly", () => {
27
- expect(store.get(PlatformMulterMiddleware)).toEqual({
28
- fields: [
29
- {
30
- maxCount: 1,
31
- name: "file1"
32
- }
33
- ]
34
- });
35
- expect(param.expression).toEqual("file1.0");
36
- expect(param.paramType).toEqual(ParamTypes.FILES);
37
- });
38
-
39
- it("should set endpoint metadata - OS3", () => {
40
- expect(getSpec(TestController, {specType: SpecTypes.OPENAPI})).toEqual({
41
- paths: {
42
- "/": {
43
- post: {
44
- operationId: "testControllerTest",
45
- parameters: [],
46
- responses: {
47
- "400": {
48
- content: {"application/json": {schema: {$ref: "#/components/schemas/BadRequest"}}},
49
- description:
50
- "<File too long | Too many parts | Too many files | Field name too long | Field value too long | Too many fields | Unexpected field> [fieldName] Example: File too long file1"
51
- }
52
- },
53
- requestBody: {
54
- required: false,
55
- content: {
56
- "multipart/form-data": {
57
- schema: {
58
- properties: {file1: {type: "string", format: "binary"}},
59
- type: "object"
60
- }
61
- }
62
- }
63
- },
64
- tags: ["TestController"]
65
- }
66
- }
67
- },
68
- tags: [{name: "TestController"}],
69
- components: {
70
- schemas: {
71
- GenericError: {
72
- additionalProperties: true,
73
- properties: {
74
- message: {
75
- description: "An error message",
76
- minLength: 1,
77
- type: "string"
78
- },
79
- name: {
80
- description: "The error name",
81
- minLength: 1,
82
- type: "string"
83
- }
84
- },
85
- required: ["name", "message"],
86
- type: "object"
87
- },
88
- BadRequest: {
89
- type: "object",
90
- properties: {
91
- name: {
92
- type: "string",
93
- minLength: 1,
94
- description: "The error name",
95
- example: "BAD_REQUEST",
96
- default: "BAD_REQUEST"
97
- },
98
- message: {type: "string", minLength: 1, description: "An error message"},
99
- status: {type: "number", description: "The status code of the exception", example: 400, default: 400},
100
- errors: {
101
- type: "array",
102
- items: {
103
- $ref: "#/components/schemas/GenericError"
104
- },
105
- description: "A list of related errors"
106
- },
107
- stack: {
108
- type: "string",
109
- description: "The stack trace (only in development mode)"
110
- }
111
- },
112
- required: ["name", "message", "status"]
113
- }
114
- }
115
- }
116
- });
117
- });
118
- });
119
-
120
- describe("multiple file schema", () => {
121
- // WHEN
122
- class TestController {
123
- @Post("/")
124
- test(@MultipartFile("file1", 4) file: any[]) {}
125
- }
126
-
127
- // THEN
128
- const store = Store.fromMethod(TestController, "test");
129
- const param = JsonParameterStore.get(TestController, "test", 0);
130
-
131
- it("should set params properly", () => {
132
- expect(store.get(PlatformMulterMiddleware)).toEqual({
133
- fields: [
134
- {
135
- maxCount: 4,
136
- name: "file1"
137
- }
138
- ]
139
- });
140
- expect(param.expression).toEqual("file1");
141
- expect(param.paramType).toEqual(ParamTypes.FILES);
142
- });
143
-
144
- it("should set endpoint metadata - OS3", () => {
145
- expect(getSpec(TestController, {specType: SpecTypes.OPENAPI})).toEqual({
146
- paths: {
147
- "/": {
148
- post: {
149
- operationId: "testControllerTest",
150
- parameters: [],
151
- responses: {
152
- "400": {
153
- content: {"application/json": {schema: {$ref: "#/components/schemas/BadRequest"}}},
154
- description:
155
- "<File too long | Too many parts | Too many files | Field name too long | Field value too long | Too many fields | Unexpected field> [fieldName] Example: File too long file1"
156
- }
157
- },
158
- requestBody: {
159
- required: false,
160
- content: {
161
- "multipart/form-data": {
162
- schema: {
163
- properties: {file1: {type: "array", items: {type: "string", format: "binary"}}},
164
- type: "object"
165
- }
166
- }
167
- }
168
- },
169
- tags: ["TestController"]
170
- }
171
- }
172
- },
173
- tags: [{name: "TestController"}],
174
- components: {
175
- schemas: {
176
- GenericError: {
177
- additionalProperties: true,
178
- properties: {
179
- message: {
180
- description: "An error message",
181
- minLength: 1,
182
- type: "string"
183
- },
184
- name: {
185
- description: "The error name",
186
- minLength: 1,
187
- type: "string"
188
- }
189
- },
190
- required: ["name", "message"],
191
- type: "object"
192
- },
193
- BadRequest: {
194
- type: "object",
195
- properties: {
196
- name: {
197
- type: "string",
198
- minLength: 1,
199
- description: "The error name",
200
- example: "BAD_REQUEST",
201
- default: "BAD_REQUEST"
202
- },
203
- message: {
204
- type: "string",
205
- minLength: 1,
206
- description: "An error message"
207
- },
208
- status: {type: "number", description: "The status code of the exception", example: 400, default: 400},
209
- errors: {
210
- type: "array",
211
- items: {
212
- $ref: "#/components/schemas/GenericError"
213
- },
214
- description: "A list of related errors"
215
- },
216
- stack: {
217
- type: "string",
218
- description: "The stack trace (only in development mode)"
219
- }
220
- },
221
- required: ["name", "message", "status"]
222
- }
223
- }
224
- }
225
- });
226
- });
227
- });
228
-
229
- describe("multiple files", () => {
230
- afterAll(() => {
231
- vi.clearAllMocks();
232
- });
233
-
234
- it("should set params metadata", () => {
235
- const store = Store.from(Test.prototype, "test", descriptorOf(Test.prototype, "test"));
236
- store.delete("multipartAdded");
237
-
238
- // @ts-ignore
239
- store.delete(PlatformMulterMiddleware);
240
- vi.spyOn(Store, "fromMethod").mockReturnValue(store);
241
- vi.spyOn(Metadata, "getParamTypes").mockReturnValue([Array]);
242
-
243
- MultipartFile("file1", 8)(Test.prototype, "test", 0);
244
-
245
- expect(store.get(PlatformMulterMiddleware)).toEqual({
246
- fields: [
247
- {
248
- maxCount: 8,
249
- name: "file1"
250
- }
251
- ]
252
- });
253
-
254
- const param = JsonParameterStore.get(Test, "test", 0);
255
- expect(param.expression).toEqual("file1");
256
- expect(param.paramType).toEqual(ParamTypes.FILES);
257
- });
258
- });
259
- });