/* --------------------------------------------------------- * Copyright (C) Microsoft Corporation. All rights reserved. *-------------------------------------------------------- */ // AUTOGENERATED CODE, DO NOT EDIT import type IDriver from '../driver/i-driver' import type * as IRPC from '../base/iapi' import { resolveCallOptions } from '../base/util' import type { CallOptions } from '../base/options' export * from '../base/iapi' export interface IResponseStream { on: ((event: 'data', fn: (item: T) => void) => this) & ((event: 'end', fn: () => void) => this) & ((event: 'status', fn: (status: any) => void) => this) & ((event: 'error', fn: (err: Error) => void) => this) } export class KVClient implements IRPC.IKVClient { constructor(private readonly client: IDriver) {} /** * Range gets the keys in the range from the key-value store. */ public async range(req: IRPC.IRangeRequest, options?: CallOptions): Promise { return await this.client.exec('KV', 'range', req, options) } /** * Put puts the given key into the key-value store. * A put request increments the revision of the key-value store * and generates one event in the event history. */ public async put(req: IRPC.IPutRequest, options?: CallOptions): Promise { return await this.client.exec('KV', 'put', req, options) } /** * DeleteRange deletes the given range from the key-value store. * A delete request increments the revision of the key-value store * and generates a delete event in the event history for every deleted key. */ public async deleteRange( req: IRPC.IDeleteRangeRequest, options?: CallOptions, ): Promise { return await this.client.exec('KV', 'deleteRange', req, options) } /** * Txn processes multiple requests in a single transaction. * A txn request increments the revision of the key-value store * and generates events with the same revision for every completed request. * It is not allowed to modify the same key several times within one txn. */ public async txn(req: IRPC.ITxnRequest, options?: CallOptions): Promise { return await this.client.exec('KV', 'txn', req, options) } /** * Compact compacts the event history in the etcd key-value store. The key-value * store should be periodically compacted or the event history will continue to grow * indefinitely. */ // public compact( // req: IRPC.ICompactionRequest, // options?: CallOptions, // ): Promise { // return this.client.exec('KV', 'compact', req, options); // } } export class WatchClient implements IRPC.IWatchClient { constructor(private readonly client: IDriver) {} /** * Watch watches for events happening or that have happened. Both input and output * are streams; the input stream is for creating and canceling watchers and the output * stream sends events. One watch RPC can watch on multiple key ranges, streaming events * for several watches at once. The entire event history can be watched starting from the * last compaction revision. */ public async watch(options?: CallOptions): Promise> { return await this.client.withConnection('Watch', ({ resource, client }) => { const resolved = resolveCallOptions(options, this.client.callOptionsFactory, { service: 'Watch', method: 'watch', isStream: true, }) // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access -- ignore const stream = (client as any).watch(resolved) // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access -- ignore stream.on('error', (err: Error) => { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- ignore stream.writable && this.client.markFailed(resource, err) }) return stream }) } } export class LeaseClient implements IRPC.ILeaseClient { constructor(private readonly client: IDriver) {} /** * LeaseGrant creates a lease which expires if the server does not receive a keepAlive * within a given time to live period. All keys attached to the lease will be expired and * deleted if the lease expires. Each expired key generates a delete event in the event history. */ public async leaseGrant( req: IRPC.ILeaseGrantRequest, options?: CallOptions, ): Promise { return await this.client.exec('Lease', 'leaseGrant', req, options) } /** * LeaseRevoke revokes a lease. All keys attached to the lease will expire and be deleted. */ public async leaseRevoke( req: IRPC.ILeaseRevokeRequest, options?: CallOptions, ): Promise { return await this.client.exec('Lease', 'leaseRevoke', req, options) } /** * LeaseKeepAlive keeps the lease alive by streaming keep alive requests from the client * to the server and streaming keep alive responses from the server to the client. */ public async leaseKeepAlive( options?: CallOptions, ): Promise> { return await this.client.withConnection('Lease', ({ resource, client }) => { const resolved = resolveCallOptions(options, this.client.callOptionsFactory, { service: 'Lease', method: 'leaseKeepAlive', isStream: true, }) // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access -- ignore const stream = (client as any).leaseKeepAlive(resolved) // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access -- ignore stream.on('error', (err: Error) => { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- ignore stream.writable && this.client.markFailed(resource, err) }) return stream }) } /** * LeaseTimeToLive retrieves lease information. */ public async leaseTimeToLive( req: IRPC.ILeaseTimeToLiveRequest, options?: CallOptions, ): Promise { return await this.client.exec('Lease', 'leaseTimeToLive', req, options) } /** * LeaseLeases lists all existing leases. */ public async leaseLeases(options?: CallOptions): Promise { return await this.client.exec('Lease', 'leaseLeases', {}, options) } } export const Services = { KV: KVClient, Watch: WatchClient, Lease: LeaseClient, }