Skip to content

Commit

Permalink
fix(userCompletedDB): update import logic and tsconfig for userComple…
Browse files Browse the repository at this point in the history
…tedDB

- Removed direct import of `userCompletedDB` and added it to `userCompletedDBPath` import
- Reformatted `fetch` call in `fetchUserCompletedList` for better readability
- Modified `filterUserCompletedList` to dynamically import `userCompletedDB` from `userCompletedDBPath`
- Updated `tsconfig.json` to include `./config/userCompletedDB.js` in the compilation process

These updates improve the readability and maintainability of the code by refactoring import logic and updating TypeScript configuration.
  • Loading branch information
tsdevau committed May 29, 2024
1 parent e8b2e13 commit 2a6aa95
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/getUserCompletedList.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fetch from "node-fetch"
import { userID } from "./config/config.js"
import { userCompletedDB } from "./config/userCompletedDB.js"
import { userCompletedDBPath, userID } from "./config/config.js"
import type { CodewarsApiResponse, UserCompletedDBEntry } from "./types.js"
import { updateUserCompletedDB } from "./writeToFile.js"

Expand Down Expand Up @@ -39,9 +38,9 @@ async function fetchUserCompletedList(): Promise<UserCompletedDBEntry[]> {

do {
responseBody = JSON.parse(
await fetch(`http://www.codewars.com/api/v1/users/${userID}/code-challenges/completed?page=${page}`).then(
(res) => res.text(),
),
await fetch(
`http://www.codewars.com/api/v1/users/${userID}/code-challenges/completed?page=${page}`,
).then((res) => res.text()),
)
fullUserCompletedList.push(...responseBody.data)
page += 1
Expand All @@ -62,9 +61,12 @@ async function fetchUserCompletedList(): Promise<UserCompletedDBEntry[]> {
* @param {UserCompletedDBEntry[]} fullCompletedKataList - Full list of completed Katas
* @returns {Promise<UserCompletedDBEntry[]>} - Filtered list of completed Katas
**/
async function filterUserCompletedList(fullUserCompletedList: UserCompletedDBEntry[]): Promise<UserCompletedDBEntry[]> {

async function filterUserCompletedList(
fullUserCompletedList: UserCompletedDBEntry[],
): Promise<UserCompletedDBEntry[]> {
try {
const userCompletedDB: UserCompletedDBEntry[] = (await import(userCompletedDBPath))
?.userCompletedDB
const filteredUserCompletedList = fullUserCompletedList.filter(
(fullListKata) =>
!userCompletedDB.find((userListKata) => userListKata.id === fullListKata.id) ||
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"outDir": "./dist",
"resolveJsonModule": true,
"baseUrl": ".",
"rootDir": "./src",
"rootDir": "./src"
},
"include": ["src/**/*"],
"include": ["src/**/*", "./config/userCompletedDB.js"],
"exclude": ["node_modules/**/*", "dist/**/*", "private/**/*"]
}

0 comments on commit 2a6aa95

Please sign in to comment.