diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts index 61548ce..f364612 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/data' +import { fetchAPI } from '$utils/api' import { PUBLIC_PREVIEW_COUNT } from '$env/static/public' diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index 676ed84..cde7005 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/data' +import { fetchAPI } from '$utils/api' 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 845104f..7a03331 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/data' +import { fetchAPI } from '$utils/api' 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 9618c00..c6a88c3 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/data' +import { fetchAPI } from '$utils/api' export const load: PageServerLoad = async ({ params }) => { try { diff --git a/src/routes/about/+page.server.ts b/src/routes/about/+page.server.ts index 9be3048..e19dda2 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/data' +import { fetchAPI } from '$utils/api' 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 d4168da..4722449 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/data' +import { fetchAPI } from '$utils/api' export const POST: RequestHandler = async ({ request }) => { diff --git a/src/routes/credits/+page.server.ts b/src/routes/credits/+page.server.ts index 0cf2e46..3f5667a 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/data' +import { fetchAPI } from '$utils/api' export const load: PageServerLoad = async () => { try { diff --git a/src/routes/photos/+page.server.ts b/src/routes/photos/+page.server.ts index 5808ab4..3fd3765 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/data' +import { fetchAPI } from '$utils/api' 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 9f21cda..b9fde74 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/data' +import { fetchAPI } from '$utils/api' 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 7e8056e..e604244 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/data' +import { fetchAPI } from '$utils/api' 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 279e96c..9df98f8 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/data' +import { fetchAPI } from '$utils/api' 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 17a4540..593a178 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/data' +import { fetchAPI } from '$utils/api' export const load: PageServerLoad = async () => { try { diff --git a/src/utils/api/index.ts b/src/utils/api.ts similarity index 54% rename from src/utils/api/index.ts rename to src/utils/api.ts index 5775912..ab2104c 100644 --- a/src/utils/api/index.ts +++ b/src/utils/api.ts @@ -1,10 +1,36 @@ import { env } from '$env/dynamic/private' -import { PUBLIC_API_URL_DEV, PUBLIC_API_URL_PROD } from '$env/static/public' +import { PUBLIC_API_URL_DEV, PUBLIC_API_URL_PROD, PUBLIC_API_GRAPHQL_PATH } 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 */ diff --git a/src/utils/api/data.ts b/src/utils/api/data.ts deleted file mode 100644 index 4abadf2..0000000 --- a/src/utils/api/data.ts +++ /dev/null @@ -1,29 +0,0 @@ -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