Skip to content
Snippets Groups Projects
Select Git revision
  • d26473c57b3e84f441688cfe26469cef1a3f9216
  • master default protected
2 results

webhookHandler.ts

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    webhookHandler.ts 722 B
    import { formatMessage } from "./formatter";
    import type { HandlerReturnType, NotificationData, NotificationType, PossibleNotificationType } from "../../notifications/types";
    
    export async function handleWebhook<T extends PossibleNotificationType>(recipient: string, type: NotificationType<T>, data: NotificationData<T>): Promise<HandlerReturnType> {
    	const body = JSON.stringify(Object.assign({}, data, {type: type.name, content: formatMessage(type, data)}));
    	const headers = {
    		"Content-Type": "application/json"
    	};
    	return fetch(recipient, {
    		method: "POST",
    		body,
    		headers
    	}).then(res => {
    		if(res.status >= 200 && res.status < 300){
    			return {
    				status: "success"
    			};
    		}else{
    			throw res;
    		}
    	});
    };