- Notifications
You must be signed in to change notification settings - Fork39
WIP: webhook demo#508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,101 @@ | ||||||||||||||||||||||||||||||
import*asTTfrom'typings/tutorial' | ||||||||||||||||||||||||||||||
importfetchfrom'node-fetch' | ||||||||||||||||||||||||||||||
importloggerfrom'../logger' | ||||||||||||||||||||||||||||||
import{WEBHOOK_TOKEN}from'../../environment' | ||||||||||||||||||||||||||||||
constWEBHOOK_EVENTS={ | ||||||||||||||||||||||||||||||
init:false, | ||||||||||||||||||||||||||||||
reset:false, | ||||||||||||||||||||||||||||||
step_complete:false, | ||||||||||||||||||||||||||||||
level_complete:false, | ||||||||||||||||||||||||||||||
tutorial_complete:false, | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
// varaibles set on init | ||||||||||||||||||||||||||||||
letWEBHOOK_URI:string|undefined | ||||||||||||||||||||||||||||||
exportconstsetupWebhook=(webhookConfig:TT.WebhookConfig)=>{ | ||||||||||||||||||||||||||||||
if(!webhookConfig.url){ | ||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
// set webhook uri | ||||||||||||||||||||||||||||||
WEBHOOK_URI=webhookConfig.url | ||||||||||||||||||||||||||||||
// set webhook event triggers | ||||||||||||||||||||||||||||||
constevents=webhookConfig.eventsasTT.WebhookConfigEvents | ||||||||||||||||||||||||||||||
for(consteventNameofObject.keys(events||{})){ | ||||||||||||||||||||||||||||||
WEBHOOK_EVENTS[eventName]=events[eventName] | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
constcallWebhookEndpoint=async<B>(bodyObject:B):Promise<void>=>{ | ||||||||||||||||||||||||||||||
if(!WEBHOOK_URI){ | ||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
constheaders={'Content-Type':'application/json'} | ||||||||||||||||||||||||||||||
// if the webhook token is specified as env var, sends a token with the request | ||||||||||||||||||||||||||||||
if(WEBHOOK_TOKEN){ | ||||||||||||||||||||||||||||||
headers['CodeRoad-User-Token']=WEBHOOK_TOKEN | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
constbody=JSON.stringify(bodyObject) | ||||||||||||||||||||||||||||||
try{ | ||||||||||||||||||||||||||||||
constsendEvent=awaitfetch(WEBHOOK_URI,{ | ||||||||||||||||||||||||||||||
method:'POST', | ||||||||||||||||||||||||||||||
headers, | ||||||||||||||||||||||||||||||
body, | ||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||
if(!sendEvent.ok){ | ||||||||||||||||||||||||||||||
thrownewError('Error sending event') | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
}catch(err:unknown){ | ||||||||||||||||||||||||||||||
logger(`Failed to call webhook endpoint${WEBHOOK_URI} with body${body}`) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
typeWebhookEventInit={ | ||||||||||||||||||||||||||||||
tutorialId:string | ||||||||||||||||||||||||||||||
coderoadVersion:string | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
exportconstonInit=(event:WebhookEventInit):void=>{ | ||||||||||||||||||||||||||||||
if(WEBHOOK_EVENTS.init){ | ||||||||||||||||||||||||||||||
callWebhookEndpoint<WebhookEventInit>(event) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
typeWebhookEventReset={ | ||||||||||||||||||||||||||||||
tutorialId:string | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
exportconstonReset=(event:WebhookEventReset):void=>{ | ||||||||||||||||||||||||||||||
if(WEBHOOK_EVENTS.reset){ | ||||||||||||||||||||||||||||||
callWebhookEndpoint<WebhookEventReset>(event) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
typeWebhookEventStepComplete={tutorialId:string;version:string;levelId:string;stepId:string} | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||||||||||||||||||||||||||
exportconstonStepComplete=(event:WebhookEventStepComplete):void=>{ | ||||||||||||||||||||||||||||||
if(WEBHOOK_EVENTS.step_complete){ | ||||||||||||||||||||||||||||||
callWebhookEndpoint<WebhookEventStepComplete>(event) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
typeWebhookEventLevelComplete={tutorialId:string;version:string;levelId:string} | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||||||||||||||||||||||||||
exportconstonLevelComplete=(event:WebhookEventLevelComplete):void=>{ | ||||||||||||||||||||||||||||||
if(WEBHOOK_EVENTS.level_complete){ | ||||||||||||||||||||||||||||||
callWebhookEndpoint<WebhookEventLevelComplete>(event) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
typeWebhookEevntTutorialComplete={tutorialId:string;version:string} | ||||||||||||||||||||||||||||||
exportconstonTutorialComplete=(event:WebhookEevntTutorialComplete):void=>{ | ||||||||||||||||||||||||||||||
if(WEBHOOK_EVENTS.tutorial_complete){ | ||||||||||||||||||||||||||||||
callWebhookEndpoint<WebhookEevntTutorialComplete>(event) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
Comment on lines +95 to +101 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Sounds good, I'll merge for now with the note that it may require more of a guarantee. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Pretty sure I was just having issues running the extension without the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. The published version has the @moT01 can you post an issue if you have any more details? |