Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert env vars in demo #1239

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions apps/demo/src/H5GroveApp.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { App, H5GroveProvider } from '@h5web/app';
import { App, H5GroveProvider, assertEnvVar } from '@h5web/app';
import { useLocation } from 'react-router-dom';

import { getFeedbackURL } from './utils';

const URL = import.meta.env.VITE_H5GROVE_URL as string;
const FILEPATH = import.meta.env.VITE_H5GROVE_FALLBACK_FILEPATH as string;
const URL = import.meta.env.VITE_H5GROVE_URL;
const FILEPATH = import.meta.env.VITE_H5GROVE_FALLBACK_FILEPATH;

function H5GroveApp() {
assertEnvVar(URL, 'VITE_H5GROVE_URL');
assertEnvVar(FILEPATH, 'VITE_H5GROVE_FALLBACK_FILEPATH');

const query = new URLSearchParams(useLocation().search);
const filepath = query.get('file') || FILEPATH;

Expand Down
18 changes: 12 additions & 6 deletions apps/demo/src/HsdsApp.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { App, HsdsProvider } from '@h5web/app';
import { App, assertEnvVar, HsdsProvider } from '@h5web/app';
import { useLocation } from 'react-router-dom';

import { getFeedbackURL } from './utils';

const URL = import.meta.env.VITE_HSDS_URL as string;
const USERNAME = import.meta.env.VITE_HSDS_USERNAME as string;
const PASSWORD = import.meta.env.VITE_HSDS_PASSWORD as string;
const SUBDOMAIN = import.meta.env.VITE_HSDS_SUBDOMAIN as string;
const FILEPATH = import.meta.env.VITE_HSDS_FALLBACK_FILEPATH as string;
const URL = import.meta.env.VITE_HSDS_URL;
const USERNAME = import.meta.env.VITE_HSDS_USERNAME;
const PASSWORD = import.meta.env.VITE_HSDS_PASSWORD;
const SUBDOMAIN = import.meta.env.VITE_HSDS_SUBDOMAIN;
const FILEPATH = import.meta.env.VITE_HSDS_FALLBACK_FILEPATH;

function HsdsApp() {
assertEnvVar(URL, 'VITE_HSDS_URL');
assertEnvVar(USERNAME, 'VITE_HSDS_USERNAME');
assertEnvVar(PASSWORD, 'VITE_HSDS_PASSWORD');
assertEnvVar(SUBDOMAIN, 'VITE_HSDS_SUBDOMAIN');
assertEnvVar(FILEPATH, 'VITE_HSDS_FALLBACK_FILEPATH');

const query = new URLSearchParams(useLocation().search);
const filepath = `${SUBDOMAIN}${query.get('file') || FILEPATH}`;

Expand Down
3 changes: 3 additions & 0 deletions packages/app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export type { FeedbackContext } from './breadcrumbs/models';
export { useDataContext } from './providers/DataProvider';
export type { DataContextValue } from './providers/DataProvider';

// Undocumented
export { assertEnvVar } from '@h5web/shared';

// Undocumented (for @h5web/h5wasm)
export { default as DataProvider } from './providers/DataProvider';
export { DataProviderApi as ProviderApi } from './providers/api';
Expand Down
10 changes: 10 additions & 0 deletions packages/shared/src/guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ export function assertStr(
}
}

export function assertEnvVar(
val: unknown,
name: string
): asserts val is string {
assertStr(val, `Expected environment variable ${name} to be defined`);
if (val === '') {
throw new Error(`Expected environment variable ${name} to not be empty`);
}
}

function assertComplex(val: unknown): asserts val is H5WebComplex {
if (
!Array.isArray(val) ||
Expand Down