# Code Generation Patterns — Integration Builder Load this reference at the start of the code generation phase. ## Required Files | File | Must contain | |------|-------------| | `src/componentRegistry.ts` | Import from manifests, export `componentManifests()` array | | `src/configPages.ts` | Use `configVar()`, `connectionConfigVar()`, `dataSourceConfigVar()` wrappers — not plain objects | | `src/flows.ts` or `src/flows/index.ts` | `onExecution` with config access via `context.configVars`. Multi-flow uses directory with barrel export. | | `src/index.ts` | Export `integration()` with display, flows, configPages, componentRegistry | | `src/documentation.md` | Document all config variables, connections, flow logic | | `.spectral/flows//payloads/sample-payload.json` | Test payload in VS Code extension format: `{ headers, data, contentType }` | The `code-plan` output includes a `` block for each connector Follow the pattern it specifies — customerActivatedConnection, manifest_helper, connectionConfigVar_inline, or organizationActivatedConnection Guess which connection pattern to use — the code-plan determines it based on whether an SCV exists `customerActivatedConnection()` and `organizationActivatedConnection()` require a pre-existing SCV If no SCV exists, use the manifest helper pattern (component exists) or connectionConfigVar (no component) Using customerActivatedConnection() with a stableKey that doesn't match an existing SCV — deploy will fail `organizationActivatedConnection()` goes in `scopedConfigVars` on integration() — NOT in configPages Access in onExecution requires a typed cast since scopedConfigVars aren't in the ConfigVars type Check `src/manifests//triggers/` for a built-in trigger first If one exists, use it as `onTrigger` — it handles HMAC validation and webhook lifecycle automatically Import: `import { triggerName } from "./manifests//triggers/"` Skip `onTrigger` entirely — extract data in `onExecution` via `params.onTrigger.results` `onTrigger: async (_context, payload) => ({ payload })` passthrough Lifecycle handlers require onTrigger to exist The component trigger's `webhookLifecycleHandlers.create/.delete` handle registration automatically These also fire in listening mode (test runner), which `onInstanceDeploy`/`onInstanceDelete` do not Use `webhookLifecycleHandlers: { create: async (context, params) => {...}, delete: async (context, params) => {...} }` on the flow Use `crossFlowState` (not `instanceState`) in lifecycle handlers Handlers must be idempotent Use `onInstanceDeploy`/`onInstanceDelete` for non-webhook resources (folders, record types) Import actions from manifest and call `.perform()` Using `context.components..()` — not available in integrations Check the manifest's `examplePayload` for the action before assuming the response type If `examplePayload` is missing, cast as `unknown` and add `logger.info(JSON.stringify(result))` to verify shape `flow({...})` without generics Add type annotations to callback parameters Using `instanceState` in `onInstanceDeploy`/`onInstanceDelete` Use `crossFlowState` in lifecycle handlers State is written in its entirety — NOT concurrency-safe For record ID mapping between systems, prefer the destination system's externalId field over state Avoids race conditions and survives failed executions Import only from `@prismatic-io/spectral` Import from internal paths like `@prismatic-io/spectral/dist/...` QueueConfig uses flat shape: `usesFifoQueue`, `concurrencyLimit`, `singletonExecutions`, `dedupeIdField` `as unknown as MyType` for payloads, `as Record` for component results Generating `src/client.ts` for integrations that use component manifests All HTTP calls go through the component's `.perform()` method `client.ts` is a component-only pattern — CNIs use manifest actions or direct SDK clients `iconPath` in `integration()` must be `"icon.png"` Use `"assets/icon.png"` — webpack copies assets/ contents to dist/ root, so the built path is just `icon.png` Generate `.spectral/trigger-config.json` during code gen for webhook flows Format: `{ "flows": { "": { "payload": ".spectral/flows//payloads/sample-payload.json" } } }` `PollingFlow.onTrigger` accepts ONLY an inline `PollingTriggerPerformFunction` Using a component trigger reference (`TriggerReference`) as `onTrigger` on a polling flow The types are incompatible — `PollingFlow` requires inline function, not `TriggerReference` Polling flow's `onTrigger` must be an inline function using `context.polling.getState()`/`setState()` Component actions remain available in `onExecution` even in polling flows `batchConfig` and a `trigger` built with `batchFlowTrigger` appear together — one without the other fails to build A flat `onTrigger`/`onDeployTrigger` on a batched flow — the fire lives inside `trigger` `batchSize`: integer >= 1 (1 = one execution per item; > 1 = a slice per execution) Set `concurrentBatchLimit` (integer >= 1) every time — omitted means unlimited parallelism An unbounded fire can take the tenant's concurrent execution slots from other flows and instances Return `paginationState` to fetch the next page; `null`/omit ends the fire. Read the cursor from `payload.paginationState` Looping the source's pages inside one fire — return the cursor and let the platform re-invoke Treat it as intra-fire paging, NOT a cross-run watermark — "only new since last run" is the flow's own query or state Add `onDeploy` only when the backfill reads a different source/query than steady state, or for a webhook flow (no fetch of its own to backfill from) Adding `onDeploy` for an initial sync that reads the SAME source as the ongoing fetch — the first fire already does it with an empty cursor `onExecution` reads `params.onTrigger.results.body.data` — a single `TItem` when `batchSize` is 1, `TItem[]` when > 1 Import: `import slack from "./manifests/slack"` (component key as variable name) Export: `export const componentRegistry = componentManifests({ slack })` Creating manifests manually — they are auto-generated during scaffolding If no component exists in the registry, use direct HTTP calls with axios from Spectral SDK Fabricating a component key like "http"