Skip to content

Commit

Permalink
If an api token is not passed in the constructor, check the env varia…
Browse files Browse the repository at this point in the history
…ble (#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]>
  • Loading branch information
elena-kolevska and shubham1172 authored Nov 6, 2023
1 parent 0fb83c1 commit 66d4bd0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
16 changes: 14 additions & 2 deletions daprdocs/content/en/js-sdk-docs/js-client/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ npm run start:dapr-grpc

### Environment Variables

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.
##### Dapr Sidecar Endpoints

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 set in the options argument of the constructor, the client will parse them automatically
out of the provided endpoints.

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

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.
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.

##### Dapr API Token

You can use the `DAPR_API_TOKEN` environment variable to set the Dapr API token. When this variable
is set, the `daprApiToken` doesn't have to be set in the options argument of the constructor,
the client will get it automatically.

## General

Expand Down
2 changes: 1 addition & 1 deletion src/utils/Client.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export function getClientOptions(
isKeepAlive: clientOptions?.isKeepAlive,
logger: clientOptions?.logger ?? defaultLoggerOptions,
actor: clientOptions?.actor,
daprApiToken: clientOptions?.daprApiToken,
daprApiToken: clientOptions?.daprApiToken ?? Settings.getDefaultApiToken(),
maxBodySizeMb: clientOptions?.maxBodySizeMb,
};
}
Expand Down
5 changes: 5 additions & 0 deletions src/utils/Settings.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import CommunicationProtocolEnum from "../enum/CommunicationProtocol.enum";

export class Settings {
private static readonly defaultAppId: string = "my-dapr-app";
private static readonly defaultApiToken = undefined;
private static readonly defaultHost: string = "127.0.0.1";
private static readonly defaultHttpAppPort: string = "3000";
private static readonly defaultHttpPort: string = "3500";
Expand Down Expand Up @@ -51,6 +52,10 @@ export class Settings {
return process.env.APP_ID ?? Settings.defaultAppId;
}

static getDefaultApiToken() {
return process.env.DAPR_API_TOKEN || Settings.defaultApiToken;
}

static getDefaultHost(): string {
return Settings.defaultHost;
}
Expand Down
29 changes: 29 additions & 0 deletions test/unit/utils/Client.util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,35 @@ describe("Client.util", () => {
expect(options).toEqual(expectedOptions);
});

it("returns correct Dapr Client Options when token provided in constructor", () => {
const inOptions: Partial<DaprClientOptions> = {
daprApiToken: "token",
};
const options = getClientOptions(inOptions, CommunicationProtocolEnum.HTTP, undefined);
expect(options.daprApiToken).toEqual("token");
});

it("returns correct Dapr Client Options when token provided in env variable", () => {
const oldToken = process.env.DAPR_API_TOKEN;
process.env.DAPR_API_TOKEN = "envtoken";
const options = getClientOptions(undefined, CommunicationProtocolEnum.HTTP, undefined);
expect(options.daprApiToken).toEqual("envtoken");
process.env.DAPR_API_TOKEN = oldToken;
});

it("returns correct Dapr Client Options when token provided both in constructor and in env variable", () => {
process.env.DAPR_API_TOKEN = "envtoken";

const inOptions: Partial<DaprClientOptions> = {
daprApiToken: "token",
};
const options = getClientOptions(inOptions, CommunicationProtocolEnum.HTTP, undefined);

expect(options.daprApiToken).toEqual("token");

delete process.env.DAPR_API_TOKEN;
});

it("returns correct Dapr Client Options when undefined options provided", () => {
const options = getClientOptions(undefined, CommunicationProtocolEnum.GRPC, undefined);
const expectedOptions: Partial<DaprClientOptions> = {
Expand Down

0 comments on commit 66d4bd0

Please sign in to comment.