Switch back to VITE env variable for API token
Can't find a way around… but this works!
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
import type { PageServerLoad } from './$types'
|
import type { PageServerLoad } from './$types'
|
||||||
import { fetchAPI } from '$utils/api/data'
|
import { fetchAPI } from '$utils/api'
|
||||||
import { PUBLIC_PREVIEW_COUNT } from '$env/static/public'
|
import { PUBLIC_PREVIEW_COUNT } from '$env/static/public'
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
import type { PageServerLoad } from './$types'
|
import type { PageServerLoad } from './$types'
|
||||||
import { fetchAPI } from '$utils/api/data'
|
import { fetchAPI } from '$utils/api'
|
||||||
import { getRandomItems } from '$utils/functions'
|
import { getRandomItems } from '$utils/functions'
|
||||||
|
|
||||||
export const load: PageServerLoad = async () => {
|
export const load: PageServerLoad = async () => {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
import type { PageServerLoad } from './$types'
|
import type { PageServerLoad } from './$types'
|
||||||
import { PUBLIC_LIST_AMOUNT } from '$env/static/public'
|
import { PUBLIC_LIST_AMOUNT } from '$env/static/public'
|
||||||
import { fetchAPI } from '$utils/api/data'
|
import { fetchAPI } from '$utils/api'
|
||||||
|
|
||||||
export const photoFields = `
|
export const photoFields = `
|
||||||
title
|
title
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
import type { PageServerLoad } from './$types'
|
import type { PageServerLoad } from './$types'
|
||||||
import { fetchAPI } from '$utils/api/data'
|
import { fetchAPI } from '$utils/api'
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ params }) => {
|
export const load: PageServerLoad = async ({ params }) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
import type { PageServerLoad } from './$types'
|
import type { PageServerLoad } from './$types'
|
||||||
import { fetchAPI } from '$utils/api/data'
|
import { fetchAPI } from '$utils/api'
|
||||||
import { getRandomItems } from '$utils/functions'
|
import { getRandomItems } from '$utils/functions'
|
||||||
|
|
||||||
export const load: PageServerLoad = async () => {
|
export const load: PageServerLoad = async () => {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
import type { RequestHandler } from './$types'
|
import type { RequestHandler } from './$types'
|
||||||
import { fetchAPI } from '$utils/api/data'
|
import { fetchAPI } from '$utils/api'
|
||||||
|
|
||||||
|
|
||||||
export const POST: RequestHandler = async ({ request }) => {
|
export const POST: RequestHandler = async ({ request }) => {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
import type { PageServerLoad } from './$types'
|
import type { PageServerLoad } from './$types'
|
||||||
import { fetchAPI } from '$utils/api/data'
|
import { fetchAPI } from '$utils/api'
|
||||||
|
|
||||||
export const load: PageServerLoad = async () => {
|
export const load: PageServerLoad = async () => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
import type { PageServerLoad } from './$types'
|
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'
|
import { PUBLIC_FILTERS_DEFAULT_COUNTRY, PUBLIC_FILTERS_DEFAULT_SORT, PUBLIC_GRID_AMOUNT } from '$env/static/public'
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
import type { PageServerLoad } from './$types'
|
import type { PageServerLoad } from './$types'
|
||||||
import { fetchAPI } from '$utils/api/data'
|
import { fetchAPI } from '$utils/api'
|
||||||
import { getProducts } from '$utils/functions/swell'
|
import { getProducts } from '$utils/functions/swell'
|
||||||
|
|
||||||
export const load: PageServerLoad = async () => {
|
export const load: PageServerLoad = async () => {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
import type { PageServerLoad } from './$types'
|
import type { PageServerLoad } from './$types'
|
||||||
import { fetchAPI } from '$utils/api/data'
|
import { fetchAPI } from '$utils/api'
|
||||||
import { getProduct } from '$utils/functions/swell'
|
import { getProduct } from '$utils/functions/swell'
|
||||||
import { getRandomItem } from '$utils/functions'
|
import { getRandomItem } from '$utils/functions'
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
import type { PageServerLoad } from './$types'
|
import type { PageServerLoad } from './$types'
|
||||||
import { fetchAPI } from '$utils/api/data'
|
import { fetchAPI } from '$utils/api'
|
||||||
import { getProduct } from '$utils/functions/swell'
|
import { getProduct } from '$utils/functions/swell'
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ params }) => {
|
export const load: PageServerLoad = async ({ params }) => {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
import type { PageServerLoad } from './$types'
|
import type { PageServerLoad } from './$types'
|
||||||
import { fetchAPI } from '$utils/api/data'
|
import { fetchAPI } from '$utils/api'
|
||||||
|
|
||||||
export const load: PageServerLoad = async () => {
|
export const load: PageServerLoad = async () => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,10 +1,36 @@
|
|||||||
import { env } from '$env/dynamic/private'
|
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
|
// Define API URL from environment
|
||||||
export const API_URL = env.DEV ? PUBLIC_API_URL_DEV : PUBLIC_API_URL_PROD
|
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
|
* Get a Directus asset URL from parameters
|
||||||
*/
|
*/
|
||||||
@@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user