@bsv/sdk
Version:
BSV Blockchain Software Development Kit
40 lines • 1.44 kB
JavaScript
/**
* Adapter for Node Https module to be used as HttpClient
*/
export class NodejsHttpClient {
https;
constructor(https) {
this.https = https;
}
async request(url, requestOptions) {
return await new Promise((resolve, reject) => {
const req = this.https.request(url, requestOptions, (res) => {
let body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
const ok = res.statusCode >= 200 && res.statusCode <= 299;
const mediaType = res.headers['content-type'];
const data = body !== '' && typeof mediaType === 'string' && mediaType.startsWith('application/json')
? JSON.parse(body)
: body;
resolve({
status: res.statusCode,
statusText: res.statusMessage,
ok,
data
});
});
});
req.on('error', (error) => {
reject(error);
});
if (requestOptions.data !== null && requestOptions.data !== undefined) {
req.write(JSON.stringify(requestOptions.data));
}
req.end();
});
}
}
//# sourceMappingURL=NodejsHttpClient.js.map