2019-07-16 08:40:01 +02:00
|
|
|
import Handlebars from 'handlebars';
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
|
|
import { OptionsWithUrl } from 'request';
|
|
|
|
import { Config, Package, RemoteUser } from '@verdaccio/types';
|
2021-03-14 08:42:46 +01:00
|
|
|
import { notifyRequest } from './notify-request';
|
2019-07-16 08:40:01 +02:00
|
|
|
|
|
|
|
type TemplateMetadata = Package & { publishedPackage: string };
|
|
|
|
|
2020-08-13 23:27:00 +02:00
|
|
|
export function handleNotify(metadata: Package, notifyEntry, remoteUser: RemoteUser, publishedPackage: string): Promise<any> | void {
|
2019-07-16 08:40:01 +02:00
|
|
|
let regex;
|
|
|
|
if (metadata.name && notifyEntry.packagePattern) {
|
|
|
|
regex = new RegExp(notifyEntry.packagePattern, notifyEntry.packagePatternFlags || '');
|
|
|
|
if (!regex.test(metadata.name)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 23:27:00 +02:00
|
|
|
const template: HandlebarsTemplateDelegate = Handlebars.compile(notifyEntry.content);
|
2019-07-16 08:40:01 +02:00
|
|
|
// don't override 'publisher' if package.json already has that
|
|
|
|
/* eslint no-unused-vars: 0 */
|
|
|
|
/* eslint @typescript-eslint/no-unused-vars: 0 */
|
|
|
|
// @ts-ignore
|
|
|
|
if (_.isNil(metadata.publisher)) {
|
|
|
|
// @ts-ignore
|
|
|
|
metadata = { ...metadata, publishedPackage, publisher: { name: remoteUser.name as string } };
|
|
|
|
}
|
|
|
|
|
|
|
|
const content: string = template(metadata);
|
|
|
|
|
|
|
|
const options: OptionsWithUrl = {
|
|
|
|
body: content,
|
2020-08-13 23:27:00 +02:00
|
|
|
url: '',
|
2019-07-16 08:40:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// provides fallback support, it's accept an Object {} and Array of {}
|
|
|
|
if (notifyEntry.headers && _.isArray(notifyEntry.headers)) {
|
|
|
|
const header = {};
|
2021-03-14 08:42:46 +01:00
|
|
|
notifyEntry.headers.map(function (item): void {
|
2019-07-16 08:40:01 +02:00
|
|
|
if (Object.is(item, item)) {
|
|
|
|
for (const key in item) {
|
2019-12-23 09:29:27 +01:00
|
|
|
/* eslint no-prototype-builtins: 0 */
|
2019-07-16 08:40:01 +02:00
|
|
|
if (item.hasOwnProperty(key)) {
|
|
|
|
header[key] = item[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
options.headers = header;
|
|
|
|
} else if (Object.is(notifyEntry.headers, notifyEntry.headers)) {
|
|
|
|
options.headers = notifyEntry.headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.method = notifyEntry.method;
|
|
|
|
|
|
|
|
if (notifyEntry.endpoint) {
|
|
|
|
options.url = notifyEntry.endpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
return notifyRequest(options, content);
|
|
|
|
}
|
|
|
|
|
2020-08-13 23:27:00 +02:00
|
|
|
export function sendNotification(metadata: Package, notify: Notification, remoteUser: RemoteUser, publishedPackage: string): Promise<any> {
|
2019-07-16 08:40:01 +02:00
|
|
|
return handleNotify(metadata, notify, remoteUser, publishedPackage) as Promise<any>;
|
|
|
|
}
|
|
|
|
|
2020-08-13 23:27:00 +02:00
|
|
|
export function notify(metadata: Package, config: Config, remoteUser: RemoteUser, publishedPackage: string): Promise<any> | void {
|
2019-07-16 08:40:01 +02:00
|
|
|
if (config.notify) {
|
|
|
|
if (config.notify.content) {
|
2020-08-13 23:27:00 +02:00
|
|
|
return sendNotification(metadata, (config.notify as unknown) as Notification, remoteUser, publishedPackage);
|
2019-07-16 08:40:01 +02:00
|
|
|
}
|
2020-01-12 14:44:22 +01:00
|
|
|
// multiple notifications endpoints PR #108
|
2020-08-13 23:27:00 +02:00
|
|
|
return Promise.all(_.map(config.notify, (key) => sendNotification(metadata, key, remoteUser, publishedPackage)));
|
2019-07-16 08:40:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|