From 93a50770d012d710810deda594a6c6770f106d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Pe=CC=81ault?= Date: Tue, 16 Aug 2022 20:00:52 +0200 Subject: [PATCH] Move API fetch data to different file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sharing multiple exportable functions in one file impacts the use of private env variables 🤷 --- package.json | 2 +- pnpm-lock.yaml | 14 ++++----- src/routes/+layout.server.ts | 2 +- src/routes/+page.server.ts | 2 +- .../[country]/[location]/+page.server.ts | 2 +- .../[location]/[photo]/+page.server.ts | 2 +- src/routes/about/+page.server.ts | 2 +- src/routes/api/data/+server.ts | 2 +- src/routes/credits/+page.server.ts | 2 +- src/routes/photos/+page.server.ts | 2 +- src/routes/shop/+layout.server.ts | 2 +- src/routes/shop/+page.server.ts | 2 +- src/routes/shop/[type]-[name]/+page.server.ts | 2 +- src/routes/subscribe/+page.server.ts | 2 +- src/utils/api/data.ts | 29 +++++++++++++++++++ src/utils/{api.ts => api/index.ts} | 28 +----------------- 16 files changed, 50 insertions(+), 47 deletions(-) create mode 100644 src/utils/api/data.ts rename src/utils/{api.ts => api/index.ts} (54%) diff --git a/package.json b/package.json index 39044df..a0593f2 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "@sveltejs/adapter-auto": "^1.0.0-next.64", "@sveltejs/adapter-node": "^1.0.0-next.86", "@sveltejs/adapter-vercel": "^1.0.0-next.66", - "@sveltejs/kit": "^1.0.0-next.410", + "@sveltejs/kit": "^1.0.0-next.411", "@typescript-eslint/eslint-plugin": "^5.33.1", "@typescript-eslint/parser": "^5.33.1", "browserslist": "^4.21.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e5c72e8..d1cc32e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,7 +5,7 @@ specifiers: '@sveltejs/adapter-auto': ^1.0.0-next.64 '@sveltejs/adapter-node': ^1.0.0-next.86 '@sveltejs/adapter-vercel': ^1.0.0-next.66 - '@sveltejs/kit': ^1.0.0-next.410 + '@sveltejs/kit': ^1.0.0-next.411 '@typescript-eslint/eslint-plugin': ^5.33.1 '@typescript-eslint/parser': ^5.33.1 browserslist: ^4.21.3 @@ -47,7 +47,7 @@ devDependencies: '@sveltejs/adapter-auto': 1.0.0-next.64 '@sveltejs/adapter-node': 1.0.0-next.86 '@sveltejs/adapter-vercel': 1.0.0-next.66 - '@sveltejs/kit': 1.0.0-next.410_svelte@3.49.0+vite@3.0.8 + '@sveltejs/kit': 1.0.0-next.411_svelte@3.49.0+vite@3.0.8 '@typescript-eslint/eslint-plugin': 5.33.1_vsoshirnpb7xw6mr7xomgfas2i '@typescript-eslint/parser': 5.33.1_4rv7y5c6xz3vfxwhbrcxxi73bq browserslist: 4.21.3 @@ -461,8 +461,8 @@ packages: - supports-color dev: true - /@sveltejs/kit/1.0.0-next.410_svelte@3.49.0+vite@3.0.8: - resolution: {integrity: sha512-/EGeitdSnm0GlAL6fH9vDtzYZsrXSEzSmDE9eoOQa6XfFZMPmgeV8fPReJtqlp+oLR4Q5I/eSV21yrvtoR+reQ==} + /@sveltejs/kit/1.0.0-next.411_svelte@3.49.0+vite@3.0.8: + resolution: {integrity: sha512-VSkQemKpztgsKjjkrp9kkYI7ESe3mspXjibKaevJ+FzTcJqB1Ehnp8lRbspXqXGtumrLLNADpLJ9c9vhXhhsSA==} engines: {node: '>=16.9'} hasBin: true requiresBuild: true @@ -522,8 +522,8 @@ packages: resolution: {integrity: sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==} dev: true - /@types/node/18.7.5: - resolution: {integrity: sha512-NcKK6Ts+9LqdHJaW6HQmgr7dT/i3GOHG+pt6BiWv++5SnjtRd4NXeiuN2kA153SjhXPR/AhHIPHPbrsbpUVOww==} + /@types/node/18.7.6: + resolution: {integrity: sha512-EdxgKRXgYsNITy5mjjXjVE/CS8YENSdhiagGrLqjG0pvA2owgJ6i4l7wy/PFZGC0B1/H20lWKN7ONVDNYDZm7A==} dev: true /@types/pug/2.0.6: @@ -533,7 +533,7 @@ packages: /@types/sass/1.43.1: resolution: {integrity: sha512-BPdoIt1lfJ6B7rw35ncdwBZrAssjcwzI5LByIrYs+tpXlj/CAkuVdRsgZDdP4lq5EjyWzwxZCqAoFyHKFwp32g==} dependencies: - '@types/node': 18.7.5 + '@types/node': 18.7.6 dev: true /@typescript-eslint/eslint-plugin/5.33.1_vsoshirnpb7xw6mr7xomgfas2i: diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts index f364612..61548ce 100644 --- a/src/routes/+layout.server.ts +++ b/src/routes/+layout.server.ts @@ -1,6 +1,6 @@ import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' -import { fetchAPI } from '$utils/api' +import { fetchAPI } from '$utils/api/data' import { PUBLIC_PREVIEW_COUNT } from '$env/static/public' diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index cde7005..676ed84 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -1,6 +1,6 @@ import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' -import { fetchAPI } from '$utils/api' +import { fetchAPI } from '$utils/api/data' import { getRandomItems } from '$utils/functions' export const load: PageServerLoad = async () => { diff --git a/src/routes/[country]/[location]/+page.server.ts b/src/routes/[country]/[location]/+page.server.ts index 7a03331..845104f 100644 --- a/src/routes/[country]/[location]/+page.server.ts +++ b/src/routes/[country]/[location]/+page.server.ts @@ -1,7 +1,7 @@ import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' import { PUBLIC_LIST_AMOUNT } from '$env/static/public' -import { fetchAPI } from '$utils/api' +import { fetchAPI } from '$utils/api/data' export const photoFields = ` title diff --git a/src/routes/[country]/[location]/[photo]/+page.server.ts b/src/routes/[country]/[location]/[photo]/+page.server.ts index c6a88c3..9618c00 100644 --- a/src/routes/[country]/[location]/[photo]/+page.server.ts +++ b/src/routes/[country]/[location]/[photo]/+page.server.ts @@ -1,6 +1,6 @@ import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' -import { fetchAPI } from '$utils/api' +import { fetchAPI } from '$utils/api/data' export const load: PageServerLoad = async ({ params }) => { try { diff --git a/src/routes/about/+page.server.ts b/src/routes/about/+page.server.ts index e19dda2..9be3048 100644 --- a/src/routes/about/+page.server.ts +++ b/src/routes/about/+page.server.ts @@ -1,6 +1,6 @@ import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' -import { fetchAPI } from '$utils/api' +import { fetchAPI } from '$utils/api/data' import { getRandomItems } from '$utils/functions' export const load: PageServerLoad = async () => { diff --git a/src/routes/api/data/+server.ts b/src/routes/api/data/+server.ts index 4722449..d4168da 100644 --- a/src/routes/api/data/+server.ts +++ b/src/routes/api/data/+server.ts @@ -1,6 +1,6 @@ import { error } from '@sveltejs/kit' import type { RequestHandler } from './$types' -import { fetchAPI } from '$utils/api' +import { fetchAPI } from '$utils/api/data' export const POST: RequestHandler = async ({ request }) => { diff --git a/src/routes/credits/+page.server.ts b/src/routes/credits/+page.server.ts index 3f5667a..0cf2e46 100644 --- a/src/routes/credits/+page.server.ts +++ b/src/routes/credits/+page.server.ts @@ -1,6 +1,6 @@ import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' -import { fetchAPI } from '$utils/api' +import { fetchAPI } from '$utils/api/data' export const load: PageServerLoad = async () => { try { diff --git a/src/routes/photos/+page.server.ts b/src/routes/photos/+page.server.ts index 3fd3765..5808ab4 100644 --- a/src/routes/photos/+page.server.ts +++ b/src/routes/photos/+page.server.ts @@ -1,6 +1,6 @@ import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' -import { fetchAPI } from '$utils/api' +import { fetchAPI } from '$utils/api/data' import { PUBLIC_FILTERS_DEFAULT_COUNTRY, PUBLIC_FILTERS_DEFAULT_SORT, PUBLIC_GRID_AMOUNT } from '$env/static/public' diff --git a/src/routes/shop/+layout.server.ts b/src/routes/shop/+layout.server.ts index b9fde74..9f21cda 100644 --- a/src/routes/shop/+layout.server.ts +++ b/src/routes/shop/+layout.server.ts @@ -1,6 +1,6 @@ import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' -import { fetchAPI } from '$utils/api' +import { fetchAPI } from '$utils/api/data' import { getProducts } from '$utils/functions/swell' export const load: PageServerLoad = async () => { diff --git a/src/routes/shop/+page.server.ts b/src/routes/shop/+page.server.ts index e604244..7e8056e 100644 --- a/src/routes/shop/+page.server.ts +++ b/src/routes/shop/+page.server.ts @@ -1,6 +1,6 @@ import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' -import { fetchAPI } from '$utils/api' +import { fetchAPI } from '$utils/api/data' import { getProduct } from '$utils/functions/swell' import { getRandomItem } from '$utils/functions' diff --git a/src/routes/shop/[type]-[name]/+page.server.ts b/src/routes/shop/[type]-[name]/+page.server.ts index 9df98f8..279e96c 100644 --- a/src/routes/shop/[type]-[name]/+page.server.ts +++ b/src/routes/shop/[type]-[name]/+page.server.ts @@ -1,6 +1,6 @@ import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' -import { fetchAPI } from '$utils/api' +import { fetchAPI } from '$utils/api/data' import { getProduct } from '$utils/functions/swell' export const load: PageServerLoad = async ({ params }) => { diff --git a/src/routes/subscribe/+page.server.ts b/src/routes/subscribe/+page.server.ts index 593a178..17a4540 100644 --- a/src/routes/subscribe/+page.server.ts +++ b/src/routes/subscribe/+page.server.ts @@ -1,6 +1,6 @@ import { error } from '@sveltejs/kit' import type { PageServerLoad } from './$types' -import { fetchAPI } from '$utils/api' +import { fetchAPI } from '$utils/api/data' export const load: PageServerLoad = async () => { try { diff --git a/src/utils/api/data.ts b/src/utils/api/data.ts new file mode 100644 index 0000000..4abadf2 --- /dev/null +++ b/src/utils/api/data.ts @@ -0,0 +1,29 @@ +import { API_TOKEN } from '$env/static/private' +import { PUBLIC_API_GRAPHQL_PATH } from '$env/static/public' +import { API_URL } from '.' + + +/** + * Fetch data from Directus API + */ +export const fetchAPI = async (query: string) => { + try { + const res = await fetch(`${API_URL}${PUBLIC_API_GRAPHQL_PATH}`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${API_TOKEN}`, + }, + body: JSON.stringify({ + query + }) + }) + + if (res.ok) { + const data = await res.json() + return data + } + } catch (error) { + throw Error(error) + } +} \ No newline at end of file diff --git a/src/utils/api.ts b/src/utils/api/index.ts similarity index 54% rename from src/utils/api.ts rename to src/utils/api/index.ts index ab2104c..5775912 100644 --- a/src/utils/api.ts +++ b/src/utils/api/index.ts @@ -1,36 +1,10 @@ import { env } from '$env/dynamic/private' -import { PUBLIC_API_URL_DEV, PUBLIC_API_URL_PROD, PUBLIC_API_GRAPHQL_PATH } from '$env/static/public' +import { PUBLIC_API_URL_DEV, PUBLIC_API_URL_PROD } from '$env/static/public' // Define API URL from environment export const API_URL = env.DEV ? PUBLIC_API_URL_DEV : PUBLIC_API_URL_PROD -/** - * Fetch data from Directus API - */ -export const fetchAPI = async (query: string) => { - try { - const res = await fetch(`${API_URL}${PUBLIC_API_GRAPHQL_PATH}`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${import.meta.env.VITE_API_TOKEN}`, - }, - body: JSON.stringify({ - query - }) - }) - - if (res.ok) { - const data = await res.json() - return data - } - } catch (error) { - throw Error(error) - } -} - - /** * Get a Directus asset URL from parameters */