refactor(api): shiny new api (#48432)

This commit is contained in:
Shaun Hamilton
2022-11-08 17:41:19 +00:00
committed by Mrugesh Mohapatra
parent 9a3d0b7bfb
commit 6d46f61fe9
11 changed files with 3222 additions and 38417 deletions

3
.gitignore vendored
View File

@@ -221,3 +221,6 @@ client/src/components/Donation/types.js
### UI Components ###
tools/ui-components/dist
### API ###
api/**/*.js

14
api/db/index.ts Normal file
View File

@@ -0,0 +1,14 @@
import fastifyPlugin from 'fastify-plugin';
import fastifyMongo from '@fastify/mongodb';
import { FastifyInstance } from 'fastify';
const URI = process.env.MONGOHQ_URL || 'mongodb://localhost:27017/freecodecamp';
// eslint-disable-next-line @typescript-eslint/require-await
async function connect(fastify: FastifyInstance) {
fastify.log.info(`Connecting to : ${URI}`);
void fastify.register(fastifyMongo, {
url: URI
});
}
export const dbConnector = fastifyPlugin(connect);

29
api/index.ts Normal file
View File

@@ -0,0 +1,29 @@
import { config } from 'dotenv';
config({ path: '../.env' });
import Fastify from 'fastify';
import { testRoutes } from './routes/test';
import { dbConnector } from './db';
const fastify = Fastify({
logger: { level: process.env.NODE_ENV === 'development' ? 'debug' : 'fatal' }
});
fastify.get('/', async (_request, _reply) => {
return { hello: 'world' };
});
void fastify.register(dbConnector);
void fastify.register(testRoutes);
const start = async () => {
try {
const port = Number(process.env.PORT) || 3000;
fastify.log.info(`Starting server on port ${port}`);
await fastify.listen({ port });
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
void start();

3
api/middleware/index.ts Normal file
View File

@@ -0,0 +1,3 @@
export async function auth0Verify() {
// Verify user authorization code with Auth0
}

31
api/package.json Normal file
View File

@@ -0,0 +1,31 @@
{
"name": "@freecodecamp/api",
"version": "0.0.1",
"description": "The freeCodeCamp.org open-source codebase and curriculum",
"license": "BSD-3-Clause",
"private": true,
"engines": {
"node": ">=18",
"npm": ">=8"
},
"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"
},
"dependencies": {
"@fastify/mongodb": "6.1.0",
"fastify": "4.9.2",
"fastify-plugin": "4.3.0"
}
}

8
api/routes/auth0.ts Normal file
View File

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

14
api/routes/test.ts Normal file
View File

@@ -0,0 +1,14 @@
import { FastifyInstance } from 'fastify';
// eslint-disable-next-line @typescript-eslint/require-await
export async function testRoutes(fastify: FastifyInstance) {
const collection = fastify?.mongo?.db?.collection('user');
fastify.get('/test', async (_request, _reply) => {
if (!collection) {
return { error: 'No collection' };
}
const user = await collection?.findOne({ email: 'bar@bar.com' });
return { user };
});
}

15
api/utils/index.ts Normal file
View File

@@ -0,0 +1,15 @@
import { randomBytes, createHash } from 'crypto';
export function base64URLEncode(buf: Buffer) {
return buf
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
}
export const verifier = base64URLEncode(randomBytes(32));
function sha256(buf: Buffer) {
return createHash('sha256').update(buf).digest();
}
export const challenge = base64URLEncode(sha256(Buffer.from(verifier)));

41520
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -19,6 +19,7 @@
"author": "freeCodeCamp <team@freecodecamp.org>",
"main": "none",
"workspaces": [
"api",
"api-server",
"client",
"client/plugins/fcc-source-challenges",

View File

@@ -1,5 +1,6 @@
{
"include": [
"api",
"client/i18n/**/*",
"client/plugins/**/*",
"client/src/**/*",