doover-js 0.16.0 → 0.17.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/dist/esm/gateway/gateway-client.js +8 -0
- package/dist/esm/http/rest-client.d.ts +4 -0
- package/dist/esm/http/rest-client.js +20 -6
- package/dist/esm/package.json +1 -1
- package/dist/esm/utils/view-as.d.ts +2 -0
- package/dist/esm/utils/view-as.js +6 -0
- package/dist/gateway/gateway-client.js +8 -0
- package/dist/http/rest-client.d.ts +4 -0
- package/dist/http/rest-client.js +20 -6
- package/dist/utils/view-as.d.ts +2 -0
- package/dist/utils/view-as.js +9 -0
- package/package.json +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { DooverGatewayError } from "../http/errors.js";
|
|
2
2
|
import { addTimestampToMessage } from "../utils/snowflake.js";
|
|
3
|
+
import { viewAsSession } from "../utils/view-as.js";
|
|
3
4
|
const RECONNECT_BASE_MS = 1000;
|
|
4
5
|
const RECONNECT_CAP_MS = 30000;
|
|
5
6
|
/** Schemes a WebSocket can be opened on — it maps http(s) to ws(s) itself. */
|
|
@@ -132,6 +133,13 @@ export class GatewayClient {
|
|
|
132
133
|
const wsParams = this.auth
|
|
133
134
|
? await this.auth.prepareWebSocket(this.config.dataWssUrl, hasFactory)
|
|
134
135
|
: { url: this.config.dataWssUrl };
|
|
136
|
+
// A browser WebSocket can't send headers, so the session goes in the URL.
|
|
137
|
+
const viewAs = viewAsSession();
|
|
138
|
+
if (viewAs) {
|
|
139
|
+
const url = new URL(wsParams.url);
|
|
140
|
+
url.searchParams.set("view_as", viewAs);
|
|
141
|
+
wsParams.url = url.toString();
|
|
142
|
+
}
|
|
135
143
|
let socket;
|
|
136
144
|
if (this.config.webSocketFactory) {
|
|
137
145
|
socket = this.config.webSocketFactory({
|
|
@@ -40,6 +40,10 @@ export interface RequestOptions {
|
|
|
40
40
|
body?: BodyInit | object | null;
|
|
41
41
|
headers?: HeadersInit;
|
|
42
42
|
omitSharingHeader?: boolean;
|
|
43
|
+
omitOrganisationHeader?: boolean;
|
|
44
|
+
/** Read a successful response as this, whatever its content type. */
|
|
45
|
+
responseType?: "blob" | "text";
|
|
46
|
+
signal?: AbortSignal;
|
|
43
47
|
}
|
|
44
48
|
type QueryValue = string | number | boolean | null | undefined | Array<string | number | boolean>;
|
|
45
49
|
type QueryParams = Record<string, QueryValue> | object;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DooverApiError, DooverMfaRequiredError, isMfaRequiredBody } from "./errors.js";
|
|
2
|
+
import { viewAsSession } from "../utils/view-as.js";
|
|
2
3
|
export class RestClient {
|
|
3
4
|
constructor(config, auth, onTransportFailure) {
|
|
4
5
|
this.onTransportFailure = onTransportFailure;
|
|
@@ -39,14 +40,14 @@ export class RestClient {
|
|
|
39
40
|
await this.prepareAuth();
|
|
40
41
|
const request = this.prepareRequest(options);
|
|
41
42
|
let response = await this.sendRequest(request);
|
|
42
|
-
let payload = await this.parseResponse(response);
|
|
43
|
+
let payload = await this.parseResponse(response, options.responseType);
|
|
43
44
|
if (response.status === 401 && this.auth) {
|
|
44
45
|
const recovered = await this.auth.handleUnauthorized();
|
|
45
46
|
if (recovered) {
|
|
46
47
|
await this.prepareAuth();
|
|
47
48
|
const retryRequest = this.prepareRequest(options);
|
|
48
49
|
response = await this.sendRequest(retryRequest);
|
|
49
|
-
payload = await this.parseResponse(response);
|
|
50
|
+
payload = await this.parseResponse(response, options.responseType);
|
|
50
51
|
}
|
|
51
52
|
}
|
|
52
53
|
if (!response.ok) {
|
|
@@ -88,17 +89,23 @@ export class RestClient {
|
|
|
88
89
|
if (!options.omitSharingHeader) {
|
|
89
90
|
headers.set("X-Doover-Sharing", this.config.sharing);
|
|
90
91
|
}
|
|
91
|
-
if (this.config.organisationId &&
|
|
92
|
+
if (this.config.organisationId &&
|
|
93
|
+
!options.omitOrganisationHeader &&
|
|
94
|
+
!headers.has("X-Doover-Organisation")) {
|
|
92
95
|
headers.set("X-Doover-Organisation", this.config.organisationId);
|
|
93
96
|
}
|
|
94
97
|
const impersonated = this.getImpersonatedUserId();
|
|
95
98
|
if (impersonated) {
|
|
96
99
|
headers.set("X-Doover-Assume", impersonated);
|
|
97
100
|
}
|
|
101
|
+
const viewAs = viewAsSession();
|
|
102
|
+
if (viewAs) {
|
|
103
|
+
headers.set("X-Doover-View-As", viewAs);
|
|
104
|
+
}
|
|
98
105
|
const credentials = this.auth
|
|
99
106
|
? this.auth.getFetchCredentials()
|
|
100
107
|
: "include";
|
|
101
|
-
return { url, method, headers, body, credentials };
|
|
108
|
+
return { url, method, headers, body, credentials, signal: options.signal };
|
|
102
109
|
}
|
|
103
110
|
async sendRequest(request) {
|
|
104
111
|
// Merge auth headers immediately before fetch so a retry can use refreshed
|
|
@@ -116,6 +123,7 @@ export class RestClient {
|
|
|
116
123
|
headers: request.headers,
|
|
117
124
|
body: request.body,
|
|
118
125
|
credentials: request.credentials,
|
|
126
|
+
signal: request.signal,
|
|
119
127
|
});
|
|
120
128
|
}
|
|
121
129
|
catch (error) {
|
|
@@ -142,7 +150,10 @@ export class RestClient {
|
|
|
142
150
|
buildUrl(baseUrl, path, query) {
|
|
143
151
|
const normalizedBase = baseUrl.replace(/\/+$/, "");
|
|
144
152
|
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
145
|
-
|
|
153
|
+
// A full URL as the path is used as it is.
|
|
154
|
+
const finalUrl = /^https?:\/\//.test(path)
|
|
155
|
+
? new URL(path)
|
|
156
|
+
: new URL(`${normalizedBase}${normalizedPath}`);
|
|
146
157
|
if (query) {
|
|
147
158
|
for (const [key, value] of Object.entries(query)) {
|
|
148
159
|
if (value === undefined || value === null) {
|
|
@@ -172,10 +183,13 @@ export class RestClient {
|
|
|
172
183
|
headers.set("Content-Type", "application/json");
|
|
173
184
|
return JSON.stringify(body);
|
|
174
185
|
}
|
|
175
|
-
async parseResponse(response) {
|
|
186
|
+
async parseResponse(response, responseType) {
|
|
176
187
|
if (response.status === 204) {
|
|
177
188
|
return undefined;
|
|
178
189
|
}
|
|
190
|
+
if (response.ok && responseType) {
|
|
191
|
+
return response[responseType]();
|
|
192
|
+
}
|
|
179
193
|
const contentType = response.headers.get("content-type") ?? "";
|
|
180
194
|
if (contentType.includes("application/json")) {
|
|
181
195
|
return response.json();
|
package/dist/esm/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"doover-js","version":"0.
|
|
1
|
+
{"name":"doover-js","version":"0.17.0","type":"module","sideEffects":false}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.GatewayClient = void 0;
|
|
4
4
|
const errors_js_1 = require("../http/errors.js");
|
|
5
5
|
const snowflake_js_1 = require("../utils/snowflake.js");
|
|
6
|
+
const view_as_js_1 = require("../utils/view-as.js");
|
|
6
7
|
const RECONNECT_BASE_MS = 1000;
|
|
7
8
|
const RECONNECT_CAP_MS = 30000;
|
|
8
9
|
/** Schemes a WebSocket can be opened on — it maps http(s) to ws(s) itself. */
|
|
@@ -135,6 +136,13 @@ class GatewayClient {
|
|
|
135
136
|
const wsParams = this.auth
|
|
136
137
|
? await this.auth.prepareWebSocket(this.config.dataWssUrl, hasFactory)
|
|
137
138
|
: { url: this.config.dataWssUrl };
|
|
139
|
+
// A browser WebSocket can't send headers, so the session goes in the URL.
|
|
140
|
+
const viewAs = (0, view_as_js_1.viewAsSession)();
|
|
141
|
+
if (viewAs) {
|
|
142
|
+
const url = new URL(wsParams.url);
|
|
143
|
+
url.searchParams.set("view_as", viewAs);
|
|
144
|
+
wsParams.url = url.toString();
|
|
145
|
+
}
|
|
138
146
|
let socket;
|
|
139
147
|
if (this.config.webSocketFactory) {
|
|
140
148
|
socket = this.config.webSocketFactory({
|
|
@@ -40,6 +40,10 @@ export interface RequestOptions {
|
|
|
40
40
|
body?: BodyInit | object | null;
|
|
41
41
|
headers?: HeadersInit;
|
|
42
42
|
omitSharingHeader?: boolean;
|
|
43
|
+
omitOrganisationHeader?: boolean;
|
|
44
|
+
/** Read a successful response as this, whatever its content type. */
|
|
45
|
+
responseType?: "blob" | "text";
|
|
46
|
+
signal?: AbortSignal;
|
|
43
47
|
}
|
|
44
48
|
type QueryValue = string | number | boolean | null | undefined | Array<string | number | boolean>;
|
|
45
49
|
type QueryParams = Record<string, QueryValue> | object;
|
package/dist/http/rest-client.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RestClient = void 0;
|
|
4
4
|
const errors_js_1 = require("./errors.js");
|
|
5
|
+
const view_as_js_1 = require("../utils/view-as.js");
|
|
5
6
|
class RestClient {
|
|
6
7
|
constructor(config, auth, onTransportFailure) {
|
|
7
8
|
this.onTransportFailure = onTransportFailure;
|
|
@@ -42,14 +43,14 @@ class RestClient {
|
|
|
42
43
|
await this.prepareAuth();
|
|
43
44
|
const request = this.prepareRequest(options);
|
|
44
45
|
let response = await this.sendRequest(request);
|
|
45
|
-
let payload = await this.parseResponse(response);
|
|
46
|
+
let payload = await this.parseResponse(response, options.responseType);
|
|
46
47
|
if (response.status === 401 && this.auth) {
|
|
47
48
|
const recovered = await this.auth.handleUnauthorized();
|
|
48
49
|
if (recovered) {
|
|
49
50
|
await this.prepareAuth();
|
|
50
51
|
const retryRequest = this.prepareRequest(options);
|
|
51
52
|
response = await this.sendRequest(retryRequest);
|
|
52
|
-
payload = await this.parseResponse(response);
|
|
53
|
+
payload = await this.parseResponse(response, options.responseType);
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
56
|
if (!response.ok) {
|
|
@@ -91,17 +92,23 @@ class RestClient {
|
|
|
91
92
|
if (!options.omitSharingHeader) {
|
|
92
93
|
headers.set("X-Doover-Sharing", this.config.sharing);
|
|
93
94
|
}
|
|
94
|
-
if (this.config.organisationId &&
|
|
95
|
+
if (this.config.organisationId &&
|
|
96
|
+
!options.omitOrganisationHeader &&
|
|
97
|
+
!headers.has("X-Doover-Organisation")) {
|
|
95
98
|
headers.set("X-Doover-Organisation", this.config.organisationId);
|
|
96
99
|
}
|
|
97
100
|
const impersonated = this.getImpersonatedUserId();
|
|
98
101
|
if (impersonated) {
|
|
99
102
|
headers.set("X-Doover-Assume", impersonated);
|
|
100
103
|
}
|
|
104
|
+
const viewAs = (0, view_as_js_1.viewAsSession)();
|
|
105
|
+
if (viewAs) {
|
|
106
|
+
headers.set("X-Doover-View-As", viewAs);
|
|
107
|
+
}
|
|
101
108
|
const credentials = this.auth
|
|
102
109
|
? this.auth.getFetchCredentials()
|
|
103
110
|
: "include";
|
|
104
|
-
return { url, method, headers, body, credentials };
|
|
111
|
+
return { url, method, headers, body, credentials, signal: options.signal };
|
|
105
112
|
}
|
|
106
113
|
async sendRequest(request) {
|
|
107
114
|
// Merge auth headers immediately before fetch so a retry can use refreshed
|
|
@@ -119,6 +126,7 @@ class RestClient {
|
|
|
119
126
|
headers: request.headers,
|
|
120
127
|
body: request.body,
|
|
121
128
|
credentials: request.credentials,
|
|
129
|
+
signal: request.signal,
|
|
122
130
|
});
|
|
123
131
|
}
|
|
124
132
|
catch (error) {
|
|
@@ -145,7 +153,10 @@ class RestClient {
|
|
|
145
153
|
buildUrl(baseUrl, path, query) {
|
|
146
154
|
const normalizedBase = baseUrl.replace(/\/+$/, "");
|
|
147
155
|
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
148
|
-
|
|
156
|
+
// A full URL as the path is used as it is.
|
|
157
|
+
const finalUrl = /^https?:\/\//.test(path)
|
|
158
|
+
? new URL(path)
|
|
159
|
+
: new URL(`${normalizedBase}${normalizedPath}`);
|
|
149
160
|
if (query) {
|
|
150
161
|
for (const [key, value] of Object.entries(query)) {
|
|
151
162
|
if (value === undefined || value === null) {
|
|
@@ -175,10 +186,13 @@ class RestClient {
|
|
|
175
186
|
headers.set("Content-Type", "application/json");
|
|
176
187
|
return JSON.stringify(body);
|
|
177
188
|
}
|
|
178
|
-
async parseResponse(response) {
|
|
189
|
+
async parseResponse(response, responseType) {
|
|
179
190
|
if (response.status === 204) {
|
|
180
191
|
return undefined;
|
|
181
192
|
}
|
|
193
|
+
if (response.ok && responseType) {
|
|
194
|
+
return response[responseType]();
|
|
195
|
+
}
|
|
182
196
|
const contentType = response.headers.get("content-type") ?? "";
|
|
183
197
|
if (contentType.includes("application/json")) {
|
|
184
198
|
return response.json();
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.viewAsSession = viewAsSession;
|
|
4
|
+
/** The view-as session the app started, if any (set by the site's "View as"). */
|
|
5
|
+
function viewAsSession() {
|
|
6
|
+
if (typeof sessionStorage === "undefined")
|
|
7
|
+
return null;
|
|
8
|
+
return sessionStorage.getItem("view_as_session");
|
|
9
|
+
}
|