diff --git a/example.mjs b/examples/example.mjs similarity index 64% rename from example.mjs rename to examples/example.mjs index 8eb68fe..d294d16 100644 --- a/example.mjs +++ b/examples/example.mjs @@ -1,5 +1,5 @@ import Fastify from 'fastify' -import fastifyFetch from './index.js' +import fastifyFetch from '../index.js' const fastify = Fastify({ logger: true }) @@ -24,3 +24,17 @@ fastify.fetch.get('/search', async (request, ctx) => { }) await fastify.listen({ port: 3000 }) + +/** + * Test the endpoints using curl: + * + * curl http://localhost:3000/hello + * + * curl http://localhost:3000/users/42 + * + * curl -X POST http://localhost:3000/data \ + * -H "Content-Type: application/json" \ + * -d '{"name": "John Doe", "age": 30}' + * + * curl "http://localhost:3000/search?q=test&limit=10" + */ diff --git a/package.json b/package.json index 13ea9fb..9731d31 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,12 @@ "description": "Handle requests using the fetch standard", "main": "index.js", "type": "commonjs", - "types": "index.d.ts", + "types": "types/index.d.ts", "scripts": { "lint": "eslint", "lint:fix": "eslint --fix", - "test": "borp", - "test:types": "tsd" + "test": "borp && npm run test:types", + "test:types": "tstyche" }, "repository": { "type": "git", @@ -46,9 +46,6 @@ "borp": "^1.0.0", "fastify": "^5.0.0", "neostandard": "^0.13.0", - "tsd": "^0.33.0" - }, - "tsd": { - "directory": "test/types" + "tstyche": "^7.0.0" } -} +} \ No newline at end of file diff --git a/test/types/index.test-d.ts b/test/types/index.test-d.ts deleted file mode 100644 index e89e0b3..0000000 --- a/test/types/index.test-d.ts +++ /dev/null @@ -1,72 +0,0 @@ -import fastify, { FastifyInstance, FastifyPluginAsync, FastifyBaseLogger, FastifyRequest, FastifyReply } from 'fastify' -import { expectType } from 'tsd' -import * as fastifyFetchStar from '../..' -import fastifyFetch, { - FetchContext, - FetchHandler, - FetchRoutes, - fastifyFetch as fastifyFetchNamed -} from '../..' - -import fastifyFetchCjsImport = require('../..') -const fastifyFetchCjs = require('../..') - -const app: FastifyInstance = fastify() - -app.register(fastifyFetch) -app.register(fastifyFetchNamed) -app.register(fastifyFetchCjs) -app.register(fastifyFetchCjsImport.default) -app.register(fastifyFetchCjsImport.fastifyFetch) -app.register(fastifyFetchStar.default) -app.register(fastifyFetchStar.fastifyFetch) - -expectType(fastifyFetch) -expectType(fastifyFetchNamed) -expectType(fastifyFetchCjs) - -app - .register(fastifyFetch) - .after(() => { - expectType(app.fetch) - - app.fetch.get('/test', async (request, ctx) => { - expectType(request) - expectType(ctx) - expectType(ctx.log) - expectType(ctx.server) - expectType>(ctx.params) - expectType>(ctx.query) - expectType(ctx.request) - expectType(ctx.reply) - return new Response('ok') - }) - - app.fetch.post('/data', async (request, ctx) => { - return Response.json({ ok: true }) - }) - - app.fetch.put('/update', async (request, ctx) => { - return new Response('updated') - }) - - app.fetch.delete('/remove', async (request, ctx) => { - return new Response('deleted') - }) - - app.fetch.patch('/patch', async (request, ctx) => { - return new Response('patched') - }) - - app.fetch.options('/options', async (request, ctx) => { - return new Response(null, { status: 204 }) - }) - - app.fetch.head('/head', async (request, ctx) => { - return new Response(null) - }) - }) - -const handler: FetchHandler = async (request, ctx) => { - return new Response('handler') -} diff --git a/index.d.ts b/types/index.d.ts similarity index 100% rename from index.d.ts rename to types/index.d.ts diff --git a/types/index.tst.ts b/types/index.tst.ts new file mode 100644 index 0000000..b9a83e4 --- /dev/null +++ b/types/index.tst.ts @@ -0,0 +1,78 @@ +import fastify, { + FastifyInstance, + FastifyPluginAsync, + FastifyBaseLogger, + FastifyRequest, + FastifyReply +} from 'fastify' +import { expect } from 'tstyche' +import * as fastifyFetchStar from '..' +import fastifyFetch, { + FetchContext, + FetchHandler, + FetchRoutes, + fastifyFetch as fastifyFetchNamed +} from '..' + +import fastifyFetchCjsImport = require('..') +const fastifyFetchCjs = require('../..') + +const app: FastifyInstance = fastify() + +app.register(fastifyFetch) +app.register(fastifyFetchNamed) +app.register(fastifyFetchCjs) +app.register(fastifyFetchCjsImport.default) +app.register(fastifyFetchCjsImport.fastifyFetch) +app.register(fastifyFetchStar.default) +app.register(fastifyFetchStar.fastifyFetch) + +expect(fastifyFetch).type.toBe() +expect(fastifyFetchNamed).type.toBe() +expect(fastifyFetchCjs).type.toBe() + +app.register(fastifyFetch).after(() => { + expect(app.fetch).type.toBe() + + app.fetch.get('/test', async (request, ctx) => { + expect(request).type.toBe() + expect(ctx).type.toBe() + expect(ctx.log).type.toBe() + expect(ctx.server).type.toBe() + expect(ctx.params).type.toBe>() + expect(ctx.query).type.toBe>() + expect(ctx.request).type.toBe() + expect(ctx.reply).type.toBe() + return new Response('ok') + }) + + app.fetch.post('/data', async (request, ctx) => { + return Response.json({ ok: true }) + }) + + app.fetch.put('/update', async (request, ctx) => { + return new Response('updated') + }) + + app.fetch.delete('/remove', async (request, ctx) => { + return new Response('deleted') + }) + + app.fetch.patch('/patch', async (request, ctx) => { + return new Response('patched') + }) + + app.fetch.options('/options', async (request, ctx) => { + return new Response(null, { status: 204 }) + }) + + app.fetch.head('/head', async (request, ctx) => { + return new Response(null) + }) +}) + +const handler: FetchHandler = async (request, ctx) => { + return new Response('handler') +} + +expect(handler).type.toBe()