@0xsequence/guard 1.9.23 → 1.9.25

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/guard.gen.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  /* eslint-disable */
2
- // sequence-guard v0.4.0 2e5d6a4c9b797598078365d7439f330bc7bbf29c
2
+ // sequence-guard v0.4.0 12059311f086716f467356ed38189f565d5f172a
3
3
  // --
4
- // Code generated by webrpc-gen@v0.12.x-dev with typescript@v0.10.0 generator. DO NOT EDIT.
4
+ // Code generated by webrpc-gen@v0.18.6 with typescript generator. DO NOT EDIT.
5
5
  //
6
- // webrpc-gen -schema=guard.ridl -target=typescript@v0.10.0 -client -out=./clients/guard.gen.ts
6
+ // webrpc-gen -schema=guard.ridl -target=typescript -client -out=./clients/guard.gen.ts
7
7
 
8
8
  // WebRPC description and code-gen version
9
9
  export const WebRPCVersion = 'v1'
@@ -12,7 +12,7 @@ export const WebRPCVersion = 'v1'
12
12
  export const WebRPCSchemaVersion = 'v0.4.0'
13
13
 
14
14
  // Schema hash generated from your RIDL schema
15
- export const WebRPCSchemaHash = '2e5d6a4c9b797598078365d7439f330bc7bbf29c'
15
+ export const WebRPCSchemaHash = '12059311f086716f467356ed38189f565d5f172a'
16
16
 
17
17
  //
18
18
  // Types
@@ -68,21 +68,21 @@ export interface RecoveryCode {
68
68
  }
69
69
 
70
70
  export interface Guard {
71
- ping(headers?: object): Promise<PingReturn>
72
- version(headers?: object): Promise<VersionReturn>
73
- runtimeStatus(headers?: object): Promise<RuntimeStatusReturn>
74
- getSignerConfig(args: GetSignerConfigArgs, headers?: object): Promise<GetSignerConfigReturn>
75
- sign(args: SignArgs, headers?: object): Promise<SignReturn>
76
- signWith(args: SignWithArgs, headers?: object): Promise<SignWithReturn>
77
- authMethods(args: AuthMethodsArgs, headers?: object): Promise<AuthMethodsReturn>
78
- setPIN(args: SetPINArgs, headers?: object): Promise<SetPINReturn>
79
- resetPIN(args: ResetPINArgs, headers?: object): Promise<ResetPINReturn>
80
- createTOTP(args: CreateTOTPArgs, headers?: object): Promise<CreateTOTPReturn>
81
- commitTOTP(args: CommitTOTPArgs, headers?: object): Promise<CommitTOTPReturn>
82
- resetTOTP(args: ResetTOTPArgs, headers?: object): Promise<ResetTOTPReturn>
83
- reset2FA(args: Reset2FAArgs, headers?: object): Promise<Reset2FAReturn>
84
- recoveryCodes(args: RecoveryCodesArgs, headers?: object): Promise<RecoveryCodesReturn>
85
- resetRecoveryCodes(args: ResetRecoveryCodesArgs, headers?: object): Promise<ResetRecoveryCodesReturn>
71
+ ping(headers?: object, signal?: AbortSignal): Promise<PingReturn>
72
+ version(headers?: object, signal?: AbortSignal): Promise<VersionReturn>
73
+ runtimeStatus(headers?: object, signal?: AbortSignal): Promise<RuntimeStatusReturn>
74
+ getSignerConfig(args: GetSignerConfigArgs, headers?: object, signal?: AbortSignal): Promise<GetSignerConfigReturn>
75
+ sign(args: SignArgs, headers?: object, signal?: AbortSignal): Promise<SignReturn>
76
+ signWith(args: SignWithArgs, headers?: object, signal?: AbortSignal): Promise<SignWithReturn>
77
+ authMethods(args: AuthMethodsArgs, headers?: object, signal?: AbortSignal): Promise<AuthMethodsReturn>
78
+ setPIN(args: SetPINArgs, headers?: object, signal?: AbortSignal): Promise<SetPINReturn>
79
+ resetPIN(args: ResetPINArgs, headers?: object, signal?: AbortSignal): Promise<ResetPINReturn>
80
+ createTOTP(args: CreateTOTPArgs, headers?: object, signal?: AbortSignal): Promise<CreateTOTPReturn>
81
+ commitTOTP(args: CommitTOTPArgs, headers?: object, signal?: AbortSignal): Promise<CommitTOTPReturn>
82
+ resetTOTP(args: ResetTOTPArgs, headers?: object, signal?: AbortSignal): Promise<ResetTOTPReturn>
83
+ reset2FA(args: Reset2FAArgs, headers?: object, signal?: AbortSignal): Promise<Reset2FAReturn>
84
+ recoveryCodes(args: RecoveryCodesArgs, headers?: object, signal?: AbortSignal): Promise<RecoveryCodesReturn>
85
+ resetRecoveryCodes(args: ResetRecoveryCodesArgs, headers?: object, signal?: AbortSignal): Promise<ResetRecoveryCodesReturn>
86
86
  }
87
87
 
88
88
  export interface PingArgs {}
@@ -206,161 +206,235 @@ export class Guard implements Guard {
206
206
  return this.hostname + this.path + name
207
207
  }
208
208
 
209
- ping = (headers?: object): Promise<PingReturn> => {
210
- return this.fetch(this.url('Ping'), createHTTPRequest({}, headers)).then(res => {
211
- return buildResponse(res).then(_data => {
212
- return {
213
- status: <boolean>_data.status
214
- }
215
- })
216
- })
209
+ ping = (headers?: object, signal?: AbortSignal): Promise<PingReturn> => {
210
+ return this.fetch(this.url('Ping'), createHTTPRequest({}, headers, signal)).then(
211
+ res => {
212
+ return buildResponse(res).then(_data => {
213
+ return {
214
+ status: <boolean>_data.status
215
+ }
216
+ })
217
+ },
218
+ error => {
219
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
220
+ }
221
+ )
217
222
  }
218
223
 
219
- version = (headers?: object): Promise<VersionReturn> => {
220
- return this.fetch(this.url('Version'), createHTTPRequest({}, headers)).then(res => {
221
- return buildResponse(res).then(_data => {
222
- return {
223
- version: <Version>_data.version
224
- }
225
- })
226
- })
224
+ version = (headers?: object, signal?: AbortSignal): Promise<VersionReturn> => {
225
+ return this.fetch(this.url('Version'), createHTTPRequest({}, headers, signal)).then(
226
+ res => {
227
+ return buildResponse(res).then(_data => {
228
+ return {
229
+ version: <Version>_data.version
230
+ }
231
+ })
232
+ },
233
+ error => {
234
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
235
+ }
236
+ )
227
237
  }
228
238
 
229
- runtimeStatus = (headers?: object): Promise<RuntimeStatusReturn> => {
230
- return this.fetch(this.url('RuntimeStatus'), createHTTPRequest({}, headers)).then(res => {
231
- return buildResponse(res).then(_data => {
232
- return {
233
- status: <RuntimeStatus>_data.status
234
- }
235
- })
236
- })
239
+ runtimeStatus = (headers?: object, signal?: AbortSignal): Promise<RuntimeStatusReturn> => {
240
+ return this.fetch(this.url('RuntimeStatus'), createHTTPRequest({}, headers, signal)).then(
241
+ res => {
242
+ return buildResponse(res).then(_data => {
243
+ return {
244
+ status: <RuntimeStatus>_data.status
245
+ }
246
+ })
247
+ },
248
+ error => {
249
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
250
+ }
251
+ )
237
252
  }
238
253
 
239
- getSignerConfig = (args: GetSignerConfigArgs, headers?: object): Promise<GetSignerConfigReturn> => {
240
- return this.fetch(this.url('GetSignerConfig'), createHTTPRequest(args, headers)).then(res => {
241
- return buildResponse(res).then(_data => {
242
- return {
243
- signerConfig: <WalletConfig>_data.signerConfig
244
- }
245
- })
246
- })
254
+ getSignerConfig = (args: GetSignerConfigArgs, headers?: object, signal?: AbortSignal): Promise<GetSignerConfigReturn> => {
255
+ return this.fetch(this.url('GetSignerConfig'), createHTTPRequest(args, headers, signal)).then(
256
+ res => {
257
+ return buildResponse(res).then(_data => {
258
+ return {
259
+ signerConfig: <WalletConfig>_data.signerConfig
260
+ }
261
+ })
262
+ },
263
+ error => {
264
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
265
+ }
266
+ )
247
267
  }
248
268
 
249
- sign = (args: SignArgs, headers?: object): Promise<SignReturn> => {
250
- return this.fetch(this.url('Sign'), createHTTPRequest(args, headers)).then(res => {
251
- return buildResponse(res).then(_data => {
252
- return {
253
- sig: <string>_data.sig
254
- }
255
- })
256
- })
269
+ sign = (args: SignArgs, headers?: object, signal?: AbortSignal): Promise<SignReturn> => {
270
+ return this.fetch(this.url('Sign'), createHTTPRequest(args, headers, signal)).then(
271
+ res => {
272
+ return buildResponse(res).then(_data => {
273
+ return {
274
+ sig: <string>_data.sig
275
+ }
276
+ })
277
+ },
278
+ error => {
279
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
280
+ }
281
+ )
257
282
  }
258
283
 
259
- signWith = (args: SignWithArgs, headers?: object): Promise<SignWithReturn> => {
260
- return this.fetch(this.url('SignWith'), createHTTPRequest(args, headers)).then(res => {
261
- return buildResponse(res).then(_data => {
262
- return {
263
- sig: <string>_data.sig
264
- }
265
- })
266
- })
284
+ signWith = (args: SignWithArgs, headers?: object, signal?: AbortSignal): Promise<SignWithReturn> => {
285
+ return this.fetch(this.url('SignWith'), createHTTPRequest(args, headers, signal)).then(
286
+ res => {
287
+ return buildResponse(res).then(_data => {
288
+ return {
289
+ sig: <string>_data.sig
290
+ }
291
+ })
292
+ },
293
+ error => {
294
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
295
+ }
296
+ )
267
297
  }
268
298
 
269
- authMethods = (args: AuthMethodsArgs, headers?: object): Promise<AuthMethodsReturn> => {
270
- return this.fetch(this.url('AuthMethods'), createHTTPRequest(args, headers)).then(res => {
271
- return buildResponse(res).then(_data => {
272
- return {
273
- methods: <Array<string>>_data.methods,
274
- active: <boolean>_data.active
275
- }
276
- })
277
- })
299
+ authMethods = (args: AuthMethodsArgs, headers?: object, signal?: AbortSignal): Promise<AuthMethodsReturn> => {
300
+ return this.fetch(this.url('AuthMethods'), createHTTPRequest(args, headers, signal)).then(
301
+ res => {
302
+ return buildResponse(res).then(_data => {
303
+ return {
304
+ methods: <Array<string>>_data.methods,
305
+ active: <boolean>_data.active
306
+ }
307
+ })
308
+ },
309
+ error => {
310
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
311
+ }
312
+ )
278
313
  }
279
314
 
280
- setPIN = (args: SetPINArgs, headers?: object): Promise<SetPINReturn> => {
281
- return this.fetch(this.url('SetPIN'), createHTTPRequest(args, headers)).then(res => {
282
- return buildResponse(res).then(_data => {
283
- return {}
284
- })
285
- })
315
+ setPIN = (args: SetPINArgs, headers?: object, signal?: AbortSignal): Promise<SetPINReturn> => {
316
+ return this.fetch(this.url('SetPIN'), createHTTPRequest(args, headers, signal)).then(
317
+ res => {
318
+ return buildResponse(res).then(_data => {
319
+ return {}
320
+ })
321
+ },
322
+ error => {
323
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
324
+ }
325
+ )
286
326
  }
287
327
 
288
- resetPIN = (args: ResetPINArgs, headers?: object): Promise<ResetPINReturn> => {
289
- return this.fetch(this.url('ResetPIN'), createHTTPRequest(args, headers)).then(res => {
290
- return buildResponse(res).then(_data => {
291
- return {}
292
- })
293
- })
328
+ resetPIN = (args: ResetPINArgs, headers?: object, signal?: AbortSignal): Promise<ResetPINReturn> => {
329
+ return this.fetch(this.url('ResetPIN'), createHTTPRequest(args, headers, signal)).then(
330
+ res => {
331
+ return buildResponse(res).then(_data => {
332
+ return {}
333
+ })
334
+ },
335
+ error => {
336
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
337
+ }
338
+ )
294
339
  }
295
340
 
296
- createTOTP = (args: CreateTOTPArgs, headers?: object): Promise<CreateTOTPReturn> => {
297
- return this.fetch(this.url('CreateTOTP'), createHTTPRequest(args, headers)).then(res => {
298
- return buildResponse(res).then(_data => {
299
- return {
300
- uri: <string>_data.uri
301
- }
302
- })
303
- })
341
+ createTOTP = (args: CreateTOTPArgs, headers?: object, signal?: AbortSignal): Promise<CreateTOTPReturn> => {
342
+ return this.fetch(this.url('CreateTOTP'), createHTTPRequest(args, headers, signal)).then(
343
+ res => {
344
+ return buildResponse(res).then(_data => {
345
+ return {
346
+ uri: <string>_data.uri
347
+ }
348
+ })
349
+ },
350
+ error => {
351
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
352
+ }
353
+ )
304
354
  }
305
355
 
306
- commitTOTP = (args: CommitTOTPArgs, headers?: object): Promise<CommitTOTPReturn> => {
307
- return this.fetch(this.url('CommitTOTP'), createHTTPRequest(args, headers)).then(res => {
308
- return buildResponse(res).then(_data => {
309
- return {
310
- codes: <Array<RecoveryCode>>_data.codes
311
- }
312
- })
313
- })
356
+ commitTOTP = (args: CommitTOTPArgs, headers?: object, signal?: AbortSignal): Promise<CommitTOTPReturn> => {
357
+ return this.fetch(this.url('CommitTOTP'), createHTTPRequest(args, headers, signal)).then(
358
+ res => {
359
+ return buildResponse(res).then(_data => {
360
+ return {
361
+ codes: <Array<RecoveryCode>>_data.codes
362
+ }
363
+ })
364
+ },
365
+ error => {
366
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
367
+ }
368
+ )
314
369
  }
315
370
 
316
- resetTOTP = (args: ResetTOTPArgs, headers?: object): Promise<ResetTOTPReturn> => {
317
- return this.fetch(this.url('ResetTOTP'), createHTTPRequest(args, headers)).then(res => {
318
- return buildResponse(res).then(_data => {
319
- return {}
320
- })
321
- })
371
+ resetTOTP = (args: ResetTOTPArgs, headers?: object, signal?: AbortSignal): Promise<ResetTOTPReturn> => {
372
+ return this.fetch(this.url('ResetTOTP'), createHTTPRequest(args, headers, signal)).then(
373
+ res => {
374
+ return buildResponse(res).then(_data => {
375
+ return {}
376
+ })
377
+ },
378
+ error => {
379
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
380
+ }
381
+ )
322
382
  }
323
383
 
324
- reset2FA = (args: Reset2FAArgs, headers?: object): Promise<Reset2FAReturn> => {
325
- return this.fetch(this.url('Reset2FA'), createHTTPRequest(args, headers)).then(res => {
326
- return buildResponse(res).then(_data => {
327
- return {}
328
- })
329
- })
384
+ reset2FA = (args: Reset2FAArgs, headers?: object, signal?: AbortSignal): Promise<Reset2FAReturn> => {
385
+ return this.fetch(this.url('Reset2FA'), createHTTPRequest(args, headers, signal)).then(
386
+ res => {
387
+ return buildResponse(res).then(_data => {
388
+ return {}
389
+ })
390
+ },
391
+ error => {
392
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
393
+ }
394
+ )
330
395
  }
331
396
 
332
- recoveryCodes = (args: RecoveryCodesArgs, headers?: object): Promise<RecoveryCodesReturn> => {
333
- return this.fetch(this.url('RecoveryCodes'), createHTTPRequest(args, headers)).then(res => {
334
- return buildResponse(res).then(_data => {
335
- return {
336
- codes: <Array<RecoveryCode>>_data.codes
337
- }
338
- })
339
- })
397
+ recoveryCodes = (args: RecoveryCodesArgs, headers?: object, signal?: AbortSignal): Promise<RecoveryCodesReturn> => {
398
+ return this.fetch(this.url('RecoveryCodes'), createHTTPRequest(args, headers, signal)).then(
399
+ res => {
400
+ return buildResponse(res).then(_data => {
401
+ return {
402
+ codes: <Array<RecoveryCode>>_data.codes
403
+ }
404
+ })
405
+ },
406
+ error => {
407
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
408
+ }
409
+ )
340
410
  }
341
411
 
342
- resetRecoveryCodes = (args: ResetRecoveryCodesArgs, headers?: object): Promise<ResetRecoveryCodesReturn> => {
343
- return this.fetch(this.url('ResetRecoveryCodes'), createHTTPRequest(args, headers)).then(res => {
344
- return buildResponse(res).then(_data => {
345
- return {
346
- codes: <Array<RecoveryCode>>_data.codes
347
- }
348
- })
349
- })
412
+ resetRecoveryCodes = (
413
+ args: ResetRecoveryCodesArgs,
414
+ headers?: object,
415
+ signal?: AbortSignal
416
+ ): Promise<ResetRecoveryCodesReturn> => {
417
+ return this.fetch(this.url('ResetRecoveryCodes'), createHTTPRequest(args, headers, signal)).then(
418
+ res => {
419
+ return buildResponse(res).then(_data => {
420
+ return {
421
+ codes: <Array<RecoveryCode>>_data.codes
422
+ }
423
+ })
424
+ },
425
+ error => {
426
+ throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
427
+ }
428
+ )
350
429
  }
351
430
  }
352
431
 
353
- export interface WebRPCError extends Error {
354
- code: string
355
- msg: string
356
- status: number
357
- }
358
-
359
- const createHTTPRequest = (body: object = {}, headers: object = {}): object => {
432
+ const createHTTPRequest = (body: object = {}, headers: object = {}, signal: AbortSignal | null = null): object => {
360
433
  return {
361
434
  method: 'POST',
362
435
  headers: { ...headers, 'Content-Type': 'application/json' },
363
- body: JSON.stringify(body || {})
436
+ body: JSON.stringify(body || {}),
437
+ signal
364
438
  }
365
439
  }
366
440
 
@@ -369,14 +443,347 @@ const buildResponse = (res: Response): Promise<any> => {
369
443
  let data
370
444
  try {
371
445
  data = JSON.parse(text)
372
- } catch (err) {
373
- throw { code: 'unknown', msg: `expecting JSON, got: ${text}`, status: res.status } as WebRPCError
446
+ } catch (error) {
447
+ let message = ''
448
+ if (error instanceof Error) {
449
+ message = error.message
450
+ }
451
+ throw WebrpcBadResponseError.new({
452
+ status: res.status,
453
+ cause: `JSON.parse(): ${message}: response text: ${text}`
454
+ })
374
455
  }
375
456
  if (!res.ok) {
376
- throw data // webrpc error response
457
+ const code: number = typeof data.code === 'number' ? data.code : 0
458
+ throw (webrpcErrorByCode[code] || WebrpcError).new(data)
377
459
  }
378
460
  return data
379
461
  })
380
462
  }
381
463
 
464
+ //
465
+ // Errors
466
+ //
467
+
468
+ export class WebrpcError extends Error {
469
+ name: string
470
+ code: number
471
+ message: string
472
+ status: number
473
+ cause?: string
474
+
475
+ /** @deprecated Use message instead of msg. Deprecated in webrpc v0.11.0. */
476
+ msg: string
477
+
478
+ constructor(name: string, code: number, message: string, status: number, cause?: string) {
479
+ super(message)
480
+ this.name = name || 'WebrpcError'
481
+ this.code = typeof code === 'number' ? code : 0
482
+ this.message = message || `endpoint error ${this.code}`
483
+ this.msg = this.message
484
+ this.status = typeof status === 'number' ? status : 0
485
+ this.cause = cause
486
+ Object.setPrototypeOf(this, WebrpcError.prototype)
487
+ }
488
+
489
+ static new(payload: any): WebrpcError {
490
+ return new this(payload.error, payload.code, payload.message || payload.msg, payload.status, payload.cause)
491
+ }
492
+ }
493
+
494
+ // Webrpc errors
495
+
496
+ export class WebrpcEndpointError extends WebrpcError {
497
+ constructor(
498
+ name: string = 'WebrpcEndpoint',
499
+ code: number = 0,
500
+ message: string = 'endpoint error',
501
+ status: number = 0,
502
+ cause?: string
503
+ ) {
504
+ super(name, code, message, status, cause)
505
+ Object.setPrototypeOf(this, WebrpcEndpointError.prototype)
506
+ }
507
+ }
508
+
509
+ export class WebrpcRequestFailedError extends WebrpcError {
510
+ constructor(
511
+ name: string = 'WebrpcRequestFailed',
512
+ code: number = -1,
513
+ message: string = 'request failed',
514
+ status: number = 0,
515
+ cause?: string
516
+ ) {
517
+ super(name, code, message, status, cause)
518
+ Object.setPrototypeOf(this, WebrpcRequestFailedError.prototype)
519
+ }
520
+ }
521
+
522
+ export class WebrpcBadRouteError extends WebrpcError {
523
+ constructor(
524
+ name: string = 'WebrpcBadRoute',
525
+ code: number = -2,
526
+ message: string = 'bad route',
527
+ status: number = 0,
528
+ cause?: string
529
+ ) {
530
+ super(name, code, message, status, cause)
531
+ Object.setPrototypeOf(this, WebrpcBadRouteError.prototype)
532
+ }
533
+ }
534
+
535
+ export class WebrpcBadMethodError extends WebrpcError {
536
+ constructor(
537
+ name: string = 'WebrpcBadMethod',
538
+ code: number = -3,
539
+ message: string = 'bad method',
540
+ status: number = 0,
541
+ cause?: string
542
+ ) {
543
+ super(name, code, message, status, cause)
544
+ Object.setPrototypeOf(this, WebrpcBadMethodError.prototype)
545
+ }
546
+ }
547
+
548
+ export class WebrpcBadRequestError extends WebrpcError {
549
+ constructor(
550
+ name: string = 'WebrpcBadRequest',
551
+ code: number = -4,
552
+ message: string = 'bad request',
553
+ status: number = 0,
554
+ cause?: string
555
+ ) {
556
+ super(name, code, message, status, cause)
557
+ Object.setPrototypeOf(this, WebrpcBadRequestError.prototype)
558
+ }
559
+ }
560
+
561
+ export class WebrpcBadResponseError extends WebrpcError {
562
+ constructor(
563
+ name: string = 'WebrpcBadResponse',
564
+ code: number = -5,
565
+ message: string = 'bad response',
566
+ status: number = 0,
567
+ cause?: string
568
+ ) {
569
+ super(name, code, message, status, cause)
570
+ Object.setPrototypeOf(this, WebrpcBadResponseError.prototype)
571
+ }
572
+ }
573
+
574
+ export class WebrpcServerPanicError extends WebrpcError {
575
+ constructor(
576
+ name: string = 'WebrpcServerPanic',
577
+ code: number = -6,
578
+ message: string = 'server panic',
579
+ status: number = 0,
580
+ cause?: string
581
+ ) {
582
+ super(name, code, message, status, cause)
583
+ Object.setPrototypeOf(this, WebrpcServerPanicError.prototype)
584
+ }
585
+ }
586
+
587
+ export class WebrpcInternalErrorError extends WebrpcError {
588
+ constructor(
589
+ name: string = 'WebrpcInternalError',
590
+ code: number = -7,
591
+ message: string = 'internal error',
592
+ status: number = 0,
593
+ cause?: string
594
+ ) {
595
+ super(name, code, message, status, cause)
596
+ Object.setPrototypeOf(this, WebrpcInternalErrorError.prototype)
597
+ }
598
+ }
599
+
600
+ export class WebrpcClientDisconnectedError extends WebrpcError {
601
+ constructor(
602
+ name: string = 'WebrpcClientDisconnected',
603
+ code: number = -8,
604
+ message: string = 'client disconnected',
605
+ status: number = 0,
606
+ cause?: string
607
+ ) {
608
+ super(name, code, message, status, cause)
609
+ Object.setPrototypeOf(this, WebrpcClientDisconnectedError.prototype)
610
+ }
611
+ }
612
+
613
+ export class WebrpcStreamLostError extends WebrpcError {
614
+ constructor(
615
+ name: string = 'WebrpcStreamLost',
616
+ code: number = -9,
617
+ message: string = 'stream lost',
618
+ status: number = 0,
619
+ cause?: string
620
+ ) {
621
+ super(name, code, message, status, cause)
622
+ Object.setPrototypeOf(this, WebrpcStreamLostError.prototype)
623
+ }
624
+ }
625
+
626
+ export class WebrpcStreamFinishedError extends WebrpcError {
627
+ constructor(
628
+ name: string = 'WebrpcStreamFinished',
629
+ code: number = -10,
630
+ message: string = 'stream finished',
631
+ status: number = 0,
632
+ cause?: string
633
+ ) {
634
+ super(name, code, message, status, cause)
635
+ Object.setPrototypeOf(this, WebrpcStreamFinishedError.prototype)
636
+ }
637
+ }
638
+
639
+ // Schema errors
640
+
641
+ export class UnauthorizedError extends WebrpcError {
642
+ constructor(
643
+ name: string = 'Unauthorized',
644
+ code: number = 1000,
645
+ message: string = 'Unauthorized access',
646
+ status: number = 0,
647
+ cause?: string
648
+ ) {
649
+ super(name, code, message, status, cause)
650
+ Object.setPrototypeOf(this, UnauthorizedError.prototype)
651
+ }
652
+ }
653
+
654
+ export class SessionExpiredError extends WebrpcError {
655
+ constructor(
656
+ name: string = 'SessionExpired',
657
+ code: number = 1002,
658
+ message: string = 'Session expired',
659
+ status: number = 0,
660
+ cause?: string
661
+ ) {
662
+ super(name, code, message, status, cause)
663
+ Object.setPrototypeOf(this, SessionExpiredError.prototype)
664
+ }
665
+ }
666
+
667
+ export class AbortedError extends WebrpcError {
668
+ constructor(
669
+ name: string = 'Aborted',
670
+ code: number = 1005,
671
+ message: string = 'Request aborted',
672
+ status: number = 0,
673
+ cause?: string
674
+ ) {
675
+ super(name, code, message, status, cause)
676
+ Object.setPrototypeOf(this, AbortedError.prototype)
677
+ }
678
+ }
679
+
680
+ export class InvalidArgumentError extends WebrpcError {
681
+ constructor(
682
+ name: string = 'InvalidArgument',
683
+ code: number = 2001,
684
+ message: string = 'Invalid argument',
685
+ status: number = 0,
686
+ cause?: string
687
+ ) {
688
+ super(name, code, message, status, cause)
689
+ Object.setPrototypeOf(this, InvalidArgumentError.prototype)
690
+ }
691
+ }
692
+
693
+ export class UnavailableError extends WebrpcError {
694
+ constructor(
695
+ name: string = 'Unavailable',
696
+ code: number = 2002,
697
+ message: string = 'Unavailable resource',
698
+ status: number = 0,
699
+ cause?: string
700
+ ) {
701
+ super(name, code, message, status, cause)
702
+ Object.setPrototypeOf(this, UnavailableError.prototype)
703
+ }
704
+ }
705
+
706
+ export class QueryFailedError extends WebrpcError {
707
+ constructor(
708
+ name: string = 'QueryFailed',
709
+ code: number = 2003,
710
+ message: string = 'Query failed',
711
+ status: number = 0,
712
+ cause?: string
713
+ ) {
714
+ super(name, code, message, status, cause)
715
+ Object.setPrototypeOf(this, QueryFailedError.prototype)
716
+ }
717
+ }
718
+
719
+ export class ValidationFailedError extends WebrpcError {
720
+ constructor(
721
+ name: string = 'ValidationFailed',
722
+ code: number = 2004,
723
+ message: string = 'Validation Failed',
724
+ status: number = 0,
725
+ cause?: string
726
+ ) {
727
+ super(name, code, message, status, cause)
728
+ Object.setPrototypeOf(this, ValidationFailedError.prototype)
729
+ }
730
+ }
731
+
732
+ export class NotFoundError extends WebrpcError {
733
+ constructor(
734
+ name: string = 'NotFound',
735
+ code: number = 3000,
736
+ message: string = 'Resource not found',
737
+ status: number = 0,
738
+ cause?: string
739
+ ) {
740
+ super(name, code, message, status, cause)
741
+ Object.setPrototypeOf(this, NotFoundError.prototype)
742
+ }
743
+ }
744
+
745
+ export enum errors {
746
+ WebrpcEndpoint = 'WebrpcEndpoint',
747
+ WebrpcRequestFailed = 'WebrpcRequestFailed',
748
+ WebrpcBadRoute = 'WebrpcBadRoute',
749
+ WebrpcBadMethod = 'WebrpcBadMethod',
750
+ WebrpcBadRequest = 'WebrpcBadRequest',
751
+ WebrpcBadResponse = 'WebrpcBadResponse',
752
+ WebrpcServerPanic = 'WebrpcServerPanic',
753
+ WebrpcInternalError = 'WebrpcInternalError',
754
+ WebrpcClientDisconnected = 'WebrpcClientDisconnected',
755
+ WebrpcStreamLost = 'WebrpcStreamLost',
756
+ WebrpcStreamFinished = 'WebrpcStreamFinished',
757
+ Unauthorized = 'Unauthorized',
758
+ SessionExpired = 'SessionExpired',
759
+ Aborted = 'Aborted',
760
+ InvalidArgument = 'InvalidArgument',
761
+ Unavailable = 'Unavailable',
762
+ QueryFailed = 'QueryFailed',
763
+ ValidationFailed = 'ValidationFailed',
764
+ NotFound = 'NotFound'
765
+ }
766
+
767
+ const webrpcErrorByCode: { [code: number]: any } = {
768
+ [0]: WebrpcEndpointError,
769
+ [-1]: WebrpcRequestFailedError,
770
+ [-2]: WebrpcBadRouteError,
771
+ [-3]: WebrpcBadMethodError,
772
+ [-4]: WebrpcBadRequestError,
773
+ [-5]: WebrpcBadResponseError,
774
+ [-6]: WebrpcServerPanicError,
775
+ [-7]: WebrpcInternalErrorError,
776
+ [-8]: WebrpcClientDisconnectedError,
777
+ [-9]: WebrpcStreamLostError,
778
+ [-10]: WebrpcStreamFinishedError,
779
+ [1000]: UnauthorizedError,
780
+ [1002]: SessionExpiredError,
781
+ [1005]: AbortedError,
782
+ [2001]: InvalidArgumentError,
783
+ [2002]: UnavailableError,
784
+ [2003]: QueryFailedError,
785
+ [2004]: ValidationFailedError,
786
+ [3000]: NotFoundError
787
+ }
788
+
382
789
  export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise<Response>