-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DEV-1364] Remove config file and create BrowserConfig (#613)
- Loading branch information
Showing
7 changed files
with
95 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"nextjs-website": patch | ||
--- | ||
|
||
[DEV-1364] Remove AppEnv and Config in favor of the BrowserEnv and BrowserConfig |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import * as t from 'io-ts'; | ||
import { pipe } from 'fp-ts/lib/function'; | ||
import * as E from 'fp-ts/lib/Either'; | ||
import * as PR from 'io-ts/lib/PathReporter'; | ||
import * as tt from 'io-ts-types'; | ||
|
||
const BrowserConfigCodec = t.type({ | ||
NEXT_PUBLIC_COGNITO_REGION: t.string, | ||
NEXT_PUBLIC_COGNITO_USER_POOL_ID: t.string, | ||
NEXT_PUBLIC_COGNITO_IDENTITY_POOL_ID: t.string, | ||
NEXT_PUBLIC_WEBINAR_QUESTION_LIFETIME_IN_SECONDS: t.string.pipe( | ||
tt.NumberFromString | ||
), | ||
}); | ||
|
||
export type BrowserConfig = t.TypeOf<typeof BrowserConfigCodec>; | ||
|
||
// TODO: Migrate all the above environment | ||
// publicEnv exists to allow nextjs to correctly replace environments at build | ||
// time, without this copy in some cases some NEXT_PUBLIC environments will be | ||
// undefined | ||
// https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables#bundling-environment-variables-for-the-browser | ||
export const publicEnv = { | ||
NEXT_PUBLIC_COGNITO_REGION: process.env.NEXT_PUBLIC_COGNITO_REGION, | ||
NEXT_PUBLIC_COGNITO_USER_POOL_ID: | ||
process.env.NEXT_PUBLIC_COGNITO_USER_POOL_ID, | ||
NEXT_PUBLIC_COGNITO_IDENTITY_POOL_ID: | ||
process.env.NEXT_PUBLIC_COGNITO_IDENTITY_POOL_ID, | ||
NEXT_PUBLIC_WEBINAR_QUESTION_LIFETIME_IN_SECONDS: | ||
process.env.NEXT_PUBLIC_WEBINAR_QUESTION_LIFETIME_IN_SECONDS, | ||
}; | ||
|
||
export const makeBrowserConfig = ( | ||
env: Record<string, undefined | string> | ||
): E.Either<string, BrowserConfig> => | ||
pipe( | ||
BrowserConfigCodec.decode(env), | ||
E.mapLeft((errors) => PR.failure(errors).join('\n')) | ||
); |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { WebinarEnv } from './lib/webinars/webinarQuestions'; | ||
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; | ||
import { makeAwsCredentialsFromCognito } from './lib/makeAwsCredentialsFromCognito'; | ||
import { Auth } from 'aws-amplify'; | ||
import { BrowserConfig } from '@/BrowserConfig'; | ||
|
||
// This type represents the environment of the browser. | ||
// Contains all dependencies required to run the application on the browser. | ||
export type BrowserEnv = { | ||
readonly config: BrowserConfig; | ||
} & WebinarEnv; | ||
|
||
// given environment variables produce an BrowserEnv | ||
export const makeBrowserEnv = (config: BrowserConfig): BrowserEnv => ({ | ||
config, | ||
questionLifetimeInSeconds: | ||
config.NEXT_PUBLIC_WEBINAR_QUESTION_LIFETIME_IN_SECONDS, | ||
nowDate: () => new Date(), | ||
dynamoDBClient: new DynamoDBClient({ | ||
region: config.NEXT_PUBLIC_COGNITO_REGION, | ||
credentials: makeAwsCredentialsFromCognito( | ||
config, | ||
// passing Auth.currentSession raise an error because | ||
// Auth.currentSession is not able to retrieve all the information | ||
() => Auth.currentSession() | ||
), | ||
}), | ||
}); |
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