Skip to content

Commit

Permalink
refactor: use new env var modues from Svelte Kit (#71)
Browse files Browse the repository at this point in the history
- Update code to use new env var modules provided by Svelte Kit
- Remove obsolete /env endpoint for loading env vars
- Remove obsolete env store
- Add .env file for default public dynamic env vars
- Add PublicEnv interface for declaring the pubic env vars
- Use vite preview for running production build so .env is used for e2e tests
- See: sveltejs/kit#5663
  • Loading branch information
camargo authored Jul 27, 2022
1 parent 1394cb9 commit df056fa
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 78 deletions.
7 changes: 7 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
AUTH_TYPE=none
GATEWAY_CLIENT_URL=http://localhost:9000
GATEWAY_SERVER_URL=http://localhost:9000
HASURA_CLIENT_URL=http://localhost:8080/v1/graphql
HASURA_SERVER_URL=http://localhost:8080/v1/graphql
HASURA_WEB_SOCKET_URL=ws://localhost:8080/v1/graphql
ORIGIN=http://localhost:3000
2 changes: 1 addition & 1 deletion docs/TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ npm run test:e2e:debug
The codegen test script runs the [Playwright test generator](https://playwright.dev/docs/codegen), which automatically generates [locators](https://playwright.dev/docs/locators) as you click elements on the page. It can greatly save test development time. The generator requires an instance of the application already running to select against.

```sh
npm run test:e2e:dev # Starts aerie-ui
npm run preview # Starts production build of aerie-ui
npm run test:e2e:codegen # Starts codegen
```

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
"optimize": "vite optimize",
"prebuild": "npm run version",
"predev": "node ./scripts/check-node.cjs",
"preview": "vite preview",
"preview": "vite preview --port 3000",
"test": "vitest",
"test:e2e": "playwright test",
"test:e2e:codegen": "playwright codegen http://localhost:3000",
"test:e2e:debug": "PWDEBUG=1 playwright test",
"test:e2e:dev": "AUTH_TYPE=none ORIGIN=http://localhost:3000 node build",
"test:unit": "vitest run",
"version": "node ./scripts/version.js"
},
Expand Down
6 changes: 1 addition & 5 deletions playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ const config: PlaywrightTestConfig = {
video: process.env.CI ? 'retain-on-failure' : 'off',
},
webServer: {
command: 'node build',
env: {
AUTH_TYPE: 'none',
ORIGIN: 'http://localhost:3000',
},
command: 'npm run preview',
port: 3000,
},
};
Expand Down
14 changes: 10 additions & 4 deletions src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint @typescript-eslint/no-unused-vars: 0, @typescript-eslint/no-empty-interface: 0 */
/* eslint @typescript-eslint/no-unused-vars: 0 */

/// <reference types="@sveltejs/kit" />

Expand All @@ -7,13 +7,19 @@ declare namespace App {
user: User | null;
}

interface Platform {}
interface PublicEnv {
AUTH_TYPE: string;
GATEWAY_CLIENT_URL: string;
GATEWAY_SERVER_URL: string;
HASURA_CLIENT_URL: string;
HASURA_SERVER_URL: string;
HASURA_WEB_SOCKET_URL: string;
ORIGIN: string;
}

interface Session {
user: User | null;
}

interface Stuff {}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/components/menus/AppMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { goto, prefetch } from '$app/navigation';
import { base } from '$app/paths';
import { session } from '$app/stores';
import { env } from '../../stores/app';
import { env } from '$env/dynamic/public';
import { showAboutModal } from '../../utilities/modal';
import Menu from './Menu.svelte';
import MenuItem from './MenuItem.svelte';
Expand Down Expand Up @@ -54,11 +54,11 @@
<i class="bi bi-calendar3" />
Scheduling
</MenuItem>
<MenuItem on:click={() => window.open($env.GATEWAY_CLIENT_URL, '_newtab')}>
<MenuItem on:click={() => window.open(env.GATEWAY_CLIENT_URL, '_newtab')}>
<i class="bi bi-diagram-3" />
Gateway
</MenuItem>
<MenuItem on:click={() => window.open(`${$env.GATEWAY_CLIENT_URL}/playground`, '_newtab')}>
<MenuItem on:click={() => window.open(`${env.GATEWAY_CLIENT_URL}/playground`, '_newtab')}>
<i class="si si-graphql" />
GraphQL Playground
</MenuItem>
Expand Down
7 changes: 2 additions & 5 deletions src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { env } from '$env/dynamic/public';
import type { GetSession, Handle } from '@sveltejs/kit';
import { parse } from 'cookie';
import { get } from 'svelte/store';
import { env as envStore } from './stores/app';
import effects from './utilities/effects';

export const handle: Handle = async ({ event, resolve }) => {
const { AUTH_TYPE } = get<Env>(envStore);

if (AUTH_TYPE === 'none') {
if (env.AUTH_TYPE === 'none') {
event.locals.user = { id: 'unknown', ssoToken: 'unknown' };
} else {
const cookies = parse(event.request.headers.get('cookie') || '');
Expand Down
7 changes: 1 addition & 6 deletions src/routes/__layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@
import { base } from '$app/paths';
import type { Load } from '@sveltejs/kit';
import '../css/app.css';
import { env as envStore, user as userStore, version as versionStore } from '../stores/app';
import { user as userStore, version as versionStore } from '../stores/app';
import { modalBodyClickListener, modalBodyKeyListener } from '../utilities/modal';
export const load: Load = async ({ fetch, session }) => {
// Set env store.
const envResponse = await fetch(`${base}/env`);
const env = await envResponse.json();
envStore.set(env);
// Set version store.
const versionResponse = await fetch(`${base}/version.json`);
const version = await versionResponse.json();
Expand Down
23 changes: 0 additions & 23 deletions src/routes/env/index.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/stores/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@ import { writable } from 'svelte/store';

/* Data. */

export const defaultEnv: Env = {
AUTH_TYPE: 'cam',
GATEWAY_CLIENT_URL: 'http://localhost:9000',
GATEWAY_SERVER_URL: 'http://localhost:9000',
HASURA_CLIENT_URL: 'http://localhost:8080/v1/graphql',
HASURA_SERVER_URL: 'http://localhost:8080/v1/graphql',
HASURA_WEB_SOCKET_URL: 'ws://localhost:8080/v1/graphql',
};

export const defaultVersion: Version = {
branch: 'unknown',
commit: 'unknown',
Expand All @@ -21,8 +12,6 @@ export const defaultVersion: Version = {

/* Writeable. */

export const env = writable<Env>(defaultEnv);

export const user = writable<User | null>(null);

export const version = writable<Version>(defaultVersion);
7 changes: 3 additions & 4 deletions src/stores/subscribable.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { browser } from '$app/env';
import { env } from '$env/dynamic/public';
import { createClient, type Client, type ClientOptions } from 'graphql-ws';
import { isEqual } from 'lodash-es';
import { get, type Subscriber, type Unsubscriber, type Updater } from 'svelte/store';
import { env as envStore } from '../stores/app';
import type { Subscriber, Unsubscriber, Updater } from 'svelte/store';

/**
* Returns a Svelte store that listens to GraphQL subscriptions via graphql-ws.
Expand Down Expand Up @@ -76,8 +76,7 @@ export function gqlSubscribable<T>(

function subscribe(next: Subscriber<T>): Unsubscriber {
if (browser && !client) {
const { HASURA_WEB_SOCKET_URL: url } = get<Env>(envStore);
const clientOptions: ClientOptions = { url };
const clientOptions: ClientOptions = { url: env.HASURA_WEB_SOCKET_URL };
client = createClient(clientOptions);
}

Expand Down
9 changes: 0 additions & 9 deletions src/types/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
type Env = {
AUTH_TYPE: string;
GATEWAY_CLIENT_URL: string;
GATEWAY_SERVER_URL: string;
HASURA_CLIENT_URL: string;
HASURA_SERVER_URL: string;
HASURA_WEB_SOCKET_URL: string;
};

type HtmlModalElement = HTMLDivElement & { resolve: (value: boolean | PromiseLike<boolean>) => void };

type User = {
Expand Down
9 changes: 4 additions & 5 deletions src/utilities/requests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { browser } from '$app/env';
import { env } from '$env/dynamic/public';
import { get } from 'svelte/store';
import { env as envStore, user as userStore } from '../stores/app';
import { user as userStore } from '../stores/app';

/**
* Function to make HTTP requests to the Aerie Gateway.
Expand All @@ -12,8 +13,7 @@ export async function reqGateway<T = any>(
ssoToken: string | null,
excludeContentType: boolean,
): Promise<T> {
const { GATEWAY_CLIENT_URL, GATEWAY_SERVER_URL } = get<Env>(envStore);
const GATEWAY_URL = browser ? GATEWAY_CLIENT_URL : GATEWAY_SERVER_URL;
const GATEWAY_URL = browser ? env.GATEWAY_CLIENT_URL : env.GATEWAY_SERVER_URL;
const user = get<User | null>(userStore);

const options: RequestInit = {
Expand Down Expand Up @@ -49,8 +49,7 @@ export async function reqGateway<T = any>(
* Function to make HTTP POST requests to the Hasura GraphQL API.
*/
export async function reqHasura<T = any>(query: string, variables: QueryVariables = {}): Promise<Record<string, T>> {
const { HASURA_CLIENT_URL, HASURA_SERVER_URL } = get<Env>(envStore);
const HASURA_URL = browser ? HASURA_CLIENT_URL : HASURA_SERVER_URL;
const HASURA_URL = browser ? env.HASURA_CLIENT_URL : env.HASURA_SERVER_URL;
const user = get<User | null>(userStore);

const options: RequestInit = {
Expand Down
3 changes: 3 additions & 0 deletions svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import preprocess from 'svelte-preprocess';
const config = {
kit: {
adapter: adapterNode(),
env: {
publicPrefix: '',
},
paths: {
base: '',
},
Expand Down

0 comments on commit df056fa

Please sign in to comment.