feat: update ms system
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
import * as querystring from 'querystring'
|
||||
|
||||
const URL = Java.type('java.net.URL')
|
||||
const Paths = Java.type('java.nio.file.Paths');
|
||||
const Files = Java.type('java.nio.file.Files');
|
||||
const StandardCopyOption = Java.type('java.nio.file.StandardCopyOption');
|
||||
const Paths = Java.type('java.nio.file.Paths')
|
||||
const Files = Java.type('java.nio.file.Files')
|
||||
const StandardCopyOption = Java.type('java.nio.file.StandardCopyOption')
|
||||
|
||||
export type Method =
|
||||
| 'get' | 'GET'
|
||||
@ -13,36 +15,49 @@ export type Method =
|
||||
| 'patch' | 'PATCH'
|
||||
|
||||
interface RequestConfig {
|
||||
url?: string;
|
||||
method?: Method;
|
||||
headers?: { [key: string]: string };
|
||||
params?: { [key: string]: string };
|
||||
data?: any;
|
||||
url?: string
|
||||
method?: Method
|
||||
headers?: { [key: string]: string }
|
||||
params?: { [key: string]: string }
|
||||
data?: any
|
||||
}
|
||||
|
||||
function request(config: RequestConfig) {
|
||||
// @ts-ignore
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open(config.method, config.url, false);
|
||||
let xhr = new XMLHttpRequest()
|
||||
xhr.open(config.method, config.url, false)
|
||||
for (const header in config.headers) {
|
||||
xhr.setRequestHeader(header, config.headers[header]);
|
||||
xhr.setRequestHeader(header, config.headers[header])
|
||||
}
|
||||
xhr.send(typeof config.data === "string" ? config.data : JSON.stringify(config.data));
|
||||
if ((xhr.getResponseHeader("Content-Type") + '').indexOf('application/json') != -1) {
|
||||
let body = config.data
|
||||
if (body && typeof body !== "string") {
|
||||
switch (config.headers['Content-Type']) {
|
||||
case "application/json":
|
||||
body = JSON.stringify(body)
|
||||
break
|
||||
case "application/x-www-form-urlencoded":
|
||||
body = querystring.encode(body)
|
||||
break
|
||||
}
|
||||
}
|
||||
xhr.send(body)
|
||||
if (xhr.getResponseHeader("Content-Type").indexOf('application/json') != -1) {
|
||||
xhr.responseType = "json"
|
||||
}
|
||||
return xhr.get();
|
||||
return xhr.get()
|
||||
}
|
||||
|
||||
function download(url: string, target: string) {
|
||||
console.debug(`Start Download file ${target} from ${url}....`)
|
||||
Files.copy(new URL(url).openStream(), Paths.get(target), StandardCopyOption.REPLACE_EXISTING);
|
||||
Files.copy(new URL(url).openStream(), Paths.get(target), StandardCopyOption.REPLACE_EXISTING)
|
||||
console.debug(`File ${target} Download Complate...`)
|
||||
}
|
||||
|
||||
function _proxy(method: Method) {
|
||||
return function (url: string, data?: any, config?: RequestConfig) {
|
||||
return request({ url, method, data, ...config });
|
||||
return function (url: string, data?: any, config: RequestConfig = {}) {
|
||||
if (!config.headers) { config.headers = {} }
|
||||
config.headers['Content-Type'] = config.headers['Content-Type'] ?? 'application/json'
|
||||
return request({ url, method, data, ...config })
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user