@notebook-intelligence/notebook-intelligence 1.2.2

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/src/handler.ts ADDED
@@ -0,0 +1,51 @@
1
+ // Copyright (c) Mehmet Bektas <mbektasgh@outlook.com>
2
+
3
+ import { URLExt } from '@jupyterlab/coreutils';
4
+
5
+ import { ServerConnection } from '@jupyterlab/services';
6
+
7
+ /**
8
+ * Call the API extension
9
+ *
10
+ * @param endPoint API REST end point for the extension
11
+ * @param init Initial values for the request
12
+ * @returns The response body interpreted as JSON
13
+ */
14
+ export async function requestAPI<T>(
15
+ endPoint = '',
16
+ init: RequestInit = {}
17
+ ): Promise<T> {
18
+ // Make request to Jupyter API
19
+ const settings = ServerConnection.makeSettings();
20
+ const requestUrl = URLExt.join(
21
+ settings.baseUrl,
22
+ 'notebook-intelligence', // API Namespace
23
+ endPoint
24
+ );
25
+
26
+ let response: Response;
27
+ try {
28
+ response = await ServerConnection.makeRequest(requestUrl, init, settings);
29
+ } catch (error) {
30
+ throw new ServerConnection.NetworkError(error as any);
31
+ }
32
+
33
+ let data: any = await response.text();
34
+
35
+ if (data.length > 0) {
36
+ try {
37
+ data = JSON.parse(data);
38
+ } catch (error) {
39
+ console.log('Not a JSON response body.', response);
40
+ }
41
+ }
42
+
43
+ if (!response.ok) {
44
+ throw new ServerConnection.ResponseError(
45
+ response,
46
+ data.message || data.error || data
47
+ );
48
+ }
49
+
50
+ return data;
51
+ }