generated from acdh-oeaw/template-app-nuxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f1032c
commit 4035d47
Showing
7 changed files
with
1,659 additions
and
1,211 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -2,6 +2,9 @@ | |
node_modules/ | ||
.pnpm-store/ | ||
|
||
#generated libs | ||
lib | ||
|
||
# logs | ||
*.log | ||
|
||
|
This file contains 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 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 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 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,15 @@ | ||
import { createApiClient } from "../lib/api.ts"; | ||
|
||
export default defineNuxtPlugin({ | ||
name: "api", | ||
setup() { | ||
const config = useRuntimeConfig(); | ||
const apiBaseUrl = config.public.NUXT_PUBLIC_API_BASE_URL; | ||
const client = createApiClient(apiBaseUrl); | ||
return { | ||
provide: { | ||
api: client, | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains 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,65 @@ | ||
import { HttpError, log } from "@acdh-oeaw/lib"; | ||
import { | ||
dehydrate, | ||
type DehydratedState, | ||
hydrate, | ||
keepPreviousData, | ||
QueryCache, | ||
QueryClient, | ||
VueQueryPlugin, | ||
type VueQueryPluginOptions, | ||
} from "@tanstack/vue-query"; | ||
|
||
export default defineNuxtPlugin((nuxt) => { | ||
const state = useState<DehydratedState | null>("vue-query"); | ||
|
||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
placeholderData: keepPreviousData, | ||
staleTime: 1000 * 60 * 15, | ||
}, | ||
}, | ||
queryCache: new QueryCache({ | ||
async onError(error) { | ||
const message = await getErrorMessage(error); | ||
log.error(message); | ||
}, | ||
}), | ||
}); | ||
|
||
const options: VueQueryPluginOptions = { queryClient }; | ||
|
||
nuxt.vueApp.use(VueQueryPlugin, options); | ||
|
||
if (process.server) { | ||
nuxt.hooks.hook("app:rendered", () => { | ||
state.value = dehydrate(queryClient); | ||
}); | ||
} | ||
|
||
if (process.client) { | ||
nuxt.hooks.hook("app:created", () => { | ||
hydrate(queryClient, state.value); | ||
}); | ||
} | ||
}); | ||
|
||
async function getErrorMessage(error: Error): Promise<string> { | ||
if (error instanceof HttpError) { | ||
const mediaType = error.response.headers.get("content-type")?.split(";", 1).at(0); | ||
if (mediaType === "application/json" && !error.response.bodyUsed) { | ||
try { | ||
const data = (await error.response.json()) as { message: string }; | ||
|
||
return data.message; | ||
} catch { | ||
/** */ | ||
} | ||
} | ||
|
||
return error.response.statusText; | ||
} | ||
|
||
return error.message; | ||
} |
Oops, something went wrong.