livekit-server-sdk 2.12.0 → 2.13.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/src/TwirpRPC.ts CHANGED
@@ -9,19 +9,32 @@ const defaultPrefix = '/twirp';
9
9
 
10
10
  export const livekitPackage = 'livekit';
11
11
  export interface Rpc {
12
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
- request(service: string, method: string, data: JsonValue, headers?: any): Promise<string>;
12
+ request(
13
+ service: string,
14
+ method: string,
15
+ data: JsonValue,
16
+ headers: any, // eslint-disable-line @typescript-eslint/no-explicit-any
17
+ timeout?: number,
18
+ ): Promise<string>;
14
19
  }
15
20
 
16
21
  export class TwirpError extends Error {
17
22
  status: number;
18
23
  code?: string;
24
+ metadata?: Record<string, string>;
19
25
 
20
- constructor(name: string, message: string, status: number, code?: string) {
26
+ constructor(
27
+ name: string,
28
+ message: string,
29
+ status: number,
30
+ code?: string,
31
+ metadata?: Record<string, string>,
32
+ ) {
21
33
  super(message);
22
34
  this.name = name;
23
35
  this.status = status;
24
36
  this.code = code;
37
+ this.metadata = metadata;
25
38
  }
26
39
  }
27
40
 
@@ -44,24 +57,36 @@ export class TwirpRpc {
44
57
  this.prefix = prefix || defaultPrefix;
45
58
  }
46
59
 
47
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
48
- async request(service: string, method: string, data: any, headers?: any): Promise<any> {
60
+ async request(
61
+ service: string,
62
+ method: string,
63
+ data: any, // eslint-disable-line @typescript-eslint/no-explicit-any
64
+ headers: any, // eslint-disable-line @typescript-eslint/no-explicit-any
65
+ timeout = 60,
66
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
67
+ ): Promise<any> {
49
68
  const path = `${this.prefix}/${this.pkg}.${service}/${method}`;
50
69
  const url = new URL(path, this.host);
51
-
52
- const response = await fetch(url, {
70
+ const init: RequestInit = {
53
71
  method: 'POST',
54
72
  headers: {
55
73
  'Content-Type': 'application/json;charset=UTF-8',
56
74
  ...headers,
57
75
  },
58
76
  body: JSON.stringify(data),
59
- });
77
+ };
78
+
79
+ if (timeout) {
80
+ init.signal = AbortSignal.timeout(timeout * 1000);
81
+ }
82
+
83
+ const response = await fetch(url, init);
60
84
 
61
85
  if (!response.ok) {
62
86
  const isJson = response.headers.get('content-type') === 'application/json';
63
87
  let errorMessage = 'Unknown internal error';
64
88
  let errorCode: string | undefined = undefined;
89
+ let metadata: Record<string, string> | undefined = undefined;
65
90
  try {
66
91
  if (isJson) {
67
92
  const parsedError = (await response.json()) as Record<string, unknown>;
@@ -71,6 +96,9 @@ export class TwirpRpc {
71
96
  if ('code' in parsedError) {
72
97
  errorCode = <string>parsedError.code;
73
98
  }
99
+ if ('meta' in parsedError) {
100
+ metadata = <Record<string, string>>parsedError.meta;
101
+ }
74
102
  } else {
75
103
  errorMessage = await response.text();
76
104
  }
@@ -79,7 +107,7 @@ export class TwirpRpc {
79
107
  console.debug(`Error when trying to parse error message, using defaults`, e);
80
108
  }
81
109
 
82
- throw new TwirpError(response.statusText, errorMessage, response.status, errorCode);
110
+ throw new TwirpError(response.statusText, errorMessage, response.status, errorCode, metadata);
83
111
  }
84
112
  const parsedResp = (await response.json()) as Record<string, unknown>;
85
113