Skip to content

Commit 2377be6

Browse files
committed
use a proxy simulate a local server and test the api, expose client production settings (not sensitive)
1 parent 1642ce9 commit 2377be6

12 files changed

+44
-11
lines changed

.devcontainer/Dockerfile

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ RUN sudo npx playwright install-deps &&\
66

77
# Install additional packages
88
RUN sudo apt-get update && sudo apt-get install -y --no-install-recommends \
9-
trash-cli
9+
trash-cli
10+
11+
# Install global npm packages
12+
RUN npm install --global \
13+
local-cors-proxy

.devcontainer/docker-compose.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,4 @@ services:
1010
environment:
1111
TZ: ${TZ} # Timezone
1212
GITHUB_TOKEN: ${GITHUB_TOKEN} # GitHub CLI
13-
API_URL: 'https://dummyjson.com'
14-
GA_MEAUSREMENT_ID: 'G-XXXXXXXXXX'
15-
ADSENSE_CLIENT_ID: 'ca-pub-XXXXXXXXXX'
13+
API_URL: 'http://localhost:3000/proxy'

.env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VITE_API_URL="${API_URL}"

.env.example

-3
This file was deleted.

.env.production

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Production environment variables. Do not put sensitive information here!
2+
VITE_API_URL="https://dummyjson.com"
3+
VITE_GA_MEASUREMENT_ID="" # G-XXXXXXXXXX
4+
VITE_ADSENSE_CLIENT_ID="" # ca-pub-XXXXXXXXXX

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ node_modules
1212
Thumbs.db
1313

1414
# Env
15-
.env
16-
.env.*
15+
# .env
16+
# .env.*
1717
!.env.example
1818
!.env.test
1919

.vscode/settings.json

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
"*.css": "tailwindcss"
1818
},
1919
"restoreTerminals.terminals": [
20+
{
21+
"splitTerminals": [
22+
{
23+
"name": "mock api",
24+
"commands": [
25+
"echo 'Pretending you have a local server running...'",
26+
"echo 'You can delete this in your .vscode/settings.json.'",
27+
"npx local-cors-proxy --proxyUrl https://dummyjson.com --port 3000"
28+
]
29+
},
30+
]
31+
},
2032
{
2133
"splitTerminals": [
2234
{

src/app.css

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
@import 'tailwindcss/utilities';
44

55
@layer base {
6+
/* Toogle light/dark mode on your OS to see changes. */
67
body {
78
@apply font-sans antialiased;
89
@apply bg-white text-neutral-900;

src/lib/config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** See https://vite.dev/guide/env-and-mode#modes */
2-
export const IS_PRODUCTION_BUILD = import.meta.env.MODE === 'production';
2+
export const BUILD_MODE = import.meta.env.MODE;
3+
export const IS_PRODUCTION_BUILD = BUILD_MODE === 'production';
34

45
/** Check if running on localhost or a local network. */
56
export const IS_LOCAL_ENVIRONMENT =

src/lib/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { App } from './app';
66
import {
77
ADSENSE_CLIENT_ID,
88
API_URL,
9+
BUILD_MODE,
910
GA_MEASUREMENT_ID,
1011
IS_LOCAL_ENVIRONMENT,
1112
IS_PRODUCTION_BUILD
@@ -20,6 +21,7 @@ export const log = new Log(IS_PRODUCTION_BUILD ? Log.INFO : Log.TRACE);
2021

2122
log.debug(init_start, 'Initializing...');
2223
log.debug('Config:', {
24+
BUILD_MODE,
2325
API_URL,
2426
IS_PRODUCTION_BUILD,
2527
IS_LOCAL_ENVIRONMENT,

src/routes/+layout.svelte

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@
88
});
99
</script>
1010

11-
<slot />
11+
<main>
12+
<slot />
13+
</main>

src/routes/+page.svelte

+11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
<script lang="ts">
22
import { app } from '$lib';
3+
import { API_URL } from '$lib/config';
34
import { onMount } from 'svelte';
45
import SvelteLogo from 'virtual:icons/logos/svelte-icon';
6+
57
let ad: HTMLElement;
8+
let testResult = $state('');
9+
610
onMount(() => {
711
app.advertising.loadAd(ad, 'adunit', 'auto');
12+
testApi();
813
});
14+
15+
async function testApi() {
16+
const res = await fetch(`${API_URL}/test`);
17+
testResult = JSON.stringify(await res.json());
18+
}
919
</script>
1020

1121
<article class="p-2">
1222
<h1>Welcome to SvelteKit <SvelteLogo class="m-2 inline w-8" /></h1>
1323
<p>
1424
Visit <a href="https://svelte.dev/docs/kit">svelte.dev/docs/kit</a> to read the documentation.
1525
</p>
26+
<p>API test: {testResult}</p>
1627
<footer>
1728
<ins bind:this={ad} class="block h-16 w-64"></ins>
1829
</footer>

0 commit comments

Comments
 (0)