-
Notifications
You must be signed in to change notification settings - Fork 279
docs: add TS to envconfig #3996
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
Open
lennessyy
wants to merge
13
commits into
main
Choose a base branch
from
ts-envconfig
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+943
−374
Open
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7ce5ca1
docs: add TS to envconfig
lennessyy e0f3c75
Merge branch 'main' into ts-envconfig
lennessyy cf46cbd
docs: add typescript envconfig
lennessyy f400b6a
docs: add typescript to envconfig
lennessyy feed81e
docs: update line highlights
lennessyy f4bc3ef
docs: add additional explanation
lennessyy ee34cd0
Merge branch 'main' into ts-envconfig
lennessyy 0427dd2
docs: add links
lennessyy 710b2d4
Merge branch 'ts-envconfig' of https://github.com/temporalio/document…
lennessyy c37bf17
docs: fix broken link
lennessyy 5fb9199
Merge branch 'main' into ts-envconfig
lennessyy 5e0de77
docs: update samples
lennessyy b3298ee
docs: add TS to public preview comment
lennessyy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -330,6 +330,61 @@ public static class LoadFromFile | |
| ``` | ||
|
|
||
| </SdkTabs.DotNet> | ||
|
|
||
| <SdkTabs.TypeScript> | ||
| To load the `default` profile along with any environment variables in TypeScript, use the `loadClientConnectConfig` helper from `@temporalio/envconfig` package. | ||
|
|
||
| {/* SNIPSTART typescript-env-config-load-profile-with-overrides {"highlightedLines": "15-18,30-31"} */} | ||
| [env-config/src/load-profile.ts](https://github.com/temporalio/samples-typescript/blob/main/env-config/src/load-profile.ts) | ||
|
|
||
| ```ts {15-18,30-31} | ||
| import { Connection, Client } from '@temporalio/client'; | ||
| import { loadClientConnectConfig } from '@temporalio/envconfig'; | ||
| import { resolve } from 'path'; | ||
|
|
||
| async function main() { | ||
| console.log("--- Loading 'staging' profile with programmatic overrides ---"); | ||
|
|
||
| const configFile = resolve(__dirname, '../config.toml'); | ||
| const profileName = 'staging'; | ||
|
|
||
| // The 'staging' profile in config.toml has an incorrect address (localhost:9999) | ||
| // We'll programmatically override it to the correct address | ||
|
|
||
| // Load the 'staging' profile. | ||
| const config = loadClientConnectConfig({ | ||
| profile: profileName, | ||
| configSource: { path: configFile }, | ||
| }); | ||
|
|
||
| // Override the target host to the correct address. | ||
| // This is the recommended way to override configuration values. | ||
| config.connectionOptions.address = 'localhost:7233'; | ||
|
|
||
| console.log(`\nLoaded '${profileName}' profile from ${configFile} with overrides.`); | ||
| console.log(` Address: ${config.connectionOptions.address} (overridden from localhost:9999)`); | ||
| console.log(` Namespace: ${config.namespace}`); | ||
|
|
||
| console.log('\nAttempting to connect to client...'); | ||
| try { | ||
| const connection = await Connection.connect(config.connectionOptions); | ||
| const client = new Client({ connection, namespace: config.namespace }); | ||
| console.log('✅ Client connected successfully!'); | ||
| await connection.close(); | ||
| } catch (err) { | ||
| console.log(`❌ Failed to connect: ${err}`); | ||
| } | ||
| } | ||
|
|
||
| main().catch((err) => { | ||
| console.error(err); | ||
| process.exit(1); | ||
| }); | ||
| ``` | ||
|
|
||
| {/* SNIPEND */} | ||
|
|
||
| </SdkTabs.TypeScript> | ||
| </SdkTabs> | ||
|
|
||
| ## Load configuration from a custom path | ||
|
|
@@ -570,4 +625,62 @@ public static class LoadProfile | |
|
|
||
| </SdkTabs.DotNet> | ||
|
|
||
| <SdkTabs.TypeScript> | ||
|
|
||
| To load a specific profile from a custom path in TypeScript, use the `loadClientConnectConfig` helper from | ||
| `@temporalio/envconfig` package with the `profile` and `configFile` options. | ||
|
|
||
| {/* SNIPSTART typescript-env-config-load-profile-with-overrides {"highlightedLines": "15-18,30-31"} */} | ||
| [env-config/src/load-profile.ts](https://github.com/temporalio/samples-typescript/blob/main/env-config/src/load-profile.ts) | ||
|
|
||
| ```ts {15-18,30-31} | ||
| import { Connection, Client } from '@temporalio/client'; | ||
| import { loadClientConnectConfig } from '@temporalio/envconfig'; | ||
| import { resolve } from 'path'; | ||
|
|
||
| async function main() { | ||
| console.log("--- Loading 'staging' profile with programmatic overrides ---"); | ||
|
|
||
| const configFile = resolve(__dirname, '../config.toml'); | ||
| const profileName = 'staging'; | ||
|
|
||
| // The 'staging' profile in config.toml has an incorrect address (localhost:9999) | ||
| // We'll programmatically override it to the correct address | ||
|
|
||
| // Load the 'staging' profile. | ||
| const config = loadClientConnectConfig({ | ||
| profile: profileName, | ||
| configSource: { path: configFile }, | ||
| }); | ||
|
|
||
| // Override the target host to the correct address. | ||
| // This is the recommended way to override configuration values. | ||
| config.connectionOptions.address = 'localhost:7233'; | ||
|
||
|
|
||
| console.log(`\nLoaded '${profileName}' profile from ${configFile} with overrides.`); | ||
| console.log(` Address: ${config.connectionOptions.address} (overridden from localhost:9999)`); | ||
| console.log(` Namespace: ${config.namespace}`); | ||
|
|
||
| console.log('\nAttempting to connect to client...'); | ||
| try { | ||
| const connection = await Connection.connect(config.connectionOptions); | ||
| const client = new Client({ connection, namespace: config.namespace }); | ||
| console.log('✅ Client connected successfully!'); | ||
| await connection.close(); | ||
| } catch (err) { | ||
| console.log(`❌ Failed to connect: ${err}`); | ||
| } | ||
| } | ||
|
|
||
| main().catch((err) => { | ||
| console.error(err); | ||
| process.exit(1); | ||
| }); | ||
| ``` | ||
|
|
||
| {/* SNIPEND */} | ||
|
|
||
| </SdkTabs.TypeScript> | ||
|
|
||
| </SdkTabs> | ||
| ``` | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to do this override bit, I would follow the simpler sample here, imo.