Skip to content
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

If an api token is not passed in the constructor, check the env variable #548

Merged
merged 6 commits into from
Nov 6, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions daprdocs/content/en/js-sdk-docs/js-client/_index.md
Original file line number Diff line number Diff line change
@@ -79,7 +79,11 @@ 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";
@@ -91,7 +95,13 @@ 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

2 changes: 1 addition & 1 deletion src/utils/Client.util.ts
Original file line number Diff line number Diff line change
@@ -292,7 +292,7 @@ export function getClientOptions(
isKeepAlive: clientOptions?.isKeepAlive,
logger: clientOptions?.logger ?? defaultLoggerOptions,
actor: clientOptions?.actor,
daprApiToken: clientOptions?.daprApiToken,
daprApiToken: clientOptions?.daprApiToken ?? process.env.DAPR_API_TOKEN,
Copy link
Member

Choose a reason for hiding this comment

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

We can move this to Settings.util.ts for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about the same thing, but it seemed like we only use it when there is a default value. Let me try it out and see how it looks.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I noticed that in some cases we even have "" as the default, which essentially is the same as not having one.

maxBodySizeMb: clientOptions?.maxBodySizeMb,
};
}
30 changes: 30 additions & 0 deletions test/unit/utils/Client.util.test.ts
Original file line number Diff line number Diff line change
@@ -359,6 +359,36 @@ 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 anv 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 anv 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> = {