Skip to content

Commit

Permalink
fix: fastify multiple endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
yamcodes committed Mar 30, 2024
1 parent be8b53a commit 5765339
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 33 deletions.
5 changes: 3 additions & 2 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import { onRequest } from 'firebase-functions/v2/https';
import { fastify } from 'fastify';
import * as logger from 'firebase-functions/logger';
import { registerRoutes, setContentTypeParser } from './utils';
import { setContentTypeParser } from './utils';
import { routes } from './routes';

const app = fastify({
logger: true,
Expand All @@ -44,7 +45,7 @@ setContentTypeParser(
}
);

registerRoutes(app);
void app.register(routes, { prefix: '/api' });

export const server = onRequest(async (request, response) => {
await app.ready();
Expand Down
14 changes: 14 additions & 0 deletions apps/api/src/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { faker } from '@faker-js/faker';
import { type FastifyPluginCallback } from 'fastify';
import { iLikeTurtles } from 'utilities';

export const routes: FastifyPluginCallback = (fastify, _options, done) => {
fastify.get('/legacy', () => ({
hello: 'world!',
randomName: faker.person.firstName(),
iLikeTurtles: iLikeTurtles(),
nice: 69,
}));

done();
};
31 changes: 0 additions & 31 deletions apps/api/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { iLikeTurtles } from 'utilities';
import { faker } from '@faker-js/faker';
import type { AddContentTypeParser, FastifyInstance } from 'fastify';

export const setContentTypeParser = (
Expand All @@ -9,32 +7,3 @@ export const setContentTypeParser = (
app.removeContentTypeParser(contentType);
app.addContentTypeParser(contentType, opts, parser);
};

export const registerRoutes = (fastify: FastifyInstance) => {
// define your endpoints here...
// fastify.post('/some-route-here', (_request) => {
// return { message: 'Hello World!!' };
// });

// fastify.get('/', async (request, reply) => {
// void reply.send({ message: 'Hello World!!' });
// });

// fastify.get('/legacy', (_req, res) => {
// void res.send({
// hello: 'world',
// randomName: faker.person.firstName(),
// iLikeTurtles: iLikeTurtles(),
// nice: 69,
// });
// });

fastify.get('*', (_req, res) => {
void res.send({
hello: 'world!',
randomName: faker.person.firstName(),
iLikeTurtles: iLikeTurtles(),
nice: 69,
});
});
};

0 comments on commit 5765339

Please sign in to comment.