Skip to content

Commit 6d46f61

Browse files
ShaunSHamiltonraisedadead
authored andcommitted
refactor(api): shiny new api (freeCodeCamp#48432)
1 parent 9a3d0b7 commit 6d46f61

File tree

11 files changed

+35372
-70567
lines changed

11 files changed

+35372
-70567
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,6 @@ client/src/components/Donation/types.js
221221

222222
### UI Components ###
223223
tools/ui-components/dist
224+
225+
### API ###
226+
api/**/*.js

api/db/index.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import fastifyPlugin from 'fastify-plugin';
2+
import fastifyMongo from '@fastify/mongodb';
3+
import { FastifyInstance } from 'fastify';
4+
5+
const URI = process.env.MONGOHQ_URL || 'mongodb://localhost:27017/freecodecamp';
6+
// eslint-disable-next-line @typescript-eslint/require-await
7+
async function connect(fastify: FastifyInstance) {
8+
fastify.log.info(`Connecting to : ${URI}`);
9+
void fastify.register(fastifyMongo, {
10+
url: URI
11+
});
12+
}
13+
14+
export const dbConnector = fastifyPlugin(connect);

api/index.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { config } from 'dotenv';
2+
config({ path: '../.env' });
3+
import Fastify from 'fastify';
4+
import { testRoutes } from './routes/test';
5+
import { dbConnector } from './db';
6+
7+
const fastify = Fastify({
8+
logger: { level: process.env.NODE_ENV === 'development' ? 'debug' : 'fatal' }
9+
});
10+
11+
fastify.get('/', async (_request, _reply) => {
12+
return { hello: 'world' };
13+
});
14+
15+
void fastify.register(dbConnector);
16+
void fastify.register(testRoutes);
17+
18+
const start = async () => {
19+
try {
20+
const port = Number(process.env.PORT) || 3000;
21+
fastify.log.info(`Starting server on port ${port}`);
22+
await fastify.listen({ port });
23+
} catch (err) {
24+
fastify.log.error(err);
25+
process.exit(1);
26+
}
27+
};
28+
29+
void start();

api/middleware/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export async function auth0Verify() {
2+
// Verify user authorization code with Auth0
3+
}

api/package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "@freecodecamp/api",
3+
"version": "0.0.1",
4+
"description": "The freeCodeCamp.org open-source codebase and curriculum",
5+
"license": "BSD-3-Clause",
6+
"private": true,
7+
"engines": {
8+
"node": ">=18",
9+
"npm": ">=8"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/freeCodeCamp/freeCodeCamp.git"
14+
},
15+
"bugs": {
16+
"url": "https://github.com/freeCodeCamp/freeCodeCamp/issues"
17+
},
18+
"homepage": "https://github.com/freeCodeCamp/freeCodeCamp#readme",
19+
"author": "freeCodeCamp <[email protected]>",
20+
"main": "none",
21+
"scripts": {
22+
"build": "tsc index.ts",
23+
"start": "NODE_ENV=production node index.js",
24+
"develop": "NODE_ENV=development nodemon index.ts"
25+
},
26+
"dependencies": {
27+
"@fastify/mongodb": "6.1.0",
28+
"fastify": "4.9.2",
29+
"fastify-plugin": "4.3.0"
30+
}
31+
}

api/routes/auth0.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { FastifyInstance } from 'fastify';
2+
3+
// eslint-disable-next-line @typescript-eslint/require-await
4+
export async function auth0Routes(fastify: FastifyInstance) {
5+
fastify.get('/oauth/token', async (_request, _reply) => {
6+
return { a: 'b' };
7+
});
8+
}

api/routes/test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { FastifyInstance } from 'fastify';
2+
3+
// eslint-disable-next-line @typescript-eslint/require-await
4+
export async function testRoutes(fastify: FastifyInstance) {
5+
const collection = fastify?.mongo?.db?.collection('user');
6+
7+
fastify.get('/test', async (_request, _reply) => {
8+
if (!collection) {
9+
return { error: 'No collection' };
10+
}
11+
const user = await collection?.findOne({ email: '[email protected]' });
12+
return { user };
13+
});
14+
}

api/utils/index.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { randomBytes, createHash } from 'crypto';
2+
3+
export function base64URLEncode(buf: Buffer) {
4+
return buf
5+
.toString('base64')
6+
.replace(/\+/g, '-')
7+
.replace(/\//g, '_')
8+
.replace(/=/g, '');
9+
}
10+
export const verifier = base64URLEncode(randomBytes(32));
11+
12+
function sha256(buf: Buffer) {
13+
return createHash('sha256').update(buf).digest();
14+
}
15+
export const challenge = base64URLEncode(sha256(Buffer.from(verifier)));

0 commit comments

Comments
 (0)