Skip to content

Commit 16f826f

Browse files
authored
add standalone env config sample (#439)
* add standalone env config sample * update lockfile and list of samples * Add to readme * comment - not print
1 parent 2967c79 commit 16f826f

File tree

16 files changed

+350
-0
lines changed

16 files changed

+350
-0
lines changed

.scripts/list-of-samples.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"ejson",
1414
"empty",
1515
"encryption",
16+
"env-config",
1617
"expense",
1718
"fetch-esm",
1819
"food-delivery",

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ and you'll be given the list of sample options.
143143
#### Production APIs
144144

145145
- [**Production Build**](./production): Build code in advance for faster Worker startup times.
146+
- [**Environment Configuration**](./env-config): Configure Temporal Client connections using TOML configuration files with support for multiple environment profiles and programmatic overrides.
146147
- [**Debugging**](https://docs.temporal.io/dev-guide/typescript/debugging): The [vscode-debugger](./vscode-debugger) sample shows how to use the Temporal VS Code plugin to debug a running or completed Workflow Execution.
147148
- [**Patching**](https://docs.temporal.io/workflows/#workflow-versioning): Patch in new Workflow code when making updates to Workflows that have executions in progress in production.
148149
- [**Custom Logger**](./custom-logger): Use a [winston](https://github.com/winstonjs/winston) logger to get logs out of all SDK components.

env-config/.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
lib
3+
.eslintrc.js

env-config/.eslintrc.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const { builtinModules } = require('module');
2+
3+
const ALLOWED_NODE_BUILTINS = new Set(['assert']);
4+
5+
module.exports = {
6+
root: true,
7+
parser: '@typescript-eslint/parser',
8+
parserOptions: {
9+
project: './tsconfig.json',
10+
tsconfigRootDir: __dirname,
11+
},
12+
plugins: ['@typescript-eslint', 'deprecation'],
13+
extends: [
14+
'eslint:recommended',
15+
'plugin:@typescript-eslint/eslint-recommended',
16+
'plugin:@typescript-eslint/recommended',
17+
'prettier',
18+
],
19+
rules: {
20+
'@typescript-eslint/no-floating-promises': 'error',
21+
'deprecation/deprecation': 'warn',
22+
'object-shorthand': ['error', 'always'],
23+
'@typescript-eslint/no-unused-vars': [
24+
'warn',
25+
{
26+
argsIgnorePattern: '^_',
27+
varsIgnorePattern: '^_',
28+
},
29+
],
30+
'@typescript-eslint/no-explicit-any': 'off',
31+
},
32+
overrides: [
33+
{
34+
files: ['src/workflows.ts', 'src/workflows-*.ts', 'src/workflows/*.ts'],
35+
rules: {
36+
'no-restricted-imports': [
37+
'error',
38+
...builtinModules.filter((m) => !ALLOWED_NODE_BUILTINS.has(m)).flatMap((m) => [m, `node:${m}`]),
39+
],
40+
},
41+
},
42+
],
43+
};

env-config/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lib
2+
node_modules

env-config/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

env-config/.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
22

env-config/.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib

env-config/.prettierrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
printWidth: 120
2+
singleQuote: true

env-config/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Temporal External Client Configuration Samples
2+
3+
This directory contains TypeScript samples that demonstrate how to use the Temporal SDK's external client configuration feature. This feature allows you to configure a `Client` using a TOML file and/or programmatic overrides, decoupling connection settings from your application code.
4+
5+
## Prerequisites
6+
7+
To run, first see [README.md](../README.md) for prerequisites.
8+
9+
## Configuration File
10+
11+
The [`config.toml`](./config.toml) file defines three profiles for different environments:
12+
13+
- `[profile.default]`: A working configuration for local development.
14+
- `[profile.staging]`: A configuration with an intentionally **incorrect** address (`localhost:9999`) to demonstrate how it can be corrected by an override.
15+
- `[profile.prod]`: A non-runnable, illustrative-only configuration showing a realistic setup for Temporal Cloud with placeholder credentials. This profile is not used by the samples but serves as a reference.
16+
17+
## Samples
18+
19+
The following TypeScript scripts demonstrate different ways to load and use these configuration profiles.
20+
21+
### [`load-from-file.ts`](./src/load-from-file.ts)
22+
23+
This sample shows the most common use case: loading the `default` profile from the `config.toml` file.
24+
25+
### [`load-profile.ts`](./src/load-profile.ts)
26+
27+
This sample demonstrates loading the `staging` profile by name (which has an incorrect address) and then correcting the address programmatically. This highlights the recommended approach for overriding configuration values at runtime.
28+
29+
## Running this sample
30+
31+
1. `temporal server start-dev` to start [Temporal Server](https://github.com/temporalio/cli/#installation).
32+
2. `npm install` to install dependencies.
33+
3. Run any of the samples:
34+
- `npm run load-default` - Load and connect using the default profile
35+
- `npm run load-profile` - Load staging profile and override the incorrect address
36+
37+
### Expected Output
38+
39+
**Running `npm run load-default`:**
40+
41+
```
42+
--- Loading default profile from config.toml ---
43+
Loaded 'default' profile from /path/to/config.toml.
44+
Address: localhost:7233
45+
Namespace: default
46+
gRPC Metadata: {"my-custom-header":"development-value","trace-id":"dev-trace-123"}
47+
48+
Attempting to connect to client...
49+
✅ Client connected successfully!
50+
```
51+
52+
**Running `npm run load-profile`:**
53+
54+
```
55+
--- Loading 'staging' profile with programmatic overrides ---
56+
The 'staging' profile in config.toml has an incorrect address (localhost:9999).
57+
We'll programmatically override it to the correct address.
58+
59+
Loaded 'staging' profile from /path/to/config.toml with overrides.
60+
Address: localhost:7233 (overridden from localhost:9999)
61+
Namespace: staging
62+
63+
Attempting to connect to client...
64+
✅ Client connected successfully!
65+
```

0 commit comments

Comments
 (0)