Skip to content

Commit 49f3a62

Browse files
authored
fix(types): make definitions nodenext compatible (#396)
* chore: make definitions nodenext compatible * chore: add index necessary changes
1 parent 4e21cdc commit 49f3a62

File tree

3 files changed

+166
-115
lines changed

3 files changed

+166
-115
lines changed

index.d.ts

Lines changed: 151 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,111 @@
11
import { Busboy, BusboyConfig, BusboyFileStream } from "@fastify/busboy";
22
import { FastifyPluginCallback } from "fastify";
3-
import { Readable } from 'stream';
3+
import { Readable } from "stream";
44
import { FastifyErrorConstructor } from "@fastify/error";
55

6-
type MultipartHandler = (
7-
field: string,
8-
file: BusboyFileStream,
9-
filename: string,
10-
encoding: string,
11-
mimetype: string,
12-
) => void;
13-
14-
interface BodyEntry {
15-
data: Buffer,
16-
filename: string,
17-
encoding: string,
18-
mimetype: string,
19-
limit: false
20-
}
21-
22-
export interface MultipartFields {
23-
[fieldname: string]: Multipart | Multipart[] | undefined;
6+
declare module "fastify" {
7+
interface FastifyRequest {
8+
isMultipart: () => boolean;
9+
10+
// promise api
11+
parts: (
12+
options?: Omit<BusboyConfig, "headers">
13+
) => AsyncIterableIterator<fastifyMultipart.Multipart>;
14+
15+
// legacy
16+
multipart: (
17+
handler: MultipartHandler,
18+
next: (err: Error) => void,
19+
options?: Omit<BusboyConfig, "headers">
20+
) => Busboy;
21+
22+
// Stream mode
23+
file: (
24+
options?: Omit<BusboyConfig, "headers">
25+
) => Promise<fastifyMultipart.MultipartFile | undefined>;
26+
files: (
27+
options?: Omit<BusboyConfig, "headers">
28+
) => AsyncIterableIterator<fastifyMultipart.MultipartFile>;
29+
30+
// Disk mode
31+
saveRequestFiles: (
32+
options?: Omit<BusboyConfig, "headers"> & { tmpdir?: string }
33+
) => Promise<Array<fastifyMultipart.SavedMultipartFile>>;
34+
cleanRequestFiles: () => Promise<void>;
35+
tmpUploads: Array<string> | null;
36+
}
37+
38+
interface FastifyInstance {
39+
multipartErrors: MultipartErrors;
40+
}
2441
}
2542

26-
export type Multipart = MultipartFile | MultipartValue;
43+
type FastifyMultipartPlugin = FastifyPluginCallback<
44+
| fastifyMultipart.FastifyMultipartBaseOptions
45+
| fastifyMultipart.FastifyMultipartOptions
46+
| fastifyMultipart.FastifyMultipartAttachFieldsToBodyOptions
47+
>;
2748

28-
export interface MultipartFile {
29-
toBuffer: () => Promise<Buffer>,
49+
type MultipartHandler = (
50+
field: string,
3051
file: BusboyFileStream,
31-
fieldname: string,
3252
filename: string,
3353
encoding: string,
34-
mimetype: string,
35-
fields: MultipartFields
36-
}
37-
38-
export interface SavedMultipartFile extends MultipartFile {
39-
/**
40-
* Path to the temporary file
41-
*/
42-
filepath: string,
43-
}
54+
mimetype: string
55+
) => void;
4456

45-
export interface MultipartValue<T = unknown> {
46-
value: T;
47-
fieldname: string;
48-
mimetype: string;
57+
interface BodyEntry {
58+
data: Buffer;
59+
filename: string;
4960
encoding: string;
50-
fieldnameTruncated: boolean;
51-
valueTruncated: boolean;
52-
fields: MultipartFields;
61+
mimetype: string;
62+
limit: false;
5363
}
5464

5565
interface MultipartErrors {
56-
PartsLimitError: FastifyErrorConstructor,
57-
FilesLimitError: FastifyErrorConstructor,
58-
FieldsLimitError: FastifyErrorConstructor,
59-
PrototypeViolationError: FastifyErrorConstructor,
60-
InvalidMultipartContentTypeError: FastifyErrorConstructor,
61-
RequestFileTooLargeError: FastifyErrorConstructor
66+
PartsLimitError: FastifyErrorConstructor;
67+
FilesLimitError: FastifyErrorConstructor;
68+
FieldsLimitError: FastifyErrorConstructor;
69+
PrototypeViolationError: FastifyErrorConstructor;
70+
InvalidMultipartContentTypeError: FastifyErrorConstructor;
71+
RequestFileTooLargeError: FastifyErrorConstructor;
6272
}
6373

64-
declare module "fastify" {
65-
interface FastifyRequest {
66-
isMultipart: () => boolean;
67-
68-
// promise api
69-
parts: (options?: Omit<BusboyConfig, 'headers'>) => AsyncIterableIterator<Multipart>
70-
71-
// legacy
72-
multipart: (handler: MultipartHandler, next: (err: Error) => void, options?: Omit<BusboyConfig, 'headers'>) => Busboy;
73-
74-
// Stream mode
75-
file: (options?: Omit<BusboyConfig, 'headers'>) => Promise<MultipartFile | undefined>
76-
files: (options?: Omit<BusboyConfig, 'headers'>) => AsyncIterableIterator<MultipartFile>
77-
78-
// Disk mode
79-
saveRequestFiles: (options?: Omit<BusboyConfig, 'headers'> & { tmpdir?: string }) => Promise<Array<SavedMultipartFile>>
80-
cleanRequestFiles: () => Promise<void>
81-
tmpUploads: Array<string> | null
82-
}
83-
84-
interface FastifyInstance {
85-
multipartErrors: MultipartErrors
86-
}
87-
}
74+
declare namespace fastifyMultipart {
75+
export interface SavedMultipartFile extends MultipartFile {
76+
/**
77+
* Path to the temporary file
78+
*/
79+
filepath: string;
80+
}
81+
82+
export type Multipart = MultipartFile | MultipartValue;
83+
84+
export interface MultipartFile {
85+
toBuffer: () => Promise<Buffer>;
86+
file: BusboyFileStream;
87+
fieldname: string;
88+
filename: string;
89+
encoding: string;
90+
mimetype: string;
91+
fields: MultipartFields;
92+
}
93+
94+
export interface MultipartValue<T = unknown> {
95+
value: T;
96+
fieldname: string;
97+
mimetype: string;
98+
encoding: string;
99+
fieldnameTruncated: boolean;
100+
valueTruncated: boolean;
101+
fields: MultipartFields;
102+
}
103+
104+
export interface MultipartFields {
105+
[fieldname: string]: Multipart | Multipart[] | undefined;
106+
}
88107

89-
export interface FastifyMultipartBaseOptions {
108+
export interface FastifyMultipartBaseOptions {
90109
/**
91110
* Append the multipart parameters to the body object
92111
*/
@@ -100,7 +119,7 @@ export interface FastifyMultipartBaseOptions {
100119
/**
101120
* Allow throwing error when file size limit reached.
102121
*/
103-
throwFileSizeLimit?: boolean
122+
throwFileSizeLimit?: boolean;
104123

105124
/**
106125
* Detect if a Part is a file.
@@ -111,64 +130,82 @@ export interface FastifyMultipartBaseOptions {
111130
*
112131
* Modify this to handle e.g. Blobs.
113132
*/
114-
isPartAFile?: (fieldName: string | undefined, contentType: string | undefined, fileName: string | undefined) => boolean;
133+
isPartAFile?: (
134+
fieldName: string | undefined,
135+
contentType: string | undefined,
136+
fileName: string | undefined
137+
) => boolean;
115138

116139
limits?: {
117-
/**
118-
* Max field name size in bytes
119-
*/
120-
fieldNameSize?: number;
121-
122-
/**
123-
* Max field value size in bytes
124-
*/
125-
fieldSize?: number;
126-
127-
/**
128-
* Max number of non-file fields
129-
*/
130-
fields?: number;
131-
132-
/**
133-
* For multipart forms, the max file size
134-
*/
135-
fileSize?: number;
136-
137-
/**
138-
* Max number of file fields
139-
*/
140-
files?: number;
141-
142-
/**
143-
* Max number of header key=>value pairs
144-
*/
145-
headerPairs?: number;
146-
}
147-
}
148-
149-
export interface FastifyMultipartOptions extends FastifyMultipartBaseOptions {
140+
/**
141+
* Max field name size in bytes
142+
*/
143+
fieldNameSize?: number;
144+
145+
/**
146+
* Max field value size in bytes
147+
*/
148+
fieldSize?: number;
149+
150+
/**
151+
* Max number of non-file fields
152+
*/
153+
fields?: number;
154+
155+
/**
156+
* For multipart forms, the max file size
157+
*/
158+
fileSize?: number;
159+
160+
/**
161+
* Max number of file fields
162+
*/
163+
files?: number;
164+
165+
/**
166+
* Max number of header key=>value pairs
167+
*/
168+
headerPairs?: number;
169+
};
170+
}
171+
172+
export interface FastifyMultipartOptions extends FastifyMultipartBaseOptions {
150173
/**
151174
* Only valid in the promise api. Append the multipart parameters to the body object.
152175
*/
153-
attachFieldsToBody?: false
176+
attachFieldsToBody?: false;
154177

155178
/**
156179
* Manage the file stream like you need
157180
*/
158-
onFile?: (fieldName: string, stream: Readable, filename: string, encoding: string, mimetype: string, body: Record<string, BodyEntry>) => void | Promise<void>;
159-
}
160-
161-
export interface FastifyMultipartAttactFieldsToBodyOptions extends FastifyMultipartBaseOptions {
181+
onFile?: (
182+
fieldName: string,
183+
stream: Readable,
184+
filename: string,
185+
encoding: string,
186+
mimetype: string,
187+
body: Record<string, BodyEntry>
188+
) => void | Promise<void>;
189+
}
190+
191+
export interface FastifyMultipartAttachFieldsToBodyOptions
192+
extends FastifyMultipartBaseOptions {
162193
/**
163194
* Only valid in the promise api. Append the multipart parameters to the body object.
164195
*/
165-
attachFieldsToBody: true | 'keyValues';
196+
attachFieldsToBody: true | "keyValues";
166197

167198
/**
168199
* Manage the file stream like you need
169200
*/
170201
onFile?: (part: MultipartFile) => void | Promise<void>;
202+
}
203+
204+
export const fastifyMultipart: FastifyMultipartPlugin;
205+
export { fastifyMultipart as default };
171206
}
207+
declare function fastifyMultipart(
208+
...params: Parameters<FastifyMultipartPlugin>
209+
): ReturnType<FastifyMultipartPlugin>;
172210

173-
declare const fastifyMultipart: FastifyPluginCallback<FastifyMultipartOptions | FastifyMultipartAttactFieldsToBodyOptions>;
174-
export default fastifyMultipart;
211+
export = fastifyMultipart;

index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,15 @@ function fastifyMultipart (fastify, options, done) {
575575
done()
576576
}
577577

578-
module.exports = fp(fastifyMultipart, {
578+
const _fastifyMultipart = fp(fastifyMultipart, {
579579
fastify: '4.x',
580580
name: '@fastify/multipart'
581581
})
582+
583+
/**
584+
* These export configurations enable JS and TS developers
585+
* to consumer fastify in whatever way best suits their needs.
586+
*/
587+
module.exports = _fastifyMultipart
588+
module.exports.fastifyMultipart = _fastifyMultipart
589+
module.exports.default = _fastifyMultipart

test/named-import.test-d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import fastify from "fastify";
2+
import { fastifyMultipart } from "..";
3+
4+
const app = fastify();
5+
6+
app.register(fastifyMultipart);

0 commit comments

Comments
 (0)