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
16 changes: 15 additions & 1 deletion example.mjs → examples/example.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Fastify from 'fastify'
import fastifyFetch from './index.js'
import fastifyFetch from '../index.js'

const fastify = Fastify({ logger: true })

Expand All @@ -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"
*/
13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
}
}
}
72 changes: 0 additions & 72 deletions test/types/index.test-d.ts

This file was deleted.

File renamed without changes.
78 changes: 78 additions & 0 deletions types/index.tst.ts
Original file line number Diff line number Diff line change
@@ -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<FastifyPluginAsync>()
expect(fastifyFetchNamed).type.toBe<FastifyPluginAsync>()
expect(fastifyFetchCjs).type.toBe<any>()

app.register(fastifyFetch).after(() => {
expect(app.fetch).type.toBe<FetchRoutes>()

app.fetch.get('/test', async (request, ctx) => {
expect(request).type.toBe<Request>()
expect(ctx).type.toBe<FetchContext>()
expect(ctx.log).type.toBe<FastifyBaseLogger>()
expect(ctx.server).type.toBe<FastifyInstance>()
expect(ctx.params).type.toBe<Record<string, string>>()
expect(ctx.query).type.toBe<Record<string, string>>()
expect(ctx.request).type.toBe<FastifyRequest>()
expect(ctx.reply).type.toBe<FastifyReply>()
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<FetchHandler>()
Loading