Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support json schema request service
close #243
- Loading branch information
1 parent
7df5705
commit f76a672
Showing
5 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/vscode-vue-languageservice/src/utils/schemaRequestService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { xhr, XHRResponse, getErrorStatusDescription } from 'request-light'; | ||
import { URI as Uri } from 'vscode-uri'; | ||
import * as fs from 'fs'; | ||
import type * as json from 'vscode-json-languageservice'; | ||
|
||
function getHTTPRequestService(): json.SchemaRequestService { | ||
return (uri: string, _encoding?: string) => { | ||
const headers = { 'Accept-Encoding': 'gzip, deflate' }; | ||
return xhr({ url: uri, followRedirects: 5, headers }).then(response => { | ||
return response.responseText; | ||
}, (error: XHRResponse) => { | ||
return Promise.reject(error.responseText || getErrorStatusDescription(error.status) || error.toString()); | ||
}); | ||
}; | ||
} | ||
|
||
function getFileRequestService(): json.SchemaRequestService { | ||
return (location: string, encoding?: string) => { | ||
return new Promise((c, e) => { | ||
const uri = Uri.parse(location); | ||
fs.readFile(uri.fsPath, encoding, (err, buf) => { | ||
if (err) { | ||
return e(err); | ||
} | ||
c(buf.toString()); | ||
}); | ||
}); | ||
}; | ||
} | ||
|
||
const http = getHTTPRequestService(); | ||
const file = getFileRequestService(); | ||
|
||
export function getSchemaRequestService(handledSchemas: string[] = ['https', 'http', 'file']) { | ||
const builtInHandlers: { [protocol: string]: json.SchemaRequestService | undefined } = {}; | ||
for (let protocol of handledSchemas) { | ||
if (protocol === 'file') { | ||
builtInHandlers[protocol] = file; | ||
} else if (protocol === 'http' || protocol === 'https') { | ||
builtInHandlers[protocol] = http; | ||
} | ||
} | ||
return (uri: string): Thenable<string> => { | ||
const protocol = uri.substr(0, uri.indexOf(':')); | ||
|
||
const builtInHandler = builtInHandlers[protocol]; | ||
if (builtInHandler) { | ||
return builtInHandler(uri); | ||
} | ||
|
||
// TODO | ||
// return connection.sendRequest(VSCodeContentRequest.type, uri).then(responseText => { | ||
// return responseText; | ||
// }, error => { | ||
// return Promise.reject(error.message); | ||
// }); | ||
return new Promise(resolve => resolve('')); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters