@taybart/corvid 0.1.18 → 0.1.19

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/dist/index.js CHANGED
@@ -493,10 +493,8 @@ class network_params {
493
493
  }
494
494
  }
495
495
  class request {
496
- auth(token) {
497
- const header = `Bearer ${token}`;
498
- this.log.debug(`adding auth token header ${header}`);
499
- this.opts.headers.Authorization = header;
496
+ auth(a) {
497
+ this.opts.auth = a;
500
498
  return this;
501
499
  }
502
500
  basicAuth(username, password) {
@@ -510,7 +508,7 @@ class request {
510
508
  return this;
511
509
  }
512
510
  build({ path, params: passedParams, override } = {}) {
513
- if (this.opts.auth && !this.opts.headers.Authorization) if ('string' == typeof this.opts.auth) this.opts.headers.Authorization = `Bearer ${this.opts.auth}`;
511
+ if (this.opts.auth) if ('string' == typeof this.opts.auth) this.opts.headers.Authorization = `Bearer ${this.opts.auth}`;
514
512
  else this.opts.headers.Authorization = `Basic ${btoa(`${this.opts.auth.username}:${this.opts.auth.password}`)}`;
515
513
  if (!override) override = {};
516
514
  const body = override.body || this.opts.body;
@@ -539,7 +537,7 @@ class request {
539
537
  }
540
538
  };
541
539
  }
542
- async do({ path, params: passedParams, override = {} } = {}) {
540
+ async do({ path, params: passedParams, override = {}, _recurselevel = 0 } = {}) {
543
541
  const { url, options } = this.build({
544
542
  path,
545
543
  params: passedParams,
@@ -550,9 +548,14 @@ class request {
550
548
  const expect = override.expect || this.opts.expect;
551
549
  if (res.status !== expect) {
552
550
  const body = await res.text();
553
- if (401 === res.status && this.opts.onUnauthorized) {
551
+ if (401 === res.status && this.opts.onUnauthorized && _recurselevel < 1) {
554
552
  this.log.warn("unauthorized");
555
- this.opts.onUnauthorized(body);
553
+ if (await this.opts.onUnauthorized.call(this, this, body)) return this.do({
554
+ path,
555
+ params: passedParams,
556
+ override,
557
+ _recurselevel: _recurselevel + 1
558
+ });
556
559
  } else throw new Error(`bad response ${res.status} !== ${expect}, body: ${body}`);
557
560
  }
558
561
  this.log.debug(`content type: ${res.headers.get('content-type')}`);
package/dist/network.d.ts CHANGED
@@ -22,14 +22,14 @@ export type requestOpts = {
22
22
  expect?: number;
23
23
  credentials?: RequestCredentials;
24
24
  insecureNoVerify?: boolean;
25
- onUnauthorized?: (body: any) => void;
25
+ onUnauthorized?: (client: request, body: any) => Promise<boolean>;
26
26
  fetch?: (url: string, init: RequestInit) => Promise<any>;
27
27
  };
28
28
  export declare class request {
29
29
  opts: requestOpts;
30
30
  log: logger;
31
31
  constructor(opts?: requestOpts, verbose?: boolean);
32
- auth(token: string | {
32
+ auth(a: string | {
33
33
  username: string;
34
34
  password: string;
35
35
  }): this;
@@ -62,7 +62,7 @@ export declare class request {
62
62
  body: string;
63
63
  };
64
64
  };
65
- do({ path, params: passedParams, override, }?: {
65
+ do({ path, params: passedParams, override, _recurselevel, }?: {
66
66
  path?: string;
67
67
  params?: Object;
68
68
  method?: string;
@@ -73,6 +73,7 @@ export declare class request {
73
73
  body?: Object;
74
74
  expect?: number;
75
75
  };
76
+ _recurselevel?: number;
76
77
  }): Promise<any>;
77
78
  }
78
79
  /*** websocket ***/
package/dist/network.js CHANGED
@@ -84,10 +84,8 @@ class network_params {
84
84
  }
85
85
  }
86
86
  class request {
87
- auth(token) {
88
- const header = `Bearer ${token}`;
89
- this.log.debug(`adding auth token header ${header}`);
90
- this.opts.headers.Authorization = header;
87
+ auth(a) {
88
+ this.opts.auth = a;
91
89
  return this;
92
90
  }
93
91
  basicAuth(username, password) {
@@ -101,7 +99,7 @@ class request {
101
99
  return this;
102
100
  }
103
101
  build({ path, params: passedParams, override } = {}) {
104
- if (this.opts.auth && !this.opts.headers.Authorization) if ('string' == typeof this.opts.auth) this.opts.headers.Authorization = `Bearer ${this.opts.auth}`;
102
+ if (this.opts.auth) if ('string' == typeof this.opts.auth) this.opts.headers.Authorization = `Bearer ${this.opts.auth}`;
105
103
  else this.opts.headers.Authorization = `Basic ${btoa(`${this.opts.auth.username}:${this.opts.auth.password}`)}`;
106
104
  if (!override) override = {};
107
105
  const body = override.body || this.opts.body;
@@ -130,7 +128,7 @@ class request {
130
128
  }
131
129
  };
132
130
  }
133
- async do({ path, params: passedParams, override = {} } = {}) {
131
+ async do({ path, params: passedParams, override = {}, _recurselevel = 0 } = {}) {
134
132
  const { url, options } = this.build({
135
133
  path,
136
134
  params: passedParams,
@@ -141,9 +139,14 @@ class request {
141
139
  const expect = override.expect || this.opts.expect;
142
140
  if (res.status !== expect) {
143
141
  const body = await res.text();
144
- if (401 === res.status && this.opts.onUnauthorized) {
142
+ if (401 === res.status && this.opts.onUnauthorized && _recurselevel < 1) {
145
143
  this.log.warn("unauthorized");
146
- this.opts.onUnauthorized(body);
144
+ if (await this.opts.onUnauthorized.call(this, this, body)) return this.do({
145
+ path,
146
+ params: passedParams,
147
+ override,
148
+ _recurselevel: _recurselevel + 1
149
+ });
147
150
  } else throw new Error(`bad response ${res.status} !== ${expect}, body: ${body}`);
148
151
  }
149
152
  this.log.debug(`content type: ${res.headers.get('content-type')}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taybart/corvid",
3
- "version": "0.1.18",
3
+ "version": "0.1.19",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "exports": {