Edits history of script submission #17545 for ' Add Webhook (paychex)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    /**
     * Add Webhook
     * Add a webhook for the calling application.When registering a webhook, you will need to provide your own URI in the request body (see our full webhook documention here on how to configure your URI). The newly registered webhook will send notifications to this endpoint as JSON payloads which vary by domain. Please configure your endpoint to accept the relevant payloads. To see an example payload for each domain, please refer to the "Callbacks" tab.
     */
    export async function main(auth: RT.Paychex, body: Body) {
    	const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token')
    	const url = new URL(`https://api.paychex.com/management/hooks`)
    
    	const response = await fetch(url, {
    		method: 'POST',
    		headers: {
    			'Content-Type': 'application/json',
    			Authorization: 'Bearer ' + accessToken
    		},
    		body: JSON.stringify(body)
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    
    async function getOAuthToken(auth: RT.Paychex, tokenUrl: string): Promise<string> {
    	const params = new URLSearchParams({
    		grant_type: 'client_credentials'
    	})
    
    	const response = await fetch(tokenUrl, {
    		method: 'POST',
    		headers: {
    			Authorization: 'Basic ' + btoa(`${auth.client_id}:${auth.client_secret}`),
    			'Content-Type': 'application/x-www-form-urlencoded'
    		},
    		body: params.toString()
    	})
    
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`OAuth token request failed: ${response.status} ${text}`)
    	}
    
    	const data = await response.json()
    	return data.access_token
    }
    
    /* eslint-disable */
    /**
     * This file was automatically generated by json-schema-to-typescript.
     * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
     * and run json-schema-to-typescript to regenerate this file.
     */
    
    /**
     * This resource is for registering/unregistering webhooks.
     */
    export interface Body {
    	/**
    	 * The unique identifier associated with this webhook. Not required for Posting.
    	 */
    	hookId?: string
    	/**
    	 * Enter a valid URL for the webhook to receive events.
    	 */
    	uri: string
    	/**
    	 * ID associated with desired company that will receive the webhook notifications. NOTE: If no Company ID is provided in the POST, then all companies linked to the app will receive notifications.
    	 */
    	companyId?: string
    	/**
    	 * Required Authentication object is going to be different based on each authType. <br />* NO_AUTH doesn't have any other fields in authentication object <br />* BASIC_AUTH needs 2 fields: username and password <br />* APIKEY requires the field: apiKey <br />* OAUTH2 requires 5 fields:  tokenUrl, clientId, clientSecret, grantType, contentType <br />* OAUTH2_BASIC requires 5 fields:  tokenUrl, clientId, clientSecret, grantType, contentType
    	 */
    	authentication: {
    		/**
    		 * The authorization method used to authorize callers to your webhook url.
    		 */
    		authType: 'NO_AUTH' | 'BASIC_AUTH' | 'API_KEY' | 'OAUTH2' | 'OAUTH2_BASIC'
    		/**
    		 * The API key required for API_KEY authorization.
    		 */
    		apiKey?: string
    		/**
    		 * The username required for BASIC_AUTH authorization.
    		 */
    		username?: string
    		/**
    		 * The password required for BASIC_AUTH authorization.
    		 */
    		password?: string
    		/**
    		 * The URL used to obtain an access token for OAUTH2 or OAUTH2_BASIC authorization.
    		 */
    		tokenUrl?: string
    		/**
    		 * The client ID required by OAUTH2 and OAUTH2_BASIC authorization.
    		 */
    		clientId?: string
    		/**
    		 * The client secret required by OAUTH2 and OAUTH2_BASIC authorization.
    		 */
    		clientSecret?: string
    		/**
    		 * The grant type used to acquire an access token by OAUTH2 and OAUTH2_BASIC authorization.
    		 */
    		grantType?: string
    		/**
    		 * The content type to use in the token request for OAUTH2 and OAUTH2_BASIC authorization.
    		 */
    		contentType?: string
    		[k: string]: unknown
    	}
    	/**
    	 * List of available domains to register to. Refer to webhook documentation https://developer.paychex.com/documentation#operation/domains.
    	 */
    	domains: string[]
    	links?: {
    		rel?: string
    		href?: string
    		hreflang?: string
    		media?: string
    		title?: string
    		type?: string
    		deprecation?: string
    		profile?: string
    		name?: string
    		[k: string]: unknown
    	}[]
    	[k: string]: unknown
    }
    

    Submitted by hugo697 235 days ago