本文整理匯總了TypeScript中electron.net.request方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript net.request方法的具體用法?TypeScript net.request怎麽用?TypeScript net.request使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類electron.net
的用法示例。
在下文中一共展示了net.request方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: resolve
await new Promise<void>((resolve, reject) => {
const request = net.request(requestOptions)
request.on('response', response => {
if (response.statusCode === 200) {
resolve()
} else {
reject(
`Got ${response.statusCode} - ${response.statusMessage} from central`
)
}
})
request.on('error', reject)
request.end(body)
})
示例2: request
export async function request(
method: HTTPMethod,
uri: string,
data: any = {},
opts: IRequestOpts = {}
): Promise<IResponse> {
let url = uri;
if (env.unitTests) {
throw new Error(`refusing to do API request in unit test`);
}
if (method === "get") {
const query = querystring.stringify(data);
if (query !== "") {
url = `${url}?${query}`;
}
}
const req = net.request({
method,
url,
partition: NET_PARTITION_NAME,
});
req.setHeader("user-agent", userAgent());
const p = new ItchPromise<IResponse>((resolve, reject) => {
req.on("response", (inputRes: any) => {
const res = inputRes as ActualElectronResponse;
const response = {
statusCode: res.statusCode,
status: res.statusMessage,
body: null,
headers: res.headers,
} as IResponse;
if (opts.cb) {
try {
opts.cb(response);
} catch (e) {
reject(e);
return;
}
}
let text: any = "";
if (opts.sink) {
res.pipe(opts.sink());
} else {
res.setEncoding("utf8");
res.on("data", function(chunk) {
text += chunk;
});
}
const contentTypeHeader = (res.headers["content-type"] || [
"text/plain",
])[0];
const contentType = /[^;]*/.exec(contentTypeHeader)![0];
res.on("end", async () => {
response.size = text.length;
if (opts.sink) {
// all good, it's up to caller to wait on promised sink
} else if (contentType === "application/json") {
try {
response.body = JSON.parse(text);
} catch (e) {
reject(new RequestParsingFailure(e.message));
return;
}
} else {
response.body = text;
}
resolve(response);
});
});
req.on("error", error => {
reject(new RequestError(error.message));
});
req.on("abort", (error: Error) => {
reject(new RequestAborted());
});
req.on("login", (authInfo, callback) => {
// cf. https://github.com/electron/electron/blob/master/docs/api/client-request.md
// "Providing empty credentials will cancel the request and report
// an authentication error on the response object"
callback(undefined as any, undefined as any);
});
req.on("close", () => {
// no-op
});
if (!opts.sink) {
//.........這裏部分代碼省略.........
示例3: doRequest
public doRequest(options: any, callback: (response: any) => void): any {
const request = net.request(options)
request.on("response", callback)
this.addProxyLoginHandler(request)
return request
}