Skip to content
Open
113 changes: 113 additions & 0 deletions docs/develop/environment-configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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)`);
Copy link
Contributor

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.

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
Expand Down Expand Up @@ -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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here - unless we explicitly want to document this and have precedent
That being said - I still think we don't need to demonstrate this twice, I think we typically have one simpler sample than a more complex one with the override?


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>
```
8 changes: 7 additions & 1 deletion docs/develop/typescript/converters-and-encryption.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ const worker = await Worker.create({
[ejson/src/client.ts](https://github.com/temporalio/samples-typescript/blob/main/ejson/src/client.ts)
```ts
const client = new Client({
connection,
dataConverter: { payloadConverterPath: require.resolve('./payload-converter') },
});
```
Expand Down Expand Up @@ -398,13 +399,17 @@ const worker = await Worker.create({
<!--SNIPSTART typescript-protobuf-client -->
[protobufs/src/client.ts](https://github.com/temporalio/samples-typescript/blob/main/protobufs/src/client.ts)
```ts
import { Client } from '@temporalio/client';
import { Client, Connection } from '@temporalio/client';
import { loadClientConnectConfig } from '@temporalio/envconfig';
import { v4 as uuid } from 'uuid';
import { foo, ProtoResult } from '../protos/root';
import { example } from './workflows';

async function run() {
const config = loadClientConnectConfig();
const connection = await Connection.connect(config.connectionOptions);
const client = new Client({
connection,
dataConverter: { payloadConverterPath: require.resolve('./payload-converter') },
});

Expand Down Expand Up @@ -558,6 +563,7 @@ As before, we provide a custom Data Converter to the Client and Worker:
[encryption/src/client.ts](https://github.com/temporalio/samples-typescript/blob/main/encryption/src/client.ts)
```ts
const client = new Client({
connection,
dataConverter: await getDataConverter(),
});

Expand Down
6 changes: 3 additions & 3 deletions docs/develop/typescript/schedules.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ The Temporal Service doesn't guarantee when this removal will happen.
[schedules/src/start-schedule.ts](https://github.com/temporalio/samples-typescript/blob/main/schedules/src/start-schedule.ts)
```ts
async function run() {
const client = new Client({
connection: await Connection.connect(),
});
const config = loadClientConnectConfig();
const connection = await Connection.connect(config.connectionOptions);
const client = new Client({ connection });

// https://typescript.temporal.io/api/classes/client.ScheduleClient#create
const schedule = await client.schedule.create({
Expand Down
Loading