Skip to content

Commit

Permalink
feat: treat redirect as reply
Browse files Browse the repository at this point in the history
  • Loading branch information
Coobaha committed Feb 24, 2021
1 parent 4ef3f40 commit 519e6ca
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 15 deletions.
9 changes: 2 additions & 7 deletions src/typed-fastify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,8 @@ interface Reply<
getHeader<Header extends keyof Headers>(header: Header): Headers[Header];
getHeaders(): Headers;

redirect<Status extends keyof Op['response']>(
statusCode: Status,
url: string,
): OpaqueReply<Op, Status, Content, Headers, Path, ServiceSchema, RawServer, RawRequest, RawReply, ContextConfig>;
redirect(
url: string,
): OpaqueReply<Op, 302, Content, Headers, Path, ServiceSchema, RawServer, RawRequest, RawReply, ContextConfig>;
redirect<Status extends keyof Op['response']>(statusCode: Status, url: string): AsReply;
redirect(url: string): AsReply;

status<
Status extends [keyof Op['response']] extends [never]
Expand Down
8 changes: 3 additions & 5 deletions test/better-fastify.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -527,8 +526,7 @@ type Created = Service<EmptyResponses>['GET /empty'];
expectType<Created>(
//@ts-expect-error
async (req, reply) => {
let reply1 = reply.status(204);
return reply1;
return reply.status(204);
},
);

Expand Down Expand Up @@ -556,6 +554,6 @@ interface Redirects extends Schema {

expectType<Service<Redirects>>({
'POST /redirect': (req, reply) => {
return reply.status(302).send();
return reply.redirect('example.com');
},
});
5 changes: 2 additions & 3 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ t.cleanSnapshot = (s) => {

const defaultService: Service<TestSchema> = {
'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;
Expand All @@ -28,7 +27,7 @@ const defaultService: Service<TestSchema> = {
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<TestSchema>) => {
Expand Down

0 comments on commit 519e6ca

Please sign in to comment.