Skip to content

Commit

Permalink
feat: support create a new state client (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Oct 15, 2023
1 parent 60d035e commit 327adec
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ChannelCredentials } from '@grpc/grpc-js';
import { RuntimeClient } from '../../proto/runtime/v1/runtime_grpc_pb';
import { ObjectStorageServiceClient } from '../../proto/extension/v1/s3/oss_grpc_pb';
import { CryptionServiceClient } from '../../proto/extension/v1/cryption/cryption_grpc_pb';
import { State } from './State';
import { State, StateOptions } from './State';
import { Hello } from './Hello';
import { Invoker } from './Invoker';
import { Lock } from './Lock';
Expand Down Expand Up @@ -112,7 +112,7 @@ export class Client {

get state() {
if (!this._state) {
this._state = new State(this._runtime, this.initAPIOptions);
this._state = new State(this._runtime, {}, this.initAPIOptions);
}
return this._state;
}
Expand Down Expand Up @@ -184,6 +184,13 @@ export class Client {
return new Oss(ossClient, options, this.initAPIOptions);
}

/**
* Create new state client instance
*/
createStateClient(options?: StateOptions) {
return new State(this._runtime, options, this.initAPIOptions);
}

get cryption() {
if (!this._cryption) {
if (!this._cryptionClient) {
Expand Down
19 changes: 19 additions & 0 deletions src/client/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,27 @@ import {
StateItem,
} from '../types/State';
import { isEmptyPBMessage, convertMapToKVString } from '../utils';
import { RuntimeClient } from '../../proto/runtime/v1/runtime_grpc_pb';
import { APIOptions } from './API';
import { RequestWithMeta } from '../types/common';

export interface StateOptions {
// set default metadata on every request
defaultRequestMeta?: Record<string, string>;
}

export class State extends RuntimeAPI {
#defaultRequestMeta?: Record<string, string>;

constructor(runtime: RuntimeClient, options?: StateOptions, apiOptions?: APIOptions) {
super(runtime, apiOptions);
this.#defaultRequestMeta = options?.defaultRequestMeta;
}

createMetadata(request: RequestWithMeta<{}>) {
return super.createMetadata(request, this.#defaultRequestMeta);
}

// Saves an array of state objects
async save(request: SaveStateRequest): Promise<void> {
let states = request.states;
Expand Down
28 changes: 28 additions & 0 deletions test/unit/client/State.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,32 @@ describe('client/State.test.ts', () => {
// assert.equal(states.length, 20);
});
});

describe('createStateClient()', () => {
it('should create a new state client success', async () => {
const key = 'js-sdk-unit-' + Date.now();
const stateClient = client.createStateClient({
defaultRequestMeta: {
foo: 'bar',
},
});
const value = `hello js-sdk, with 中文, 😄, at ${Date()}`;
await stateClient.save({
storeName,
states: { key, value },
requestMeta: { traceid: `mock-traceid-unittest-${Date.now()}` },
});
const state = await stateClient.get({ storeName, key });
assert(state);
assert.equal(Buffer.from(state.value).toString(), value);

await stateClient.save({
storeName,
states: { key, value },
});
const state2 = await stateClient.get({ storeName, key });
assert(state2);
assert.equal(Buffer.from(state2.value).toString(), value);
});
});
});

0 comments on commit 327adec

Please sign in to comment.