From ac892dcbde952a2bc4011c5d3893f877f170c3ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Jungmann?= Date: Sat, 24 Feb 2024 15:49:26 +0100 Subject: [PATCH 1/6] feat: use sveltekit RequestHandler for file download --- .../(authenticated)/export/xlsx/+server.ts | 24 ++++++++++++++ .../admin/(authenticated)/users/+page.svelte | 33 +++++-------------- packages/trpc/server/services/testService.ts | 4 +-- 3 files changed, 34 insertions(+), 27 deletions(-) create mode 100644 apps/web/src/routes/admin/(authenticated)/export/xlsx/+server.ts diff --git a/apps/web/src/routes/admin/(authenticated)/export/xlsx/+server.ts b/apps/web/src/routes/admin/(authenticated)/export/xlsx/+server.ts new file mode 100644 index 0000000..86287dd --- /dev/null +++ b/apps/web/src/routes/admin/(authenticated)/export/xlsx/+server.ts @@ -0,0 +1,24 @@ +import type { RequestHandler } from './$types'; + +import { router } from '@testy/trpc/server/router'; +import { createContext } from '@testy/trpc/server/createContext'; + +export const GET: RequestHandler = async (event) => { + try { + const trpc = router.createCaller(await createContext(event)); + + await trpc.auth.admin(); + + const xlsx = await trpc.users.xlsx(); + return new Response(xlsx, { + status: 200, + headers: { + 'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + // TODO: Můžeme doplnit čas atd. + 'Content-Disposition': `attachment; filename="vysledky.xlsx"` + } + }); + } catch { + return new Response('Čus 👀', { status: 403 }); + } +}; diff --git a/apps/web/src/routes/admin/(authenticated)/users/+page.svelte b/apps/web/src/routes/admin/(authenticated)/users/+page.svelte index 457dafb..8ad8d24 100644 --- a/apps/web/src/routes/admin/(authenticated)/users/+page.svelte +++ b/apps/web/src/routes/admin/(authenticated)/users/+page.svelte @@ -1,38 +1,17 @@