forked from redhat-developer/vscode-yaml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-schema-content-provider.ts
More file actions
73 lines (66 loc) · 3.08 KB
/
json-schema-content-provider.ts
File metadata and controls
73 lines (66 loc) · 3.08 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { TextDocumentContentProvider, Uri, workspace, window } from 'vscode';
import { xhr, configure as configureHttpRequests, getErrorStatusDescription, XHRResponse } from 'request-light';
import { JSONSchemaCache } from './json-schema-cache';
import { SchemaExtensionAPI } from './schema-extension-api';
export class JSONSchemaDocumentContentProvider implements TextDocumentContentProvider {
constructor(private readonly schemaCache: JSONSchemaCache, private readonly schemaApi: SchemaExtensionAPI) {}
async provideTextDocumentContent(uri: Uri): Promise<string> {
if (uri.fragment) {
const origUri = uri.fragment;
const schemaUri = Uri.parse(origUri);
// handle both 'http' and 'https'
if (origUri.startsWith('http')) {
return getJsonSchemaContent(origUri, this.schemaCache);
} else if (this.schemaApi.hasProvider(schemaUri.scheme)) {
let content = this.schemaApi.requestCustomSchemaContent(origUri);
content = await Promise.resolve(content);
// prettify JSON
if (content.indexOf('\n') === -1) {
content = JSON.stringify(JSON.parse(content), null, 2);
}
return content;
} else {
window.showErrorMessage(`Cannot Load content for: ${origUri}. Unknown schema: '${schemaUri.scheme}'`);
return null;
}
} else {
window.showErrorMessage(`Cannot Load content for: '${uri.toString()}' `);
return null;
}
}
}
export async function getJsonSchemaContent(uri: string, schemaCache: JSONSchemaCache): Promise<string> {
const cachedETag = schemaCache.getETag(uri);
const httpSettings = workspace.getConfiguration('http');
configureHttpRequests(httpSettings.http && httpSettings.http.proxy, httpSettings.http && httpSettings.http.proxyStrictSSL);
const headers: { [key: string]: string } = { 'Accept-Encoding': 'gzip, deflate' };
if (cachedETag) {
headers['If-None-Match'] = cachedETag;
}
return xhr({ url: uri, followRedirects: 5, headers })
.then(async (response) => {
// cache only if server supports 'etag' header
if (response.headers['etag']) {
await schemaCache.putSchema(uri, response.headers['etag'], response.responseText);
}
return response.responseText;
})
.then((text) => {
return text;
})
.catch((error: XHRResponse) => {
// content not changed, return cached
if (error.status === 304) {
return schemaCache.getSchema(uri);
}
// in case of some error, like internet connection issue, check if cached version exist and return it
if (schemaCache.getETag(uri)) {
return schemaCache.getSchema(uri);
}
return Promise.reject(error.responseText || getErrorStatusDescription(error.status) || error.toString());
});
}