# Code Anti-Patterns Common mistakes in generated integration code. Each pattern shows what goes wrong, why it fails, and the correct approach. --- ## Config Pages ```typescript configPages: [ { name: "Configuration", elements: [ { key: "apiUrl", dataType: "string" }, { key: "shopifyConnection", dataType: "connection" }, ], }, ], ``` Plain objects are not valid ConfigPage elements. The platform rejects them at deploy time with type errors or silent misconfiguration. Wrapper functions add required metadata and type safety. ```typescript configPages: [ { name: "Configuration", elements: [ configVar("apiUrl", { dataType: "string", stableKey: "api-url" }), connectionConfigVar("shopifyConnection", "Shopify Connection", shopifyManifest.connections.shopifyOAuth), dataSourceConfigVar("customerId", "Customer", shopifyManifest.dataSources.selectCustomer), ], }, ], ``` ```typescript elements: [ connection({ key: "slack", label: "Slack" }), ] ``` Config pages use `connectionConfigVar()` to reference a connection defined in a component manifest — not the `connection()` constructor, which defines a new connection type. Using the wrong function causes deploy failures. ```typescript elements: [ connectionConfigVar("slackConnection", "Slack Connection", slackManifest.connections.slackOAuth), ] ``` --- ## Flow Callbacks ```typescript onInstanceDeploy: async (context) => { context.instanceState["webhookId"] = webhookId; } ``` `instanceState` is not available in lifecycle hooks. The platform throws a runtime error. Use `crossFlowState` instead — it's the only state store accessible during deploy/delete. ```typescript onInstanceDeploy: async (context) => { context.crossFlowState["webhookId"] = webhookId; } ``` ```typescript flow({ name: "Order Sync", onInstanceDeploy: async (context) => { /* register webhook */ }, onInstanceDelete: async (context) => { /* deregister webhook */ }, onExecution: async (context, params) => { const payload = params.onTrigger.results; // ... }, }) ``` When a flow has lifecycle hooks (onInstanceDeploy/onInstanceDelete), the platform requires an explicit `onTrigger` callback. Without it, the webhook payload is not forwarded to `onExecution` — `params.onTrigger.results` is empty. ```typescript flow({ name: "Order Sync", onTrigger: async (_context, payload) => ({ payload }), onInstanceDeploy: async (context) => { /* register webhook */ }, onInstanceDelete: async (context) => { /* deregister webhook */ }, onExecution: async (context, params) => { const payload = params.onTrigger.results; // ... }, }) ``` ```typescript flow<{ trigger: TriggerPayload; execute: ExecutionResult }>({ name: "Sync", onExecution: async (context: ExecutionContext, params: ExecutionParams) => { ... }, }) ``` The `flow()` function does not accept generic type parameters. Adding them causes TS2558. Adding type annotations to callback parameters causes TS2345 mismatches with Spectral's internal types. ```typescript flow({ name: "Sync", onExecution: async (context, params) => { ... }, }) ``` ```typescript flow({ name: "Webhook Handler", isSynchronous: true, retryConfig: { maxAttempts: 3, delayMinutes: 1 }, onExecution: async (context, params) => { ... }, }) ``` The platform rejects `retryConfig` on synchronous flows at deploy time. Synchronous flows return a response to the caller — they can't retry asynchronously. Remove `retryConfig` or set `isSynchronous: false`. ```typescript flow({ name: "Webhook Handler", isSynchronous: true, onExecution: async (context, params) => { ... }, }) ``` --- ## Imports and Types ```typescript import { flow } from "@prismatic-io/spectral/dist/types/IntegrationDefinition"; import { Connection } from "@prismatic-io/spectral/dist/types/Inputs"; ``` Internal paths are not part of the public API. They break on SDK version updates. Everything needed is exported from the root package. ```typescript import { flow, integration, configVar, connectionConfigVar } from "@prismatic-io/spectral"; ``` ```typescript const order = payload.body.data as ShopifyOrder; ``` TypeScript rejects direct casts from `Record` (TS2352). Webhook payloads and component results are generic types that need a two-step cast. ```typescript const order = payload.body.data as unknown as ShopifyOrder; ``` ```typescript const result = await slackManifest.actions.postMessage.perform(params as any); ``` `as any` silences real type errors that indicate incorrect usage. The resulting code compiles but fails at runtime. If a type doesn't match, the code structure is wrong — fix the structure, not the types. ```typescript const result = await slackManifest.actions.postMessage.perform(params); // If this fails, check that params matches the action's expected input shape ``` --- ## Component Usage ```typescript const result = await context.components.slack.postMessage({ channel, text }); ``` The `context.components..()` API does not exist in CNIs. Components are accessed through imported manifests. This pattern silently returns undefined. ```typescript import slackManifest from "./manifests/slack"; const result = await slackManifest.actions.postMessage.perform({ connection: context.configVars["slackConnection"], channel, text, }); ``` ```typescript // src/manifests/slack.ts (hand-written) export default { actions: { postMessage: { perform: async (params) => { ... } } }, connections: { slackOAuth: { key: "slack-oauth2" } }, }; ``` Manifests are auto-generated by `prismatic-tools install-manifest ` and contain the full typed interface for a component's actions, connections, and data sources. Hand-written manifests miss fields, have wrong types, and break when the component updates. ```bash prismatic-tools install-manifest slack # Generates src/manifests/slack.json with full type definitions ``` ```typescript import slackManifest from "./manifests/slack"; ``` --- ## Trigger Configuration ```typescript flow({ name: "Webhook Flow", webhookLifecycleHandlers: { create: async (context, webhookUrls) => { ... }, delete: async (context) => { ... }, }, }) ``` `webhookLifecycleHandlers` has been reported to cause "Invalid trigger configuration" on some platform versions. Use `onInstanceDeploy`/`onInstanceDelete` — they're the stable, documented lifecycle callbacks. ```typescript flow({ name: "Webhook Flow", onTrigger: async (_context, payload) => ({ payload }), onInstanceDeploy: async (context) => { const webhookUrl = context.webhookUrls["Webhook Flow"]; // register webhook with external API }, onInstanceDelete: async (context) => { // deregister webhook }, }) ``` ```typescript flow({ name: "Secure Webhook", endpointSecurityType: "organization", onExecution: async (context, params) => { ... }, }) ``` The platform rejects flows with `endpointSecurityType: "organization"` that don't provide `organizationApiKeys`. The security type declares that incoming requests must present an API key — but if no keys are defined, no request can ever pass validation. ```typescript flow({ name: "Secure Webhook", endpointSecurityType: "organization", organizationApiKeys: [configVar("apiKey", { dataType: "string", stableKey: "org-api-key" })], onExecution: async (context, params) => { ... }, }) ``` ```typescript flow({ name: "Sync Orders", batchConfig: { batchSize: 10, concurrentBatchLimit: 5 }, onExecution: async (context, params) => { ... }, }) ``` `batchConfig` and the batched `trigger` are a coupled pair. The trigger returns the `{ items, paginationState? }` the platform chunks into batches, so `batchConfig` without it has nothing to batch — the flow fails to typecheck and to build. ```typescript flow({ name: "Sync Orders", schedule: { value: "*/15 * * * *" }, batchConfig: { batchSize: 10, concurrentBatchLimit: 5 }, trigger: batchFlowTrigger({ onTrigger: async (context, payload) => { const page = await fetchOrders(payload.paginationState?.cursor); return { items: page.orders, paginationState: page.nextCursor ? { cursor: page.nextCursor } : null }; }, }), onExecution: async (context, params) => { ... }, }) ``` ```typescript flow({ batchConfig: { batchSize: 10, concurrentBatchLimit: 5 }, trigger: batchFlowTrigger({ onTrigger: async () => ({ items: [] }) }), onTrigger: async (context, payload) => ({ payload }), // ❌ onExecution: async () => ({ data: null }), }) ``` On a batched flow the fire lives inside `trigger`, so the flat `onTrigger`/`onDeployTrigger` are typed `never`. Fetch logic belongs in the batched trigger's `onTrigger`, and a deploy-time backfill in its `onDeploy`. ```typescript flow({ batchConfig: { batchSize: 10, concurrentBatchLimit: 5 }, trigger: batchFlowTrigger({ onTrigger: async (context, payload) => ({ items: await fetchOrders() }), }), onExecution: async () => ({ data: null }), }) ``` ```typescript batchConfig: { batchSize: 1 } ``` Omitted, a fire's batches dispatch with unlimited parallelism — a large fetch such as an initial sync produces hundreds of batches that take the tenant's execution slots from its other flows. Bound it to what the destination absorbs. ```typescript batchConfig: { batchSize: 1, concurrentBatchLimit: 5 } ``` ```typescript onExecution: async (context, params) => { const records = params.onTrigger.results.body.data as Order[]; for (let i = 0; i < records.length; i += 50) { await bulkInsert(records.slice(i, i + 50)); // ❌ one execution does all the work } return { data: null }; } ``` Chunking inside `onExecution` keeps every chunk in one execution: one timeout, one retry, and a failure that loses the whole run's progress. `batchConfig` gives each chunk its own execution with independent retries and isolated failures. ```typescript batchConfig: { batchSize: 50, concurrentBatchLimit: 5 }, trigger: batchFlowTrigger({ onTrigger: fetchPage }), onExecution: async (context, params) => { const records = params.onTrigger.results.body.data as Order[]; // one 50-record slice await bulkInsert(records); return { data: { count: records.length } }; } ```