-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathindex.ts
More file actions
116 lines (102 loc) · 3.01 KB
/
index.ts
File metadata and controls
116 lines (102 loc) · 3.01 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { URLExt } from '@jupyterlab/coreutils';
import { ServerConnection } from '../serverconnection';
import { PromiseDelegate } from '@lumino/coreutils';
/**
* The url for the lab nbconvert service.
*/
const NBCONVERT_SETTINGS_URL = 'api/nbconvert';
/**
* The nbconvert API service manager.
*/
export class NbConvertManager {
/**
* Create a new nbconvert manager.
*/
constructor(options: NbConvertManager.IOptions = {}) {
this.serverSettings =
options.serverSettings ?? ServerConnection.makeSettings();
}
/**
* The server settings used to make API requests.
*/
readonly serverSettings: ServerConnection.ISettings;
/**
* Fetch and cache the export formats from the expensive nbconvert handler.
*/
protected async fetchExportFormats(): Promise<NbConvertManager.IExportFormats> {
this._requestingFormats = new PromiseDelegate();
this._exportFormats = null;
const base = this.serverSettings.baseUrl;
const url = URLExt.join(base, NBCONVERT_SETTINGS_URL);
const { serverSettings } = this;
const response = await ServerConnection.makeRequest(
url,
{},
serverSettings
);
if (response.status !== 200) {
const err = await ServerConnection.ResponseError.create(response);
throw err;
}
const data = await response.json();
const exportList: NbConvertManager.IExportFormats = {};
const keys = Object.keys(data);
keys.forEach(function (key) {
const mimeType: string = data[key].output_mimetype;
exportList[key] = { output_mimetype: mimeType };
});
this._exportFormats = exportList;
this._requestingFormats.resolve(exportList);
return exportList;
}
/**
* Get the list of export formats, preferring pre-cached ones.
*/
async getExportFormats(
force: boolean = true
): Promise<NbConvertManager.IExportFormats> {
if (this._requestingFormats) {
return this._requestingFormats.promise;
}
if (force || !this._exportFormats) {
return await this.fetchExportFormats();
}
return this._exportFormats;
}
protected _requestingFormats: PromiseDelegate<NbConvertManager.IExportFormats> | null;
protected _exportFormats: NbConvertManager.IExportFormats | null = null;
}
/**
* A namespace for `BuildManager` statics.
*/
export namespace NbConvertManager {
/**
* The instantiation options for a setting manager.
*/
export interface IOptions {
/**
* The server settings used to make API requests.
*/
serverSettings?: ServerConnection.ISettings;
}
/**
* A namespace for nbconvert API interfaces.
*/
export interface IExportFormats {
/**
* The list of supported export formats.
*/
[key: string]: { output_mimetype: string };
}
}
/**
* A namespace for builder API interfaces.
*/
export namespace NbConvert {
/**
* The interface for the build manager.
*/
export interface IManager extends NbConvertManager {}
}