@@ -4,83 +4,77 @@ import { FastifyInstance } from "fastify";
4
4
import { StatusCodes } from "http-status-codes" ;
5
5
import { insertWebhook } from "../../../db/webhooks/createWebhook" ;
6
6
import { WebhooksEventTypes } from "../../../schema/webhooks" ;
7
+ import { isLocalhost } from "../../../utils/url" ;
7
8
import { standardResponseSchema } from "../../schemas/sharedApiSchemas" ;
8
9
9
10
const uriFormat = TypeSystem . Format ( "uri" , ( input : string ) => {
11
+ // Assert valid URL.
10
12
try {
11
- if ( input . startsWith ( "http://localhost" ) ) return true ;
12
-
13
- const url = new URL ( input ) ;
14
-
15
- if ( url . protocol === "http:" ) {
16
- return false ;
17
- }
18
- return true ;
13
+ new URL ( input ) ;
19
14
} catch ( err ) {
20
15
return false ;
21
16
}
17
+
18
+ return ! isLocalhost ( input ) ;
22
19
} ) ;
23
20
24
21
const BodySchema = Type . Object ( {
25
22
url : Type . String ( {
26
- description : "URL to send the webhook to " ,
23
+ description : "Webhook URL " ,
27
24
format : uriFormat ,
28
- examples : [
29
- "http://localhost:3000/webhooks" ,
30
- "https://example.com/webhooks" ,
31
- ] ,
25
+ examples : [ "https://example.com/webhooks" ] ,
32
26
} ) ,
33
27
name : Type . Optional (
34
28
Type . String ( {
35
- minLength : 5 ,
29
+ minLength : 3 ,
36
30
} ) ,
37
31
) ,
38
32
eventType : Type . Enum ( WebhooksEventTypes ) ,
39
33
} ) ;
40
34
41
35
BodySchema . examples = [
42
36
{
43
- url : "http ://localhost:3000 /allTxUpdate" ,
37
+ url : "https ://example.com /allTxUpdate" ,
44
38
name : "All Transaction Events" ,
45
39
eventType : WebhooksEventTypes . ALL_TX ,
46
40
} ,
47
41
{
48
- url : "http ://localhost:3000 /queuedTx" ,
42
+ url : "https ://example.com /queuedTx" ,
49
43
name : "QueuedTx" ,
50
44
eventType : WebhooksEventTypes . QUEUED_TX ,
51
45
} ,
52
46
{
53
- url : "http ://localhost:3000 /retiredTx" ,
47
+ url : "https ://example.com /retiredTx" ,
54
48
name : "RetriedTx" ,
55
49
eventType : WebhooksEventTypes . RETRIED_TX ,
56
50
} ,
57
51
{
58
- url : "http ://localhost:3000 /sentTx" ,
52
+ url : "https ://example.com /sentTx" ,
59
53
name : "Sent Transaction Event" ,
60
54
eventType : WebhooksEventTypes . SENT_TX ,
61
55
} ,
62
56
{
63
- url : "http ://localhost:3000 /minedTx" ,
57
+ url : "https ://example.com /minedTx" ,
64
58
name : "Mined Transaction Event" ,
65
59
eventType : WebhooksEventTypes . MINED_TX ,
66
60
} ,
67
61
{
68
- url : "http ://localhost:3000 /erroredTx" ,
62
+ url : "https ://example.com /erroredTx" ,
69
63
name : "Errored Transaction Event" ,
70
64
eventType : WebhooksEventTypes . ERRORED_TX ,
71
65
} ,
72
66
{
73
- url : "http ://localhost:3000 /cancelledTx" ,
67
+ url : "https ://example.com /cancelledTx" ,
74
68
name : "Cancelled Transaction Event" ,
75
69
eventType : WebhooksEventTypes . CANCELLED_TX ,
76
70
} ,
77
71
{
78
- url : "http ://localhost:3000 /walletBalance" ,
72
+ url : "https ://example.com /walletBalance" ,
79
73
name : "Backend Wallet Balance Event" ,
80
74
eventType : WebhooksEventTypes . BACKEND_WALLET_BALANCE ,
81
75
} ,
82
76
{
83
- url : "http ://localhost:3000 /auth" ,
77
+ url : "https ://example.com /auth" ,
84
78
name : "Auth Check" ,
85
79
eventType : WebhooksEventTypes . AUTH ,
86
80
} ,
@@ -104,8 +98,9 @@ export async function createWebhook(fastify: FastifyInstance) {
104
98
method : "POST" ,
105
99
url : "/webhooks/create" ,
106
100
schema : {
107
- summary : "Create a new webhook" ,
108
- description : "Create a new webhook" ,
101
+ summary : "Create a webhook" ,
102
+ description :
103
+ "Create a webhook to call when certain blockchain events occur." ,
109
104
tags : [ "Webhooks" ] ,
110
105
operationId : "create" ,
111
106
body : BodySchema ,
0 commit comments