-
-
Notifications
You must be signed in to change notification settings - Fork 550
/
base.ts
81 lines (76 loc) · 2.49 KB
/
base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/** tslint:disable:jsdoc-format */
import Cookies from "js-cookie";
import { AjaxRequest, AjaxResponse } from "rxjs/ajax";
import { ServerConfig } from "@nteract/types";
export const normalizeBaseURL = (url = "") => url.replace(/\/+$/, "");
/**
* Creates an AJAX request to connect to a given server. This function
* handles setting the authorization tokens on the request.
*
* @param serverConfig Details about the server to connect to.
* @param uri The URL to connect to, not including the base URL
* @param opts A set of options to pass to the AJAX request. We mimic jquery.ajax support of a cache option here.
*
* @returns A fully-configured AJAX request for connecting to the server.
*/
export const createAJAXSettings = (
serverConfig: ServerConfig,
uri = "/",
opts: Partial<AjaxRequest & { cache?: boolean }> = {}
): AjaxRequest => {
const baseURL = normalizeBaseURL(serverConfig.endpoint || serverConfig.url);
let url = `${baseURL}${uri}`;
if (opts.cache === false) {
const parsed = new URL(url);
parsed.searchParams.set("_", Date.now().toString());
url = parsed.href;
}
// Use the server config provided token if available before trying cookies
const xsrfToken = serverConfig.xsrfToken || Cookies.get("_xsrf");
const headers = {
...(xsrfToken && { "X-XSRFToken": xsrfToken }),
...(serverConfig.token && {
Authorization: `token ${serverConfig.token}`
})
};
// Merge in our typical settings for responseType, allow setting additional
// options like the method
const settings = {
url,
responseType: "json",
createXHR: () => new XMLHttpRequest(),
...serverConfig,
...serverConfig.ajaxOptions,
...opts,
// Make sure we merge in the auth headers with user given headers
headers: {
...headers,
...opts.headers,
...(serverConfig.ajaxOptions && serverConfig.ajaxOptions.headers)
}
};
delete settings.endpoint;
delete settings.cache;
return settings;
};
/**
* RxJS's AjaxResponse sets `response` as any when we can declare the type returned.
*
* This interface is meant to be internal, used as:
*
* ```
ajax('/api/contents') as Observable<JupyterAjaxResponse<Contents>>
```
* or as
* ```
ajax('/api/contents').map(resp => {
if(resp.response.type === "notebook")
})
```
*
* NOTE: the response can still be invalid and should likely be validated
*/
export interface JupyterAjaxResponse<ResponseType, ErrorType = string>
extends AjaxResponse {
response: ResponseType | ErrorType;
}