@taybart/corvid 0.1.20 → 0.1.21

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,20 +537,26 @@ 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,
546
544
  override
547
545
  });
548
546
  this.log.debug(`${this.opts.method} ${url}`);
549
- const res = await fetch(url, options);
547
+ let res;
548
+ res = this.opts.fetch ? await this.opts.fetch(url, options) : await fetch(url, options);
550
549
  const expect = override.expect || this.opts.expect;
551
550
  if (res.status !== expect) {
552
551
  const body = await res.text();
553
- if (401 === res.status && this.opts.onUnauthorized) {
552
+ if (401 === res.status && this.opts.onUnauthorized && _recurselevel < 1) {
554
553
  this.log.warn("unauthorized");
555
- this.opts.onUnauthorized(body);
554
+ if (await this.opts.onUnauthorized.call(this, this, body)) return this.do({
555
+ path,
556
+ params: passedParams,
557
+ override,
558
+ _recurselevel: _recurselevel + 1
559
+ });
556
560
  } else throw {
557
561
  message: `bad wesponse ${res.status} !== ${expect}, body: ${body}`,
558
562
  status: res.status,
@@ -578,7 +582,6 @@ class request {
578
582
  this.log.debug("converting object params to class");
579
583
  this.opts.params = new network_params(this.opts.params);
580
584
  }
581
- if (!this.opts.fetch) this.opts.fetch = window.fetch;
582
585
  this.log.debug(`with options: ${JSON.stringify(this.opts)}`);
583
586
  }
584
587
  }
package/dist/network.d.ts CHANGED
@@ -27,14 +27,14 @@ export type RequestOpts = {
27
27
  expect?: number;
28
28
  credentials?: RequestCredentials;
29
29
  insecureNoVerify?: boolean;
30
- onUnauthorized?: (body: any) => void;
30
+ onUnauthorized?: (client: request, body: any) => Promise<boolean>;
31
31
  fetch?: (url: string, init: RequestInit) => Promise<any>;
32
32
  };
33
33
  export declare class request {
34
34
  opts: RequestOpts;
35
35
  log: logger;
36
36
  constructor(opts?: RequestOpts, verbose?: boolean);
37
- auth(token: string | {
37
+ auth(a: string | {
38
38
  username: string;
39
39
  password: string;
40
40
  }): this;
@@ -67,7 +67,7 @@ export declare class request {
67
67
  body: string;
68
68
  };
69
69
  };
70
- do({ path, params: passedParams, override, }?: {
70
+ do({ path, params: passedParams, override, _recurselevel, }?: {
71
71
  path?: string;
72
72
  params?: Object;
73
73
  method?: string;
@@ -78,6 +78,7 @@ export declare class request {
78
78
  body?: Object;
79
79
  expect?: number;
80
80
  };
81
+ _recurselevel?: number;
81
82
  }): Promise<any>;
82
83
  }
83
84
  /*** 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,20 +128,26 @@ 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,
137
135
  override
138
136
  });
139
137
  this.log.debug(`${this.opts.method} ${url}`);
140
- const res = await fetch(url, options);
138
+ let res;
139
+ res = this.opts.fetch ? await this.opts.fetch(url, options) : await fetch(url, options);
141
140
  const expect = override.expect || this.opts.expect;
142
141
  if (res.status !== expect) {
143
142
  const body = await res.text();
144
- if (401 === res.status && this.opts.onUnauthorized) {
143
+ if (401 === res.status && this.opts.onUnauthorized && _recurselevel < 1) {
145
144
  this.log.warn("unauthorized");
146
- this.opts.onUnauthorized(body);
145
+ if (await this.opts.onUnauthorized.call(this, this, body)) return this.do({
146
+ path,
147
+ params: passedParams,
148
+ override,
149
+ _recurselevel: _recurselevel + 1
150
+ });
147
151
  } else throw {
148
152
  message: `bad wesponse ${res.status} !== ${expect}, body: ${body}`,
149
153
  status: res.status,
@@ -169,7 +173,6 @@ class request {
169
173
  this.log.debug("converting object params to class");
170
174
  this.opts.params = new network_params(this.opts.params);
171
175
  }
172
- if (!this.opts.fetch) this.opts.fetch = window.fetch;
173
176
  this.log.debug(`with options: ${JSON.stringify(this.opts)}`);
174
177
  }
175
178
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taybart/corvid",
3
- "version": "0.1.20",
3
+ "version": "0.1.21",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "exports": {