Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
"noop-stream": "^0.1.0",
"pump": "^3.0.0",
"readable-stream": "^4.5.2",
"tsd": "^0.33.0"
"tstyche": "^7.0.0"
},
"scripts": {
"climem": "climem 8999 localhost",
"lint": "eslint",
"lint:fix": "eslint --fix",
"start": "CLIMEM=8999 node -r climem ./examples/example",
"test": "npm run test:unit && npm run test:typescript",
"test:typescript": "tsd",
"test:typescript": "tstyche",
"test:unit": "c8 --100 node --test"
},
"repository": {
Expand Down Expand Up @@ -84,10 +84,7 @@
}
],
"license": "MIT",
"tsd": {
"directory": "test"
},
"publishConfig": {
"access": "public"
}
}
}
13 changes: 0 additions & 13 deletions types/avj-plugin.test-d.ts

This file was deleted.

6 changes: 6 additions & 0 deletions types/avj-plugin.tst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as fastifyMultipart from '..'
import { ajvFilePlugin } from '..'
import { expect } from 'tstyche'

expect(ajvFilePlugin).type.toBeAssignableTo<Function>()
expect(fastifyMultipart.ajvFilePlugin).type.toBe(ajvFilePlugin)
83 changes: 41 additions & 42 deletions types/index.test-d.ts → types/index.tst.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable @typescript-eslint/no-unused-expressions */
import fastify from 'fastify'
import fastifyMultipart, { MultipartValue, MultipartFields, MultipartFile } from '..'
import fastifyMultipart, { MultipartValue, MultipartFields, MultipartFile } from '.'
import * as util from 'node:util'
import { pipeline } from 'node:stream'
import * as fs from 'node:fs'
import { expectError, expectType } from 'tsd'
import { expect } from 'tstyche'
import { FastifyErrorConstructor } from '@fastify/error'
import { BusboyConfig, BusboyFileStream } from '@fastify/busboy'

Expand All @@ -26,18 +26,18 @@ const runServer = async () => {

// usage
app.post('/', async (req, reply) => {
expectType<Promise<FormData>>(req.formData())
expect(req.formData()).type.toBe<Promise<FormData>>()
const data = await req.file()
if (data == null) throw new Error('missing file')

expectType<'file'>(data.type)
expectType<BusboyFileStream>(data.file)
expectType<boolean>(data.file.truncated)
expectType<MultipartFields>(data.fields)
expectType<string>(data.fieldname)
expectType<string>(data.filename)
expectType<string>(data.encoding)
expectType<string>(data.mimetype)
expect(data.type).type.toBe<'file'>()
expect(data.file).type.toBe<BusboyFileStream>()
expect(data.file.truncated).type.toBe<boolean>()
expect(data.fields).type.toBe<MultipartFields>()
expect(data.fieldname).type.toBe<string>()
expect(data.filename).type.toBe<string>()
expect(data.encoding).type.toBe<string>()
expect(data.mimetype).type.toBe<string>()

const field = data.fields.myField
if (field === undefined) {
Expand All @@ -59,22 +59,21 @@ const runServer = async () => {

// Multiple fields including scalar values
app.post<{ Body: { file: MultipartFile, foo: MultipartValue<string> } }>('/upload/stringvalue', async (req, reply) => {
expectError(req.body.foo.file)
expectType<'field'>(req.body.foo.type)
expectType<string>(req.body.foo.value)
expect(req.body.foo).type.not.toHaveProperty('file')
expect(req.body.foo.type).type.toBe<'field'>()
expect(req.body.foo.value).type.toBe<string>()

expectType<BusboyFileStream>(req.body.file.file)
expectType<'file'>(req.body.file.type)
expect(req.body.file.file).type.toBe<BusboyFileStream>()
expect(req.body.file.type).type.toBe<'file'>()
reply.send()
})

app.post<{ Body: { file: MultipartFile, num: MultipartValue<number> } }>('/upload/stringvalue', async (req, reply) => {
expectType<number>(req.body.num.value)
expect(req.body.num.value).type.toBe<number>()
reply.send()

// file is a file
expectType<BusboyFileStream>(req.body.file.file)
expectError(req.body.file.value)
expect(req.body.file.file).type.toBe<BusboyFileStream>()
expect(req.body.file).type.not.toHaveProperty('value')
})

// busboy
Expand All @@ -84,9 +83,9 @@ const runServer = async () => {
throwFileSizeLimit: true,
sharedSchemaId: 'schemaId',
isPartAFile: (fieldName, contentType, fileName) => {
expectType<string | undefined>(fieldName)
expectType<string | undefined>(contentType)
expectType<string | undefined>(fileName)
expect(fieldName).type.toBe<string | undefined>()
expect(contentType).type.toBe<string | undefined>()
expect(fileName).type.toBe<string | undefined>()
return true
}
})
Expand All @@ -102,9 +101,9 @@ const runServer = async () => {
throwFileSizeLimit: true,
sharedSchemaId: 'schemaId',
isPartAFile: (fieldName, contentType, fileName) => {
expectType<string | undefined>(fieldName)
expectType<string | undefined>(contentType)
expectType<string | undefined>(fileName)
expect(fieldName).type.toBe<string | undefined>()
expect(contentType).type.toBe<string | undefined>()
expect(fileName).type.toBe<string | undefined>()
return true
}
})
Expand All @@ -131,7 +130,7 @@ const runServer = async () => {
app.post('/upload/raw/any', async function (req, reply) {
const data = await req.file()
if (!data) throw new Error('missing file')
expectType<Buffer>(await data.toBuffer())
expect(await data.toBuffer()).type.toBe<Buffer>()
// upload to S3
reply.send()
})
Expand All @@ -140,13 +139,13 @@ const runServer = async () => {
app.post('/upload/files', async function (req, reply) {
// stores files to tmp dir and return files + values
const { files, values } = await req.saveRequestFiles()
files[0].type // "file"
files[0].filepath
files[0].fieldname
files[0].filename
files[0].encoding
files[0].mimetype
files[0].fields // other parsed parts
files[0]!.type // "file"
files[0]!.filepath
files[0]!.fieldname
files[0]!.filename
files[0]!.encoding
files[0]!.mimetype
files[0]!.fields // other parsed parts
values.foo

reply.send()
Expand All @@ -164,12 +163,12 @@ const runServer = async () => {
app.post('/upload/files', async function (_req, reply) {
const { FilesLimitError } = app.multipartErrors

expectType<FastifyErrorConstructor>(app.multipartErrors.FieldsLimitError)
expectType<FastifyErrorConstructor>(app.multipartErrors.FilesLimitError)
expectType<FastifyErrorConstructor>(app.multipartErrors.InvalidMultipartContentTypeError)
expectType<FastifyErrorConstructor>(app.multipartErrors.PartsLimitError)
expectType<FastifyErrorConstructor>(app.multipartErrors.PrototypeViolationError)
expectType<FastifyErrorConstructor>(app.multipartErrors.RequestFileTooLargeError)
expect(app.multipartErrors.FieldsLimitError).type.toBe<FastifyErrorConstructor>()
expect(app.multipartErrors.FilesLimitError).type.toBe<FastifyErrorConstructor>()
expect(app.multipartErrors.InvalidMultipartContentTypeError).type.toBe<FastifyErrorConstructor>()
expect(app.multipartErrors.PartsLimitError).type.toBe<FastifyErrorConstructor>()
expect(app.multipartErrors.PrototypeViolationError).type.toBe<FastifyErrorConstructor>()
expect(app.multipartErrors.RequestFileTooLargeError).type.toBe<FastifyErrorConstructor>()

// test instanceof Error
const a = new FilesLimitError()
Expand All @@ -185,12 +184,12 @@ const runServer = async () => {
multipartOptions: {}
}
}, async function (req, reply) {
expectType<Omit<BusboyConfig, 'headers'>>(req.routeOptions.config.multipartOptions)
expect(req.routeOptions.config.multipartOptions).type.toBe<Omit<BusboyConfig, 'headers'>>()
reply.send()
})

app.post('/upload/files', async function (req, reply) {
expectError<Omit<BusboyConfig, 'headers'>>(req.routeOptions.config?.multipartOptions)
expect(req.routeOptions.config.multipartOptions).type.toBe<Omit<BusboyConfig, 'headers'> | undefined>()
reply.send()
})

Expand Down
File renamed without changes.
Loading