Skip to content

Commit cddccd4

Browse files
authored
refactor(types): migrate from tsd to tstyche (#617)
1 parent 6640652 commit cddccd4

5 files changed

Lines changed: 50 additions & 61 deletions

File tree

package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828
"noop-stream": "^0.1.0",
2929
"pump": "^3.0.0",
3030
"readable-stream": "^4.5.2",
31-
"tsd": "^0.33.0"
31+
"tstyche": "^7.0.0"
3232
},
3333
"scripts": {
3434
"climem": "climem 8999 localhost",
3535
"lint": "eslint",
3636
"lint:fix": "eslint --fix",
3737
"start": "CLIMEM=8999 node -r climem ./examples/example",
3838
"test": "npm run test:unit && npm run test:typescript",
39-
"test:typescript": "tsd",
39+
"test:typescript": "tstyche",
4040
"test:unit": "c8 --100 node --test"
4141
},
4242
"repository": {
@@ -84,10 +84,7 @@
8484
}
8585
],
8686
"license": "MIT",
87-
"tsd": {
88-
"directory": "test"
89-
},
9087
"publishConfig": {
9188
"access": "public"
9289
}
93-
}
90+
}

types/avj-plugin.test-d.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

types/avj-plugin.tst.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as fastifyMultipart from '..'
2+
import { ajvFilePlugin } from '..'
3+
import { expect } from 'tstyche'
4+
5+
expect(ajvFilePlugin).type.toBeAssignableTo<Function>()
6+
expect(fastifyMultipart.ajvFilePlugin).type.toBe(ajvFilePlugin)
Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* eslint-disable @typescript-eslint/no-unused-expressions */
22
import fastify from 'fastify'
3-
import fastifyMultipart, { MultipartValue, MultipartFields, MultipartFile } from '..'
3+
import fastifyMultipart, { MultipartValue, MultipartFields, MultipartFile } from '.'
44
import * as util from 'node:util'
55
import { pipeline } from 'node:stream'
66
import * as fs from 'node:fs'
7-
import { expectError, expectType } from 'tsd'
7+
import { expect } from 'tstyche'
88
import { FastifyErrorConstructor } from '@fastify/error'
99
import { BusboyConfig, BusboyFileStream } from '@fastify/busboy'
1010

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

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

33-
expectType<'file'>(data.type)
34-
expectType<BusboyFileStream>(data.file)
35-
expectType<boolean>(data.file.truncated)
36-
expectType<MultipartFields>(data.fields)
37-
expectType<string>(data.fieldname)
38-
expectType<string>(data.filename)
39-
expectType<string>(data.encoding)
40-
expectType<string>(data.mimetype)
33+
expect(data.type).type.toBe<'file'>()
34+
expect(data.file).type.toBe<BusboyFileStream>()
35+
expect(data.file.truncated).type.toBe<boolean>()
36+
expect(data.fields).type.toBe<MultipartFields>()
37+
expect(data.fieldname).type.toBe<string>()
38+
expect(data.filename).type.toBe<string>()
39+
expect(data.encoding).type.toBe<string>()
40+
expect(data.mimetype).type.toBe<string>()
4141

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

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

66-
expectType<BusboyFileStream>(req.body.file.file)
67-
expectType<'file'>(req.body.file.type)
66+
expect(req.body.file.file).type.toBe<BusboyFileStream>()
67+
expect(req.body.file.type).type.toBe<'file'>()
6868
reply.send()
6969
})
7070

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

75-
// file is a file
76-
expectType<BusboyFileStream>(req.body.file.file)
77-
expectError(req.body.file.value)
75+
expect(req.body.file.file).type.toBe<BusboyFileStream>()
76+
expect(req.body.file).type.not.toHaveProperty('value')
7877
})
7978

8079
// busboy
@@ -84,9 +83,9 @@ const runServer = async () => {
8483
throwFileSizeLimit: true,
8584
sharedSchemaId: 'schemaId',
8685
isPartAFile: (fieldName, contentType, fileName) => {
87-
expectType<string | undefined>(fieldName)
88-
expectType<string | undefined>(contentType)
89-
expectType<string | undefined>(fileName)
86+
expect(fieldName).type.toBe<string | undefined>()
87+
expect(contentType).type.toBe<string | undefined>()
88+
expect(fileName).type.toBe<string | undefined>()
9089
return true
9190
}
9291
})
@@ -102,9 +101,9 @@ const runServer = async () => {
102101
throwFileSizeLimit: true,
103102
sharedSchemaId: 'schemaId',
104103
isPartAFile: (fieldName, contentType, fileName) => {
105-
expectType<string | undefined>(fieldName)
106-
expectType<string | undefined>(contentType)
107-
expectType<string | undefined>(fileName)
104+
expect(fieldName).type.toBe<string | undefined>()
105+
expect(contentType).type.toBe<string | undefined>()
106+
expect(fileName).type.toBe<string | undefined>()
108107
return true
109108
}
110109
})
@@ -131,7 +130,7 @@ const runServer = async () => {
131130
app.post('/upload/raw/any', async function (req, reply) {
132131
const data = await req.file()
133132
if (!data) throw new Error('missing file')
134-
expectType<Buffer>(await data.toBuffer())
133+
expect(await data.toBuffer()).type.toBe<Buffer>()
135134
// upload to S3
136135
reply.send()
137136
})
@@ -140,13 +139,13 @@ const runServer = async () => {
140139
app.post('/upload/files', async function (req, reply) {
141140
// stores files to tmp dir and return files + values
142141
const { files, values } = await req.saveRequestFiles()
143-
files[0].type // "file"
144-
files[0].filepath
145-
files[0].fieldname
146-
files[0].filename
147-
files[0].encoding
148-
files[0].mimetype
149-
files[0].fields // other parsed parts
142+
files[0]!.type // "file"
143+
files[0]!.filepath
144+
files[0]!.fieldname
145+
files[0]!.filename
146+
files[0]!.encoding
147+
files[0]!.mimetype
148+
files[0]!.fields // other parsed parts
150149
values.foo
151150

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

167-
expectType<FastifyErrorConstructor>(app.multipartErrors.FieldsLimitError)
168-
expectType<FastifyErrorConstructor>(app.multipartErrors.FilesLimitError)
169-
expectType<FastifyErrorConstructor>(app.multipartErrors.InvalidMultipartContentTypeError)
170-
expectType<FastifyErrorConstructor>(app.multipartErrors.PartsLimitError)
171-
expectType<FastifyErrorConstructor>(app.multipartErrors.PrototypeViolationError)
172-
expectType<FastifyErrorConstructor>(app.multipartErrors.RequestFileTooLargeError)
166+
expect(app.multipartErrors.FieldsLimitError).type.toBe<FastifyErrorConstructor>()
167+
expect(app.multipartErrors.FilesLimitError).type.toBe<FastifyErrorConstructor>()
168+
expect(app.multipartErrors.InvalidMultipartContentTypeError).type.toBe<FastifyErrorConstructor>()
169+
expect(app.multipartErrors.PartsLimitError).type.toBe<FastifyErrorConstructor>()
170+
expect(app.multipartErrors.PrototypeViolationError).type.toBe<FastifyErrorConstructor>()
171+
expect(app.multipartErrors.RequestFileTooLargeError).type.toBe<FastifyErrorConstructor>()
173172

174173
// test instanceof Error
175174
const a = new FilesLimitError()
@@ -185,12 +184,12 @@ const runServer = async () => {
185184
multipartOptions: {}
186185
}
187186
}, async function (req, reply) {
188-
expectType<Omit<BusboyConfig, 'headers'>>(req.routeOptions.config.multipartOptions)
187+
expect(req.routeOptions.config.multipartOptions).type.toBe<Omit<BusboyConfig, 'headers'>>()
189188
reply.send()
190189
})
191190

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

0 commit comments

Comments
 (0)