From c324bfcfee274fe3a66dff71269ee95f4915fdee Mon Sep 17 00:00:00 2001 From: "Chen, Tai-Lin" Date: Thu, 22 Feb 2024 21:00:28 +0800 Subject: [PATCH] update utils --- app/src/utils/{api.js => api.ts} | 0 app/src/utils/{auth.js => auth.ts} | 10 +++++----- app/src/utils/{errors.js => errors.ts} | 12 +++++++----- app/src/utils/{markdown.js => markdown.ts} | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) rename app/src/utils/{api.js => api.ts} (100%) rename app/src/utils/{auth.js => auth.ts} (84%) rename app/src/utils/{errors.js => errors.ts} (69%) rename app/src/utils/{markdown.js => markdown.ts} (70%) diff --git a/app/src/utils/api.js b/app/src/utils/api.ts similarity index 100% rename from app/src/utils/api.js rename to app/src/utils/api.ts diff --git a/app/src/utils/auth.js b/app/src/utils/auth.ts similarity index 84% rename from app/src/utils/auth.js rename to app/src/utils/auth.ts index 4094066..b4104e3 100644 --- a/app/src/utils/auth.js +++ b/app/src/utils/auth.ts @@ -12,7 +12,7 @@ export async function isLoggedIn() { return loggedIn } -function handleRequestError(err) { +function handleRequestError(err: { response: { data?: any; status?: any } }) { if (!err.response) throw err // Not an axios error const { status } = err.response if (status === 429) { @@ -41,14 +41,14 @@ function handleRequestError(err) { throw err } -export const login = (username, password) => { +export const login = (username: any, password: any) => { return api .post('/auth/login', { username, password }) .then(() => true) .catch(handleRequestError) } -export const register = (username, email, password) => { +export const register = (username: any, email: any, password: any) => { return api .post('/auth/register', { username, @@ -59,7 +59,7 @@ export const register = (username, email, password) => { .catch(handleRequestError) } -export const checkUsername = async (username) => { +export const checkUsername = async (username: any) => { return api .get('/auth/check-username', { params: { username }, @@ -68,7 +68,7 @@ export const checkUsername = async (username) => { .catch(handleRequestError) } -export const chooseUsername = async (username) => { +export const chooseUsername = async (username: any) => { return api .post('/auth/register-from-session', { username }) .then(() => true) diff --git a/app/src/utils/errors.js b/app/src/utils/errors.ts similarity index 69% rename from app/src/utils/errors.js rename to app/src/utils/errors.ts index ff8af71..0f94185 100644 --- a/app/src/utils/errors.js +++ b/app/src/utils/errors.ts @@ -1,27 +1,28 @@ /* eslint-disable max-classes-per-file */ export class TooManyRequestsError extends Error { - constructor(message) { + constructor(message: string | undefined) { super(message) this.name = 'TooManyRequestsError' } } export class ValidationError extends Error { - constructor(message) { + constructor(message: string | undefined) { super(message) this.name = 'ValidationError' } } export class UnauthorizedError extends Error { - constructor(message) { + constructor(message: string | undefined) { super(message) this.name = 'UnauthorizedError' } } export class LoadQuestError extends Error { - constructor(message, questId) { + questId: any + constructor(message: string | undefined, questId: any) { super(message) this.name = 'LoadQuestError' this.questId = questId @@ -29,7 +30,8 @@ export class LoadQuestError extends Error { } export class LoadSessionError extends Error { - constructor(message, questId) { + questId: any + constructor(message: string | undefined, questId: any) { super(message) this.name = 'LoadSessionError' this.questId = questId diff --git a/app/src/utils/markdown.js b/app/src/utils/markdown.ts similarity index 70% rename from app/src/utils/markdown.js rename to app/src/utils/markdown.ts index 32b4276..7733041 100644 --- a/app/src/utils/markdown.js +++ b/app/src/utils/markdown.ts @@ -1,7 +1,7 @@ import { marked } from 'marked' import DOMPurify from 'dompurify' -export default function markdown2HTML(markdown) { +export default function markdown2HTML(markdown: string) { const html = marked.parse(markdown) return DOMPurify.sanitize(html) }