@chevre/domain
Version:
Chevre Domain Library for Node.js
46 lines (41 loc) • 1.47 kB
text/typescript
// tslint:disable:no-console
const url = String(process.env.LINE_NOTIFY_URL);
const LINE_NOTIFY_ACCESS_TOKEN = String(process.env.LINE_NOTIFY_ACCESS_TOKEN);
const TIMEOUT = 5000;
const MESSAGE = 'sample message';
async function main() {
try {
const form = new FormData();
form.set('message', `${MESSAGE} ${(new Date()).toString()}`);
const res = await fetch(
url,
{
signal: AbortSignal.timeout(TIMEOUT),
method: 'POST',
headers: {
Authorization: `Bearer ${LINE_NOTIFY_ACCESS_TOKEN}`
},
body: form
}
);
const result = await res.json();
console.log('result', result);
} catch (err) {
console.error(err);
if (err.name === 'TimeoutError') {
console.error('Timeout: It took more than 5 seconds to get the result!');
} else if (err.name === 'AbortError') {
console.error('Fetch aborted by user action (browser stop button, closing tab, etc.');
} else if (err.name === 'TypeError') {
console.error('AbortSignal.timeout() method is not supported');
} else {
// A network error, or some other problem.
console.error(`Error: type: ${err.name}, message: ${err.message}`);
}
}
}
main()
.then(() => {
console.log('success!');
})
.catch(console.error);