From dfb924bb3f98ae1684e2e25e8a1aa592da29d3f1 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 11 Nov 2021 11:35:11 +0100 Subject: [PATCH] [API] Update dependencies, fix error checking Typescript now defaults to unknown instead of any for Errors (catch). Implemented helper function to check AxiosErros. --- api/package.json | 22 +++++++++++----------- api/src/stores/index.ts | 20 ++++++++++++++++++++ api/src/stores/session.ts | 8 ++++---- api/src/stores/user.ts | 7 +++---- 4 files changed, 38 insertions(+), 19 deletions(-) diff --git a/api/package.json b/api/package.json index cbe07a7..3f61f6b 100644 --- a/api/package.json +++ b/api/package.json @@ -1,6 +1,6 @@ { "license": "MIT", - "version": "1.0.0-alpha.1", + "version": "1.0.0-alpha.2", "name": "@flaschengeist/api", "author": "Tim Gröger ", "homepage": "https://flaschengeist.dev/Flaschengeist", @@ -19,21 +19,21 @@ "pinia": "^2.0.0-alpha.19" }, "devDependencies": { - "@flaschengeist/types": "^1.0.0-alpha.1", - "@types/node": "^12.20.13", - "@typescript-eslint/eslint-plugin": "^4.24.0", - "@typescript-eslint/parser": "^4.24.0", - "eslint": "^7.26.0", + "@flaschengeist/types": "^1.0.0-alpha.4", + "@types/node": "^12.20.37", + "@typescript-eslint/eslint-plugin": "^5.3.1", + "@typescript-eslint/parser": "^5.3.1", + "eslint": "^8.2.0", "eslint-config-prettier": "^8.3.0", - "eslint-plugin-vue": "^7.9.0", - "eslint-webpack-plugin": "^2.5.4", - "prettier": "^2.3.0", - "typescript": "^4.2.4" + "eslint-plugin-vue": "^8.0.3", + "eslint-webpack-plugin": "^3.1.0", + "prettier": "^2.4.1", + "typescript": "^4.4.4" }, "prettier": { "singleQuote": true, "semi": true, - "printWidth": 120, + "printWidth": 100, "arrowParens": "always" } } diff --git a/api/src/stores/index.ts b/api/src/stores/index.ts index 858387d..b70f901 100644 --- a/api/src/stores/index.ts +++ b/api/src/stores/index.ts @@ -1,3 +1,23 @@ +import { AxiosError } from 'axios'; + +/** + * Check if error is an AxiosError, and optional if a specific status was returned + * + * @param error Thrown error to check + * @param status If set, check if this error has set thouse status code + */ +export function isAxiosError(error: unknown, status?: number) { + // Check if it is an axios error (with axios 1.0 `error instanceof AxiosError` will be possible) + if (!error || typeof error !== 'object' || !('isAxiosError' in error)) return false; + // Check status code if status was given + if (status !== undefined) + return ( + (error).response !== undefined && (error).response?.status === status + ); + + return true; +} + export * from './main'; export * from './session'; export * from './user'; diff --git a/api/src/stores/session.ts b/api/src/stores/session.ts index bbdbf4c..d3301d5 100644 --- a/api/src/stores/session.ts +++ b/api/src/stores/session.ts @@ -1,7 +1,7 @@ -import { AxiosError, AxiosResponse } from 'axios'; +import { AxiosResponse } from 'axios'; import { defineStore } from 'pinia'; import { api } from '../internal'; -import { useMainStore } from '.'; +import { isAxiosError, useMainStore } from '.'; export const useSessionStore = defineStore({ id: 'sessions', @@ -46,8 +46,8 @@ export const useSessionStore = defineStore({ await api.delete(`/auth/${token}`); return true; } catch (error) { - if (!error || !('response' in error) || (error).response?.status != 401) - throw error; + // Ignore 401, as this means we are already logged out, throw all other + if (!isAxiosError(error, 401)) throw error; } return false; }, diff --git a/api/src/stores/user.ts b/api/src/stores/user.ts index b22469c..055f11f 100644 --- a/api/src/stores/user.ts +++ b/api/src/stores/user.ts @@ -1,7 +1,6 @@ import { defineStore } from 'pinia'; -import { AxiosError } from 'axios'; import { api } from '../internal'; -import { useMainStore } from '.'; +import { isAxiosError, useMainStore } from '.'; export const useUserStore = defineStore({ id: 'users', @@ -30,8 +29,8 @@ export const useUserStore = defineStore({ else this.users[idx] = data; return data; } catch (error) { - if (!error || !('response' in error) || (error).response?.status !== 404) - throw error; + // Ignore 404, throw all other + if (!isAxiosError(error, 404)) throw error; } } else { return this.users[idx];