diff --git a/package.json b/package.json index aa890833..90417724 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "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", @@ -36,7 +36,7 @@ "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": { @@ -84,10 +84,7 @@ } ], "license": "MIT", - "tsd": { - "directory": "test" - }, "publishConfig": { "access": "public" } -} +} \ No newline at end of file diff --git a/types/avj-plugin.test-d.ts b/types/avj-plugin.test-d.ts deleted file mode 100644 index 22f999a9..00000000 --- a/types/avj-plugin.test-d.ts +++ /dev/null @@ -1,13 +0,0 @@ -import fastify from 'fastify' -import { fastifyMultipart, ajvFilePlugin } from '..' - -const app = fastify({ - ajv: { - plugins: [ - ajvFilePlugin, - (await import('..')).ajvFilePlugin - ] - } -}) - -app.register(fastifyMultipart) diff --git a/types/avj-plugin.tst.ts b/types/avj-plugin.tst.ts new file mode 100644 index 00000000..efd30c29 --- /dev/null +++ b/types/avj-plugin.tst.ts @@ -0,0 +1,6 @@ +import * as fastifyMultipart from '..' +import { ajvFilePlugin } from '..' +import { expect } from 'tstyche' + +expect(ajvFilePlugin).type.toBeAssignableTo() +expect(fastifyMultipart.ajvFilePlugin).type.toBe(ajvFilePlugin) diff --git a/types/index.test-d.ts b/types/index.tst.ts similarity index 65% rename from types/index.test-d.ts rename to types/index.tst.ts index 9562213d..949de7bf 100644 --- a/types/index.test-d.ts +++ b/types/index.tst.ts @@ -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' @@ -26,18 +26,18 @@ const runServer = async () => { // usage app.post('/', async (req, reply) => { - expectType>(req.formData()) + expect(req.formData()).type.toBe>() const data = await req.file() if (data == null) throw new Error('missing file') - expectType<'file'>(data.type) - expectType(data.file) - expectType(data.file.truncated) - expectType(data.fields) - expectType(data.fieldname) - expectType(data.filename) - expectType(data.encoding) - expectType(data.mimetype) + expect(data.type).type.toBe<'file'>() + expect(data.file).type.toBe() + expect(data.file.truncated).type.toBe() + expect(data.fields).type.toBe() + expect(data.fieldname).type.toBe() + expect(data.filename).type.toBe() + expect(data.encoding).type.toBe() + expect(data.mimetype).type.toBe() const field = data.fields.myField if (field === undefined) { @@ -59,22 +59,21 @@ const runServer = async () => { // Multiple fields including scalar values app.post<{ Body: { file: MultipartFile, foo: MultipartValue } }>('/upload/stringvalue', async (req, reply) => { - expectError(req.body.foo.file) - expectType<'field'>(req.body.foo.type) - expectType(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() - expectType(req.body.file.file) - expectType<'file'>(req.body.file.type) + expect(req.body.file.file).type.toBe() + expect(req.body.file.type).type.toBe<'file'>() reply.send() }) app.post<{ Body: { file: MultipartFile, num: MultipartValue } }>('/upload/stringvalue', async (req, reply) => { - expectType(req.body.num.value) + expect(req.body.num.value).type.toBe() reply.send() - // file is a file - expectType(req.body.file.file) - expectError(req.body.file.value) + expect(req.body.file.file).type.toBe() + expect(req.body.file).type.not.toHaveProperty('value') }) // busboy @@ -84,9 +83,9 @@ const runServer = async () => { throwFileSizeLimit: true, sharedSchemaId: 'schemaId', isPartAFile: (fieldName, contentType, fileName) => { - expectType(fieldName) - expectType(contentType) - expectType(fileName) + expect(fieldName).type.toBe() + expect(contentType).type.toBe() + expect(fileName).type.toBe() return true } }) @@ -102,9 +101,9 @@ const runServer = async () => { throwFileSizeLimit: true, sharedSchemaId: 'schemaId', isPartAFile: (fieldName, contentType, fileName) => { - expectType(fieldName) - expectType(contentType) - expectType(fileName) + expect(fieldName).type.toBe() + expect(contentType).type.toBe() + expect(fileName).type.toBe() return true } }) @@ -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(await data.toBuffer()) + expect(await data.toBuffer()).type.toBe() // upload to S3 reply.send() }) @@ -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() @@ -164,12 +163,12 @@ const runServer = async () => { app.post('/upload/files', async function (_req, reply) { const { FilesLimitError } = app.multipartErrors - expectType(app.multipartErrors.FieldsLimitError) - expectType(app.multipartErrors.FilesLimitError) - expectType(app.multipartErrors.InvalidMultipartContentTypeError) - expectType(app.multipartErrors.PartsLimitError) - expectType(app.multipartErrors.PrototypeViolationError) - expectType(app.multipartErrors.RequestFileTooLargeError) + expect(app.multipartErrors.FieldsLimitError).type.toBe() + expect(app.multipartErrors.FilesLimitError).type.toBe() + expect(app.multipartErrors.InvalidMultipartContentTypeError).type.toBe() + expect(app.multipartErrors.PartsLimitError).type.toBe() + expect(app.multipartErrors.PrototypeViolationError).type.toBe() + expect(app.multipartErrors.RequestFileTooLargeError).type.toBe() // test instanceof Error const a = new FilesLimitError() @@ -185,12 +184,12 @@ const runServer = async () => { multipartOptions: {} } }, async function (req, reply) { - expectType>(req.routeOptions.config.multipartOptions) + expect(req.routeOptions.config.multipartOptions).type.toBe>() reply.send() }) app.post('/upload/files', async function (req, reply) { - expectError>(req.routeOptions.config?.multipartOptions) + expect(req.routeOptions.config.multipartOptions).type.toBe | undefined>() reply.send() }) diff --git a/types/named-import.test-d.ts b/types/named-import.tst.ts similarity index 100% rename from types/named-import.test-d.ts rename to types/named-import.tst.ts