From bf4e8ff8da196ec318f5aa533cbd4cce750a557d Mon Sep 17 00:00:00 2001 From: Dominik Stumpf Date: Mon, 2 Dec 2024 10:01:00 +0100 Subject: [PATCH] chore: add ts-reset and default react-query error, refactor fetchers --- package.json | 1 + query.d.ts | 9 +++++++++ reset.d.ts | 2 ++ src/app/(dashboard)/explorer/fetchers.ts | 8 ++------ src/lib/fetcher.ts | 5 +++-- src/lib/types.ts | 13 +++++++++++++ tsconfig.json | 3 ++- 7 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 query.d.ts create mode 100644 reset.d.ts diff --git a/package.json b/package.json index 6b8ab3ce6c..2319e2cb33 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "@storybook/react": "^8.4.4", "@storybook/test": "^8.4.4", "@svgr/webpack": "^8.1.0", + "@total-typescript/ts-reset": "^0.6.1", "@types/node": "^20", "@types/react": "^18", "@types/react-dom": "^18", diff --git a/query.d.ts b/query.d.ts new file mode 100644 index 0000000000..a600ecfe04 --- /dev/null +++ b/query.d.ts @@ -0,0 +1,9 @@ +import '@tanstack/react-query' +import { ErrorLike } from './src/lib/types' + +declare module '@tanstack/react-query' { + interface Register { + defaultError: ErrorLike + } +} + diff --git a/reset.d.ts b/reset.d.ts new file mode 100644 index 0000000000..e4d600ccb0 --- /dev/null +++ b/reset.d.ts @@ -0,0 +1,2 @@ +// Do not add any other lines of code to this file! +import "@total-typescript/ts-reset"; diff --git a/src/app/(dashboard)/explorer/fetchers.ts b/src/app/(dashboard)/explorer/fetchers.ts index 7bfc490ea4..b783b5281b 100644 --- a/src/app/(dashboard)/explorer/fetchers.ts +++ b/src/app/(dashboard)/explorer/fetchers.ts @@ -1,17 +1,13 @@ import { env } from "@/lib/env"; import type { Guild } from "@/lib/schemas/guild"; import type { PaginatedResponse } from "@/lib/types"; +import { fetcher } from "../../../lib/fetcher"; import { PAGE_SIZE } from "./constants"; export const getGuildSearch = (search = "") => async ({ pageParam }: { pageParam: number }) => { - const res = await fetch( + return fetcher>( `${env.NEXT_PUBLIC_API}/guild/search?page=${pageParam}&pageSize=${PAGE_SIZE}&search=${search}`, ); - const json = await res.json(); - - if (json.error) throw new Error(json.error); - - return json as PaginatedResponse; }; diff --git a/src/lib/fetcher.ts b/src/lib/fetcher.ts index 425892f08d..2a3340bc48 100644 --- a/src/lib/fetcher.ts +++ b/src/lib/fetcher.ts @@ -1,4 +1,5 @@ import { env } from "./env"; +import type { ErrorLike } from "./types"; export const fetcher = async ( resource: string, @@ -13,8 +14,8 @@ export const fetcher = async ( if (!response.ok) { if (resource.includes(env.NEXT_PUBLIC_API)) { return Promise.reject({ - error: res.error, - } as { error: string }); + error: (res as ErrorLike).error || (res as ErrorLike).message, + }); } return Promise.reject(res as Error); diff --git a/src/lib/types.ts b/src/lib/types.ts index c289f700f7..57b5ecc82c 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -12,3 +12,16 @@ export type PaginatedResponse = { export type DynamicRoute> = { params: T; }; + +/** + * Unstable error structure coming from v3 backend + * + * @property message most common error response + * @property error on some endpoints this field is given instead of message + */ +// TODO: align this to backend when error handling gets consistent +export type ErrorLike = { + message: string; + status?: string; + error?: string; +}; diff --git a/tsconfig.json b/tsconfig.json index 70fb7eb54f..381fb4f92b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -39,7 +39,8 @@ "next-env.d.ts", "tailwind.config.ts", ".next/types/**/*.ts", - "guild.d.ts" + "reset.d.ts", + "query.d.ts" ], "exclude": [ "node_modules",