Skip to content

Commit

Permalink
Add support for Metafields to webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
rsperko committed Jan 9, 2025
1 parent ed55fc7 commit 4be2a60
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/app/src/cli/models/app/app.test-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ export async function testSingleWebhookSubscriptionExtension({
topic,
api_version: '2024-01',
uri: 'https://my-app.com/webhooks',
metafields: [{namespace: 'custom', key: 'test'}],
},
}: {
emptyConfig?: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ describe('webhooks', () => {
{
topics: ['orders/create'],
uri: 'https://example.com/webhooks/orders',
metafields: [{namespace: 'custom', key: 'test'}],
},
{
topics: ['products/create'],
uri: 'https://example.com/webhooks/products',
metafields: [{namespace: 'custom', key: 'test'}],
},
],
},
Expand All @@ -41,10 +43,12 @@ describe('webhooks', () => {
{
topic: 'orders/create',
uri: 'https://example.com/webhooks/orders',
metafields: [{namespace: 'custom', key: 'test'}],
},
{
topic: 'products/create',
uri: 'https://example.com/webhooks/products',
metafields: [{namespace: 'custom', key: 'test'}],
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ export const WebhookSubscriptionSchema = zod.object({
}),
include_fields: zod.array(zod.string({invalid_type_error: 'Value must be a string'})).optional(),
filter: zod.string({invalid_type_error: 'Value must be a string'}).optional(),
metafields: zod
.array(
zod.object({
namespace: zod.string({invalid_type_error: 'Metafield namespace must be a string'}),
key: zod.string({invalid_type_error: 'Metafield key must be a string'}),
}),
{invalid_type_error: 'Metafields must be an array of objects with namespace and key'},
)
.optional(),
compliance_topics: zod
.array(
zod.enum([ComplianceTopic.CustomersRedact, ComplianceTopic.CustomersDataRequest, ComplianceTopic.ShopRedact]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ interface TransformedWebhookSubscription {
compliance_topics?: string[]
include_fields?: string[]
filter?: string
metafields?: {
namespace: string
key: string
}[]
}

export const SingleWebhookSubscriptionSchema = zod.object({
Expand All @@ -23,6 +27,15 @@ export const SingleWebhookSubscriptionSchema = zod.object({
}),
include_fields: zod.array(zod.string({invalid_type_error: 'Value must be a string'})).optional(),
filter: zod.string({invalid_type_error: 'Value must be a string'}).optional(),
metafields: zod
.array(
zod.object({
namespace: zod.string({invalid_type_error: 'Metafield namespace must be a string'}),
key: zod.string({invalid_type_error: 'Metafield key must be a string'}),
}),
{invalid_type_error: 'Metafields must be an array of objects with namespace and key'},
)
.optional(),
})

/* this transforms webhooks remotely to be accepted by the TOML
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export interface WebhookSubscription {
compliance_topics?: string[]
include_fields?: string[]
filter?: string
metafields?: {
namespace: string
key: string
}[]
}

interface PrivacyComplianceConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ export function webhookValidator(schema: object, ctx: zod.RefinementCtx) {
}

function validateSubscriptions(webhookConfig: WebhooksConfig) {
const {subscriptions = []} = webhookConfig
// eslint-disable-next-line @typescript-eslint/naming-convention
const {subscriptions = [], api_version} = webhookConfig

const hasMetafields = subscriptions.some((sub) => sub.metafields && sub.metafields.length > 0)
if (hasMetafields && !isVersionGreaterOrEqual(api_version, '2025-04')) {
return {
code: zod.ZodIssueCode.custom,
message: 'Webhook metafields are only supported in API version 2025-04 or later, or with version "unstable"',
path: ['api_version'],
}
}

const uniqueSubscriptionSet = new Set()
const duplicatedSubscriptionsFields: string[] = []

Expand Down Expand Up @@ -76,3 +87,10 @@ function validateSubscriptions(webhookConfig: WebhooksConfig) {
}
}
}

function isVersionGreaterOrEqual(version: string, minVersion: string): boolean {
if (version === 'unstable') return true
const [versionYear = 0, versionMonth = 0] = version.split('-').map(Number)
const [minYear = 0, minMonth = 0] = minVersion.split('-').map(Number)
return versionYear > minYear || (versionYear === minYear && versionMonth >= minMonth)
}

0 comments on commit 4be2a60

Please sign in to comment.