Skip to content

Commit 66d4bd0

Browse files
If an api token is not passed in the constructor, check the env variable (#548)
* If an api token is not passed in the constructor, check the env variable Signed-off-by: Elena Kolevska <[email protected]> * Adds tests Signed-off-by: Elena Kolevska <[email protected]> * Updates docs Signed-off-by: Elena Kolevska <[email protected]> * Apply suggestions from code review Co-authored-by: Shubham Sharma <[email protected]> Signed-off-by: Elena Kolevska <[email protected]> * Fixes linter Signed-off-by: Elena Kolevska <[email protected]> * Moves default api token to Settings Signed-off-by: Elena Kolevska <[email protected]> --------- Signed-off-by: Elena Kolevska <[email protected]> Signed-off-by: Elena Kolevska <[email protected]> Co-authored-by: Shubham Sharma <[email protected]>
1 parent 0fb83c1 commit 66d4bd0

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

daprdocs/content/en/js-sdk-docs/js-client/_index.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ npm run start:dapr-grpc
7979

8080
### Environment Variables
8181

82-
You can use the `DAPR_HTTP_ENDPOINT` and `DAPR_GRPC_ENDPOINT` environment variables to set the Dapr Sidecar's HTTP and gRPC endpoints respectively. When these variables are set, the `daprHost` and `daprPort` don't have to be passed to the constructor, the client will parse them automatically out of the provided endpoints.
82+
##### Dapr Sidecar Endpoints
83+
84+
You can use the `DAPR_HTTP_ENDPOINT` and `DAPR_GRPC_ENDPOINT` environment variables to set the Dapr
85+
Sidecar's HTTP and gRPC endpoints respectively. When these variables are set, the `daprHost`
86+
and `daprPort` don't have to be set in the options argument of the constructor, the client will parse them automatically
87+
out of the provided endpoints.
8388

8489
```typescript
8590
import { DaprClient, CommunicationProtocol } from "@dapr/dapr";
@@ -91,7 +96,14 @@ const client = new DaprClient();
9196
const client = new DaprClient({ communicationProtocol: CommunicationProtocol.GRPC });
9297
```
9398

94-
If the environment variables are set, but `daprHost` and `daprPort` values are passed to the constructor, the latter will take precedence over the environment variables.
99+
If the environment variables are set, but `daprHost` and `daprPort` values are passed to the
100+
constructor, the latter will take precedence over the environment variables.
101+
102+
##### Dapr API Token
103+
104+
You can use the `DAPR_API_TOKEN` environment variable to set the Dapr API token. When this variable
105+
is set, the `daprApiToken` doesn't have to be set in the options argument of the constructor,
106+
the client will get it automatically.
95107

96108
## General
97109

src/utils/Client.util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export function getClientOptions(
292292
isKeepAlive: clientOptions?.isKeepAlive,
293293
logger: clientOptions?.logger ?? defaultLoggerOptions,
294294
actor: clientOptions?.actor,
295-
daprApiToken: clientOptions?.daprApiToken,
295+
daprApiToken: clientOptions?.daprApiToken ?? Settings.getDefaultApiToken(),
296296
maxBodySizeMb: clientOptions?.maxBodySizeMb,
297297
};
298298
}

src/utils/Settings.util.ts

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import CommunicationProtocolEnum from "../enum/CommunicationProtocol.enum";
1515

1616
export class Settings {
1717
private static readonly defaultAppId: string = "my-dapr-app";
18+
private static readonly defaultApiToken = undefined;
1819
private static readonly defaultHost: string = "127.0.0.1";
1920
private static readonly defaultHttpAppPort: string = "3000";
2021
private static readonly defaultHttpPort: string = "3500";
@@ -51,6 +52,10 @@ export class Settings {
5152
return process.env.APP_ID ?? Settings.defaultAppId;
5253
}
5354

55+
static getDefaultApiToken() {
56+
return process.env.DAPR_API_TOKEN || Settings.defaultApiToken;
57+
}
58+
5459
static getDefaultHost(): string {
5560
return Settings.defaultHost;
5661
}

test/unit/utils/Client.util.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,35 @@ describe("Client.util", () => {
359359
expect(options).toEqual(expectedOptions);
360360
});
361361

362+
it("returns correct Dapr Client Options when token provided in constructor", () => {
363+
const inOptions: Partial<DaprClientOptions> = {
364+
daprApiToken: "token",
365+
};
366+
const options = getClientOptions(inOptions, CommunicationProtocolEnum.HTTP, undefined);
367+
expect(options.daprApiToken).toEqual("token");
368+
});
369+
370+
it("returns correct Dapr Client Options when token provided in env variable", () => {
371+
const oldToken = process.env.DAPR_API_TOKEN;
372+
process.env.DAPR_API_TOKEN = "envtoken";
373+
const options = getClientOptions(undefined, CommunicationProtocolEnum.HTTP, undefined);
374+
expect(options.daprApiToken).toEqual("envtoken");
375+
process.env.DAPR_API_TOKEN = oldToken;
376+
});
377+
378+
it("returns correct Dapr Client Options when token provided both in constructor and in env variable", () => {
379+
process.env.DAPR_API_TOKEN = "envtoken";
380+
381+
const inOptions: Partial<DaprClientOptions> = {
382+
daprApiToken: "token",
383+
};
384+
const options = getClientOptions(inOptions, CommunicationProtocolEnum.HTTP, undefined);
385+
386+
expect(options.daprApiToken).toEqual("token");
387+
388+
delete process.env.DAPR_API_TOKEN;
389+
});
390+
362391
it("returns correct Dapr Client Options when undefined options provided", () => {
363392
const options = getClientOptions(undefined, CommunicationProtocolEnum.GRPC, undefined);
364393
const expectedOptions: Partial<DaprClientOptions> = {

0 commit comments

Comments
 (0)