From 519e6cad45ba780c78f6c0938f75b032b6bf2da1 Mon Sep 17 00:00:00 2001 From: Alexander Ryzhikov Date: Wed, 24 Feb 2021 15:13:24 +0200 Subject: [PATCH] feat: treat redirect as reply --- src/typed-fastify.ts | 9 ++------- test/better-fastify.test-d.ts | 8 +++----- test/integration.test.ts | 5 ++--- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/typed-fastify.ts b/src/typed-fastify.ts index 227c6cf..7c86d7f 100644 --- a/src/typed-fastify.ts +++ b/src/typed-fastify.ts @@ -206,13 +206,8 @@ interface Reply< getHeader
(header: Header): Headers[Header]; getHeaders(): Headers; - redirect( - statusCode: Status, - url: string, - ): OpaqueReply; - redirect( - url: string, - ): OpaqueReply; + redirect(statusCode: Status, url: string): AsReply; + redirect(url: string): AsReply; status< Status extends [keyof Op['response']] extends [never] diff --git a/test/better-fastify.test-d.ts b/test/better-fastify.test-d.ts index b8f5b15..da5833e 100644 --- a/test/better-fastify.test-d.ts +++ b/test/better-fastify.test-d.ts @@ -354,8 +354,7 @@ const complexHandlers = ( //@ts-expect-error return reply; } else if (randomNumber) { - const redirect = reply.redirect('/'); - return redirect.send(); + return reply.redirect('/'); } else if (randomNumber) { //@ts-expect-error return reply.redirect(200, '/').done(); @@ -527,8 +526,7 @@ type Created = Service['GET /empty']; expectType( //@ts-expect-error async (req, reply) => { - let reply1 = reply.status(204); - return reply1; + return reply.status(204); }, ); @@ -556,6 +554,6 @@ interface Redirects extends Schema { expectType>({ 'POST /redirect': (req, reply) => { - return reply.status(302).send(); + return reply.redirect('example.com'); }, }); diff --git a/test/integration.test.ts b/test/integration.test.ts index 8b840ac..84defc9 100644 --- a/test/integration.test.ts +++ b/test/integration.test.ts @@ -13,8 +13,7 @@ t.cleanSnapshot = (s) => { const defaultService: Service = { 'GET /': (req, reply) => { - let res = reply.status(200).headers({ 'x-custom': '1' }).send({ name: 'hello' }); - return res; + return reply.status(200).headers({ 'x-custom': '1' }).send({ name: 'hello' }); }, 'POST /': (req, reply) => { const { user: userData } = req.body; @@ -28,7 +27,7 @@ const defaultService: Service = { return fastifyReply.send(); }, 'POST /redirect': async (req, reply) => { - return reply.redirect('example.com').send(); + return reply.redirect('example.com'); }, }; const buildApp = async (t: Test, service?: Service) => {