@kanun-hq/plugin-file 0.1.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/LICENSE +21 -0
- package/README.md +670 -0
- package/dist/index.d.ts +164 -0
- package/dist/index.js +665 -0
- package/package.json +38 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { ValidatorPlugin } from "kanun";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
5
|
+
interface FileRuleResolverArgs {
|
|
6
|
+
attribute: string;
|
|
7
|
+
context: Record<string, any>;
|
|
8
|
+
data: Record<string, any>;
|
|
9
|
+
value: unknown;
|
|
10
|
+
}
|
|
11
|
+
interface FileValidatorPluginOptions {
|
|
12
|
+
resolveFiles?: (args: FileRuleResolverArgs) => MaybePromise<unknown>;
|
|
13
|
+
}
|
|
14
|
+
interface ValidatorWithData<TSelf = any> {
|
|
15
|
+
getContext: () => Record<string, any>;
|
|
16
|
+
getData: () => Record<string, any>;
|
|
17
|
+
setData: (data: Record<string, any>) => TSelf;
|
|
18
|
+
}
|
|
19
|
+
type WildcardRuleInput = string | string[];
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/adapters.d.ts
|
|
22
|
+
interface ValidatorWithContext<TSelf = any> {
|
|
23
|
+
getContext: () => Record<string, any>;
|
|
24
|
+
withContext: (context: Record<string, any>) => TSelf;
|
|
25
|
+
}
|
|
26
|
+
interface ExpressLikeRequest {
|
|
27
|
+
file?: unknown;
|
|
28
|
+
files?: unknown;
|
|
29
|
+
}
|
|
30
|
+
interface FastifyLikeRequest {
|
|
31
|
+
body?: unknown;
|
|
32
|
+
file?: unknown;
|
|
33
|
+
files?: unknown;
|
|
34
|
+
parts?: (() => AsyncIterable<unknown>) | unknown;
|
|
35
|
+
raw?: unknown;
|
|
36
|
+
}
|
|
37
|
+
interface HonoLikeContext {
|
|
38
|
+
req?: {
|
|
39
|
+
parseBody?: (options?: Record<string, any>) => Promise<Record<string, unknown>>;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
interface H3LikeEvent {
|
|
43
|
+
context?: Record<string, any>;
|
|
44
|
+
req?: {
|
|
45
|
+
formData?: () => Promise<FormData>;
|
|
46
|
+
};
|
|
47
|
+
request?: {
|
|
48
|
+
formData?: () => Promise<FormData>;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Attaches uploaded files from an Express-like request object to the validator
|
|
53
|
+
* context under `requestFiles`. It supports both `request.file`
|
|
54
|
+
* and `request.files` properties, normalizing them into a consistent format.
|
|
55
|
+
* The original request object is also included in the context for plugin use.
|
|
56
|
+
*
|
|
57
|
+
* @param validator The validator instance to attach the files to.
|
|
58
|
+
* @param request The Express-like request object containing the uploaded files.
|
|
59
|
+
* @returns The validator instance with the updated context.
|
|
60
|
+
*/
|
|
61
|
+
declare function withExpressUploadContext<TValidator extends ValidatorWithContext<TValidator>>(validator: TValidator, request: ExpressLikeRequest): TValidator;
|
|
62
|
+
/**
|
|
63
|
+
* Attaches uploaded files from an Express-like request object to the validator
|
|
64
|
+
* context for use within validation rules.
|
|
65
|
+
*
|
|
66
|
+
* @param request The Express-like request object containing the uploaded files.
|
|
67
|
+
* @returns A context object containing the uploaded files and the original request.
|
|
68
|
+
*/
|
|
69
|
+
declare function useExpressUploadContext(request: ExpressLikeRequest): Record<string, any>;
|
|
70
|
+
/**
|
|
71
|
+
* Attaches uploaded files from a Fastify-like request object to the validator context under `requestFiles`.
|
|
72
|
+
* It supports multiple ways that files may be provided (e.g., `request.files()`,
|
|
73
|
+
* `request.parts()`, `request.file()`, and nested `request.raw.files`), normalizing
|
|
74
|
+
* them into a consistent format. The original request object is also included in
|
|
75
|
+
* the context for plugin use.
|
|
76
|
+
*
|
|
77
|
+
* @param validator
|
|
78
|
+
* @param request
|
|
79
|
+
* @returns
|
|
80
|
+
*/
|
|
81
|
+
declare function withFastifyUploadContext<TValidator extends ValidatorWithContext<TValidator>>(validator: TValidator, request: FastifyLikeRequest): Promise<TValidator>;
|
|
82
|
+
/**
|
|
83
|
+
* Attaches uploaded files from a Fastify-like request object to the validator
|
|
84
|
+
* context for use within validation rules.
|
|
85
|
+
*
|
|
86
|
+
* @param request
|
|
87
|
+
* @returns
|
|
88
|
+
*/
|
|
89
|
+
declare function useFastifyUploadContext(request: FastifyLikeRequest): Promise<Record<string, any>>;
|
|
90
|
+
/**
|
|
91
|
+
* Attaches uploaded files from a Hono-like context to the validator context under `requestFiles`.
|
|
92
|
+
*
|
|
93
|
+
* @param validator
|
|
94
|
+
* @param context
|
|
95
|
+
* @returns
|
|
96
|
+
*/
|
|
97
|
+
declare function withHonoUploadContext<TValidator extends ValidatorWithContext<TValidator>>(validator: TValidator, context: HonoLikeContext): Promise<TValidator>;
|
|
98
|
+
/**
|
|
99
|
+
* Attaches uploaded files from a Hono-like context to the validator context for use within validation rules.
|
|
100
|
+
*
|
|
101
|
+
* @param context
|
|
102
|
+
* @returns
|
|
103
|
+
*/
|
|
104
|
+
declare function useHonoUploadContext(context: HonoLikeContext): Promise<Record<string, any>>;
|
|
105
|
+
/**
|
|
106
|
+
* Read files from multiple sources on the
|
|
107
|
+
* event, including `event.context.requestFiles`, `request.formData()`, and multipart
|
|
108
|
+
* parsing when available. It merges found files into the validator context
|
|
109
|
+
* under `requestFiles` and also attaches the original event and request for plugin use.
|
|
110
|
+
*
|
|
111
|
+
* @param validator The validator instance to which the files will be added.
|
|
112
|
+
* @param event The H3 event containing potential file uploads.
|
|
113
|
+
* @returns The validator instance with the merged file context.
|
|
114
|
+
*/
|
|
115
|
+
declare function withH3UploadContext<TValidator extends ValidatorWithContext<TValidator>>(validator: TValidator, event: H3LikeEvent): Promise<TValidator>;
|
|
116
|
+
/**
|
|
117
|
+
* Uses files from multiple sources on the event, including
|
|
118
|
+
* `event.context.requestFiles`, `request.formData()`, and multipart parsing when available.
|
|
119
|
+
*
|
|
120
|
+
* @param event
|
|
121
|
+
* @returns
|
|
122
|
+
*/
|
|
123
|
+
declare function useH3UploadContext(event: H3LikeEvent): Promise<Record<string, any>>;
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region src/index.d.ts
|
|
126
|
+
declare module 'kanun' {
|
|
127
|
+
interface CustomValidationRuleNameMap {
|
|
128
|
+
dimensions: 'paramable';
|
|
129
|
+
extensions: 'paramable';
|
|
130
|
+
file: 'plain';
|
|
131
|
+
files: 'plain';
|
|
132
|
+
image: 'plain';
|
|
133
|
+
mimes: 'paramable';
|
|
134
|
+
mimetypes: 'paramable';
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Create the validation rules for a wildcard attribute based on the given item and
|
|
139
|
+
* collection rules.
|
|
140
|
+
*
|
|
141
|
+
* @param attribute The name of the attribute.
|
|
142
|
+
* @param itemRules The rules to apply to each individual file.
|
|
143
|
+
* @param collectionRules The rules to apply to the array of files as a whole.
|
|
144
|
+
* @returns An object containing the normalized rules for the attribute.
|
|
145
|
+
*/
|
|
146
|
+
declare function createWildcardFileRules<TAttribute extends string>(attribute: TAttribute, itemRules: WildcardRuleInput, collectionRules?: WildcardRuleInput): Record<TAttribute | `${TAttribute}.*`, string[]>;
|
|
147
|
+
/**
|
|
148
|
+
* Sync the files from the request context to the validator's data.
|
|
149
|
+
*
|
|
150
|
+
* @param validator
|
|
151
|
+
* @param attributes
|
|
152
|
+
* @returns
|
|
153
|
+
*/
|
|
154
|
+
declare function syncRequestFilesToData<TValidator extends ValidatorWithData<TValidator>>(validator: TValidator, attributes?: string[]): TValidator;
|
|
155
|
+
/**
|
|
156
|
+
* Create the file validator plugin with the given options.
|
|
157
|
+
*
|
|
158
|
+
* @param options
|
|
159
|
+
* @returns
|
|
160
|
+
*/
|
|
161
|
+
declare function createFileValidatorPlugin(options?: FileValidatorPluginOptions): ValidatorPlugin;
|
|
162
|
+
declare const fileValidatorPlugin: ValidatorPlugin;
|
|
163
|
+
//#endregion
|
|
164
|
+
export { ExpressLikeRequest, FastifyLikeRequest, H3LikeEvent, HonoLikeContext, ValidatorWithContext, createFileValidatorPlugin, createWildcardFileRules, fileValidatorPlugin, syncRequestFilesToData, useExpressUploadContext, useFastifyUploadContext, useH3UploadContext, useHonoUploadContext, withExpressUploadContext, withFastifyUploadContext, withH3UploadContext, withHonoUploadContext };
|