-
Notifications
You must be signed in to change notification settings - Fork 81
/
typescript-validate.ts
205 lines (170 loc) · 5.5 KB
/
typescript-validate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import {
Webhooks,
createEventHandler,
createMiddleware,
createWebhooksApi,
sign,
verify,
EmitterWebhookEvent,
WebhookError,
} from "../src/index";
import { createServer } from "http";
import { HandlerFunction, EmitterWebhookEventName } from "../src/types";
// ************************************************************
// THIS CODE IS NOT EXECUTED. IT IS FOR TYPECHECKING ONLY
// ************************************************************
const fn = (webhookEvent: EmitterWebhookEvent) => {
// @ts-expect-error TS2367:
// This condition will always return 'false' since the types '"check_run" | ... many more ... | "workflow_run"' and '"check_run.completed"' have no overlap.
if (webhookEvent.name === "check_run.completed") {
//
}
};
declare const on: <E extends EmitterWebhookEventName>(
name: E | E[],
callback: HandlerFunction<E, unknown>
) => void;
on(["check_run.completed", "code_scanning_alert.fixed"], (event) => {
if (event.payload.action === "fixed") {
console.log("an alert was fixed!");
}
if (event.payload.action === "completed") {
console.log("a run was completed!");
}
});
on("code_scanning_alert.fixed", (event) => {
if (event.payload.action === "fixed") {
console.log("an alert was fixed!");
}
// @ts-expect-error TS2367:
// This condition will always return 'false' since the types '"fixed"' and '"completed"' have no overlap.
if (event.payload.action === "completed") {
console.log("a run was completed!");
}
// @ts-expect-error TS2367:
// This condition will always return 'false' since the types '"fixed"' and '"completed"' have no overlap.
if (event.payload.action === "random-string") {
console.log("a run was completed!");
}
fn(event);
});
export default async function () {
// @ts-expect-error TS2554:
// Expected 1 arguments, but got 0.
new Webhooks();
// Check that all options are optional except for secret
new Webhooks({
secret: "blah",
});
// Check all supported options
const webhooks = new Webhooks({
secret: "blah",
path: "/webhooks",
});
// Check named exports of new API work
createWebhooksApi({
secret: "blah",
});
createEventHandler({ secret: "blah" });
createMiddleware({
secret: "mysecret",
path: "/github-webhooks",
});
sign("randomSecret", {});
sign({ secret: "randomSecret" }, {});
sign({ secret: "randomSecret", algorithm: "sha1" }, {});
sign({ secret: "randomSecret", algorithm: "sha256" }, {});
verify("randomSecret", {}, "randomSignature");
webhooks.onAny(({ id, name, payload }) => {
console.log(name, "event received", id);
const sig = webhooks.sign(payload);
webhooks.verify(payload, sig);
});
webhooks.on("check_run.completed", () => {});
webhooks.on(
["check_run.completed", "commit_comment", "label"],
({ name, payload }) => {
console.log(name);
if ("check_run" in payload) {
console.log(payload.check_run.output.title);
}
if ("comment" in payload) {
console.log(payload.comment.user.login);
}
}
);
webhooks.on(["check_run.completed", "check_run.created"], () => {});
webhooks.on("check_run.created", ({ name, payload }) => {
console.log(payload.check_run.conclusion, name);
});
webhooks.on("check_run.created", () => {
return 2;
});
webhooks.on("check_run.created", () => {
return Promise.resolve(10);
});
webhooks.removeListener("check_run.created", ({ name, payload }) => {
console.log(payload.check_run.conclusion, name);
});
webhooks.removeListener(["commit_comment", "label"], ({ name, payload }) => {
console.log(name);
if ("label" in payload) {
console.log(payload.label.name);
} else {
console.log(payload.comment.body);
}
});
webhooks.on(["check_run.completed", "code_scanning_alert.fixed"], (event) => {
if (event.name === "check_run") {
console.log(`a run was ${event.payload.action}!`);
if (event.payload.action === "completed") {
console.log("it was the completed action, obviously!");
}
// @ts-expect-error TS2367:
// This condition will always return 'false' since the types '"completed"' and '"created"' have no overlap.
if (event.payload.action === "created") {
//
}
}
// @ts-expect-error TS2367:
// This condition will always return 'false' since the types '"check_run" | "code_scanning_alert"' and '"repository"' have no overlap.
if (event.name === "repository") {
//
}
});
webhooks.on("issues", (event) => {
console.log(event.payload.issue);
});
}
{
const webhooks = new Webhooks({
secret: "blah",
path: "/webhooks",
transform: (event) => {
console.log(event.payload);
return Object.assign(event, { foo: "bar" });
},
});
webhooks.on("issues", (event) => {
// foo is set by options.transform
console.log(event.foo);
});
// @ts-expect-error TS2345:
// Argument of type '"does_not_exist"' is not assignable to parameter of type ...
webhooks.on("does_not_exist", (what) => {
// this works because we pass a transform that adds foo to all events
console.log(what.foo);
});
webhooks.onError((error) => {
console.log(error.event.name);
const [firstError] = Array.from(error);
console.log(firstError.status);
console.log(firstError.headers);
console.log(firstError.request);
});
createServer(webhooks.middleware).listen(3000);
}
export function webhookErrorTest(error: WebhookError) {
const { request } = error;
console.log(request);
}