-
Notifications
You must be signed in to change notification settings - Fork 0
Erick/profile #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Erick/profile #15
Changes from all commits
0964c3a
7f4e25e
453822d
b535c5c
533f3f3
98dc269
ba1f6aa
0728c34
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| dist | ||
| node_modules | ||
| .github |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "printWidth": 100, | ||
| "semi": true, | ||
| "singleQuote": true, | ||
| "trailingComma": "es5", | ||
| "arrowParens": "avoid" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "accessibility.signals.chatRequestSent": { | ||
| "sound": "off", | ||
| "announcement": "off" | ||
| } | ||
| } | ||
| "accessibility.signals.chatRequestSent": { | ||
| "sound": "off", | ||
| "announcement": "off" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,45 @@ | ||
| # Node.js and Express Backend | ||
|
|
||
| [](https://github.com/pakeku/backend-api/actions/workflows/tests.yml) | ||
| [](https://snyk.io/test/github/pakeku/backend-api) | ||
|
|
||
| ## Requirements | ||
|
|
||
| Identify your MongoDB URL. Visit MongoDB to sign up and get started. | ||
|
|
||
| Environmental Variables: | ||
|
|
||
| 1. MONGO_URL | ||
| 2. PORT (optional) | ||
| 3. ALLOWED_ORIGINS (optional) | ||
| 4. ALLOWED_METHODS (optional) | ||
| 5. ALLOWED_HEADERS (optional) | ||
| 6. NODE_ENV=test --- When set to ***"test"***, a `mongodb-memory-server` test URI is used, and no `MONGO_URL` is required. This allows for out-of-the-box testing without a live database. | ||
| 6. NODE\*ENV=test --- When set to \*\*\*"test"\_\*\*, a `mongodb-memory-server` test URI is used, and no `MONGO_URL` is required. This allows for out-of-the-box testing without a live database. | ||
| 7. JWT_SECRET --- A cryptographically secure secret used to sign and verify JSON Web Tokens (JWTs). This is required for authentication to work correctly. | ||
| Use a long, random string—at least 32 characters, ideally generated using a password manager or Node.js: `bash node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"` | ||
| 8. | ||
|
|
||
| ## Getting Started | ||
|
|
||
| 1. Copy this file to .env and fill in the actual values | ||
| ```bash | ||
|
|
||
| ```bash | ||
| cp .env.sample .env | ||
| ``` | ||
|
|
||
| 1. Run a script: | ||
| ```json | ||
|
|
||
| ```json | ||
| "scripts": { | ||
| "prebuild":"rm -rf dist", | ||
| "build":"tsc", | ||
| "start": "node ./src/index.js", | ||
| "dev": "env-cmd nodemon ./src/index.js", | ||
| "test": "jest" | ||
| "dev": "env-cmd nodemon ./src/index.ts", | ||
| "test": "jest", | ||
| "test:watch": "jest --watch", | ||
| "lint": "eslint . --ext .ts", | ||
| "lint:fix": "eslint . --ext .ts --fix", | ||
| "lint:check": "eslint . --ext .ts --no-ignore", | ||
| "format": "prettier --write ." | ||
| } | ||
| ``` | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // @ts-check | ||
|
|
||
| import eslint from '@eslint/js'; | ||
| import tseslint from 'typescript-eslint'; | ||
| import perfectionist from 'eslint-plugin-perfectionist'; | ||
|
|
||
| export default tseslint.config( | ||
| { | ||
| ignores: ['**/*.js'], | ||
| }, | ||
| eslint.configs.recommended, | ||
| tseslint.configs.strictTypeChecked, | ||
| tseslint.configs.stylisticTypeChecked, | ||
| { | ||
| languageOptions: { | ||
| parserOptions: { | ||
| projectService: true, | ||
| tsconfigRootDir: import.meta.dirname, | ||
| }, | ||
| }, | ||
| }, | ||
| perfectionist.configs['recommended-natural'] | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| /** @type {import('ts-jest').JestConfigWithTsJest} **/ | ||
| export default { | ||
| testEnvironment: "node", | ||
| testEnvironment: 'node', | ||
| transform: { | ||
| "^.+\.tsx?$": ["ts-jest",{}], | ||
| '^.+\.tsx?$': ['ts-jest', {}], | ||
| }, | ||
| }; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,17 @@ | ||
| import express, { Application } from 'express'; | ||
|
|
||
| import errorHandler from './midleware/errorHandler'; | ||
| import rateLimiter from './midleware/rateLimiter'; | ||
| import compression from './midleware/compression'; | ||
| import cors from './midleware/cors'; | ||
| import errorHandler from './midleware/errorHandler'; | ||
| import helmet from './midleware/helmet'; | ||
| import json from './midleware/json'; | ||
| import cors from './midleware/cors'; | ||
| import morgan from './midleware/morgan'; | ||
|
|
||
| import notFoundRouter from './routes/notFoundRoute'; | ||
| import rateLimiter from './midleware/rateLimiter'; | ||
|
Comment on lines
3
to
+9
|
||
| import authRouter from './routes/authRoute'; | ||
| import healthRouter from './routes/healthRoute'; | ||
| import storesRouter from './routes/storesRoute'; | ||
| import notFoundRouter from './routes/notFoundRoute'; | ||
| import rootRouter from './routes/rootRoute'; | ||
| import authRouter from './routes/authRoute'; | ||
| import storesRouter from './routes/storesRoute'; | ||
|
|
||
| const app: Application = express(); | ||
|
|
||
|
|
@@ -35,4 +34,4 @@ app.use('/stores', storesRouter); | |
| app.use('/auth', authRouter); | ||
| app.use('*', notFoundRouter); | ||
|
|
||
| export default app; | ||
| export default app; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,74 +1,73 @@ | ||||||||||||||||
| import { getDatabase } from './mongo-common'; | ||||||||||||||||
| import { ObjectId } from 'mongodb'; | ||||||||||||||||
|
|
||||||||||||||||
| import getUserName from '../utils/git-user-name'; | ||||||||||||||||
| import { getDatabase } from './mongo-common'; | ||||||||||||||||
|
|
||||||||||||||||
| // Define the Store interface | ||||||||||||||||
| interface Store { | ||||||||||||||||
| _id?: string; | ||||||||||||||||
| name: string; | ||||||||||||||||
| export interface Store { | ||||||||||||||||
| _id: ObjectId; | ||||||||||||||||
| addedBy?: string; | ||||||||||||||||
| metadata?: string; | ||||||||||||||||
| name: string; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| const collectionName = 'stores'; | ||||||||||||||||
|
|
||||||||||||||||
| // Create a Store | ||||||||||||||||
| async function createStore(store: Store): Promise<Store | null> { | ||||||||||||||||
| async function createStore(store: Store): Promise<null | Store> { | ||||||||||||||||
| const database = await getDatabase(); | ||||||||||||||||
| store.addedBy = getUserName(); | ||||||||||||||||
|
|
||||||||||||||||
| const storeToInsert = { ...store, _id: store._id ? new ObjectId(store._id) : undefined }; | ||||||||||||||||
| const { insertedId } = await database.collection(collectionName).insertOne(storeToInsert); | ||||||||||||||||
|
|
||||||||||||||||
| // Return the store document with the inserted _id | ||||||||||||||||
| return await database.collection(collectionName).findOne({ _id: insertedId }) as Store | null; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // Get all stores | ||||||||||||||||
| async function getStores(): Promise<Store[]> { | ||||||||||||||||
| const database = await getDatabase(); | ||||||||||||||||
| const stores = await database.collection(collectionName).find({}).toArray(); | ||||||||||||||||
| return stores.map(store => ({ | ||||||||||||||||
| _id: store._id?.toString(), | ||||||||||||||||
| name: store.name, | ||||||||||||||||
| addedBy: store.addedBy, | ||||||||||||||||
| })) as Store[]; | ||||||||||||||||
| return (await database.collection(collectionName).findOne({ _id: insertedId })) as null | Store; | ||||||||||||||||
|
||||||||||||||||
| return (await database.collection(collectionName).findOne({ _id: insertedId })) as null | Store; | |
| const insertedStore = await database.collection(collectionName).findOne({ _id: insertedId }); | |
| if (!insertedStore) return null; | |
| return { | |
| ...insertedStore, | |
| _id: insertedId.toString(), | |
| } as Store; |
Copilot
AI
May 17, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Map the ObjectId to a string (e.g. store._id.toString()) so that JSON serialization and clients receive a plain string identifier.
| _id: store._id, | |
| _id: store._id.toString(), |
Copilot
AI
May 17, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid wrapping an existing ObjectId in new ObjectId(...), which will generate a new ID. Use the returned updated._id directly or call updated._id.toString() if a string is required.
| _id: new ObjectId(updated._id), | |
| _id: updated._id, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line contains malformed Markdown and an incorrect environment variable name (
NODE*ENV). It should readNODE_ENV=testwithout backslashes and with proper emphasis syntax.