Skip to content

Commit a2171c0

Browse files
authored
[ts-sdk] Add Authorization header to manager options (#1618)
1 parent e636b25 commit a2171c0

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

ts/smelter-core/src/api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ export type ApiRequest = {
1010
method: 'GET' | 'POST';
1111
route: string;
1212
body?: object;
13+
headers?: Record<string, string>;
1314
};
1415

1516
export type MultipartRequest = {
1617
method: 'POST';
1718
route: string;
1819
body: any; //FormData
20+
headers?: Record<string, string>;
1921
};
2022

2123
export type RegisterInputResponse = {

ts/smelter-node/src/fetch.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export async function sendRequest(baseUrl: string | URL, request: ApiRequest): P
1818
method: request.method,
1919
body: request.body && JSON.stringify(request.body),
2020
headers: {
21+
...request.headers,
2122
'Content-Type': 'application/json',
2223
},
2324
agent: url => (url.protocol === 'http:' ? httpAgent : httpsAgent),
@@ -39,6 +40,7 @@ export async function sendMultipartRequest(
3940
method: request.method,
4041
body: request.body as FormData,
4142
agent: url => (url.protocol === 'http:' ? httpAgent : httpsAgent),
43+
headers: request.headers,
4244
});
4345
if (response.status >= 400) {
4446
const err: any = new Error(`Request to Smelter server failed.`);

ts/smelter-node/src/manager/existingInstance.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ import { getSmelterStatus } from '../getSmelterStatus';
1212

1313
export type ExistingInstanceOptions = {
1414
url: string | URL;
15+
authorizationHeader?: string;
1516
};
1617

1718
/**
1819
* SmelterManager that will connect to existing instance
1920
*/
2021
class ExistingInstanceManager implements SmelterManager {
2122
private url: URL;
23+
private authorizationHeader?: string;
2224
private wsConnection: WebSocketConnection;
2325

2426
constructor(opts: ExistingInstanceOptions) {
@@ -34,10 +36,11 @@ class ExistingInstanceManager implements SmelterManager {
3436
}
3537

3638
this.url = url;
39+
this.authorizationHeader = opts.authorizationHeader;
3740

3841
const wsUrl = joinUrl(url, 'ws');
3942
wsUrl.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
40-
this.wsConnection = new WebSocketConnection(wsUrl);
43+
this.wsConnection = new WebSocketConnection(wsUrl, opts.authorizationHeader);
4144
}
4245

4346
public async setupInstance(opts: SetupInstanceOptions): Promise<void> {
@@ -80,11 +83,19 @@ class ExistingInstanceManager implements SmelterManager {
8083
}
8184

8285
public async sendRequest(request: ApiRequest): Promise<object> {
83-
return await sendRequest(this.url, request);
86+
let headers = {
87+
...request.headers,
88+
...(this.authorizationHeader ? { Authorization: this.authorizationHeader } : {}),
89+
};
90+
return await sendRequest(this.url, { ...request, headers });
8491
}
8592

8693
async sendMultipartRequest(request: MultipartRequest): Promise<object> {
87-
return await sendMultipartRequest(this.url, request);
94+
let headers = {
95+
...request.headers,
96+
...(this.authorizationHeader ? { Authorization: this.authorizationHeader } : {}),
97+
};
98+
return await sendMultipartRequest(this.url, { ...request, headers });
8899
}
89100

90101
public registerEventListener(cb: (event: object) => void): void {

ts/smelter-node/src/ws.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@ import WebSocket from 'ws';
33

44
export class WebSocketConnection {
55
private url: string | URL;
6+
private authorizationHeader?: string;
67
private listeners: Set<(event: object) => void>;
78
private ws: WebSocket | null = null;
89
private donePromise?: Promise<void>;
910

10-
constructor(url: string | URL) {
11+
constructor(url: string | URL, authorizationHeader?: string) {
1112
this.url = url;
13+
this.authorizationHeader = authorizationHeader;
1214
this.listeners = new Set();
1315
}
1416

1517
public async connect(logger: Logger): Promise<void> {
16-
const ws = new WebSocket(this.url);
18+
const ws = new WebSocket(this.url, {
19+
headers: this.authorizationHeader ? { Authorization: this.authorizationHeader } : {},
20+
});
1721

1822
let connected = false;
1923
await new Promise<void>((res, rej) => {

0 commit comments

Comments
 (0)