inital commit

This commit is contained in:
Lee
2023-11-14 17:56:44 +00:00
commit 161486bf49
38 changed files with 5094 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# `@turbo/eslint-config`
Collection of internal eslint configurations.

View File

@ -0,0 +1,34 @@
const { resolve } = require("node:path");
const project = resolve(process.cwd(), "tsconfig.json");
/*
* This is a custom ESLint configuration for use with
* typescript packages.
*
* This config extends the Vercel Engineering Style Guide.
* For more information, see https://github.com/vercel/style-guide
*
*/
module.exports = {
extends: [
"@vercel/style-guide/eslint/node",
"@vercel/style-guide/eslint/typescript",
].map(require.resolve),
parserOptions: {
project,
},
globals: {
React: true,
JSX: true,
},
settings: {
"import/resolver": {
typescript: {
project,
},
},
},
ignorePatterns: ["node_modules/", "dist/"],
};

View File

@ -0,0 +1,42 @@
const { resolve } = require("node:path");
const project = resolve(process.cwd(), "tsconfig.json");
/*
* This is a custom ESLint configuration for use with
* Next.js apps.
*
* This config extends the Vercel Engineering Style Guide.
* For more information, see https://github.com/vercel/style-guide
*
*/
module.exports = {
extends: [
"@vercel/style-guide/eslint/node",
"@vercel/style-guide/eslint/browser",
"@vercel/style-guide/eslint/typescript",
"@vercel/style-guide/eslint/react",
"@vercel/style-guide/eslint/next",
"eslint-config-turbo",
].map(require.resolve),
parserOptions: {
project,
},
globals: {
React: true,
JSX: true,
},
settings: {
"import/resolver": {
typescript: {
project,
},
},
},
ignorePatterns: ["node_modules/", "dist/"],
// add rules configurations here
rules: {
"import/no-default-export": "off",
},
};

View File

@ -0,0 +1,11 @@
{
"name": "eslint-config-custom",
"license": "MIT",
"version": "0.0.0",
"private": true,
"devDependencies": {
"@vercel/style-guide": "^5.0.0",
"eslint-config-turbo": "^1.10.12",
"typescript": "^5.2.2"
}
}

View File

@ -0,0 +1,39 @@
const { resolve } = require("node:path");
const project = resolve(process.cwd(), "tsconfig.json");
/*
* This is a custom ESLint configuration for use with
* internal (bundled by their consumer) libraries
* that utilize React.
*
* This config extends the Vercel Engineering Style Guide.
* For more information, see https://github.com/vercel/style-guide
*
*/
module.exports = {
extends: [
"@vercel/style-guide/eslint/browser",
"@vercel/style-guide/eslint/typescript",
"@vercel/style-guide/eslint/react",
].map(require.resolve),
parserOptions: {
project,
},
globals: {
JSX: true,
},
settings: {
"import/resolver": {
typescript: {
project,
},
},
},
ignorePatterns: ["node_modules/", "dist/", ".eslintrc.js"],
rules: {
// add specific rules configurations here
},
};

View File

@ -0,0 +1,11 @@
{
"name": "server",
"version": "0.0.0",
"private": true,
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "./src/index.ts",
"types": "./src/index.ts"
}

View File

@ -0,0 +1,11 @@
import { badRequest } from "./messages/badRequest";
import { unknownRoute } from "./messages/unknownRoute";
export const RouteMessages = {
unknownRoute: unknownRoute,
badRequest: badRequest,
};
export * from "./route/route";
export * from "./route/routeManager";
export * from "./server/server";

View File

@ -0,0 +1,10 @@
import { baseMessage } from "./baseMessage";
/**
* Creates a response for a bad request
*
* @returns the bad request message
*/
export function badRequest(message: string) {
return baseMessage(true, message);
}

View File

@ -0,0 +1,13 @@
/**
* Creates a base message for use in JSON responses
*
* @param error the error status
* @param message the message to show
* @returns the base message
*/
export function baseMessage(error: boolean, message: string) {
return {
error: error,
message: message,
};
}

View File

@ -0,0 +1,10 @@
import { baseMessage } from "./baseMessage";
/**
* Creates a response for an unknown route
*
* @returns the unknown route message
*/
export function unknownRoute() {
return baseMessage(true, "Unknown route");
}

View File

@ -0,0 +1,34 @@
import { Request, Response } from "express";
type RouteType = {
/**
* The path to handle
*/
path: string;
};
export abstract class Route {
private path: string;
constructor({ path }: RouteType) {
this.path = path;
console.log(`Created route handler for ${path}`);
}
/**
* Handle the incoming request
*
* @param req the request
* @param res the response
*/
abstract handle(req: Request, res: Response): void;
/**
* Get the path of the route
*
* @returns the path
*/
getPath() {
return this.path;
}
}

View File

@ -0,0 +1,33 @@
import { Route } from "./route";
export class RouteManager {
private routes: Route[] = [];
/**
* Add a route to the route manager
*
* @param route the route to add
*/
addRoute(route: Route) {
this.routes.push(route);
}
/**
* Get a route by path
*
* @param path the path to get the route for
* @returns the route or undefined if not found
*/
getRoute(path: string) {
return this.routes.find((route) => route.getPath() === path);
}
/**
* Get all routes
*
* @returns all routes
*/
getRoutes() {
return this.routes;
}
}

View File

@ -0,0 +1,38 @@
import express from "express";
type ServerType = {
/**
* The port to listen on
* @default 3000
*/
port: number | string;
onLoaded?: () => void;
};
export function createServer({ port = 3000, onLoaded }: ServerType) {
if (typeof port === "string") {
port = Number(port);
if (isNaN(port)) {
throw new Error("Port must be a number");
}
}
const server = express();
// Enable JSON parsing
server.use(express.json());
// Remove the X-Powered-By header
server.disable("x-powered-by");
// Turn on ETag support (for caching)
server.enable("etag");
// Listen on the specified port
server.listen(port, () => {
console.log(`Server listening on http://localhost:${port}`);
onLoaded && onLoaded(); // Call the onLoaded callback if it exists
});
return server;
}

View File

@ -0,0 +1,7 @@
{
"extends": "tsconfig/server.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}

View File

@ -0,0 +1,18 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Default",
"compilerOptions": {
"esModuleInterop": true,
"skipLibCheck": true,
"target": "es2022",
"resolveJsonModule": true,
"isolatedModules": true,
"moduleDetection": "force",
"strict": true,
"noUncheckedIndexedAccess": true,
"module": "NodeNext",
"noEmit": true,
"lib": ["es2022", "dom", "dom.iterable"]
},
"exclude": ["node_modules"]
}

View File

@ -0,0 +1,13 @@
{
"name": "tsconfig",
"version": "0.0.0",
"private": true,
"license": "MIT",
"publishConfig": {
"access": "public"
},
"files": [
"base.json",
"server.json"
]
}

View File

@ -0,0 +1,5 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Server",
"extends": "./base.json"
}

View File

@ -0,0 +1,11 @@
{
"name": "utils",
"version": "0.0.0",
"private": true,
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "./src/index.ts",
"types": "./src/index.ts"
}

View File

@ -0,0 +1,15 @@
/**
* Checks if all environment variables are set
*
* @param variables the environment variables to check
* @returns true if all variables are set, false otherwise
*/
export function checkEnvironmentVariables(...variables: string[]): boolean {
let allVariablesSet = true;
variables.forEach((variable) => {
if (!process.env[variable]) {
throw new Error(`${variable} not set in environment variables`);
}
});
return allVariablesSet;
}

View File

@ -0,0 +1,2 @@
export * from "./envVariables";
export * from "./secrets";

View File

@ -0,0 +1,16 @@
import InfisicalClient from "infisical-node";
/**
* Create an infisical client
*
* @param token the infisical token
* @returns the infisical client
*/
function createInfisicalClient(token: string) {
return new InfisicalClient({
token,
siteURL: "https://secrets.fascinated.cc",
});
}
export { createInfisicalClient };

View File

@ -0,0 +1,7 @@
{
"extends": "tsconfig/server.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}