0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-03-04 02:02:39 -05:00
verdaccio/packages/hooks/src/notify-request.ts
Behrang Yarahmadi 13310814da
#2606 add prettier plugin sort imports (#2607)
* #2606 add prettier plugin sort imprts

* #2606 update pnpm-lock.yaml

* #2606 update eslint rules

* #2606 fixes website directory formatting

Co-authored-by: Ayush Sharma <ayush.sharma@trivago.com>
2021-10-29 17:33:05 +02:00

43 lines
1.1 KiB
TypeScript

import buildDebug from 'debug';
import { HTTP_STATUS } from '@verdaccio/core';
import { logger } from '@verdaccio/logger';
const debug = buildDebug('verdaccio:hooks:request');
const fetch = require('undici-fetch');
export type FetchOptions = {
body: string;
headers?: {};
method?: string;
};
export async function notifyRequest(url: string, options: FetchOptions): Promise<boolean> {
let response;
try {
debug('uri %o', url);
response = await fetch(url, {
body: JSON.stringify(options.body),
method: 'POST',
headers: { 'Content-Type': 'application/json' },
});
debug('response.status %o', response.status);
const body = await response.json();
if (response.status >= HTTP_STATUS.BAD_REQUEST) {
throw new Error(body);
}
logger.info(
{ content: options.body },
'The notification @{content} has been successfully dispatched'
);
return true;
} catch (err: any) {
debug('request error %o', err);
logger.error(
{ errorMessage: err?.message },
'notify service has thrown an error: @{errorMessage}'
);
return false;
}
}