Skip to content

Commit

Permalink
feat(api): example middleware (freeCodeCamp#48434)
Browse files Browse the repository at this point in the history
* feat(api): add middleware example

* feat(api): add `@fastify/middie`, reorder for alphabetness

* [skip-ci] [skip ci]
  • Loading branch information
ShaunSHamilton authored and raisedadead committed Feb 24, 2023
1 parent 6d46f61 commit f7644be
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 393 deletions.
16 changes: 13 additions & 3 deletions api/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { config } from 'dotenv';
config({ path: '../.env' });
import Fastify from 'fastify';
import middie from '@fastify/middie';
import { testRoutes } from './routes/test';
import { dbConnector } from './db';
import { testMiddleware } from './middleware';

const fastify = Fastify({
logger: { level: process.env.NODE_ENV === 'development' ? 'debug' : 'fatal' }
Expand All @@ -12,10 +14,18 @@ fastify.get('/', async (_request, _reply) => {
return { hello: 'world' };
});

void fastify.register(dbConnector);
void fastify.register(testRoutes);

const start = async () => {
// NOTE: Awaited to ensure `.use` is registered on `fastify`
await fastify.register(middie);

// @ts-expect-error Types are not exported from Fastify,
// and TypeScript is not smart enough to realise types
// defined within this module have the same signature
void fastify.use('/test', testMiddleware);

void fastify.register(dbConnector);
void fastify.register(testRoutes);

try {
const port = Number(process.env.PORT) || 3000;
fastify.log.info(`Starting server on port ${port}`);
Expand Down
13 changes: 13 additions & 0 deletions api/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
import { NextFunction } from '../utils';

export async function auth0Verify() {
// Verify user authorization code with Auth0
}

export function testMiddleware(
req: Request,
_res: Response,
next: NextFunction
) {
//
console.log('Test middleware running');
console.log(req.headers);
next();
}
41 changes: 25 additions & 16 deletions api/package.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
{
"name": "@freecodecamp/api",
"version": "0.0.1",
"author": "freeCodeCamp <team@freecodecamp.org>",
"bugs": {
"url": "https://github.com/freeCodeCamp/freeCodeCamp/issues"
},
"dependencies": {
"@fastify/middie": "8.0.0",
"@fastify/mongodb": "6.1.0",
"fastify": "4.9.2",
"fastify-plugin": "4.3.0"
},
"description": "The freeCodeCamp.org open-source codebase and curriculum",
"license": "BSD-3-Clause",
"private": true,
"engines": {
"node": ">=18",
"npm": ">=8"
},
"homepage": "https://github.com/freeCodeCamp/freeCodeCamp#readme",
"license": "BSD-3-Clause",
"main": "none",
"name": "@freecodecamp/api",
"nodemonConfig": {
"env": {
"NODE_ENV": "development"
},
"ignore": [
"**/*.js"
]
},
"private": true,
"repository": {
"type": "git",
"url": "git+https://github.com/freeCodeCamp/freeCodeCamp.git"
},
"bugs": {
"url": "https://github.com/freeCodeCamp/freeCodeCamp/issues"
},
"homepage": "https://github.com/freeCodeCamp/freeCodeCamp#readme",
"author": "freeCodeCamp <team@freecodecamp.org>",
"main": "none",
"scripts": {
"build": "tsc index.ts",
"start": "NODE_ENV=production node index.js",
"develop": "NODE_ENV=development nodemon index.ts"
"develop": "nodemon index.ts"
},
"dependencies": {
"@fastify/mongodb": "6.1.0",
"fastify": "4.9.2",
"fastify-plugin": "4.3.0"
}
"version": "0.0.1"
}
4 changes: 4 additions & 0 deletions api/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ function sha256(buf: Buffer) {
return createHash('sha256').update(buf).digest();
}
export const challenge = base64URLEncode(sha256(Buffer.from(verifier)));

// This is used for Fastify middleware, but is not exported from Fastify itself.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type NextFunction = (err?: any) => void;
Loading

0 comments on commit f7644be

Please sign in to comment.