🚧 Migrate env variables to use internal SvelteKit system

Some API fetching function rewriting needed as privates cannot be included into public code
This commit is contained in:
2022-08-16 16:58:57 +02:00
parent 5e5c08ddd1
commit 6e904850aa
12 changed files with 131 additions and 92 deletions

View File

@@ -7,6 +7,7 @@
import { spring } from 'svelte/motion' import { spring } from 'svelte/motion'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { lerp } from '$utils/functions' import { lerp } from '$utils/functions'
import { PUBLIC_PREVIEW_COUNT } from '$env/static/public'
// Components // Components
import Image from '$components/atoms/Image.svelte' import Image from '$components/atoms/Image.svelte'
import Badge from '$components/atoms/Badge.svelte' import Badge from '$components/atoms/Badge.svelte'
@@ -48,7 +49,7 @@
})) }))
// Change photo index from mouse position percentage // Change photo index from mouse position percentage
photoIndex = Math.round(lerp(0, Number(import.meta.env.VITE_PREVIEW_COUNT) - 1, moveProgress)) photoIndex = Math.round(lerp(0, Number(PUBLIC_PREVIEW_COUNT) - 1, moveProgress))
} }
// Leaving mouseover // Leaving mouseover

View File

@@ -1,6 +1,7 @@
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' import { fetchAPI } from '$utils/api'
import { PUBLIC_PREVIEW_COUNT } from '$env/static/public'
export const load: PageServerLoad = async () => { export const load: PageServerLoad = async () => {
@@ -21,7 +22,7 @@ export const load: PageServerLoad = async () => {
date_updated date_updated
photos ( photos (
sort: "-date_created", sort: "-date_created",
limit: ${import.meta.env.VITE_PREVIEW_COUNT} limit: ${PUBLIC_PREVIEW_COUNT}
) { ) {
image { image {
id id
@@ -87,14 +88,14 @@ export const load: PageServerLoad = async () => {
`) `)
if (res) { if (res) {
const { data } = res const { data: { countPhotos, countLocations, countCountries, ...rest }} = res
return { return {
...data, ...rest,
count: { count: {
photos: data.countPhotos[0].count.id, photos: countPhotos[0].count.id,
locations: data.countLocations[0].count.id, locations: countLocations[0].count.id,
countries: data.countCountries[0].count.id, countries: countCountries[0].count.id,
}, },
} }
} }

View File

@@ -9,6 +9,7 @@
import { pageLoading, previousPage } from '$utils/stores' import { pageLoading, previousPage } from '$utils/stores'
import { DURATION } from '$utils/contants' import { DURATION } from '$utils/contants'
import '$utils/polyfills' import '$utils/polyfills'
import { PUBLIC_ANALYTICS_KEY, PUBLIC_ANALYTICS_URL } from '$env/static/public'
// Components // Components
import SVGSprite from '$components/SVGSprite.svelte' import SVGSprite from '$components/SVGSprite.svelte'
import SmoothScroll from '$components/SmoothScroll.svelte' import SmoothScroll from '$components/SmoothScroll.svelte'
@@ -84,7 +85,7 @@
{#if browser} {#if browser}
<Analytics <Analytics
appKey={import.meta.env.VITE_ANALYTICS_KEY} appKey={PUBLIC_ANALYTICS_KEY}
url={import.meta.env.VITE_ANALYTICS_URL} url={PUBLIC_ANALYTICS_URL}
/> />
{/if} {/if}

View File

@@ -1,5 +1,6 @@
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 { fetchAPI } from '$utils/api' import { fetchAPI } from '$utils/api'
export const photoFields = ` export const photoFields = `
@@ -56,7 +57,7 @@ export const load: PageServerLoad = async ({ params }) => {
location: { slug: { _eq: "${slug}" }} location: { slug: { _eq: "${slug}" }}
}, },
sort: "-date_created", sort: "-date_created",
limit: ${import.meta.env.VITE_LIST_AMOUNT}, limit: ${PUBLIC_LIST_AMOUNT},
page: 1, page: 1,
) { ) {
${photoFields} ${photoFields}

View File

@@ -10,9 +10,10 @@
import dayjs from 'dayjs' import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime' import relativeTime from 'dayjs/plugin/relativeTime'
import { quartOut } from '$animations/easings' import { quartOut } from '$animations/easings'
import { fetchAPI, getAssetUrlKey } from '$utils/api' import { getAssetUrlKey } from '$utils/api'
import { DELAY } from '$utils/contants' import { DELAY } from '$utils/contants'
import { photoFields } from './+page.server' import { photoFields } from './+page.server'
import { PUBLIC_LIST_INCREMENT } from '$env/static/public'
// Components // Components
import Metas from '$components/Metas.svelte' import Metas from '$components/Metas.svelte'
import PageTransition from '$components/PageTransition.svelte' import PageTransition from '$components/PageTransition.svelte'
@@ -61,7 +62,7 @@
photos = [...photos, ...newPhotos] photos = [...photos, ...newPhotos]
// Define actions if the number of new photos is the expected ones // Define actions if the number of new photos is the expected ones
if (newPhotos.length === Number(import.meta.env.VITE_LIST_INCREMENT)) { if (newPhotos.length === Number(PUBLIC_LIST_INCREMENT)) {
// Increment the current page // Increment the current page
currentPage++ currentPage++
} }
@@ -71,9 +72,11 @@
} }
} }
// [function] Load photos helper // Load photos helper
const loadPhotos = async (page?: number) => { const loadPhotos = async (page?: number) => {
const res = fetchAPI(` const res = await fetch('/api/data', {
method: 'POST',
body: `
query { query {
photos: photo ( photos: photo (
filter: { filter: {
@@ -81,14 +84,15 @@
status: { _eq: "published" }, status: { _eq: "published" },
}, },
sort: "-date_created", sort: "-date_created",
limit: ${import.meta.env.VITE_LIST_INCREMENT}, limit: ${PUBLIC_LIST_INCREMENT},
page: ${page}, page: ${page},
) { ) {
${photoFields} ${photoFields}
} }
} }
`) `,
const { data: { photos }} = await res })
const { data: { photos }} = await res.json()
if (photos) { if (photos) {
// Return new photos // Return new photos

View File

@@ -12,7 +12,7 @@
import { quartOut } from 'svelte/easing' import { quartOut } from 'svelte/easing'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { stagger, timeline } from 'motion' import { stagger, timeline } from 'motion'
import { fetchAPI, getAssetUrlKey } from '$utils/api' import { getAssetUrlKey } from '$utils/api'
import { previousPage } from '$utils/stores' import { previousPage } from '$utils/stores'
import { DELAY } from '$utils/contants' import { DELAY } from '$utils/contants'
import { throttle } from '$utils/functions' import { throttle } from '$utils/functions'
@@ -151,7 +151,9 @@
// Load new prev or next photos // Load new prev or next photos
const isPrev = direction === directions.PREV const isPrev = direction === directions.PREV
const res = await fetchAPI(` const res = await fetch('/api/data', {
method: 'POST',
body: `
query { query {
photos: photo ( photos: photo (
filter: { filter: {
@@ -173,8 +175,9 @@
city city
} }
} }
`) `,
const { data: { photos: newPhotos }} = res })
const { data: { photos: newPhotos }} = await res.json()
// Not loading anymore // Not loading anymore
isLoading = false isLoading = false

View File

@@ -0,0 +1,23 @@
import { error } from '@sveltejs/kit'
import type { RequestHandler } from './$types'
import { fetchAPI } from '$utils/api'
export const POST: RequestHandler = async ({ request }) => {
try {
const body = await request.text()
if (body) {
const req = await fetchAPI(body)
const res = await req
if (res) {
return new Response(JSON.stringify({
...res
}))
}
}
} catch (err) {
throw error(500, err)
}
}

View File

@@ -1,5 +1,6 @@
import { error } from '@sveltejs/kit' import { error } from '@sveltejs/kit'
import type { RequestHandler } from './$types' import type { RequestHandler } from './$types'
import { NEWSLETTER_API_TOKEN, NEWSLETTER_LIST_ID } from '$env/static/private'
export const POST: RequestHandler = async ({ request }) => { export const POST: RequestHandler = async ({ request }) => {
@@ -7,11 +8,11 @@ export const POST: RequestHandler = async ({ request }) => {
const body = await request.text() const body = await request.text()
if (body) { if (body) {
const req = await fetch(`https://emailoctopus.com/api/1.6/lists/${import.meta.env.VITE_NEWSLETTER_LIST_ID}/contacts`, { const req = await fetch(`https://emailoctopus.com/api/1.6/lists/${NEWSLETTER_LIST_ID}/contacts`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ body: JSON.stringify({
api_key: import.meta.env.VITE_NEWSLETTER_API_TOKEN, api_key: NEWSLETTER_API_TOKEN,
email_address: body, email_address: body,
}) })
}) })

View File

@@ -1,16 +1,14 @@
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' import { fetchAPI } from '$utils/api'
import { PUBLIC_FILTERS_DEFAULT_COUNTRY, PUBLIC_FILTERS_DEFAULT_SORT, PUBLIC_GRID_AMOUNT } from '$env/static/public'
// Default filters values
const defaultCountry = String(import.meta.env.VITE_FILTERS_DEFAULT_COUNTRY)
const defaultSort = String(import.meta.env.VITE_FILTERS_DEFAULT_SORT)
export const load: PageServerLoad = async ({ url }) => { export const load: PageServerLoad = async ({ url }) => {
try { try {
// Query parameters // Query parameters
const queryCountry = url.searchParams.get('country') || defaultCountry const queryCountry = url.searchParams.get('country') || PUBLIC_FILTERS_DEFAULT_COUNTRY
const querySort = url.searchParams.get('sort') || defaultSort const querySort = url.searchParams.get('sort') || PUBLIC_FILTERS_DEFAULT_SORT
// Query // Query
const res = await fetchAPI(` const res = await fetchAPI(`
@@ -21,7 +19,7 @@ export const load: PageServerLoad = async ({ url }) => {
status: { _eq: "published" }, status: { _eq: "published" },
}, },
sort: "${querySort === 'latest' ? '-' : ''}date_created", sort: "${querySort === 'latest' ? '-' : ''}date_created",
limit: ${import.meta.env.VITE_GRID_AMOUNT}, limit: ${PUBLIC_GRID_AMOUNT},
page: 1, page: 1,
) { ) {
id id

View File

@@ -13,9 +13,9 @@
import relativeTime from 'dayjs/plugin/relativeTime' import relativeTime from 'dayjs/plugin/relativeTime'
import { stagger, timeline } from 'motion' import { stagger, timeline } from 'motion'
import { DELAY } from '$utils/contants' import { DELAY } from '$utils/contants'
import { fetchAPI } from '$utils/api'
import { quartOut } from '$animations/easings' import { quartOut } from '$animations/easings'
import { map, lerp, throttle } from '$utils/functions' import { map, lerp, throttle } from '$utils/functions'
import { PUBLIC_FILTERS_DEFAULT_COUNTRY, PUBLIC_FILTERS_DEFAULT_SORT, PUBLIC_GRID_INCREMENT } from '$env/static/public'
// Components // Components
import Metas from '$components/Metas.svelte' import Metas from '$components/Metas.svelte'
import PageTransition from '$components/PageTransition.svelte' import PageTransition from '$components/PageTransition.svelte'
@@ -57,8 +57,8 @@
/** /**
* Filters * Filters
*/ */
const defaultCountry: string = import.meta.env.VITE_FILTERS_DEFAULT_COUNTRY const defaultCountry: string = PUBLIC_FILTERS_DEFAULT_COUNTRY
const defaultSort: string = import.meta.env.VITE_FILTERS_DEFAULT_SORT const defaultSort: string = PUBLIC_FILTERS_DEFAULT_SORT
const urlFiltersParams = new URLSearchParams() const urlFiltersParams = new URLSearchParams()
let filtered: boolean let filtered: boolean
let filterCountry = $page.url.searchParams.get('country') || defaultCountry let filterCountry = $page.url.searchParams.get('country') || defaultCountry
@@ -158,7 +158,9 @@
*/ */
// [function] Load photos helper // [function] Load photos helper
const loadPhotos = async (page: number) => { const loadPhotos = async (page: number) => {
const res = fetchAPI(` const res = await fetch('/api/data', {
method: 'POST',
body: `
query { query {
photos: photo ( photos: photo (
filter: { filter: {
@@ -166,7 +168,7 @@
status: { _eq: "published" }, status: { _eq: "published" },
}, },
sort: "${filterSort === 'latest' ? '-' : ''}date_created", sort: "${filterSort === 'latest' ? '-' : ''}date_created",
limit: ${import.meta.env.VITE_GRID_INCREMENT}, limit: ${PUBLIC_GRID_INCREMENT},
page: ${page}, page: ${page},
) { ) {
id id
@@ -189,8 +191,10 @@
city city
} }
} }
`) `,
const { data: { photos }} = await res })
const { data: { photos }} = await res.json()
if (photos) { if (photos) {
// Return new photos // Return new photos
@@ -209,7 +213,7 @@
photos = [...photos, ...newPhotos] photos = [...photos, ...newPhotos]
// Define actions if the number of new photos is the expected ones // Define actions if the number of new photos is the expected ones
if (newPhotos.length === Number(import.meta.env.VITE_GRID_INCREMENT)) { if (newPhotos.length === Number(PUBLIC_GRID_INCREMENT)) {
// Increment the current page // Increment the current page
currentPage++ currentPage++
} }

View File

@@ -1,7 +1,8 @@
import { env } from '$env/dynamic/private'
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 = import.meta.env.DEV export const API_URL = env.DEV ? PUBLIC_API_URL_DEV : PUBLIC_API_URL_PROD
? `${import.meta.env.VITE_API_URL_DEV}`
: `${import.meta.env.VITE_API_URL_PROD}`
/** /**
@@ -9,7 +10,7 @@ export const API_URL = import.meta.env.DEV
*/ */
export const fetchAPI = async (query: string) => { export const fetchAPI = async (query: string) => {
try { try {
const res = await fetch(`${API_URL}${import.meta.env.VITE_API_GRAPHQL_PATH}`, { const res = await fetch(`${API_URL}${PUBLIC_API_GRAPHQL_PATH}`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -25,7 +26,7 @@ export const fetchAPI = async (query: string) => {
return data return data
} }
} catch (error) { } catch (error) {
console.error(error) throw Error(error)
} }
} }

View File

@@ -1,7 +1,8 @@
import swell from 'swell-node' import swell from 'swell-node'
import { SWELL_API_TOKEN, SWELL_STORE_ID } from '$env/static/private'
// Init Swell // Init Swell
swell.init(import.meta.env.VITE_SWELL_STORE_ID, import.meta.env.VITE_SWELL_API_TOKEN) swell.init(SWELL_STORE_ID, SWELL_API_TOKEN)