@useairfoil/producer-shopify 0.0.1 → 0.1.1

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.
package/README.md ADDED
@@ -0,0 +1,256 @@
1
+ # @useairfoil/producer-shopify
2
+
3
+ Shopify producer connector for Airfoil Connector Kit.
4
+
5
+ Current scope:
6
+
7
+ - entity: `products`
8
+ - event: `cart_events`
9
+ - backfill source: Shopify Admin GraphQL `products` query
10
+ - live source: Shopify webhooks on `/webhooks/shopify`
11
+
12
+ ## Public Exports
13
+
14
+ - `ShopifyApiClient`
15
+ - `ShopifyConnector`
16
+ - `CartEvent`
17
+ - `CartEventSchema`
18
+ - `CartLineItem`
19
+ - `CartLineItemSchema`
20
+ - `CartWebhookPayload`
21
+ - `CartWebhookPayloadSchema`
22
+ - `Money`
23
+ - `MoneyBag`
24
+ - `MoneyBagSchema`
25
+ - `MoneySchema`
26
+ - `PageInfo`
27
+ - `PageInfoSchema`
28
+ - `Product`
29
+ - `ProductDeleteWebhookPayload`
30
+ - `ProductDeleteWebhookPayloadSchema`
31
+ - `ProductOption`
32
+ - `ProductOptionSchema`
33
+ - `ProductWebhookPayload`
34
+ - `ProductWebhookPayloadSchema`
35
+ - `ProductSchema`
36
+ - `ProductStatus`
37
+ - `ProductStatusSchema`
38
+ - `ProductVariant`
39
+ - `ProductVariantInventoryPolicy`
40
+ - `ProductVariantInventoryPolicySchema`
41
+ - `ProductVariantSchema`
42
+ - `ShopifyNormalize`
43
+ - `WebhookPayload`
44
+ - `WebhookPayloadSchema`
45
+ - manifest subpath: `@useairfoil/producer-shopify/manifest` exports browser-safe connector metadata
46
+
47
+ Connector config and runtime types are exported from the `ShopifyConnector` namespace.
48
+
49
+ ## Configuration
50
+
51
+ The sandbox reads connector values from the environment:
52
+
53
+ ```env
54
+ SHOPIFY_SHOP_DOMAIN=your-store.myshopify.com
55
+ SHOPIFY_CLIENT_ID=your-dev-dashboard-app-client-id
56
+ SHOPIFY_CLIENT_SECRET=your-dev-dashboard-app-client-secret
57
+ SHOPIFY_WEBHOOK_SECRET=your-webhook-signing-value
58
+ ```
59
+
60
+ Hosted `start` requires a read-only connector JSON file selected by `AIRFOIL_CONFIG_PATH`; matching environment values override file values per key. The file includes manifest runtime keys such as `SHOPIFY_SHOP_DOMAIN`, `SHOPIFY_CLIENT_ID`, `SHOPIFY_CLIENT_SECRET`, `SHOPIFY_API_VERSION`, and `SHOPIFY_WEBHOOK_SECRET`.
61
+
62
+ `SHOPIFY_WEBHOOK_SECRET` is the signing value Shopify shows when the merchant creates a webhook under Shopify Admin → Settings → Notifications → Webhooks. It is separate from `SHOPIFY_CLIENT_SECRET`.
63
+
64
+ Common sandbox/runtime values:
65
+
66
+ ```env
67
+ SHOPIFY_API_VERSION=2026-07
68
+ # SHOPIFY_RESPONSE_MAX_RETRIES=5
69
+ # SHOPIFY_TRANSPORT_MAX_RETRIES=5
70
+ # SHOPIFY_GRAPHQL_MAX_RETRIES=5
71
+ # SHOPIFY_RETRY_BASE_DELAY_MS=200
72
+ # SHOPIFY_GRAPHQL_RETRY_BASE_DELAY_MS=500
73
+ # SHOPIFY_RETRY_AFTER_FALLBACK_SECONDS=1
74
+ # SHOPIFY_REQUEST_TIMEOUT_SECONDS=120
75
+ SHOPIFY_WEBHOOK_PORT=8080
76
+ OTEL_ENABLED=false
77
+ OTEL_SERVICE_NAME=producer-shopify
78
+ # OTEL_SERVICE_VERSION=0.1.0
79
+ # OTEL_RESOURCE_ATTRIBUTES=deployment.environment=production,team=data
80
+ OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
81
+ # OTEL_EXPORTER_OTLP_HEADERS=Authorization=Bearer <token>,X-Axiom-Dataset=<dataset>
82
+ ```
83
+
84
+ These optional API policy values are loaded with the connector config. Shopify's reported query cost, available budget, restore rate, and `Retry-After` remain authoritative.
85
+
86
+ Production `start` also requires platform-owned Wings, table, and PostgreSQL state config:
87
+
88
+ ```env
89
+ WINGS_HOST=localhost:7777
90
+ WINGS_NAMESPACE=namespaces/default
91
+ SHOPIFY_PRODUCTS_TABLE=namespaces/default/tables/shopify-products
92
+ SHOPIFY_CART_EVENTS_TABLE=namespaces/default/tables/shopify-cart-events
93
+ AIRFOIL_CONFIG_PATH=/var/run/airfoil/config/config.json
94
+ AIRFOIL_CONNECTOR_INSTANCE_ID=team-acme-shopify-primary
95
+ # AIRFOIL_STATE_TABLE=_airfoil_connectors_state
96
+ POSTGRES_CONNECTION_STRING=postgresql://...
97
+ ```
98
+
99
+ The sandbox uses `Telemetry.layerOtlp(...)` and `Telemetry.layerMetricsConsoleDump()` from Connector Kit. Connector Kit reads `OTEL_ENABLED`, `OTEL_EXPORTER_OTLP_ENDPOINT`, and `OTEL_EXPORTER_OTLP_HEADERS` for OTLP trace/metric export. Effect reads `OTEL_SERVICE_NAME`, `OTEL_SERVICE_VERSION`, and `OTEL_RESOURCE_ATTRIBUTES` for resource metadata. HTTP runtimes expose shallow process health at `GET /health`, metrics at `GET /metrics`, and durable progress or source errors at `GET /status`.
100
+
101
+ The current product backfill requires `read_products`. Add more Admin API scopes to the app when more resources are enabled.
102
+
103
+ ### Getting Shopify credentials
104
+
105
+ The merchant follows Shopify's [client-credentials setup](https://shopify.dev/docs/apps/build/dev-dashboard/get-api-access-tokens?lang=node): create a Dev Dashboard app in the same Shopify organization as the store, enable the required Admin API scopes, install it on the store, and give Airfoil:
106
+
107
+ - the store's `*.myshopify.com` domain
108
+ - the app client ID
109
+ - the app client secret
110
+
111
+ The connector exchanges those credentials at `/admin/oauth/access_token`. Shopify access tokens normally last 24 hours. The connector keeps the token only in memory and lazily exchanges again after half of the reported lifetime. Pod restarts simply exchange on the first API request. Do not store access tokens in `StateStore` or commit either credential.
112
+
113
+ Store webhooks remain manual. The merchant creates each required topic in Shopify Admin, points it to the connector's public `/webhooks/shopify` URL, and provides the generated signing value as `SHOPIFY_WEBHOOK_SECRET`.
114
+
115
+ ## ConnectorApp Entrypoint
116
+
117
+ This connector exposes a single CLI entrypoint in `src/main.ts`:
118
+
119
+ ```bash
120
+ pnpm --filter @useairfoil/producer-shopify run sandbox
121
+ pnpm --filter @useairfoil/producer-shopify run start
122
+ ```
123
+
124
+ `sandbox` runs the real connector with `Publisher.layerConsole`. `start` passes the configured Wings table names to `Publisher.layerWings`.
125
+
126
+ The CLI assembly lives in `src/main.ts`; connector-specific platform keys live in `src/constants.ts`; production runtime wiring lives in `src/start.ts`; sandbox runtime wiring lives in `src/sandbox.ts`.
127
+
128
+ Before provisioning, the dashboard backend passes `ShopifyConnector.ShopifyConnector` and `ShopifyConnector.layerConfig(ShopifyConnector.ShopifyConfigDef.config)` to `ConnectorApp.check(...)`. `products` requests one product ID only, while webhook-only `cart_events` uses a minimal shop identity query that does not require product access.
129
+
130
+ ## Production Image
131
+
132
+ The build emits a separate `dist/main.js` CLI while keeping it outside the package exports. Build and smoke-test the non-root Node 24 image through Nx:
133
+
134
+ ```bash
135
+ pnpm nx run @useairfoil/producer-shopify:docker:build
136
+ docker run --rm airfoil/producer-shopify:local --help
137
+ ```
138
+
139
+ The image runs `node dist/main.js start`, exposes port `8080`, and contains only the pruned production package. Mount the complete connector JSON file read-only at the path supplied by `AIRFOIL_CONFIG_PATH`; inject the platform-owned values listed above separately. Never bake `.env`, API tokens, webhook secrets, or database credentials into the image. The operator owns Kubernetes readiness and liveness probes against `/health`.
140
+
141
+ ## Minimal ConnectorApp Wiring
142
+
143
+ The following is local/example wiring. Hosted production uses `RuntimeConfig.layerHosted()`, `StateStore.layerSql()` over `PgClient`, and the Wings publisher as shown by `src/start.ts`; it must not fall back to memory state.
144
+
145
+ ```ts
146
+ import { Publisher, ConnectorApp, StateStore, Telemetry } from "@useairfoil/connector-kit";
147
+ import { Effect, Layer } from "effect";
148
+ import { FetchHttpClient } from "effect/unstable/http";
149
+
150
+ import { ShopifyConnector } from "@useairfoil/producer-shopify";
151
+
152
+ const BootstrapLayer = FetchHttpClient.layer;
153
+
154
+ const ConnectorLayer = ShopifyConnector.layerConfig(ShopifyConnector.ShopifyConfigDef.config).pipe(
155
+ Layer.provide(BootstrapLayer),
156
+ );
157
+ const TelemetryLayer = Telemetry.layerOtlp({
158
+ redactedHeaders: ["x-shopify-access-token"],
159
+ }).pipe(Layer.provide(BootstrapLayer));
160
+
161
+ const program = Effect.gen(function* () {
162
+ const connector = yield* ShopifyConnector.ShopifyConnector;
163
+ return yield* ConnectorApp.start(connector, { port: 8080 });
164
+ });
165
+
166
+ const RuntimeLayer = Layer.mergeAll(
167
+ StateStore.layerMemory,
168
+ Publisher.layerConsole,
169
+ ConnectorLayer,
170
+ TelemetryLayer,
171
+ );
172
+
173
+ const runnable = Effect.scoped(program).pipe(Effect.provide(RuntimeLayer));
174
+
175
+ Effect.runPromise(runnable);
176
+ ```
177
+
178
+ ## Webhook Behavior
179
+
180
+ - webhook path: `POST /webhooks/shopify`
181
+ - expected topic headers include `products/create`, `products/update`, `products/delete`, `carts/create`, and `carts/update`
182
+ - `SHOPIFY_WEBHOOK_SECRET` is required and every request verifies `x-shopify-hmac-sha256` against the raw request body
183
+ - `products/create` and `products/update` publish directly when the webhook contains every variant and its creation time
184
+ - Shopify only includes full details for the first 100 variants; when `variant_gids` shows truncation, or `created_at` is missing, the handler refetches the complete product over GraphQL before publishing
185
+ - product rows expose nested variants as `variants`; backfill and webhook upserts keep this list complete
186
+ - signed `products/delete` webhooks convert Shopify's numeric product ID to a GraphQL product GID and publish a delete mutation using `X-Shopify-Triggered-At` as the version
187
+ - product webhook decoding is strict for the fields required to normalize product rows; if you use Shopify `include_fields`, include the product fields required by `ProductWebhookPayloadSchema`
188
+ - cart webhooks are normalized into the `cart_events` event stream using the documented cart payload fields
189
+ - product upserts are versioned by `updatedAt`; product deletes use `X-Shopify-Triggered-At`
190
+
191
+ ## API Client Layer
192
+
193
+ `ShopifyApiClient.layer(config)` builds `ShopifyApiClient.ShopifyApiClient` from a raw `ShopifyConfig` value.
194
+
195
+ The client:
196
+
197
+ - lazily exchanges the configured client credentials for an access token
198
+ - caches the token in memory until half its reported lifetime
199
+ - authenticates GraphQL calls with `X-Shopify-Access-Token`
200
+ - posts GraphQL operations to `/admin/api/<version>/graphql.json`
201
+ - sends `Accept: application/json` and `Content-Type: application/json`
202
+ - follows product and nested variant pagination through `pageInfo.endCursor`
203
+
204
+ Use `ShopifyConnector.layer(...)` or `ShopifyConnector.layerConfig(...)` for complete runtime wiring. `ShopifyApiClient.layer(...)` is the lower-level connector-internal layer and also requires `ShopifyAuth`.
205
+
206
+ ## Notes
207
+
208
+ - the connector uses Shopify Admin GraphQL
209
+ - the API version is pinned through `SHOPIFY_API_VERSION`
210
+ - product backfill paginates both products and their nested variants
211
+ - product row output is GraphQL-native camelCase; Shopify webhook payloads are normalized before publishing
212
+
213
+ ## Testing
214
+
215
+ - `test/api.vcr.test.ts`: VCR product response and mocked nested-variant pagination
216
+ - `test/auth.test.ts`: client-credentials exchange, safe errors, and request authentication
217
+ - `test/check.test.ts`: selected read-only resource checks
218
+ - `test/rate-limit.test.ts`: HTTP, transport, and GraphQL retry behavior
219
+ - `test/webhook.test.ts`: in-memory webhook flow with HMAC verification
220
+
221
+ Run:
222
+
223
+ ```bash
224
+ pnpm --filter @useairfoil/producer-shopify run test:ci
225
+ ```
226
+
227
+ To record the API cassette, put real Shopify values in the connector `.env` and run:
228
+
229
+ ```bash
230
+ pnpm --filter @useairfoil/producer-shopify exec dotenvx run \
231
+ --ignore=MISSING_ENV_FILE \
232
+ --quiet \
233
+ -- vitest run test/api.vcr.test.ts
234
+ ```
235
+
236
+ The VCR's auto mode records requests missing from the cassette. Do not set `ACK_DISABLE_VCR`, because that bypasses recording. Token request credentials and response access tokens are replaced with fixed placeholders automatically. Shopify cookies and GraphQL `X-Shopify-Access-Token` headers are removed before the cassette is written.
237
+
238
+ ## Sandbox Tracing
239
+
240
+ Set `OTEL_ENABLED=true` to export traces and metrics from the sandbox. Metrics are also logged locally by `Telemetry.layerMetricsConsoleDump()`.
241
+
242
+ The sandbox uses `Telemetry.layerOtlp({ redactedHeaders: ["x-shopify-access-token"] })` so Shopify access tokens are redacted in addition to Connector Kit defaults. See `@useairfoil/connector-kit` for the full telemetry env var list, metric names, and redaction defaults.
243
+
244
+ For local Jaeger with persistent storage, start it from the traceview package:
245
+
246
+ ```bash
247
+ pnpm --filter @useairfoil/traceview run jaeger:up
248
+ ```
249
+
250
+ Then set `OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318` and run the sandbox. After triggering a webhook or backfill, render the trace:
251
+
252
+ ```bash
253
+ traceview <trace-id> --source jaeger
254
+ # or for Axiom:
255
+ traceview <trace-id> --source axiom
256
+ ```
@@ -0,0 +1,13 @@
1
+ //#region rolldown:runtime
2
+ var __defProp = Object.defineProperty;
3
+ var __export = (all) => {
4
+ let target = {};
5
+ for (var name in all) __defProp(target, name, {
6
+ get: all[name],
7
+ enumerable: true
8
+ });
9
+ return target;
10
+ };
11
+
12
+ //#endregion
13
+ export { __export as t };