forked from SummerOfCode2020/week-17-store-manager-api-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Migrate to ts #16
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
Merged
Merged
Migrate to ts #16
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0964c3a
feat: get profile info
pakeku 7f4e25e
fix: add documentation about JWT_SECRET
pakeku 453822d
fix build output and start command, update documentation
pakeku b535c5c
use err message returned
pakeku 533f3f3
remove unused variable
pakeku 98dc269
enhancement: add prettier and eslint for consistent code
pakeku ba1f6aa
fix wrong response body check on test
pakeku 0728c34
fix bug re: failed test
pakeku ca9dffa
fix store responses
pakeku 11cf818
This commit enhances the function to detect the execution
pakeku File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| dist | ||
| node_modules | ||
| .github |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "printWidth": 100, | ||
| "semi": true, | ||
| "singleQuote": true, | ||
| "trailingComma": "es5", | ||
| "arrowParens": "avoid" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ." | ||
| } | ||
| ``` | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'] | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', {}], | ||
| }, | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 storeToInsert = { ...store, _id: store._id }; | ||
| 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; | ||
| } | ||
|
|
||
| // Delete a store by id | ||
| async function deleteStore(_id: string): Promise<{ message: string }> { | ||
| const database = await getDatabase(); | ||
|
|
||
| const result = await database.collection(collectionName).deleteOne({ | ||
| _id: new ObjectId(_id), | ||
| }); | ||
|
|
||
| if (result.deletedCount === 0) { | ||
| return { message: "No store found with that id" }; | ||
| return { message: 'No store found with that id' }; | ||
| } | ||
|
|
||
| return { message: "Store deleted" }; | ||
| return { message: 'Store deleted' }; | ||
| } | ||
|
|
||
| // Get all stores | ||
| async function getStores(): Promise<Store[]> { | ||
| const database = await getDatabase(); | ||
| const stores = await database.collection<Store>(collectionName).find({}).toArray(); | ||
| return stores.map(store => ({ | ||
| _id: store._id, | ||
| addedBy: store.addedBy, | ||
| name: store.name, | ||
| })); | ||
| } | ||
|
|
||
| // Update a store | ||
| async function updateStore(id: string, store: Partial<Store>): Promise<Store | null> { | ||
| async function updateStore(id: string, store: Partial<Store>): Promise<null | Store> { | ||
| const database = await getDatabase(); | ||
| delete store._id; | ||
|
|
||
| await database.collection(collectionName).updateOne( | ||
| { _id: new ObjectId(id) }, | ||
| { $set: store } | ||
| ); | ||
| await database.collection(collectionName).updateOne({ _id: new ObjectId(id) }, { $set: store }); | ||
|
|
||
| const updated = await database.collection(collectionName).findOne({ _id: new ObjectId(id) }); | ||
| const updated = await database | ||
| .collection<Store>(collectionName) | ||
| .findOne({ _id: new ObjectId(id) }); | ||
| if (!updated) return null; | ||
| return { | ||
| _id: updated._id?.toString(), | ||
| name: updated.name, | ||
| _id: new ObjectId(updated._id), | ||
| addedBy: updated.addedBy, | ||
| metadata: updated.metadata, | ||
| } as Store; | ||
| name: updated.name, | ||
| }; | ||
| } | ||
|
|
||
|
|
||
| export { createStore, getStores, deleteStore, updateStore }; | ||
| export { createStore, deleteStore, getStores, updateStore }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.