@travetto/web-upload 6.0.3 → 7.0.0-rc.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.
- package/package.json +5 -5
- package/src/decorator.ts +36 -34
- package/support/test/server.ts +5 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/web-upload",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0-rc.1",
|
|
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": "^
|
|
30
|
-
"@travetto/web": "^
|
|
31
|
-
"file-type": "^21.
|
|
29
|
+
"@travetto/config": "^7.0.0-rc.0",
|
|
30
|
+
"@travetto/web": "^7.0.0-rc.1",
|
|
31
|
+
"file-type": "^21.1.1",
|
|
32
32
|
"mime": "^4.1.0"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
|
-
"@travetto/test": "^
|
|
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 {
|
|
3
|
-
import {
|
|
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/
|
|
18
|
-
* @
|
|
17
|
+
* @augments `@travetto/schema:Input`
|
|
18
|
+
* @kind decorator
|
|
19
19
|
*/
|
|
20
20
|
export function Upload(
|
|
21
|
-
param:
|
|
22
|
-
): (
|
|
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 (
|
|
26
|
+
return (instance: ClassInstance, property: string | symbol, idx: number): void => {
|
|
31
27
|
// Register field
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
|
52
|
-
const
|
|
53
|
+
extract: (request) => {
|
|
54
|
+
const input = SchemaRegistryIndex.getMethodConfig(instance.constructor, property).parameters[idx];
|
|
53
55
|
|
|
54
|
-
if (!
|
|
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 (!(
|
|
59
|
-
throw new AppError(`Cannot use upload decorator with ${
|
|
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 =
|
|
64
|
+
const isMap = input.type === FileMapContract;
|
|
63
65
|
const map = WebUploadUtil.getRequestUploads(request);
|
|
64
|
-
return isMap ? map : map[
|
|
66
|
+
return isMap ? map : map[getName()];
|
|
65
67
|
}
|
|
66
|
-
})(
|
|
68
|
+
})(instance, property, idx);
|
|
67
69
|
};
|
|
68
70
|
}
|
package/support/test/server.ts
CHANGED
|
@@ -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 {
|
|
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(
|
|
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({
|
|
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({
|
|
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
|
|
66
|
+
await Registry.init();
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
@Test()
|