Skip to content
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

Merged
merged 2 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 22 additions & 10 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FastifyErrorConstructor } from "@fastify/error";

type MultipartHandler = (
field: string,
file: any,
file: BusboyFileStream,
filename: string,
encoding: string,
mimetype: string,
Expand All @@ -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;
Copy link
Contributor Author

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.

fieldname: string;
mimetype?: string;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string | undefined because mime type get swallowed when we parse the value as JSON. I think this is a bug, but that's for another PR.

encoding: string;
fieldnameTruncated: boolean;
valueTruncated: boolean;
fields: MultipartFields;
}

interface MultipartErrors {
Expand All @@ -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 {
Expand Down
29 changes: 22 additions & 7 deletions test/index.test-d.ts
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"
Expand Down Expand Up @@ -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)
Expand All @@ -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);

Expand All @@ -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();

Expand All @@ -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()
})
Expand All @@ -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) {

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your problem is TypeScript issue.
TypeScript cannot restrict the type in certain format that's the cause.

The README example is certainly valid in Javascript.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, typescript wants you to use in for narrowing in this situation, which works for both JS and TS.
https://www.typescriptlang.org/docs/handbook/2/narrowing.html#the-in-operator-narrowing

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()
Expand All @@ -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()
Expand Down