Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9688f14

Browse files
committed
webhook demo
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parente4ab477 commit9688f14

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

‎src/services/hooks/index.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ import runCommands from './utils/runCommands'
77
importrunVSCodeCommandsfrom'./utils/runVSCodeCommands'
88
import*astelemetryfrom'../telemetry'
99
import{runTest}from'../../actions/onTest'
10-
importloggerfrom'../logger'
1110
import{VERSION}from'../../environment'
11+
import*aswebhooksfrom'./webhooks'
1212

1313
// run at the end of when a tutorial is configured
1414
exportconstonInit=async(actions:TT.StepActions):Promise<void>=>{
1515
awaitloadCommits(actions?.commits)
1616
awaitrunCommands(actions?.commands)
1717
awaitrunVSCodeCommands(actions?.vscodeCommands)
18+
webhooks.onInit({
19+
// tutorialId,
20+
version:VERSION,
21+
})
1822
}
1923

2024
// run when a level starts
@@ -47,6 +51,10 @@ export const onReset = async (actions: TT.StepActions): Promise<void> => {
4751
awaitresetWatchers()
4852
awaitrunCommands(actions?.commands)
4953
awaitrunVSCodeCommands(actions?.vscodeCommands)
54+
webhooks.onReset({
55+
// tutorialId,
56+
version:VERSION,
57+
})
5058
}
5159

5260
// run when an uncaught exception is thrown
@@ -66,6 +74,12 @@ export const onStepComplete = async ({
6674
}):Promise<void>=>{
6775
git.saveCommit(`Save progress:${stepId}`)
6876
telemetry.onEvent('step_complete',{ tutorialId, stepId, levelId,version:VERSION})
77+
webhooks.onStepComplete({
78+
tutorialId,
79+
version:VERSION,
80+
levelId,
81+
stepId,
82+
})
6983
}
7084

7185
// run when a level is complete (all tasks pass or no tasks)
@@ -77,9 +91,18 @@ export const onLevelComplete = async ({
7791
levelId:string
7892
}):Promise<void>=>{
7993
telemetry.onEvent('level_complete',{ tutorialId, levelId,version:VERSION})
94+
webhooks.onLevelComplete({
95+
tutorialId,
96+
version:VERSION,
97+
levelId,
98+
})
8099
}
81100

82101
// run when all levels are complete
83102
exportconstonTutorialComplete=async({ tutorialId}:{tutorialId:string}):Promise<void>=>{
84103
telemetry.onEvent('tutorial_complete',{ tutorialId,version:VERSION})
104+
webhooks.onTutorialComplete({
105+
tutorialId,
106+
version:VERSION,
107+
})
85108
}

‎src/services/hooks/webhooks.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
importfetchfrom'node-fetch'
2+
importloggerfrom'../logger'
3+
4+
constWEBHOOKS={
5+
init:true,
6+
reset:true,
7+
step_complete:true,
8+
level_complete:true,
9+
tutorial_complete:true,
10+
}
11+
12+
constcallWebhookEndpoint=async<B>(bodyObject:B):Promise<void>=>{
13+
constendpoint='http://localhost:3000'
14+
constbody=JSON.stringify(bodyObject)
15+
try{
16+
constsendEvent=awaitfetch(endpoint,{
17+
method:'POST',
18+
headers:{'Content-Type':'application/json'},
19+
body,
20+
})
21+
if(!sendEvent.ok){
22+
thrownewError('Error sending event')
23+
}
24+
}catch(err:unknown){
25+
logger(`Failed to call webhook endpoint${endpoint} with body${body}`)
26+
}
27+
}
28+
29+
typeInitEvent={
30+
// tutorialId: string;
31+
version:string
32+
}
33+
34+
exportconstonInit=(event:InitEvent):void=>{
35+
if(WEBHOOKS.init){
36+
callWebhookEndpoint<InitEvent>(event)
37+
}
38+
}
39+
40+
typeResetEvent={
41+
// tutorialId: string;
42+
version:string
43+
}
44+
45+
exportconstonReset=(event:ResetEvent):void=>{
46+
if(WEBHOOKS.reset){
47+
callWebhookEndpoint<ResetEvent>(event)
48+
}
49+
}
50+
51+
typeStepCompleteEvent={tutorialId:string;version:string;levelId:string;stepId:string}
52+
53+
exportconstonStepComplete=(event:StepCompleteEvent):void=>{
54+
if(WEBHOOKS.step_complete){
55+
callWebhookEndpoint<StepCompleteEvent>(event)
56+
}
57+
}
58+
59+
typeLevelCompleteEvent={tutorialId:string;version:string;levelId:string}
60+
61+
exportconstonLevelComplete=(event:LevelCompleteEvent):void=>{
62+
if(WEBHOOKS.level_complete){
63+
callWebhookEndpoint<LevelCompleteEvent>(event)
64+
}
65+
}
66+
67+
typeTutorialCompleteEvent={tutorialId:string;version:string}
68+
69+
exportconstonTutorialComplete=(event:TutorialCompleteEvent):void=>{
70+
if(WEBHOOKS.tutorial_complete){
71+
callWebhookEndpoint<TutorialCompleteEvent>(event)
72+
}
73+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp