@ditojs/server 2.86.0 → 2.88.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 +8 -7
- package/src/app/Application.js +5 -2
- package/src/controllers/Controller.js +6 -2
- package/src/mixins/AssetMixin.js +4 -2
- package/src/models/Model.js +3 -3
- package/src/storage/AssetFile.js +2 -0
- package/src/storage/Storage.js +50 -4
- package/src/storage/Storage.test.js +58 -0
- package/src/utils/asset.js +7 -0
- package/types/index.d.ts +1712 -349
- package/types/tests/application.test-d.ts +26 -0
- package/types/tests/controller.test-d.ts +113 -0
- package/types/tests/errors.test-d.ts +53 -0
- package/types/tests/fixtures.ts +19 -0
- package/types/tests/model.test-d.ts +193 -0
- package/types/tests/query-builder.test-d.ts +106 -0
- package/types/tests/relation.test-d.ts +83 -0
- package/types/tests/storage.test-d.ts +113 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { expectTypeOf, describe, it } from 'vitest'
|
|
2
|
+
import type multer from '@koa/multer'
|
|
3
|
+
import type Koa from 'koa'
|
|
4
|
+
import type { Storage, StorageFile, AssetFileObject } from '../index.d.ts'
|
|
5
|
+
|
|
6
|
+
describe('Storage', () => {
|
|
7
|
+
const storage = {} as Storage
|
|
8
|
+
|
|
9
|
+
describe('StorageFile', () => {
|
|
10
|
+
it('inherits multer.File properties', () => {
|
|
11
|
+
const file = {} as StorageFile
|
|
12
|
+
expectTypeOf(file.originalname)
|
|
13
|
+
.not.toBeAny()
|
|
14
|
+
expectTypeOf(file.originalname)
|
|
15
|
+
.toEqualTypeOf<string>()
|
|
16
|
+
expectTypeOf(file.mimetype).not.toBeAny()
|
|
17
|
+
expectTypeOf(file.mimetype)
|
|
18
|
+
.toEqualTypeOf<string>()
|
|
19
|
+
expectTypeOf(file.size).not.toBeAny()
|
|
20
|
+
expectTypeOf(file.size).toEqualTypeOf<number>()
|
|
21
|
+
expectTypeOf(file.key).not.toBeAny()
|
|
22
|
+
expectTypeOf(file.key).toEqualTypeOf<string>()
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('is assignable to multer.File', () => {
|
|
26
|
+
const storageFile = {} as StorageFile
|
|
27
|
+
expectTypeOf(
|
|
28
|
+
storageFile
|
|
29
|
+
).toMatchTypeOf<multer.File>()
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('multer.File is not assignable to StorageFile', () => {
|
|
33
|
+
const multerFile = {} as multer.File
|
|
34
|
+
expectTypeOf(
|
|
35
|
+
multerFile
|
|
36
|
+
).not.toMatchTypeOf<StorageFile>()
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
describe('convertStorageFile', () => {
|
|
41
|
+
it('converts StorageFile to AssetFileObject', () => {
|
|
42
|
+
const file = {} as StorageFile
|
|
43
|
+
expectTypeOf(
|
|
44
|
+
storage.convertStorageFile(file)
|
|
45
|
+
).not.toBeAny()
|
|
46
|
+
expectTypeOf(
|
|
47
|
+
storage.convertStorageFile(file)
|
|
48
|
+
).toEqualTypeOf<AssetFileObject>()
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('converts array via convertStorageFiles', () => {
|
|
52
|
+
const files = [] as StorageFile[]
|
|
53
|
+
expectTypeOf(
|
|
54
|
+
storage.convertStorageFiles(files)
|
|
55
|
+
).not.toBeAny()
|
|
56
|
+
expectTypeOf(
|
|
57
|
+
storage.convertStorageFiles(files)
|
|
58
|
+
).toEqualTypeOf<AssetFileObject[]>()
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('rejects plain objects', () => {
|
|
62
|
+
// @ts-expect-error - not a StorageFile
|
|
63
|
+
storage.convertStorageFile({ key: 'x' })
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
describe('convertAssetFile', () => {
|
|
68
|
+
it('accepts AssetFileObject', () => {
|
|
69
|
+
const file: AssetFileObject = {
|
|
70
|
+
key: 'abc.jpg',
|
|
71
|
+
name: 'photo.jpg',
|
|
72
|
+
type: 'image/jpeg',
|
|
73
|
+
size: 1024,
|
|
74
|
+
url: '/uploads/abc.jpg'
|
|
75
|
+
}
|
|
76
|
+
expectTypeOf(
|
|
77
|
+
storage.convertAssetFile(file)
|
|
78
|
+
).toEqualTypeOf<void>()
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('rejects plain objects missing required fields', () => {
|
|
82
|
+
// @ts-expect-error - missing required AssetFileObject fields
|
|
83
|
+
storage.convertAssetFile({ key: 'x' })
|
|
84
|
+
})
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
describe('getUploadStorage', () => {
|
|
88
|
+
it('returns StorageEngine or null', () => {
|
|
89
|
+
const result = storage.getUploadStorage({})
|
|
90
|
+
expectTypeOf(result).not.toBeAny()
|
|
91
|
+
expectTypeOf(result)
|
|
92
|
+
.toEqualTypeOf<multer.StorageEngine | null>()
|
|
93
|
+
})
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
describe('getUploadHandler', () => {
|
|
97
|
+
it('returns Koa middleware or null', () => {
|
|
98
|
+
expectTypeOf(
|
|
99
|
+
storage.getUploadHandler({})
|
|
100
|
+
).not.toBeAny()
|
|
101
|
+
expectTypeOf(
|
|
102
|
+
storage.getUploadHandler({})
|
|
103
|
+
).toEqualTypeOf<Koa.Middleware | null>()
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('accepts multer options', () => {
|
|
107
|
+
storage.getUploadHandler({
|
|
108
|
+
dest: '/tmp/uploads',
|
|
109
|
+
limits: { fileSize: 1024 * 1024 }
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
})
|
|
113
|
+
})
|