@taybart/corvid 0.1.19 → 0.1.20

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