-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8839f96
commit 6440572
Showing
8 changed files
with
78 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import { useRouteError } from 'react-router' | ||
import { logger } from 'src/common' | ||
|
||
export const Fallback = () => { | ||
const error = useRouteError() | ||
console.error(error) | ||
logger.error(error) | ||
|
||
return <p>Something went wrong</p> | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,39 @@ | ||
import { string, mixed, object, type InferType } from 'yup' | ||
import { getOrDefault } from './get.util' | ||
import { createEnv } from './create-env' | ||
import type { ObjectSchema, InferType, AnyObject } from 'yup' | ||
import { parse } from './parse.util' | ||
|
||
if (IS_SERVER) require('dotenv/config') | ||
|
||
const envSchema = object({ | ||
CLIENT_HOST: string().default('http://localhost:3000'), | ||
CLIENT_PUBLIC_PATH: string().default('0.0.0'), | ||
APP_VERSION: string().default('0.0.0'), | ||
NODE_ENV: mixed<'production' | 'development' | 'test'>() | ||
.oneOf(['production', 'development', 'test']) | ||
.default('development') | ||
}) | ||
interface Props<T extends AnyObject> { | ||
schema: ObjectSchema<T> | ||
envs: Partial<Record<keyof InferType<ObjectSchema<T>>, unknown>> | ||
clientPrefix?: string | ||
} | ||
export function createEnv<S extends AnyObject>({ | ||
schema, | ||
envs, | ||
clientPrefix = 'CLIENT_' | ||
}: Props<S>) { | ||
const client = schema.pick(Object.keys(schema.shape).filter(k => k.startsWith(clientPrefix))) | ||
const { data, error } = parse((IS_SERVER ? schema : client) as ObjectSchema<S>, envs) | ||
|
||
export type Env = InferType<typeof envSchema> | ||
if (error) { | ||
console.error('❌ Invalid environment variables:', error.errors) | ||
throw new Error('Invalid environment variables') | ||
} | ||
|
||
export const getENV = getOrDefault( | ||
createEnv({ | ||
clientPrefix, | ||
schema: envSchema, | ||
envs: IS_SERVER ? process.env : window.env_vars | ||
}) | ||
) | ||
return new Proxy(data, { | ||
get(target, prop, receiver) { | ||
if (typeof prop !== 'string') return undefined | ||
|
||
export const setEnvVars = (nonce: string) => { | ||
const clientEnv = Object.entries(getENV()) | ||
.filter(([k]) => k.startsWith(clientPrefix)) | ||
.reduce<Collection<string, unknown>>((res, [k, v]) => { | ||
res[k] = v | ||
return res | ||
}, {}) | ||
if ( | ||
!IS_SERVER && | ||
(!clientPrefix || !prop.startsWith(clientPrefix)) && | ||
!['toJSON', 'toString'].includes(prop) | ||
) { | ||
throw new Error( | ||
`❌ Attempted to access a server-side environment variable "${prop}" on the client` | ||
) | ||
} | ||
|
||
return `<script nonce='${nonce}'>window.env_vars = Object.freeze(${JSON.stringify(clientEnv)})</script>` | ||
return Reflect.get(target, prop, receiver) | ||
} | ||
}) as InferType<ObjectSchema<S>> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,37 @@ | ||
export * from './env.util' | ||
import { string, mixed, object, type InferType } from 'yup' | ||
import { getOrDefault } from './get.util' | ||
import { createEnv } from './env.util' | ||
|
||
if (IS_SERVER) require('dotenv/config') | ||
|
||
// you can find implementation for a `zod` library | ||
// in the `feat/pipable-stream` git branch | ||
const envSchema = object({ | ||
CLIENT_HOST: string().default('http://localhost:3000'), | ||
CLIENT_PUBLIC_PATH: string().default('0.0.0'), | ||
APP_VERSION: string().default('0.0.0'), | ||
NODE_ENV: mixed<'production' | 'development' | 'test'>() | ||
.oneOf(['production', 'development', 'test']) | ||
.default('development') | ||
}) | ||
|
||
export type Env = InferType<typeof envSchema> | ||
|
||
export const getENV = getOrDefault( | ||
createEnv({ | ||
clientPrefix, | ||
schema: envSchema, | ||
envs: IS_SERVER ? process.env : window.env_vars | ||
}) | ||
) | ||
|
||
export const setEnvVars = (nonce: string) => { | ||
const clientEnv = Object.entries(getENV()) | ||
.filter(([k]) => k.startsWith(clientPrefix)) | ||
.reduce<Collection<string, unknown>>((res, [k, v]) => { | ||
res[k] = v | ||
return res | ||
}, {}) | ||
|
||
return `<script nonce='${nonce}'>window.env_vars=Object.freeze(${JSON.stringify(clientEnv)})</script>` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters