Skip to content

Commit

Permalink
Some more cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
mjameswh committed Oct 1, 2024
1 parent b22c079 commit 1347229
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 33 deletions.
49 changes: 16 additions & 33 deletions packages/cloud/src/cloud-operations-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AsyncLocalStorage } from 'node:async_hooks';
import os from 'node:os';
import * as grpc from '@grpc/grpc-js';
import type { RPCImpl } from 'protobufjs';
import {
Expand All @@ -23,13 +22,6 @@ import { CloudService } from './types';
* @experimental
*/
export interface CloudOperationsClientOptions {
/**
* Identity to report to the server
*
* @default `${process.pid}@${os.hostname()}`
*/
identity?: string;

/**
* Connection to use to communicate with the server.
*
Expand All @@ -44,29 +36,18 @@ export interface CloudOperationsClientOptions {
apiVersion?: string;
}

/**
* @experimental
*/
export type LoadedCloudOperationsClientWithDefaults = Required<
Pick<CloudOperationsClientOptions, 'connection' | 'identity'>
> &
CloudOperationsClientOptions;

/**
* High level SDK client.
*
* @experimental
*/
export class CloudOperationsClient {
public readonly connection: CloudOperationsConnection;
public readonly options: LoadedCloudOperationsClientWithDefaults;
public readonly options: CloudOperationsClientOptions;

constructor(options: CloudOperationsClientOptions) {
this.connection = options.connection;
this.options = {
identity: `${process.pid}@${os.hostname()}`,
...filterNullAndUndefined(options ?? {}),
};
this.options = filterNullAndUndefined(options ?? {});
}

/**
Expand Down Expand Up @@ -161,16 +142,16 @@ export interface CloudOperationsConnectionOptions {
interceptors?: grpc.Interceptor[];

/**
* Optional mapping of gRPC metadata (HTTP headers) to send with each request to the server.
* Setting the `Authorization` header is mutually exclusive with the {@link apiKey} option.
* Optional mapping of gRPC metadata (HTTP headers) to send with each request to the server. An
* `Authorization` header set through `metadata` will be ignored and overriden by the value of the
* {@link apiKey} option.
*
* In order to dynamically set metadata, use {@link Connection.withMetadata}
*/
metadata?: Metadata;

/**
* API key for Temporal. This becomes the "Authorization" HTTP header with "Bearer " prepended.
* This is mutually exclusive with the `Authorization` header in {@link ConnectionOptions.metadata}.
*
* You may provide a static string or a callback. Also see {@link Connection.withApiKey} or
* {@link Connection.setApiKey}
Expand All @@ -190,7 +171,7 @@ export interface CloudOperationsConnectionOptions {
}

export type ResolvedCloudOperationsConnectionOptions = Required<
Omit<CloudOperationsConnectionOptions, 'tls' | 'connectTimeout' | 'callCredentials'>
Omit<CloudOperationsConnectionOptions, 'tls' | 'connectTimeout'>
> & {
connectTimeoutMs: number;
credentials: grpc.ChannelCredentials;
Expand Down Expand Up @@ -250,7 +231,7 @@ interface RPCImplOptions {
apiKeyFnRef: { fn?: () => string };
}

export interface CloudOperationsConnectionCtorOptions {
interface CloudOperationsConnectionCtorOptions {
readonly options: ResolvedCloudOperationsConnectionOptions;
readonly client: grpc.Client;
/**
Expand Down Expand Up @@ -278,7 +259,7 @@ export class CloudOperationsConnection {
/**
* @internal
*/
public static readonly Client = grpc.makeGenericClientConstructor({}, 'WorkflowService', {});
public static readonly Client = grpc.makeGenericClientConstructor({}, 'CloudService', {});

public readonly options: ResolvedCloudOperationsConnectionOptions;
protected readonly client: grpc.Client;
Expand All @@ -300,6 +281,7 @@ export class CloudOperationsConnection {
public readonly cloudService: CloudService;

public readonly healthService: HealthService;

readonly callContextStorage: AsyncLocalStorage<CallContext>;
private readonly apiKeyFnRef: { fn?: () => string };

Expand Down Expand Up @@ -332,6 +314,7 @@ export class CloudOperationsConnection {
apiKeyFnRef,
});
const cloudService = CloudService.create(cloudRpcImpl, false, false);

const healthRpcImpl = this.generateRPCImplementation({
serviceName: 'grpc.health.v1.Health',
client,
Expand Down Expand Up @@ -372,7 +355,7 @@ export class CloudOperationsConnection {
}

/**
* Create a lazy `Connection` instance.
* Create a lazy `CloudOperationsConnection` instance.
*
* This method does not verify connectivity with the server. We recommend using {@link connect} instead.
*/
Expand All @@ -381,7 +364,7 @@ export class CloudOperationsConnection {
}

/**
* Establish a connection with the server and return a `Connection` instance.
* Establish a connection with the server and return a `CloudOperationsConnection` instance.
*
* This is the preferred method of creating connections as it verifies connectivity by calling
* {@link ensureConnected}.
Expand Down Expand Up @@ -469,7 +452,7 @@ export class CloudOperationsConnection {
* const ctrl = new AbortController();
* setTimeout(() => ctrl.abort(), 10_000);
* // 👇 throws if incomplete by the timeout.
* await conn.withAbortSignal(ctrl.signal, () => client.workflow.execute(myWorkflow, options));
* await conn.withAbortSignal(ctrl.signal, () => client.cloudService.someOperation(...));
* ```
*/
async withAbortSignal<ReturnType>(abortSignal: AbortSignal, fn: () => Promise<ReturnType>): Promise<ReturnType> {
Expand All @@ -488,8 +471,8 @@ export class CloudOperationsConnection {
* @example
*
* ```ts
* const workflowHandle = await conn.withMetadata({ apiKey: 'secret' }, () =>
* conn.withMetadata({ otherKey: 'set' }, () => client.start(options)))
* const result = await conn.withMetadata({ someMetadata: 'value' }, () =>
* conn.withMetadata({ otherKey: 'set' }, () => client.cloudService.someOperation(...)))
* );
* ```
*/
Expand Down Expand Up @@ -560,7 +543,7 @@ export class CloudOperationsConnection {
});
}

// This method is async for uniformity with NativeConnection which could be used in the future to power clients
// This method is async for uniformity with Connection and NativeConnection
/**
* Close the underlying gRPC client.
*
Expand Down
File renamed without changes.

0 comments on commit 1347229

Please sign in to comment.