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
Show file tree
Hide file tree
Changes from all 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
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