@viamrobotics/sdk 0.7.0 → 0.7.1-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Viam TypeScript SDK
2
2
 
3
+ Build and connect to robots with TypeScript
4
+
3
5
  [![license](https://img.shields.io/badge/license-Apache_2.0-blue)](https://github.com/viamrobotics/viam-typescript-sdk/blob/main/LICENSE)
4
6
  [![npm](https://img.shields.io/npm/v/@viamrobotics/sdk?color=33cd56&logo=npm)](https://www.npmjs.com/package/@viamrobotics/sdk)
5
7
  [![documentation](https://img.shields.io/static/v1?label=docs&message=ts.viam.dev&color=lightgray)](https://ts.viam.dev)
@@ -1,11 +1,17 @@
1
- import { type DialOptions } from '@viamrobotics/rpc/src/dial';
1
+ import { grpc } from '@improbable-eng/grpc-web';
2
+ import { type Credential, type AccessToken } from './viam-transport';
2
3
  import { DataClient } from './data-client';
4
+ export interface ViamClientOptions {
5
+ serviceHost?: string;
6
+ credential: Credential | AccessToken;
7
+ }
8
+ /** Instantiate a connected gRPC client that interfaces with Viam app. */
9
+ export declare const createViamClient: ({ serviceHost, credential, }: ViamClientOptions) => Promise<ViamClient>;
10
+ /** A gRPC client for method calls to Viam app. */
3
11
  export declare class ViamClient {
4
- private serviceHost;
5
- private dialOpts;
6
12
  private transportFactory;
13
+ private serviceHost;
7
14
  dataClient: DataClient | undefined;
8
- constructor(dialOpts: DialOptions, serviceHost?: string);
9
- private getTransportFactory;
10
- connect(): Promise<void>;
15
+ constructor(transportFactory: grpc.TransportFactory, serviceHost: string);
16
+ connect(): void;
11
17
  }
@@ -1,4 +1,21 @@
1
1
  import { grpc } from '@improbable-eng/grpc-web';
2
+ /** A credential that can be exchanged to obtain an access token */
3
+ export interface Credential {
4
+ authEntity: string;
5
+ type: CredentialType;
6
+ payload: string;
7
+ }
8
+ export type CredentialType = 'robot-location-secret' | 'api-key' | 'robot-secret';
9
+ /** An access token used to access protected resources. */
10
+ export interface AccessToken {
11
+ type: 'access-token';
12
+ payload: string;
13
+ }
14
+ /**
15
+ * Initialize an authenticated transport factory that can access protected
16
+ * resources.
17
+ */
18
+ export declare const createViamTransportFactory: (serviceHost: string, credential: Credential | AccessToken) => Promise<grpc.TransportFactory>;
2
19
  export declare class ViamTransport implements grpc.Transport {
3
20
  private accessToken;
4
21
  protected readonly transport: grpc.Transport;
package/dist/main.d.ts CHANGED
@@ -15,7 +15,8 @@ export { type Robot, type DialConf, type DialDirectConf, type DialWebRTCConf, ty
15
15
  * @group Clients
16
16
  */
17
17
  export { RobotClient as Client } from './robot';
18
- export { ViamClient } from './app/viam-client';
18
+ export { createViamClient, type ViamClient, type ViamClientOptions, } from './app/viam-client';
19
+ export type { Credential, CredentialType, AccessToken, } from './app/viam-transport';
19
20
  /**
20
21
  * Raw Protobuf interfaces for a Data service.
21
22
  *
package/dist/main.es.js CHANGED
@@ -42308,10 +42308,10 @@ class GRPCConnectionManager {
42308
42308
  connecting;
42309
42309
  connectResolve;
42310
42310
  connectReject;
42311
- constructor(e, n) {
42311
+ constructor(e, n, s = 1e4) {
42312
42312
  this.innerTransportFactory = n, this.client = new RobotServiceClient_1(e, {
42313
42313
  transport: this.innerTransportFactory
42314
- });
42314
+ }), this.heartbeatIntervalMs = s;
42315
42315
  }
42316
42316
  heartbeat() {
42317
42317
  let e;
@@ -42731,6 +42731,77 @@ class RobotClient extends EventDispatcher {
42731
42731
  }), t;
42732
42732
  }
42733
42733
  }
42734
+ const isPosInt = (i) => !!(i > 0 && Number.isInteger(i)), isLocalConnection = (i) => i.includes("local"), dialDirect$1 = async (i) => {
42735
+ if (console.debug("dialing via gRPC..."), !isLocalConnection(i.host))
42736
+ throw new Error(
42737
+ `cannot dial "${i.host}" directly, please use a local url instead.`
42738
+ );
42739
+ const e = {
42740
+ noReconnect: i.noReconnect,
42741
+ reconnectMaxWait: i.reconnectMaxWait,
42742
+ reconnectMaxAttempts: i.reconnectMaxAttempts
42743
+ };
42744
+ let n;
42745
+ i.disableSessions && (n = { disabled: !0 });
42746
+ const s = new RobotClient(i.host, void 0, n, e);
42747
+ let l;
42748
+ return i.credential && (l = i.credential), await s.connect({ authEntity: i.authEntity, creds: l }), console.debug("connected via gRPC"), s;
42749
+ }, dialWebRTC = async (i) => {
42750
+ console.debug("dialing via WebRTC...");
42751
+ const e = i.host, { signalingAddress: n } = i, l = { iceServers: i.iceServers ?? [] }, g = {
42752
+ enabled: !0,
42753
+ host: i.host,
42754
+ signalingAddress: n,
42755
+ rtcConfig: l,
42756
+ noReconnect: i.noReconnect,
42757
+ reconnectMaxWait: i.reconnectMaxWait,
42758
+ reconnectMaxAttempts: i.reconnectMaxAttempts
42759
+ };
42760
+ let p;
42761
+ i.disableSessions && (p = { disabled: !0 });
42762
+ const t = new RobotClient(e, g, p);
42763
+ let o;
42764
+ return i.credential && (o = i.credential), await t.connect({
42765
+ authEntity: i.authEntity || e,
42766
+ creds: o,
42767
+ priority: i.priority
42768
+ }), console.debug("connected via WebRTC"), t;
42769
+ }, isDialWebRTCConf = (i) => {
42770
+ const e = i;
42771
+ return typeof e.signalingAddress != "string" ? !1 : !e.iceServers || Array.isArray(e.iceServers);
42772
+ }, createRobotClient = async (i) => {
42773
+ let e;
42774
+ if (i.authEntity)
42775
+ try {
42776
+ i.authEntity = new URL(i.authEntity).host;
42777
+ } catch (n) {
42778
+ if (!(n instanceof TypeError))
42779
+ throw n;
42780
+ }
42781
+ if (i.reconnectMaxAttempts && !isPosInt(i.reconnectMaxAttempts))
42782
+ throw new Error(
42783
+ `Value of max reconnect attempts (${i.reconnectMaxAttempts}) should be a positive integer`
42784
+ );
42785
+ if (i.reconnectMaxWait && !isPosInt(i.reconnectMaxWait))
42786
+ throw new Error(
42787
+ `Value of max reconnect wait (${i.reconnectMaxWait}) should be a positive integer`
42788
+ );
42789
+ if (isDialWebRTCConf(i))
42790
+ try {
42791
+ e = await dialWebRTC(i);
42792
+ } catch {
42793
+ console.debug("failed to connect via WebRTC...");
42794
+ }
42795
+ if (!e)
42796
+ try {
42797
+ e = await dialDirect$1(i);
42798
+ } catch {
42799
+ console.debug("failed to connect via gRPC...");
42800
+ }
42801
+ if (!e)
42802
+ throw new Error("failed to connect to robot");
42803
+ return e;
42804
+ };
42734
42805
  var grpcWebClient_umdExports = {}, grpcWebClient_umd = {
42735
42806
  get exports() {
42736
42807
  return grpcWebClient_umdExports;
@@ -46810,7 +46881,7 @@ SignalingServiceClient.prototype.optionalWebRTCConfig = function i(e, n, s) {
46810
46881
  }
46811
46882
  };
46812
46883
  };
46813
- async function dialDirect$1(i, e) {
46884
+ async function dialDirect(i, e) {
46814
46885
  validateDialOptions(e);
46815
46886
  const n = (s) => grpcWebClient_umdExports.grpc.CrossBrowserHttpTransport({ withCredentials: !1 })(s);
46816
46887
  if (e?.accessToken && !(e?.externalAuthAddress && e?.externalAuthToEntity)) {
@@ -46930,25 +47001,6 @@ function validateDialOptions(i) {
46930
47001
  }
46931
47002
  }
46932
47003
  }
46933
- class ViamTransport {
46934
- accessToken;
46935
- transport;
46936
- constructor(e, n, s) {
46937
- this.transport = e(n), this.accessToken = s;
46938
- }
46939
- start(e) {
46940
- e.set("authorization", `Bearer ${this.accessToken}`), this.transport.start(e);
46941
- }
46942
- sendMessage(e) {
46943
- this.transport.sendMessage(e);
46944
- }
46945
- finishSend() {
46946
- this.transport.finishSend();
46947
- }
46948
- cancel() {
46949
- this.transport.cancel();
46950
- }
46951
- }
46952
47004
  var auth_pb = {};
46953
47005
  (function(i) {
46954
47006
  var e = googleProtobuf, n = e, s = typeof globalThis < "u" && globalThis || typeof window < "u" && window || typeof s < "u" && s || typeof self < "u" && self || function() {
@@ -47297,104 +47349,49 @@ ExternalAuthServiceClient.prototype.authenticateTo = function i(e, n, s) {
47297
47349
  }
47298
47350
  };
47299
47351
  };
47300
- const isPosInt = (i) => !!(i > 0 && Number.isInteger(i)), isLocalConnection = (i) => i.includes("local"), dialDirect = async (i) => {
47301
- if (console.debug("dialing via gRPC..."), !isLocalConnection(i.host))
47302
- throw new Error(
47303
- `cannot dial "${i.host}" directly, please use a local url instead.`
47304
- );
47305
- const e = {
47306
- noReconnect: i.noReconnect,
47307
- reconnectMaxWait: i.reconnectMaxWait,
47308
- reconnectMaxAttempts: i.reconnectMaxAttempts
47309
- };
47310
- let n;
47311
- i.disableSessions && (n = { disabled: !0 });
47312
- const s = new RobotClient(i.host, void 0, n, e);
47313
- let l;
47314
- return i.credential && (l = i.credential), await s.connect({ authEntity: i.authEntity, creds: l }), console.debug("connected via gRPC"), s;
47315
- }, dialWebRTC = async (i) => {
47316
- console.debug("dialing via WebRTC...");
47317
- const e = i.host, { signalingAddress: n } = i, l = { iceServers: i.iceServers ?? [] }, g = {
47318
- enabled: !0,
47319
- host: i.host,
47320
- signalingAddress: n,
47321
- rtcConfig: l,
47322
- noReconnect: i.noReconnect,
47323
- reconnectMaxWait: i.reconnectMaxWait,
47324
- reconnectMaxAttempts: i.reconnectMaxAttempts
47325
- };
47326
- let p;
47327
- i.disableSessions && (p = { disabled: !0 });
47328
- const t = new RobotClient(e, g, p);
47329
- let o;
47330
- return i.credential && (o = i.credential), await t.connect({
47331
- authEntity: i.authEntity || e,
47332
- creds: o,
47333
- priority: i.priority
47334
- }), console.debug("connected via WebRTC"), t;
47335
- }, isDialWebRTCConf = (i) => {
47336
- const e = i;
47337
- return typeof e.signalingAddress != "string" ? !1 : !e.iceServers || Array.isArray(e.iceServers);
47338
- }, createRobotClient = async (i) => {
47339
- let e;
47340
- if (i.authEntity)
47341
- try {
47342
- i.authEntity = new URL(i.authEntity).host;
47343
- } catch (n) {
47344
- if (!(n instanceof TypeError))
47345
- throw n;
47346
- }
47347
- if (i.reconnectMaxAttempts && !isPosInt(i.reconnectMaxAttempts))
47352
+ const createViamTransportFactory = async (i, e) => e.type === "access-token" ? createWithAccessToken(i, e) : createWithCredential(i, e), createWithAccessToken = async (i, e) => {
47353
+ const n = await dialDirect(i);
47354
+ return (s) => new ViamTransport(n, s, e.payload);
47355
+ }, createWithCredential = async (i, e) => {
47356
+ const n = await dialDirect(i), s = new AuthServiceClient_1(i, {
47357
+ transport: n
47358
+ });
47359
+ if (e.type === "robot-secret")
47348
47360
  throw new Error(
47349
- `Value of max reconnect attempts (${i.reconnectMaxAttempts}) should be a positive integer`
47361
+ "credential type cannot be 'robot-secret'. Must be either 'robot-location-secret' or 'api-key'."
47350
47362
  );
47351
- if (i.reconnectMaxWait && !isPosInt(i.reconnectMaxWait))
47363
+ if (!e.authEntity)
47352
47364
  throw new Error(
47353
- `Value of max reconnect wait (${i.reconnectMaxWait}) should be a positive integer`
47354
- );
47355
- if (isDialWebRTCConf(i))
47356
- try {
47357
- e = await dialWebRTC(i);
47358
- } catch {
47359
- console.debug("failed to connect via WebRTC...");
47360
- }
47361
- if (!e)
47362
- try {
47363
- e = await dialDirect(i);
47364
- } catch {
47365
- console.debug("failed to connect via gRPC...");
47366
- }
47367
- if (!e)
47368
- throw new Error("failed to connect to robot");
47369
- return e;
47370
- }, createViamTransportFactory = async (i, e) => {
47371
- if (e.credentials) {
47372
- if (e.credentials.type === "robot-secret")
47373
- throw new Error(
47374
- "credential type cannot be 'robot-secret'. Must be either 'robot-location-secret' or 'api-key'."
47375
- );
47376
- if (!e.authEntity)
47377
- throw new Error(
47378
- "auth entity cannot be null, undefined, or an empty value."
47379
- );
47380
- } else
47381
- throw new Error("credential cannot be none");
47382
- const n = e.authEntity, s = new auth_pb.Credentials();
47383
- s.setType(e.credentials.type), s.setPayload(e.credentials.payload);
47384
- const l = new auth_pb.AuthenticateRequest();
47385
- l.setEntity(n), l.setCredentials(s);
47386
- const g = await dialDirect$1(i), p = new AuthServiceClient_1(i, {
47387
- transport: g
47388
- }), t = await new Promise((o, r) => {
47389
- p.authenticate(l, new grpcWebClient_umdExports$1.grpc.Metadata(), (u, c) => {
47390
- if (u)
47391
- return r(u);
47392
- const d = c?.getAccessToken().toString() ?? "";
47393
- return o(d);
47394
- });
47365
+ "auth entity cannot be null, undefined, or an empty value."
47366
+ );
47367
+ const l = e.authEntity, g = new auth_pb.Credentials();
47368
+ g.setType(e.type), g.setPayload(e.payload);
47369
+ const p = new auth_pb.AuthenticateRequest();
47370
+ p.setEntity(l), p.setCredentials(g);
47371
+ const t = await new Promise((o, r) => {
47372
+ s.authenticate(p, new grpcWebClient_umdExports$1.grpc.Metadata(), (u, c) => u ? r(u) : o(c?.getAccessToken().toString() ?? ""));
47395
47373
  });
47396
- return (o) => new ViamTransport(g, o, t);
47374
+ return (o) => new ViamTransport(n, o, t);
47397
47375
  };
47376
+ class ViamTransport {
47377
+ accessToken;
47378
+ transport;
47379
+ constructor(e, n, s) {
47380
+ this.transport = e(n), this.accessToken = s;
47381
+ }
47382
+ start(e) {
47383
+ e.set("authorization", `Bearer ${this.accessToken}`), this.transport.start(e);
47384
+ }
47385
+ sendMessage(e) {
47386
+ this.transport.sendMessage(e);
47387
+ }
47388
+ finishSend() {
47389
+ this.transport.finishSend();
47390
+ }
47391
+ cancel() {
47392
+ this.transport.cancel();
47393
+ }
47394
+ }
47398
47395
  var data_pb = {};
47399
47396
  (function(i) {
47400
47397
  var e = googleProtobuf, n = e, s = typeof globalThis < "u" && globalThis || typeof window < "u" && window || typeof s < "u" && s || typeof self < "u" && self || function() {
@@ -51920,17 +51917,24 @@ class DataClient {
51920
51917
  return e.tags && (s.setTagsList(e.tags), n.setTagsFilter(s)), n;
51921
51918
  }
51922
51919
  }
51920
+ const createViamClient = async ({
51921
+ serviceHost: i = "https://app.viam.com",
51922
+ credential: e
51923
+ }) => {
51924
+ const n = await createViamTransportFactory(
51925
+ i,
51926
+ e
51927
+ ), s = new ViamClient(n, i);
51928
+ return s.connect(), s;
51929
+ };
51923
51930
  class ViamClient {
51924
- serviceHost;
51925
- dialOpts;
51926
51931
  transportFactory;
51932
+ serviceHost;
51927
51933
  dataClient;
51928
51934
  constructor(e, n) {
51929
- this.serviceHost = n ?? "https://app.viam.com:443", this.dialOpts = e;
51935
+ this.transportFactory = e, this.serviceHost = n;
51930
51936
  }
51931
- getTransportFactory = async () => createViamTransportFactory(this.serviceHost, this.dialOpts);
51932
- async connect() {
51933
- this.transportFactory = await this.getTransportFactory();
51937
+ connect() {
51934
51938
  const e = { transport: this.transportFactory };
51935
51939
  this.dataClient = new DataClient(this.serviceHost, e);
51936
51940
  }
@@ -55117,7 +55121,7 @@ const concatArrayU8 = (i) => {
55117
55121
  for (const l of i)
55118
55122
  n.set(l, s), s += l.length;
55119
55123
  return n;
55120
- }, version = "0.7.0";
55124
+ }, version = "0.7.1-next.0";
55121
55125
  export {
55122
55126
  ArmClient,
55123
55127
  BaseClient,
@@ -55140,13 +55144,13 @@ export {
55140
55144
  ServoClient,
55141
55145
  SlamClient,
55142
55146
  StreamClient,
55143
- ViamClient,
55144
55147
  arm_pb as armApi,
55145
55148
  base_pb as baseApi,
55146
55149
  board_pb as boardApi,
55147
55150
  camera_pb as cameraApi,
55148
55151
  common_pb as commonApi,
55149
55152
  createRobotClient,
55153
+ createViamClient,
55150
55154
  data_pb as dataApi,
55151
55155
  encoder_pb as encoderApi,
55152
55156
  gantry_pb as gantryApi,