From 03734cadba4c0d531eb4ec0349ee0d0e6dcc2278 Mon Sep 17 00:00:00 2001 From: Rash Date: Sat, 10 Aug 2024 20:21:00 +0100 Subject: [PATCH] |#1 | @rashjredmund | installed express and configured base project template. setup hustky, prettier, and eslint. also updated readme file --- .env.example | 2 + .eslintrc.json | 13 +++ .gitignore | 1 + .hintrc | 8 ++ .husky/pre-commit | 4 + .prettierignore | 9 ++ .prettierrc.json | 6 ++ README.md | 76 ++++++++++++- nodemon.json | 10 ++ package.json | 46 ++++++++ public/404.html | 64 +++++++++++ public/main.html | 52 +++++++++ src/config/app-config.ts | 8 ++ src/main.ts | 19 ++++ src/middlewares/index.ts | 3 + src/modules/auth/auth.controller.ts | 16 +++ src/modules/auth/auth.service.ts | 5 + src/modules/languages/language.controller.ts | 16 +++ src/modules/languages/language.service.ts | 5 + src/modules/match/match.controller.ts | 16 +++ src/modules/match/match.service.ts | 5 + src/routes/auth-router.ts | 10 ++ src/routes/language-router.ts | 10 ++ src/routes/match-router.ts | 10 ++ src/server/index.ts | 38 +++++++ tsconfig.json | 108 +++++++++++++++++++ 26 files changed, 558 insertions(+), 2 deletions(-) create mode 100644 .env.example create mode 100644 .eslintrc.json create mode 100644 .hintrc create mode 100644 .husky/pre-commit create mode 100644 .prettierignore create mode 100644 .prettierrc.json create mode 100644 nodemon.json create mode 100644 package.json create mode 100644 public/404.html create mode 100644 public/main.html create mode 100644 src/config/app-config.ts create mode 100644 src/main.ts create mode 100644 src/middlewares/index.ts create mode 100644 src/modules/auth/auth.controller.ts create mode 100644 src/modules/auth/auth.service.ts create mode 100644 src/modules/languages/language.controller.ts create mode 100644 src/modules/languages/language.service.ts create mode 100644 src/modules/match/match.controller.ts create mode 100644 src/modules/match/match.service.ts create mode 100644 src/routes/auth-router.ts create mode 100644 src/routes/language-router.ts create mode 100644 src/routes/match-router.ts create mode 100644 src/server/index.ts create mode 100644 tsconfig.json diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..6519c5a --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +PORT="8080" +NODE_ENV="dev" \ No newline at end of file diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..ebd8457 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,13 @@ +{ + "rules": { + "quotes": ["warn", "double", { "avoidEscape": true }], + "semi": ["warn", "always"], + "indent": ["warn", 2], + "class-methods-use-this": "warn", + "eol-last": ["warn", "always"], + "no-unused-vars": ["warn"], + "no-unused-expressions": ["warn"], + "no-multiple-empty-lines": ["warn", { "max": 1 }], + "no-trailing-spaces": ["warn"] + } +} diff --git a/.gitignore b/.gitignore index c6bba59..f694098 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ yarn-debug.log* yarn-error.log* lerna-debug.log* .pnpm-debug.log* +*-lock.json # Diagnostic reports (https://nodejs.org/api/report.html) report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json diff --git a/.hintrc b/.hintrc new file mode 100644 index 0000000..5c813c4 --- /dev/null +++ b/.hintrc @@ -0,0 +1,8 @@ +{ + "extends": [ + "development" + ], + "hints": { + "disown-opener": "off" + } +} \ No newline at end of file diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100644 index 0000000..14ccce4 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,4 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +npm run pre-commit \ No newline at end of file diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..f1f55e4 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,9 @@ +.next +.cache +package-lock.json +public +node_modules +next-env.d.ts +next.config.ts +yarn.lock +src/components/ui diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..a405bf4 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,6 @@ +{ + "trailingComma": "es5", + "tabWidth": 2, + "semi": true, + "singleQuote": false +} \ No newline at end of file diff --git a/README.md b/README.md index 33fa1e3..a60b58f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,74 @@ -# WMA_HackKmer_Yaounde_Backend -REST API server for WMA HackKmer Yaounde at Rebase Code Camp +# wiki-lex-match + +__table of content__ + +- [local setup](#setup-repo-locally) +- [how to render the](#how-to-render-the-documentation) +- [contributing](#contributing) + +## setup repo locally + +- Clone repo with + + ```bash + git clone git@github.com:Wikidata-Cameroon/WMA_HackKmer_Yaounde_Backend.git wiki-lex-match-backend + ``` + +- Navigate to project with from your terminal + + ```bash + cd wiki-lex-match-backend + ``` + +## How to start server + +- Start local server in watch mode + + ```bash + npm run dev + ``` + +- Build to js + + ```bash + npm run build + ``` + +## Contributing + +- __Git flow__ + + - Branches should be name with `feature/` format + + - Writing commits + - Commit messages should follow the for mat `# | | ` + + - @example is `"|#1 | @rashjredmund | installed express and configured base project template. setup hustky, prettier, and eslint. also updated readme file"` + + - The numbering is with respect to the issue number your are working on + + - When creating a `PR`; + + - If that pr is for an existing `issue`, be sure to tag the issue in the PR description, to auto-close the issue if it's approved, you can use any of these commands + + - `Resolves #` + + - `Fixes #` or + + - `Closes #` + + - Always assign the PR to yourself and your team-mate + +- __Building__ + + - Navigate to `src/modules/` to start contributing + + - Set up your routes in the `src/route/*` directory in the right file or add one if need be + + - Set up your route handlers in the `.controller.ts` file and add the endpoints to the corresponding route in `src/routes/*` directory + +| + +___ + +__Happy Coding Everyone 🚀__ diff --git a/nodemon.json b/nodemon.json new file mode 100644 index 0000000..24c12eb --- /dev/null +++ b/nodemon.json @@ -0,0 +1,10 @@ +{ + "watch": [ + "src/" + ], + "ext": "ts,json", + "ignore": [ + "src/**/*.spec.ts" + ], + "exec": "npx ts-node ./src/main.ts" +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..f1a6b9c --- /dev/null +++ b/package.json @@ -0,0 +1,46 @@ +{ + "name": "wiki-lex-match", + "version": "1.0.0", + "description": "The API for wiki-lex-match", + "main": "main.js", + "lint-staged": { + "src/**/*.ts": [ + "npm run format" + ] + }, + "scripts": { + "test": "jest", + "start": "node ./dist/main.js", + "dev": "npx nodemon", + "build": "rm -rf ./dist && npx tsc", + "format": "npx prettier --config ./.prettierrc.json --write ./src", + "format:check": "npx prettier --config ./.prettierrc.json --check ./src", + "prepare": "npx husky install", + "pre-commit": "npx lint-staged" + }, + "keywords": [ + "wikimedia", + "lex-match", + "wiki-lex-match", + "wikicommon", + "wikidata" + ], + "author": "Wiki mentor Africa Cameroon - Yaounde", + "license": "ISC", + "devDependencies": { + "@types/cors": "^2.8.17", + "@types/dotenv": "^8.2.0", + "@types/express": "^4.17.21", + "@types/node": "^22.2.0", + "nodemon": "^3.1.4", + "ts-node": "^10.9.2", + "typescript": "^5.5.4" + }, + "dependencies": { + "cors": "^2.8.5", + "dotenv": "^16.4.5", + "express": "^4.19.2", + "lint-staged": "^15.2.5", + "prettier": "^3.3.3" + } +} diff --git a/public/404.html b/public/404.html new file mode 100644 index 0000000..741dfb8 --- /dev/null +++ b/public/404.html @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + wiki-lex-match - 404 route + + + + + +
+
+

+ 404 +

+ +
+

+ Route not found +

+ + + Home + +
+
+ +

+ cannot Get route: + + +

+
+ + + + + + \ No newline at end of file diff --git a/public/main.html b/public/main.html new file mode 100644 index 0000000..7d043dc --- /dev/null +++ b/public/main.html @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + wiki-lex-match + + + + + +
+

+ welcome to wiki-lex-match +

+ +

+ By Wiki mentor Africa Cameroon - Yaounde developer at + @rebase code camp +

+ + + API docs + +
+ + + \ No newline at end of file diff --git a/src/config/app-config.ts b/src/config/app-config.ts new file mode 100644 index 0000000..a68944f --- /dev/null +++ b/src/config/app-config.ts @@ -0,0 +1,8 @@ +import { config } from "dotenv"; + +config(); + +export const APP_CONF = { + port: process.env.PORT!, + env: process.env.NODE_ENV as "dev" | "prod", +}; diff --git a/src/main.ts b/src/main.ts new file mode 100644 index 0000000..075c205 --- /dev/null +++ b/src/main.ts @@ -0,0 +1,19 @@ +import express, { Application } from "express"; +import App from "./server"; +import { APP_CONF } from "./config/app-config"; + +(async () => { + const app: Application = express(); + + const { port: PORT, env } = APP_CONF; + + App(app) + .then(() => + app.listen(PORT, () => + console.log( + `\nserver listening on ${env === "dev" ? "http://localhost:" : "port "}${PORT}` + ) + ) + ) + .catch(console.error); +})(); diff --git a/src/middlewares/index.ts b/src/middlewares/index.ts new file mode 100644 index 0000000..4091b1e --- /dev/null +++ b/src/middlewares/index.ts @@ -0,0 +1,3 @@ +/** + * export middle wares from here + */ diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts new file mode 100644 index 0000000..65c654c --- /dev/null +++ b/src/modules/auth/auth.controller.ts @@ -0,0 +1,16 @@ +import { type RequestHandler } from "express"; +import { AuthService } from "./auth.service"; + +export class AuthController { + private authService: AuthService; + + constructor() { + this.authService = new AuthService(); + } + + index: RequestHandler = (req, res) => { + return res.status(200).json({ + message: "Intergrade wikimedia OAuth 1.0a", + }); + }; +} diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts new file mode 100644 index 0000000..4c1dc30 --- /dev/null +++ b/src/modules/auth/auth.service.ts @@ -0,0 +1,5 @@ +export class AuthService { + constructor() { + // + } +} diff --git a/src/modules/languages/language.controller.ts b/src/modules/languages/language.controller.ts new file mode 100644 index 0000000..f56db1d --- /dev/null +++ b/src/modules/languages/language.controller.ts @@ -0,0 +1,16 @@ +import { type RequestHandler } from "express"; +import { LanguageService } from "./language.service"; + +export class LanguageController { + private languageService: LanguageService; + + constructor() { + this.languageService = new LanguageService(); + } + + getAllLanguageList: RequestHandler = (req, res) => { + return res.status(200).json({ + message: "return languages", + }); + }; +} diff --git a/src/modules/languages/language.service.ts b/src/modules/languages/language.service.ts new file mode 100644 index 0000000..07cff1e --- /dev/null +++ b/src/modules/languages/language.service.ts @@ -0,0 +1,5 @@ +export class LanguageService { + constructor() { + // + } +} diff --git a/src/modules/match/match.controller.ts b/src/modules/match/match.controller.ts new file mode 100644 index 0000000..c0003b9 --- /dev/null +++ b/src/modules/match/match.controller.ts @@ -0,0 +1,16 @@ +import { type RequestHandler } from "express"; +import { MatchService } from "./match.service"; + +export class MatchController { + private matchService: MatchService; + + constructor() { + this.matchService = new MatchService(); + } + + list: RequestHandler = (req, res) => { + return res.status(200).json({ + message: "return dictionaries", + }); + }; +} diff --git a/src/modules/match/match.service.ts b/src/modules/match/match.service.ts new file mode 100644 index 0000000..ac5df37 --- /dev/null +++ b/src/modules/match/match.service.ts @@ -0,0 +1,5 @@ +export class MatchService { + constructor() { + // + } +} diff --git a/src/routes/auth-router.ts b/src/routes/auth-router.ts new file mode 100644 index 0000000..f8f39c9 --- /dev/null +++ b/src/routes/auth-router.ts @@ -0,0 +1,10 @@ +import { Router } from "express"; +import { AuthController } from "../modules/auth/auth.controller"; + +const router = Router(); + +const authController = new AuthController(); + +router.get("/", authController.index.bind(authController)); + +export const authRouter = router; diff --git a/src/routes/language-router.ts b/src/routes/language-router.ts new file mode 100644 index 0000000..d0dc046 --- /dev/null +++ b/src/routes/language-router.ts @@ -0,0 +1,10 @@ +import { Router } from "express"; +import { LanguageController } from "../modules/languages/language.controller"; + +const router = Router(); + +const languageController = new LanguageController(); + +router.get("/", languageController.getAllLanguageList.bind(languageController)); + +export const languageRouter = router; diff --git a/src/routes/match-router.ts b/src/routes/match-router.ts new file mode 100644 index 0000000..7cf1d38 --- /dev/null +++ b/src/routes/match-router.ts @@ -0,0 +1,10 @@ +import { Router } from "express"; +import { MatchController } from "../modules/match/match.controller"; + +const router = Router(); + +const matchController = new MatchController(); + +router.get("/", matchController.list.bind(matchController)); + +export const matchRouter = router; diff --git a/src/server/index.ts b/src/server/index.ts new file mode 100644 index 0000000..d1d8f2a --- /dev/null +++ b/src/server/index.ts @@ -0,0 +1,38 @@ +import express, { type Application } from "express"; +import { authRouter } from "../routes/auth-router"; +import { languageRouter } from "../routes/language-router"; +import { matchRouter } from "../routes/match-router"; +import path from "path"; +import cors, { type CorsOptions } from "cors"; + +export default async (app: Application) => { + const cors_options: CorsOptions = { + origin: "*", + }; + + app.use(cors(cors_options)); + app.use(express.json()); + app.use(express.urlencoded({ extended: true })); + + app.get("/", (_, res) => { + return res + .status(200) + .sendFile(path.join(__dirname, "../../public/main.html")); + }); + + app.use("/auth", authRouter); + app.use("/languages", languageRouter); + app.use("/match", matchRouter); + + app.get("/health", (_, res) => { + res.status(200).json({ + message: "🚀 server up and running 🚀", + }); + }); + + app.get("*", (_, res) => { + return res + .status(200) + .sendFile(path.join(__dirname, "../../public/404.html")); + }); +}; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..ba74b4b --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,108 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig to read more about this file */ + + /* Projects */ + // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ + // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ + // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ + // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ + // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ + // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ + + /* Language and Environment */ + "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + // "jsx": "preserve", /* Specify what JSX code is generated. */ + // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ + // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ + // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ + // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ + // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ + // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ + // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ + // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ + + /* Modules */ + "module": "commonjs", /* Specify what module code is generated. */ + // "rootDir": "./", /* Specify the root folder within your source files. */ + // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ + // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ + // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ + // "types": [], /* Specify type package names to be included without being referenced in a source file. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ + // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */ + // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */ + // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ + // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ + // "resolveJsonModule": true, /* Enable importing .json files. */ + // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ + // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ + + /* JavaScript Support */ + // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ + // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ + // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ + + /* Emit */ + // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + // "declarationMap": true, /* Create sourcemaps for d.ts files. */ + // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ + // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ + "outDir": "./dist", /* Specify an output folder for all emitted files. */ + // "removeComments": true, /* Disable emitting comments. */ + // "noEmit": true, /* Disable emitting files from a compilation. */ + // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ + // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ + // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ + // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ + // "newLine": "crlf", /* Set the newline character for emitting files. */ + // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ + // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ + // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ + // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ + // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + + /* Interop Constraints */ + // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ + // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */ + // "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */ + // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ + "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ + // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ + "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ + + /* Type Checking */ + "strict": true, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ + // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ + // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ + // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ + // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ + // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ + // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ + // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ + // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ + // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ + // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ + // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ + // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ + // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ + // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ + + /* Completeness */ + // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ + } +}