@@ -15,7 +15,6 @@ import {
15
15
RegisterReturn ,
16
16
WebhookRegistryEntry ,
17
17
WebhookCheckResponse ,
18
- WebhookCheckResponseLegacy ,
19
18
ShortenedRegisterOptions ,
20
19
} from './types' ;
21
20
@@ -113,35 +112,20 @@ function isSuccess(
113
112
) ;
114
113
}
115
114
116
- // 2020-07 onwards
117
- function versionSupportsEndpointField ( ) {
118
- return ShopifyUtilities . versionCompatible ( ApiVersion . July20 ) ;
119
- }
120
-
121
115
function versionSupportsPubSub ( ) {
122
116
return ShopifyUtilities . versionCompatible ( ApiVersion . July21 ) ;
123
117
}
124
118
125
119
function validateDeliveryMethod ( deliveryMethod : DeliveryMethod ) {
126
- if (
127
- deliveryMethod === DeliveryMethod . EventBridge &&
128
- ! versionSupportsEndpointField ( )
129
- ) {
130
- throw new ShopifyErrors . UnsupportedClientType (
131
- `EventBridge webhooks are not supported in API version "${ Context . API_VERSION } ".` ,
132
- ) ;
133
- } else if (
134
- deliveryMethod === DeliveryMethod . PubSub &&
135
- ! versionSupportsPubSub ( )
136
- ) {
120
+ if ( deliveryMethod === DeliveryMethod . PubSub && ! versionSupportsPubSub ( ) ) {
137
121
throw new ShopifyErrors . UnsupportedClientType (
138
122
`Pub/Sub webhooks are not supported in API version "${ Context . API_VERSION } ".` ,
139
123
) ;
140
124
}
141
125
}
142
126
143
127
function buildCheckQuery ( topic : string ) : string {
144
- const query = `{
128
+ return `{
145
129
webhookSubscriptions(first: 1, topics: ${ topic } ) {
146
130
edges {
147
131
node {
@@ -167,19 +151,6 @@ function buildCheckQuery(topic: string): string {
167
151
}
168
152
}
169
153
}` ;
170
-
171
- const legacyQuery = `{
172
- webhookSubscriptions(first: 1, topics: ${ topic } ) {
173
- edges {
174
- node {
175
- id
176
- callbackUrl
177
- }
178
- }
179
- }
180
- }` ;
181
-
182
- return versionSupportsEndpointField ( ) ? query : legacyQuery ;
183
154
}
184
155
185
156
function buildQuery (
@@ -243,7 +214,10 @@ function buildQuery(
243
214
const WebhooksRegistry : RegistryInterface = {
244
215
webhookRegistry : { } ,
245
216
246
- addHandler ( topic : string , { path, webhookHandler} : WebhookRegistryEntry ) : void {
217
+ addHandler (
218
+ topic : string ,
219
+ { path, webhookHandler} : WebhookRegistryEntry ,
220
+ ) : void {
247
221
WebhooksRegistry . webhookRegistry [ topic ] = { path, webhookHandler} ;
248
222
} ,
249
223
@@ -256,7 +230,7 @@ const WebhooksRegistry: RegistryInterface = {
256
230
} ,
257
231
258
232
getHandler ( topic : string ) : WebhookRegistryEntry | null {
259
- return topic in WebhooksRegistry . webhookRegistry ? WebhooksRegistry . webhookRegistry [ topic ] : null ;
233
+ return WebhooksRegistry . webhookRegistry [ topic ] ?? null ;
260
234
} ,
261
235
262
236
getTopics ( ) : string [ ] {
@@ -279,21 +253,18 @@ const WebhooksRegistry: RegistryInterface = {
279
253
: path ;
280
254
const checkResult = ( await client . query ( {
281
255
data : buildCheckQuery ( topic ) ,
282
- } ) ) as { body : WebhookCheckResponse | WebhookCheckResponseLegacy ; } ;
256
+ } ) ) as { body : WebhookCheckResponse ; } ;
283
257
let webhookId : string | undefined ;
284
258
let mustRegister = true ;
285
259
if ( checkResult . body . data . webhookSubscriptions . edges . length ) {
286
260
const { node} = checkResult . body . data . webhookSubscriptions . edges [ 0 ] ;
287
261
let endpointAddress = '' ;
288
- if ( 'endpoint' in node ) {
289
- if ( node . endpoint . __typename === 'WebhookHttpEndpoint' ) {
290
- endpointAddress = node . endpoint . callbackUrl ;
291
- } else if ( node . endpoint . __typename === 'WebhookEventBridgeEndpoint' ) {
292
- endpointAddress = node . endpoint . arn ;
293
- }
294
- } else {
295
- endpointAddress = node . callbackUrl ;
262
+ if ( node . endpoint . __typename === 'WebhookHttpEndpoint' ) {
263
+ endpointAddress = node . endpoint . callbackUrl ;
264
+ } else if ( node . endpoint . __typename === 'WebhookEventBridgeEndpoint' ) {
265
+ endpointAddress = node . endpoint . arn ;
296
266
}
267
+
297
268
webhookId = node . id ;
298
269
if ( endpointAddress === address ) {
299
270
mustRegister = false ;
@@ -336,7 +307,7 @@ const WebhooksRegistry: RegistryInterface = {
336
307
shop,
337
308
deliveryMethod,
338
309
} ;
339
- const returnedRegister : RegisterReturn = await WebhooksRegistry . register ( webhook ) ;
310
+ const returnedRegister = await WebhooksRegistry . register ( webhook ) ;
340
311
registerReturn = { ...registerReturn , ...returnedRegister } ;
341
312
}
342
313
}
@@ -414,7 +385,9 @@ const WebhooksRegistry: RegistryInterface = {
414
385
. digest ( 'base64' ) ;
415
386
416
387
if ( ShopifyUtilities . safeCompare ( generatedHash , hmac as string ) ) {
417
- const graphqlTopic = ( topic as string ) . toUpperCase ( ) . replace ( / \/ / g, '_' ) ;
388
+ const graphqlTopic = ( topic as string )
389
+ . toUpperCase ( )
390
+ . replace ( / \/ / g, '_' ) ;
418
391
const webhookEntry = WebhooksRegistry . getHandler ( graphqlTopic ) ;
419
392
420
393
if ( webhookEntry ) {
0 commit comments