@travetto/web-upload 6.0.3 → 7.0.0-rc.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/web-upload",
3
- "version": "6.0.3",
3
+ "version": "7.0.0-rc.0",
4
4
  "description": "Provides integration between the travetto asset and web module.",
5
5
  "keywords": [
6
6
  "web",
@@ -26,13 +26,13 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "@fastify/busboy": "^3.2.0",
29
- "@travetto/config": "^6.0.1",
30
- "@travetto/web": "^6.0.3",
31
- "file-type": "^21.0.0",
29
+ "@travetto/config": "^7.0.0-rc.0",
30
+ "@travetto/web": "^7.0.0-rc.0",
31
+ "file-type": "^21.1.1",
32
32
  "mime": "^4.1.0"
33
33
  },
34
34
  "peerDependencies": {
35
- "@travetto/test": "^6.0.2"
35
+ "@travetto/test": "^7.0.0-rc.0"
36
36
  },
37
37
  "peerDependenciesMeta": {
38
38
  "@travetto/test": {
package/src/decorator.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { AppError, toConcrete, ClassInstance } from '@travetto/runtime';
2
- import { ControllerRegistry, EndpointParamConfig, Param } from '@travetto/web';
3
- import { SchemaRegistry } from '@travetto/schema';
1
+ import { AppError, toConcrete, ClassInstance, getClass } from '@travetto/runtime';
2
+ import { ControllerRegistryIndex, EndpointParameterConfig, Param } from '@travetto/web';
3
+ import { SchemaRegistryIndex } from '@travetto/schema';
4
4
 
5
5
  import { WebUploadInterceptor } from './interceptor.ts';
6
6
  import { WebUploadConfig } from './config.ts';
@@ -14,55 +14,57 @@ const FileMapContract = toConcrete<FileMap>();
14
14
  /**
15
15
  * Allows for supporting uploads
16
16
  *
17
- * @augments `@travetto/web-upload:Upload`
18
- * @augments `@travetto/web:Param`
17
+ * @augments `@travetto/schema:Input`
18
+ * @kind decorator
19
19
  */
20
20
  export function Upload(
21
- param: string | Partial<EndpointParamConfig> & UploadConfig = {},
22
- ): (inst: ClassInstance, prop: string, idx: number) => void {
23
-
24
- if (typeof param === 'string') {
25
- param = { name: param };
26
- }
21
+ param: Partial<EndpointParameterConfig> & UploadConfig = {},
22
+ ): (instance: ClassInstance, property: string | symbol, idx: number) => void {
27
23
 
28
24
  const finalConf = { ...param };
29
25
 
30
- return (inst: ClassInstance, prop: string, idx: number): void => {
26
+ return (instance: ClassInstance, property: string | symbol, idx: number): void => {
31
27
  // Register field
32
- ControllerRegistry.registerEndpointInterceptorConfig(
33
- inst.constructor, inst[prop], WebUploadInterceptor,
34
- {
35
- applies: true,
36
- maxSize: finalConf.maxSize,
37
- types: finalConf.types,
38
- cleanupFiles: finalConf.cleanupFiles,
39
- uploads: {
40
- [finalConf.name ?? prop]: {
41
- maxSize: finalConf.maxSize,
42
- types: finalConf.types,
43
- cleanupFiles: finalConf.cleanupFiles
28
+ const cls = ControllerRegistryIndex.getForRegister(getClass(instance));
29
+ const getName = (): string => SchemaRegistryIndex.getMethodConfig(instance.constructor, property).parameters[idx].name!.toString();
30
+
31
+ cls.registerFinalizeHandler(() => {
32
+ cls.registerEndpointInterceptorConfig(
33
+ property,
34
+ WebUploadInterceptor,
35
+ {
36
+ applies: true,
37
+ maxSize: finalConf.maxSize,
38
+ types: finalConf.types,
39
+ cleanupFiles: finalConf.cleanupFiles,
40
+ uploads: {
41
+ [getName()]: {
42
+ maxSize: finalConf.maxSize,
43
+ types: finalConf.types,
44
+ cleanupFiles: finalConf.cleanupFiles
45
+ }
44
46
  }
45
47
  }
46
- }
47
- );
48
+ );
49
+ });
48
50
 
49
51
  return Param('body', {
50
52
  ...finalConf,
51
- extract: (request, config) => {
52
- const field = SchemaRegistry.getMethodSchema(inst.constructor, prop)[idx];
53
+ extract: (request) => {
54
+ const input = SchemaRegistryIndex.getMethodConfig(instance.constructor, property).parameters[idx];
53
55
 
54
- if (!field) {
56
+ if (!input) {
55
57
  throw new AppError(`Unknown field type, ensure you are using ${Blob.name}, ${File.name} or ${FileMapContract.name}`);
56
58
  }
57
59
 
58
- if (!(field.type === Blob || field.type === File || field.type === FileMapContract)) {
59
- throw new AppError(`Cannot use upload decorator with ${field.type.name}, but only an ${Blob.name}, ${File.name} or ${FileMapContract.name}`);
60
+ if (!(input.type === Blob || input.type === File || input.type === FileMapContract)) {
61
+ throw new AppError(`Cannot use upload decorator with ${input.type.name}, but only an ${Blob.name}, ${File.name} or ${FileMapContract.name}`);
60
62
  }
61
63
 
62
- const isMap = field.type === FileMapContract;
64
+ const isMap = input.type === FileMapContract;
63
65
  const map = WebUploadUtil.getRequestUploads(request);
64
- return isMap ? map : map[config.name!];
66
+ return isMap ? map : map[getName()];
65
67
  }
66
- })(inst, prop, idx);
68
+ })(instance, property, idx);
67
69
  };
68
70
  }
@@ -3,7 +3,7 @@ import assert from 'node:assert';
3
3
  import { BinaryUtil, castTo } from '@travetto/runtime';
4
4
  import { Controller, Post } from '@travetto/web';
5
5
  import { BeforeAll, Suite, Test, TestFixtures } from '@travetto/test';
6
- import { RootRegistry } from '@travetto/registry';
6
+ import { Registry } from '@travetto/registry';
7
7
 
8
8
  import { BaseWebSuite } from '@travetto/web/support/test/suite/base.ts';
9
9
 
@@ -28,17 +28,17 @@ class TestUploadController {
28
28
  }
29
29
 
30
30
  @Post('/all-named')
31
- async uploads(@Upload('file1') file1: Blob, @Upload('file2') file2: Blob) {
31
+ async uploads(@Upload() file1: Blob, @Upload() file2: Blob) {
32
32
  return { hash1: bHash(file1), hash2: bHash(file2) };
33
33
  }
34
34
 
35
35
  @Post('/all-named-custom')
36
- async uploadVariousLimits(@Upload({ name: 'file1', types: ['!image/png'] }) file1: Blob, @Upload('file2') file2: Blob) {
36
+ async uploadVariousLimits(@Upload({ types: ['!image/png'] }) file1: Blob, @Upload() file2: Blob) {
37
37
  return { hash1: bHash(file1), hash2: bHash(file2) };
38
38
  }
39
39
 
40
40
  @Post('/all-named-size')
41
- async uploadVariousSizeLimits(@Upload({ name: 'file1', maxSize: 100 }) file1: File, @Upload({ name: 'file2', maxSize: 8000 }) file2: File) {
41
+ async uploadVariousSizeLimits(@Upload({ maxSize: 100 }) file1: File, @Upload({ maxSize: 8000 }) file2: File) {
42
42
  return { hash1: bHash(file1), hash2: bHash(file2) };
43
43
  }
44
44
  }
@@ -63,7 +63,7 @@ export abstract class WebUploadServerSuite extends BaseWebSuite {
63
63
  @BeforeAll()
64
64
  async init() {
65
65
  this.fixture = new TestFixtures(['@travetto/asset']);
66
- await RootRegistry.init();
66
+ await Registry.init();
67
67
  }
68
68
 
69
69
  @Test()