@automate.ax/integration-contracts 0.144.1 → 0.145.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,75 @@
1
+ /* oxlint-disable typescript/no-unsafe-type-assertion -- Generated schema names index a frozen external contract. */
2
+ import { Validator } from "@cfworker/json-schema"
3
+ import type { Encodable } from "@automate.ax/codec"
4
+ import * as z from "zod"
5
+ import schemaData from "./openapi-schemas.generated.json" with { type: "json" }
6
+ import type { components } from "./openapi-types.generated"
7
+
8
+ type OpenApiSchemas = components["schemas"]
9
+
10
+ export type CalcomSchemaName = keyof OpenApiSchemas
11
+
12
+ type CalcomEncodable<TValue> = unknown extends TValue
13
+ ? Encodable
14
+ : TValue extends null | undefined | boolean | number | string
15
+ ? TValue
16
+ : TValue extends readonly (infer TItem)[]
17
+ ? CalcomEncodable<TItem>[]
18
+ : TValue extends object
19
+ ? keyof TValue extends never
20
+ ? Record<string, Encodable>
21
+ : { [TKey in keyof TValue]: CalcomEncodable<TValue[TKey]> }
22
+ : never
23
+
24
+ /** Public JSON-safe value for one generated Cal.com component schema. */
25
+ export type CalcomSchemaValue<TName extends CalcomSchemaName> = CalcomEncodable<
26
+ OpenApiSchemas[TName]
27
+ >
28
+
29
+ /** Data carried by one successful Cal.com response envelope. */
30
+ export type CalcomResponseData<TName extends CalcomSchemaName> =
31
+ CalcomSchemaValue<TName> extends { data: infer TData } ? TData : never
32
+
33
+ const VALIDATOR = new Validator(
34
+ {
35
+ $defs: schemaData.definitions,
36
+ $schema: "https://json-schema.org/draft/2020-12/schema",
37
+ additionalProperties: false,
38
+ maxProperties: 1,
39
+ minProperties: 1,
40
+ properties: Object.fromEntries(
41
+ Object.keys(schemaData.definitions).map((name) => [
42
+ name,
43
+ { $ref: `#/$defs/${name}` },
44
+ ]),
45
+ ),
46
+ type: "object",
47
+ },
48
+ "2020-12",
49
+ )
50
+
51
+ /**
52
+ * Validates one value against a frozen Cal.com OpenAPI component schema.
53
+ *
54
+ * @param name OpenAPI component schema name.
55
+ */
56
+ export function calcomSchema<TName extends CalcomSchemaName>(
57
+ name: TName,
58
+ ): z.ZodType<CalcomSchemaValue<TName>> {
59
+ return z.custom<CalcomSchemaValue<TName>>(
60
+ (value) => VALIDATOR.validate({ [name]: value }).valid,
61
+ )
62
+ }
63
+
64
+ /**
65
+ * Validates data after it has been removed from a Cal.com success envelope.
66
+ *
67
+ * @param name OpenAPI success-envelope component schema name.
68
+ */
69
+ export function calcomResponseDataSchema<TName extends CalcomSchemaName>(
70
+ name: TName,
71
+ ): z.ZodType<CalcomResponseData<TName>> {
72
+ return z.custom<CalcomResponseData<TName>>(
73
+ (data) => VALIDATOR.validate({ [name]: { data, status: "success" } }).valid,
74
+ )
75
+ }
@@ -44,10 +44,7 @@ export type GoogleAdsEventType = keyof typeof googleAdsTriggerContracts
44
44
  export function getGoogleAdsChangeEventTypes(
45
45
  event: z.output<typeof GOOGLE_ADS_CHANGE_EVENT_PAYLOAD_SCHEMA>,
46
46
  ): GoogleAdsEventType[] {
47
- const semanticType = getSemanticEventType(
48
- event.event.changeResourceType,
49
- event.event.resourceChangeOperation,
50
- )
47
+ const semanticType = getSemanticEventType(event.event)
51
48
  return semanticType
52
49
  ? ["googleAds.change", semanticType]
53
50
  : ["googleAds.change"]
@@ -56,19 +53,20 @@ export function getGoogleAdsChangeEventTypes(
56
53
  /**
57
54
  * Maps provider resource and operation enums to public event types.
58
55
  *
59
- * @param resource - Provider change resource enum.
60
- * @param operation - Provider mutation operation.
56
+ * Google reports status-based removal of campaigns, ad groups, and ads as an
57
+ * update. Treat the terminal status as removal so the public event matches the
58
+ * user-visible mutation.
59
+ *
60
+ * @param event - Normalized provider change event.
61
61
  */
62
62
  function getSemanticEventType(
63
- resource: z.output<
64
- typeof GOOGLE_ADS_CHANGE_EVENT_PAYLOAD_SCHEMA
65
- >["event"]["changeResourceType"],
66
- operation: z.output<
67
- typeof GOOGLE_ADS_CHANGE_EVENT_PAYLOAD_SCHEMA
68
- >["event"]["resourceChangeOperation"],
63
+ event: z.output<typeof GOOGLE_ADS_CHANGE_EVENT_PAYLOAD_SCHEMA>["event"],
69
64
  ): GoogleAdsEventType | undefined {
65
+ const operation = isStatusRemoval(event)
66
+ ? "REMOVE"
67
+ : event.resourceChangeOperation
70
68
  if (operation === "UNSPECIFIED" || operation === "UNKNOWN") return undefined
71
- switch (resource) {
69
+ switch (event.changeResourceType) {
72
70
  case "AD_GROUP":
73
71
  return (
74
72
  {
@@ -129,3 +127,35 @@ function getSemanticEventType(
129
127
  return undefined
130
128
  }
131
129
  }
130
+
131
+ /**
132
+ * Returns whether Google encoded a status-based resource removal as update.
133
+ *
134
+ * @param event - Normalized provider change event.
135
+ */
136
+ function isStatusRemoval(
137
+ event: z.output<typeof GOOGLE_ADS_CHANGE_EVENT_PAYLOAD_SCHEMA>["event"],
138
+ ) {
139
+ if (event.resourceChangeOperation !== "UPDATE") return false
140
+ let resourceKey: "adGroup" | "adGroupAd" | "campaign"
141
+ switch (event.changeResourceType) {
142
+ case "AD_GROUP":
143
+ resourceKey = "adGroup"
144
+ break
145
+ case "AD_GROUP_AD":
146
+ resourceKey = "adGroupAd"
147
+ break
148
+ case "CAMPAIGN":
149
+ resourceKey = "campaign"
150
+ break
151
+ default:
152
+ return false
153
+ }
154
+ const resource = event.newResource?.[resourceKey]
155
+ return (
156
+ typeof resource === "object" &&
157
+ resource !== null &&
158
+ !Array.isArray(resource) &&
159
+ resource.status === "REMOVED"
160
+ )
161
+ }
package/src/triggers.ts CHANGED
@@ -5,6 +5,7 @@ import type { axiomTriggerContracts } from "./axiom"
5
5
  import type { automateTriggerContracts } from "./automate"
6
6
  import type { asanaTriggerContracts } from "./asana"
7
7
  import type { brevoTriggerContracts } from "./brevo"
8
+ import type { calcomTriggerContracts } from "./calcom"
8
9
  import type { convexTriggerContracts } from "./convex"
9
10
  import type { discordTriggerContracts } from "./discord"
10
11
  import type { closeTriggerContracts } from "./close"
@@ -43,6 +44,7 @@ export type TriggerContractMap = typeof airtableTriggerContracts &
43
44
  typeof automateTriggerContracts &
44
45
  typeof asanaTriggerContracts &
45
46
  typeof brevoTriggerContracts &
47
+ typeof calcomTriggerContracts &
46
48
  typeof convexTriggerContracts &
47
49
  typeof discordTriggerContracts &
48
50
  typeof closeTriggerContracts &