-
-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve typescript types #390
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ import { FastifyErrorConstructor } from "@fastify/error"; | |
|
||
type MultipartHandler = ( | ||
field: string, | ||
file: any, | ||
file: BusboyFileStream, | ||
filename: string, | ||
encoding: string, | ||
mimetype: string, | ||
|
@@ -20,24 +20,36 @@ interface BodyEntry { | |
} | ||
|
||
export interface MultipartFields { | ||
[x: string]: Multipart | Multipart[]; | ||
[fieldname: string]: Multipart | Multipart[] | undefined; | ||
} | ||
|
||
export type Multipart<T = true> = T extends true ? MultipartFile : MultipartValue<T>; | ||
export type Multipart = MultipartFile | MultipartValue; | ||
|
||
export interface MultipartFile { | ||
toBuffer: () => Promise<Buffer>, | ||
file: BusboyFileStream, | ||
filepath: string, | ||
fieldname: string, | ||
filename: string, | ||
encoding: string, | ||
mimetype: string, | ||
fields: MultipartFields | ||
} | ||
|
||
export interface MultipartValue<T> { | ||
value: T | ||
export interface SavedMultipartFile extends MultipartFile { | ||
/** | ||
* Path to the temporary file | ||
*/ | ||
filepath: string, | ||
} | ||
|
||
export interface MultipartValue<T = unknown> { | ||
value: T; | ||
fieldname: string; | ||
mimetype?: string; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
encoding: string; | ||
fieldnameTruncated: boolean; | ||
valueTruncated: boolean; | ||
fields: MultipartFields; | ||
} | ||
|
||
interface MultipartErrors { | ||
|
@@ -60,13 +72,13 @@ declare module "fastify" { | |
multipart: (handler: MultipartHandler, next: (err: Error) => void, options?: Omit<BusboyConfig, 'headers'>) => Busboy; | ||
|
||
// Stream mode | ||
file: (options?: Omit<BusboyConfig, 'headers'>) => Promise<Multipart> | ||
files: (options?: Omit<BusboyConfig, 'headers'>) => AsyncIterableIterator<Multipart> | ||
file: (options?: Omit<BusboyConfig, 'headers'>) => Promise<MultipartFile | undefined> | ||
files: (options?: Omit<BusboyConfig, 'headers'>) => AsyncIterableIterator<MultipartFile> | ||
|
||
// Disk mode | ||
saveRequestFiles: (options?: Omit<BusboyConfig, 'headers'> & { tmpdir?: string }) => Promise<Array<Multipart>> | ||
saveRequestFiles: (options?: Omit<BusboyConfig, 'headers'> & { tmpdir?: string }) => Promise<Array<SavedMultipartFile>> | ||
cleanRequestFiles: () => Promise<void> | ||
tmpUploads: Array<Multipart> | ||
tmpUploads: Array<string> | null | ||
} | ||
|
||
interface FastifyInstance { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
import fastify from 'fastify' | ||
import fastifyMultipart from '..' | ||
import { Multipart, MultipartFields, MultipartFile } from '..' | ||
import fastifyMultipart, {MultipartValue, MultipartFields, MultipartFile } from '..' | ||
import * as util from 'util' | ||
import { pipeline, Readable } from 'stream' | ||
import { pipeline } from 'stream' | ||
import * as fs from 'fs' | ||
import { expectError, expectType } from 'tsd' | ||
import { FastifyErrorConstructor } from "@fastify/error" | ||
|
@@ -55,6 +54,7 @@ const runServer = async () => { | |
// usage | ||
app.post('/', async (req, reply) => { | ||
const data = await req.file() | ||
if (data == null) throw new Error('missing file') | ||
|
||
expectType<BusboyFileStream>(data.file) | ||
expectType<boolean>(data.file.truncated) | ||
|
@@ -63,14 +63,27 @@ const runServer = async () => { | |
expectType<string>(data.filename) | ||
expectType<string>(data.encoding) | ||
expectType<string>(data.mimetype) | ||
|
||
const field = data.fields.myField; | ||
if (field === undefined) { | ||
// field missing from the request | ||
} else if (Array.isArray(field)) { | ||
// multiple fields with the same name | ||
} else if ('file' in field) { | ||
// field containing a file | ||
field.file.resume() | ||
} else { | ||
// field containing a value | ||
field.fields.value; | ||
} | ||
|
||
await pump(data.file, fs.createWriteStream(data.filename)) | ||
|
||
reply.send() | ||
}) | ||
|
||
// Multiple fields including scalar values | ||
app.post<{Body: {file: Multipart, foo: Multipart<string>}}>('/upload/stringvalue', async (req, reply) => { | ||
app.post<{Body: {file: MultipartFile, foo: MultipartValue<string>}}>('/upload/stringvalue', async (req, reply) => { | ||
expectError(req.body.foo.file); | ||
expectType<string>(req.body.foo.value); | ||
|
||
|
@@ -79,7 +92,7 @@ const runServer = async () => { | |
reply.send(); | ||
}) | ||
|
||
app.post<{Body: {file: Multipart, num: Multipart<number>}}>('/upload/stringvalue', async (req, reply) => { | ||
app.post<{Body: {file: MultipartFile, num: MultipartValue<number>}}>('/upload/stringvalue', async (req, reply) => { | ||
expectType<number>(req.body.num.value); | ||
reply.send(); | ||
|
||
|
@@ -92,6 +105,7 @@ const runServer = async () => { | |
app.post('/', async function (req, reply) { | ||
const options: Partial<BusboyConfig> = { limits: { fileSize: 1000 } }; | ||
const data = await req.file(options) | ||
if (!data) throw new Error('missing file') | ||
await pump(data.file, fs.createWriteStream(data.filename)) | ||
reply.send() | ||
}) | ||
|
@@ -109,10 +123,10 @@ const runServer = async () => { | |
app.post('/upload/raw/any', async function (req, reply) { | ||
const parts = req.parts() | ||
for await (const part of parts) { | ||
if (part.file) { | ||
if ('file' in part) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be modified on README as well. But this needs to be revisited to be on the type. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your problem is The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, typescript wants you to use A discriminator would perhaps be ideal: export interface MultipartFile {
type: 'file',
...
}
export interface MultipartValue<T = unknown> {
type: 'value',
...
}
if (part.type === 'file') {
part.toBuffer()
} |
||
await pump(part.file, fs.createWriteStream(part.filename)) | ||
} else { | ||
console.log(part) | ||
console.log(part.value) | ||
} | ||
} | ||
reply.send() | ||
|
@@ -121,6 +135,7 @@ const runServer = async () => { | |
// accumulate whole file in memory | ||
app.post('/upload/raw/any', async function (req, reply) { | ||
const data = await req.file() | ||
if (!data) throw new Error('missing file') | ||
const buffer = await data.toBuffer() | ||
// upload to S3 | ||
reply.send() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The value should be
unknown
, since it might be parsed as JSON based on the content-type provided by the caller. I've left it generic in case someone wants to assert its type.