@proveanything/smartlinks 2.1.39 → 2.1.41
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 +754 -754
- package/dist/README.md +573 -573
- package/dist/adminCardsView.js +55 -55
- package/dist/adminViews.d.ts +1 -1
- package/dist/adminViews.js +3 -1
- package/dist/ai-tools.d.ts +2 -1
- package/dist/ai-tools.js +3 -0
- package/dist/api/ai.d.ts +2 -21
- package/dist/api/ai.js +0 -35
- package/dist/api/collection.d.ts +8 -1
- package/dist/api/collection.js +10 -0
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.js +1 -0
- package/dist/api/knowledge.d.ts +64 -0
- package/dist/api/knowledge.js +97 -0
- package/dist/baseline.css +194 -194
- package/dist/docs/API_SUMMARY.md +1046 -964
- package/dist/docs/admin-cards.md +189 -189
- package/dist/docs/admin-views.md +257 -255
- package/dist/docs/agent-tools.md +156 -156
- package/dist/docs/ai-tools-and-skills.md +180 -180
- package/dist/docs/ai.md +1721 -1834
- package/dist/docs/analytics.md +39 -39
- package/dist/docs/app-data-storage.md +277 -277
- package/dist/docs/app-docs.md +73 -73
- package/dist/docs/app-manifest.md +852 -852
- package/dist/docs/app-records-pattern.md +388 -388
- package/dist/docs/appConfig.md +550 -550
- package/dist/docs/assistant-apps.md +172 -172
- package/dist/docs/assistant-context.md +119 -119
- package/dist/docs/bookings.md +442 -442
- package/dist/docs/comms.md +686 -686
- package/dist/docs/connectors.md +340 -340
- package/dist/docs/css-baseline.md +133 -133
- package/dist/docs/customer-guide.md +70 -70
- package/dist/docs/deploying-apps.md +340 -340
- package/dist/docs/entitlements.md +90 -90
- package/dist/docs/files.md +69 -69
- package/dist/docs/host-dependency-contract.md +210 -210
- package/dist/docs/integrations.md +322 -322
- package/dist/docs/interactions.md +545 -545
- package/dist/docs/item-context.md +181 -181
- package/dist/docs/knowledge.md +102 -0
- package/dist/docs/lots.md +111 -111
- package/dist/docs/loyalty.md +352 -352
- package/dist/docs/members.md +182 -182
- package/dist/docs/mobile-admin-container.md +693 -693
- package/dist/docs/operations.md +281 -281
- package/dist/docs/overview.md +482 -482
- package/dist/docs/proof-comms-triggers.md +142 -142
- package/dist/docs/proof-ownership-transfer.md +155 -155
- package/dist/docs/proof-product-data-scoping.md +462 -462
- package/dist/docs/proof-share-grants.md +305 -305
- package/dist/docs/realtime.md +138 -138
- package/dist/docs/records-admin-pattern.md +304 -304
- package/dist/docs/standard-query.md +187 -187
- package/dist/docs/tag-codes.md +97 -97
- package/dist/docs/theme-tokens.md +252 -252
- package/dist/docs/wallet-passes.md +107 -107
- package/dist/docs/websites.md +123 -123
- package/dist/openapi.yaml +320 -306
- package/dist/theme-boot.js +78 -78
- package/dist/theme.css +75 -75
- package/dist/types/ai.d.ts +9 -91
- package/dist/types/appManifest.d.ts +21 -0
- package/dist/types/collection.d.ts +26 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +1 -0
- package/dist/types/knowledge.d.ts +120 -0
- package/dist/types/knowledge.js +2 -0
- package/docs/API_SUMMARY.md +1046 -964
- package/docs/admin-cards.md +189 -189
- package/docs/admin-views.md +257 -255
- package/docs/agent-tools.md +156 -156
- package/docs/ai-tools-and-skills.md +180 -180
- package/docs/ai.md +1721 -1834
- package/docs/analytics.md +39 -39
- package/docs/app-data-storage.md +277 -277
- package/docs/app-docs.md +73 -73
- package/docs/app-manifest.md +852 -852
- package/docs/app-records-pattern.md +388 -388
- package/docs/appConfig.md +550 -550
- package/docs/assistant-apps.md +172 -172
- package/docs/assistant-context.md +119 -119
- package/docs/bookings.md +442 -442
- package/docs/comms.md +686 -686
- package/docs/connectors.md +340 -340
- package/docs/css-baseline.md +133 -133
- package/docs/customer-guide.md +70 -70
- package/docs/deploying-apps.md +340 -340
- package/docs/entitlements.md +90 -90
- package/docs/files.md +69 -69
- package/docs/host-dependency-contract.md +210 -210
- package/docs/integrations.md +322 -322
- package/docs/interactions.md +545 -545
- package/docs/item-context.md +181 -181
- package/docs/knowledge.md +102 -0
- package/docs/lots.md +111 -111
- package/docs/loyalty.md +352 -352
- package/docs/members.md +182 -182
- package/docs/mobile-admin-container.md +693 -693
- package/docs/operations.md +281 -281
- package/docs/overview.md +482 -482
- package/docs/proof-comms-triggers.md +142 -142
- package/docs/proof-ownership-transfer.md +155 -155
- package/docs/proof-product-data-scoping.md +462 -462
- package/docs/proof-share-grants.md +305 -305
- package/docs/realtime.md +138 -138
- package/docs/standard-query.md +187 -187
- package/docs/tag-codes.md +97 -97
- package/docs/theme-lookbook.v1.json +120 -120
- package/docs/theme-tokens.md +252 -252
- package/docs/wallet-passes.md +107 -107
- package/docs/websites.md +123 -123
- package/openapi.yaml +320 -306
- package/package.json +87 -87
- package/scripts/headless-check.mjs +87 -87
- package/scripts/lib/bare-imports.mjs +57 -57
- package/scripts/lib/keyed-app-data.mjs +21 -21
- package/scripts/publish.mjs +272 -272
- package/scripts/register-release.mjs +91 -91
package/docs/connectors.md
CHANGED
|
@@ -1,340 +1,340 @@
|
|
|
1
|
-
# Integration connectors
|
|
2
|
-
|
|
3
|
-
A **connector** is an app that knows one external provider (a PIM, a content network, a shop
|
|
4
|
-
platform, a loyalty or CRM system…). It is installed on a collection like any app, versioned and released like any app, and
|
|
5
|
-
it adds a manifest `connector` block plus a handful of [server functions](server-functions.md) the
|
|
6
|
-
integration engine and the integrations console call by convention.
|
|
7
|
-
|
|
8
|
-
Three layers, each with one job:
|
|
9
|
-
|
|
10
|
-
| Layer | Holds |
|
|
11
|
-
|---|---|
|
|
12
|
-
| **Engine** (the API) | [Integration flows](integrations.md), runs and their logs, triggers (events, schedule, Run now), the secret store, **the mapping** (each customer's field mappings and rules), generic transports (HTTP, Event Hub). No provider code. |
|
|
13
|
-
| **Integrations console** (a core app) | The one admin screen: installed connectors, connect forms, settings, the mapper with AI assist, flows, runs. |
|
|
14
|
-
| **Connector apps** | One per provider: an **adapter**. Talks to the provider and converts between its format and a clean, provider-neutral document. |
|
|
15
|
-
|
|
16
|
-
**A connector adapts; core maps.** A connector never holds a customer's mapping. It turns the
|
|
17
|
-
provider's records into provider-neutral documents (`shape`), or provider-ready payloads out of
|
|
18
|
-
documents core has already mapped (`serialize`). Every customer's mapping, routing and rules live in
|
|
19
|
-
core, so they work the same for every provider and can be replayed and traced. This is the line
|
|
20
|
-
SnapLogic (endpoint Snaps vs the Mapper), MuleSoft and Airbyte all draw.
|
|
21
|
-
|
|
22
|
-
A flow that only posts JSON to a webhook or an Event Hub needs no connector; it uses the engine's
|
|
23
|
-
generic transports (see [integrations.md](integrations.md)).
|
|
24
|
-
|
|
25
|
-
**No AI at run time.** The AI helps an admin *author* a mapping in the console (from the connector's
|
|
26
|
-
target schema plus real sample records); a person approves it, and it is saved as data and runs
|
|
27
|
-
deterministically. A connector's `shape` and `serialize` must be deterministic too.
|
|
28
|
-
|
|
29
|
-
---
|
|
30
|
-
|
|
31
|
-
## The manifest block
|
|
32
|
-
|
|
33
|
-
```jsonc
|
|
34
|
-
// app.manifest.json
|
|
35
|
-
"connector": {
|
|
36
|
-
"provider": "oneworldsync",
|
|
37
|
-
"title": "1WorldSync",
|
|
38
|
-
"directions": ["inbound"],
|
|
39
|
-
"entities": ["product"],
|
|
40
|
-
"connection": {
|
|
41
|
-
"fields": [
|
|
42
|
-
// Optional: SmartLinks' own credentials are the default; a customer may enter theirs.
|
|
43
|
-
{ "name": "appId", "label": "App ID", "secret": "oneworldsync-app-id" },
|
|
44
|
-
{ "name": "secretKey", "label": "Secret key", "secret": "oneworldsync-secret-key" },
|
|
45
|
-
{ "name": "gln", "label": "GLN", "required": true },
|
|
46
|
-
{ "name": "environment", "label": "Environment", "type": "select",
|
|
47
|
-
"options": [ { "value": "preprod", "label": "Pre-production" }, { "value": "production", "label": "Production" } ] }
|
|
48
|
-
]
|
|
49
|
-
},
|
|
50
|
-
"settings": { // JSON Schema → a form in the console
|
|
51
|
-
"type": "object",
|
|
52
|
-
"properties": { "targetMarket": { "type": "string", "default": "GB" } }
|
|
53
|
-
},
|
|
54
|
-
"presets": [
|
|
55
|
-
{ "id": "import-products", "title": "Import products", "direction": "inbound", "entity": "product",
|
|
56
|
-
"flow": { "config": { "fieldMappings": [ /* a suggested mapping the customer can edit */ ] } } }
|
|
57
|
-
],
|
|
58
|
-
"functions": { // names of this app's server functions
|
|
59
|
-
"testConnection": "testConnection",
|
|
60
|
-
"describeTarget": "describeTarget",
|
|
61
|
-
"sample": "sample",
|
|
62
|
-
"shape": "shape",
|
|
63
|
-
"fetch": "fetch"
|
|
64
|
-
},
|
|
65
|
-
"panels": [ { "id": "explorer", "title": "Explorer", "component": "Explorer" } ],
|
|
66
|
-
"aiGuide": "connector-ai-guide.md"
|
|
67
|
-
},
|
|
68
|
-
"functions": {
|
|
69
|
-
"files": { "js": { "umd": "functions.umd.js" } },
|
|
70
|
-
"definitions": [
|
|
71
|
-
{ "name": "testConnection", "trigger": { "type": "http", "method": "POST" },
|
|
72
|
-
"visibility": "admin", "authority": "collection",
|
|
73
|
-
"capabilities": ["network:content1-api.1worldsync.com", "secrets:oneworldsync-app-id", "secrets:oneworldsync-secret-key"] }
|
|
74
|
-
// …one definition per function named above
|
|
75
|
-
]
|
|
76
|
-
}
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
Types: `AppConnectorDeclaration` and friends in `@proveanything/smartlinks` (`src/types/appManifest.ts`).
|
|
80
|
-
|
|
81
|
-
| Field | Meaning |
|
|
82
|
-
|---|---|
|
|
83
|
-
| `provider` | Stable provider key (`syndigo`, `oneworldsync`). |
|
|
84
|
-
| `title` | Provider display name; defaults to the app name. |
|
|
85
|
-
| `directions` | `inbound` (provider → SmartLinks), `outbound` (SmartLinks → provider), or both. |
|
|
86
|
-
| `entities` | SmartLinks entities it maps: `product`, and for people-shaped providers `interaction`, `contact`, `event` (see [Loyalty and CRM connectors](#loyalty-and-crm-connectors)). |
|
|
87
|
-
| `connection.fields[]` | The console's **Connect** form. A field with `secret` is stored as this app's secret of that name on the collection (write-only; never returned to the browser). Other fields are saved in the installation's connection settings. |
|
|
88
|
-
| `settings` | JSON Schema for installation settings; the console renders it as a form. |
|
|
89
|
-
| `presets[]` | Ready-made flows ("Products → provider"). A preset's `flow.config.fieldMappings` is a **suggested** mapping: the connector's knowledge of the provider, which the customer accepts or edits in core. |
|
|
90
|
-
| `functions` | Which of this app's server functions implement each step (below). Fill in only what applies. |
|
|
91
|
-
| `panels[]` | Provider-specific screens the console mounts (an API explorer, a status page). |
|
|
92
|
-
| `aiGuide` | A guide the mapping assistant reads: the provider's quirks, required fields, good defaults. |
|
|
93
|
-
| `recordId` | Path of the provider's own id in a fetched record (e.g. `gtin`). Inbound records are landed and matched by it, so a re-import updates the same product instead of creating another. Default `id`; a flow can override it (`config.upsert.externalIdPath`). |
|
|
94
|
-
| `limits` | `{ requestsPerSecond }` toward the provider, enforced across every SmartLinks worker; `{ maxEvents, maxAttributes }` when the provider caps registered events — a flow's `eventMapping` is checked against them when saved. |
|
|
95
|
-
| `actions[]` | Provider operations invoked on demand (redeem, enrol, issue…): name, title, function, `visibility`, input schema. See [Actions](#actions-provider-operations-on-demand). |
|
|
96
|
-
|
|
97
|
-
---
|
|
98
|
-
|
|
99
|
-
## The functions
|
|
100
|
-
|
|
101
|
-
Each step names one of the app's server functions. Declare them as **http POST, `visibility:
|
|
102
|
-
"admin"`, `authority: "collection"`**: the engine calls them with no signed-in user (a product edit, a
|
|
103
|
-
schedule), and the console calls the same functions while an admin configures the flow. The input
|
|
104
|
-
arrives as `event.body`; return the result as JSON. Capabilities still cap what each one can do:
|
|
105
|
-
declare the provider's hosts (`network:<host>`) and the secrets it reads (`secrets:<name>`).
|
|
106
|
-
|
|
107
|
-
| Step | Called by | Input (`event.body`) | Returns |
|
|
108
|
-
|---|---|---|---|
|
|
109
|
-
| `testConnection` | Connect form, **Test** | `{ installation }` | `ConnectorTestResult` `{ ok, message }` |
|
|
110
|
-
| `describeTarget` | Mapper opens | `{ installation }` | `ConnectorTargetSchema` `{ fields: [{ path, label, type, required, enum }] }`: the provider's fields *for this customer*. Outbound mappings target these paths. |
|
|
111
|
-
| `sample` | Mapper, AI assist, dry run | `{ installation, limit }` | `ConnectorRecords` `{ records }`: real provider records |
|
|
112
|
-
| `fetch` | Every inbound run | `ConnectorFetchInput` `{ installation, cursor, limit }` | `ConnectorRecords` `{ records, nextCursor }`: one page per call |
|
|
113
|
-
| `shape` | Every inbound run (and sample dry runs) | `ConnectorShapeInput` `{ installation, records }` | `ConnectorShapeResult` `{ documents, skipped }`: one provider-neutral document per record, same order; `null` skips one |
|
|
114
|
-
| `serialize` | Every outbound run | `ConnectorSerializeInput` `{ installation, documents, entities, targetOptions, eventType? }`: documents core mapped to `describeTarget` paths; `entities` = the SmartLinks record each came from (id, name); `targetOptions` = per-field options the mapping carries (e.g. locale, unit); a deletion arrives as `{ id, _deleted: true }` | `ConnectorSerializeResult` `{ payloads, skipped }`: one provider payload per document, same order; `null` skips one |
|
|
115
|
-
| `send` | Every outbound run | `ConnectorSendInput` `{ installation, payloads, eventType? }` | `ConnectorSendResult` `{ results: [{ index, ok, status, error, externalId, retryable?, retryAfterSeconds? }] }` — `retryable: false` for a provider validation error (dead at once), `true` / a 5xx or 429 status for anything worth retrying |
|
|
116
|
-
| `describeEvents` | Mapper opens; a flow with an `eventMapping` is saved | `{ installation }` | `ConnectorEventsSchema` `{ events: [{ name, title, attributes: [{ name, type, required }] }], limits: { maxEvents, maxAttributes } }`: the event names the provider has registered for this customer |
|
|
117
|
-
| `lookup` | An entitlement check (`docs/entitlements.md`) | `ConnectorLookupInput` `{ installation, identity: { externalId, email, phone } \| { externalId, emailHash, phoneHash }, want }` | `ConnectorLookupResult` `{ found, memberId, tier, points, status, attributes, asOf }`. Throw for an outage — never `found: false` |
|
|
118
|
-
|
|
119
|
-
**How a run flows:**
|
|
120
|
-
- **Inbound:** `fetch` (paged; every record is landed as received) → `shape` → core maps the documents
|
|
121
|
-
with the flow's mapping → upsert, matched by `recordId`. Unchanged records aren't processed again; a
|
|
122
|
-
changed flow configuration replays the landed records without fetching.
|
|
123
|
-
- **Outbound:** gather the SmartLinks records → core maps them to the provider's fields → `serialize`
|
|
124
|
-
→ `send` (or a generic transport). A record identical to its last successful delivery isn't sent again.
|
|
125
|
-
- Large runs are processed in short slices that resume after an interruption; there's no record cap.
|
|
126
|
-
|
|
127
|
-
**What happens when a step is left out:**
|
|
128
|
-
- **No `shape`:** core maps the provider records as they come.
|
|
129
|
-
- **No `serialize`:** the mapped documents are sent as they are.
|
|
130
|
-
- **No `send`:** the flow's own transport is used: HTTP (`config.connection`), or Azure Event Hub with
|
|
131
|
-
`config.transport: 'eventhub'` and `config.eventHub`.
|
|
132
|
-
- **No `describeTarget`:** the mapper offers free-text target paths.
|
|
133
|
-
|
|
134
|
-
**What `shape` should produce:** plain, predictable JSON a person can map without reading the
|
|
135
|
-
provider's documentation. Flatten the provider's envelopes, name things plainly, keep codes alongside
|
|
136
|
-
their meanings, and put repeated sections (ingredients, allergens, nutrients, images) in arrays. The
|
|
137
|
-
same provider record always gives the same document.
|
|
138
|
-
|
|
139
|
-
### `installation`
|
|
140
|
-
|
|
141
|
-
Every step receives the installation it serves:
|
|
142
|
-
|
|
143
|
-
```ts
|
|
144
|
-
interface ConnectorInstallation {
|
|
145
|
-
collectionId: string
|
|
146
|
-
appId: string // the connector app
|
|
147
|
-
flowId?: string | null // when called for a flow
|
|
148
|
-
connection: Record<string, unknown> // non-secret Connect-form fields
|
|
149
|
-
settings: Record<string, unknown> // installation settings, with the flow's overrides on top
|
|
150
|
-
}
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
Secrets are never in it. Read them in the function with `ctx.secrets.get('<name>')`, which needs the
|
|
154
|
-
matching `secrets:<name>` capability. A secret the customer set on their collection wins; otherwise the
|
|
155
|
-
connector's own app-level secret of that name (set in Forge) is used, so a connector can ship with
|
|
156
|
-
SmartLinks' credentials as the default.
|
|
157
|
-
|
|
158
|
-
### Example: `fetch` and `shape`
|
|
159
|
-
|
|
160
|
-
```ts
|
|
161
|
-
// src/functions/index.ts (the app's functions bundle)
|
|
162
|
-
import type { ConnectorFetchInput, ConnectorRecords, ConnectorShapeInput, ConnectorShapeResult } from '@proveanything/smartlinks'
|
|
163
|
-
|
|
164
|
-
export async function fetch(ctx: any, event: any): Promise<ConnectorRecords> {
|
|
165
|
-
const { installation, cursor, limit = 50 } = event.body as ConnectorFetchInput
|
|
166
|
-
const appId = await ctx.secrets.get('provider-app-id')
|
|
167
|
-
const key = await ctx.secrets.get('provider-secret-key')
|
|
168
|
-
const res = await ctx.fetch(buildSignedUrl(installation, { cursor, limit }, appId, key))
|
|
169
|
-
const page = await res.json()
|
|
170
|
-
return { records: page.items, nextCursor: page.next ?? null }
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
export async function shape(_ctx: any, event: any): Promise<ConnectorShapeResult> {
|
|
174
|
-
const { records } = event.body as ConnectorShapeInput
|
|
175
|
-
return {
|
|
176
|
-
documents: records.map((r: any) => ({
|
|
177
|
-
gtin: r.item?.gtin,
|
|
178
|
-
name: r.item?.tradeItemDescription?.[0]?.value,
|
|
179
|
-
brand: r.item?.brandName,
|
|
180
|
-
ingredients: (r.item?.ingredients ?? []).map((i: any) => ({ name: i.name, percent: i.percentage })),
|
|
181
|
-
})),
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
---
|
|
187
|
-
|
|
188
|
-
## Loyalty and CRM connectors
|
|
189
|
-
|
|
190
|
-
A provider that is about **people** rather than products uses the same contract with three more
|
|
191
|
-
pieces; nothing provider-specific lives in core. Full contract: `loyalty-connector-authoring.md` in the
|
|
192
|
-
platform's design docs; the shapes are in [integrations.md](integrations.md#event-sourced-flows-and-the-envelope).
|
|
193
|
-
|
|
194
|
-
| Flow | What core does | What the connector does |
|
|
195
|
-
|---|---|---|
|
|
196
|
-
| **Earn** (us → them) | an outbound flow with `sourceEntity: 'interaction' \| 'contact' \| 'event'` sends one **envelope** per event, through the flow's identity policy and `eventMapping`, signed, with retries and a run log | `describeEvents` (what the provider has registered), `serialize` (envelope → the provider's event payload), `send` if the generic HTTP destination can't make the call |
|
|
197
|
-
| **Profile** (them → us) | `ctx.sl.members.apply(document)` matches the contact and writes member id, tier, points, tags, external ids; or an inbound flow with `targetEntity: 'contact'` | an **HTTP-trigger function** receiving the provider's member webhook (verify their signature, then apply); or `fetch` + `shape` for a poll |
|
|
198
|
-
| **Gate / display** (on demand) | `entitlements.check` — claims → cached `lookup` → the contact's own loyalty fields → fallback; Valkey cache keyed on an identity hash, brand-set TTL | `lookup`: one identity → tier / points / status |
|
|
199
|
-
|
|
200
|
-
```ts
|
|
201
|
-
// A member webhook: the provider tells us about a person; we land it on the contact.
|
|
202
|
-
export default async function memberWebhook(event: any, ctx: any) {
|
|
203
|
-
verifyProviderSignature(event, await ctx.secrets.get('webhookSecret'))
|
|
204
|
-
const m = event.body
|
|
205
|
-
return ctx.sl.members.apply({
|
|
206
|
-
identity: { externalId: m.memberId, email: m.email },
|
|
207
|
-
contact: { firstName: m.firstName, lastName: m.lastName }, // fills empty fields only
|
|
208
|
-
loyalty: { memberId: m.memberId, tier: m.tier, points: m.points, status: m.status, asOf: m.updatedAt },
|
|
209
|
-
tags: { add: [`tier:${m.tier}`] },
|
|
210
|
-
}, { provider: 'acme-loyalty' }) // → { contactId, op: 'created' | 'updated' | 'unchanged' }
|
|
211
|
-
}
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
Declare it `{ "trigger": { "type": "http" }, "capabilities": ["sl:members:write", "secrets:webhookSecret"] }`;
|
|
215
|
-
`sl:members:write` is approved by the admin on install. The envelope's `identity.externalIds[<provider>]`
|
|
216
|
-
is the member id this wrote back, so **Earn** can address the member by the provider's own id.
|
|
217
|
-
|
|
218
|
-
A connector should ship an `eventMapping` **preset** with the provider's standard event names and an
|
|
219
|
-
`aiGuide` paragraph, so the console's assistant proposes a customer's mapping in the provider's terms.
|
|
220
|
-
|
|
221
|
-
---
|
|
222
|
-
|
|
223
|
-
## Actions: provider operations on demand
|
|
224
|
-
|
|
225
|
-
Steps are called by the engine. **Actions** are called when someone asks: redeem a reward, enrol a
|
|
226
|
-
member, issue a coupon, raise a ticket, send a receipt, check a balance. A connector declares them in
|
|
227
|
-
the manifest and implements each as a server function; the platform decides who may call them and
|
|
228
|
-
runs them with the connector's credentials, telling the function who asked.
|
|
229
|
-
|
|
230
|
-
```jsonc
|
|
231
|
-
"connector": {
|
|
232
|
-
"actions": [
|
|
233
|
-
{ "name": "redeem", "title": "Redeem a reward", "function": "redeemReward", "visibility": "member",
|
|
234
|
-
"input": { "type": "object", "required": ["rewardId"], "properties": { "rewardId": { "type": "string" } } } },
|
|
235
|
-
{ "name": "enrol", "title": "Join the programme", "function": "enrolMember", "visibility": "member" },
|
|
236
|
-
{ "name": "coupon-status", "title": "Coupon status", "function": "couponStatus", "visibility": "public" },
|
|
237
|
-
{ "name": "adjust-points", "title": "Adjust points", "function": "adjustPoints" } // admin only (the default)
|
|
238
|
-
]
|
|
239
|
-
}
|
|
240
|
-
```
|
|
241
|
-
|
|
242
|
-
| `visibility` | Who may invoke it |
|
|
243
|
-
|---|---|
|
|
244
|
-
| `admin` (default) | the console and admin API; any app function |
|
|
245
|
-
| `member` | also a signed-in person through the Hub / a site |
|
|
246
|
-
| `public` | also anyone |
|
|
247
|
-
|
|
248
|
-
The function receives `event.body = { input, installation, caller: { userId, contactId, role, surface } }`
|
|
249
|
-
and returns JSON (or a `Response`). `input.required` fields from the declared schema are checked before
|
|
250
|
-
the function runs (400 with `missing[]`); fuller validation is the connector's job.
|
|
251
|
-
|
|
252
|
-
```ts
|
|
253
|
-
export async function redeemReward(ctx: any, event: any) {
|
|
254
|
-
const { input, caller } = event.body
|
|
255
|
-
if (!caller.contactId) return new Response(JSON.stringify({ error: 'no_contact' }), { status: 400 })
|
|
256
|
-
const memberId = (await ctx.sl.contacts.forCaller())?.customFields?.loyaltyMemberId // written by Profile
|
|
257
|
-
const res = await ctx.fetch(`${api}/members/${memberId}/redemptions`, { method: 'POST', headers: auth(await ctx.secrets.get('apiKey')), body: JSON.stringify({ reward: input.rewardId }) })
|
|
258
|
-
return res.json()
|
|
259
|
-
}
|
|
260
|
-
```
|
|
261
|
-
|
|
262
|
-
Calling an action:
|
|
263
|
-
|
|
264
|
-
```ts
|
|
265
|
-
// From the Hub or a site (visibility public / member):
|
|
266
|
-
const { actions } = await integrations.listConnectorActions(collectionId, 'acme-loyalty')
|
|
267
|
-
const out = await integrations.callConnectorAction(collectionId, 'acme-loyalty', 'redeem', { rewardId: 'R-5' })
|
|
268
|
-
|
|
269
|
-
// From another app's server function (any visibility; needs sl:connectors:write):
|
|
270
|
-
const out = await ctx.sl.connectors.call('acme-loyalty', 'redeem', { rewardId: 'R-5' })
|
|
271
|
-
|
|
272
|
-
// From the console (any visibility):
|
|
273
|
-
await integrations.runConnectorAction(collectionId, 'acme-loyalty', 'adjust-points', { memberId, delta: 100 })
|
|
274
|
-
```
|
|
275
|
-
|
|
276
|
-
---
|
|
277
|
-
|
|
278
|
-
## Where configuration lives
|
|
279
|
-
|
|
280
|
-
| What | Where |
|
|
281
|
-
|---|---|
|
|
282
|
-
| Connect-form values (non-secret), installation settings | The connector app's config on the collection, in its admin-only zone (`admin.connector.connection`, `admin.connector.settings`). |
|
|
283
|
-
| Credentials | The connector app's secrets, by the names its fields declare (the customer's on their collection, else the connector's default). |
|
|
284
|
-
| One flow's use of the connector | The flow: `config.connector = { appId, preset?, settings? }` (per-flow settings override the installation's), plus `config.fieldMappings`, which core runs. |
|
|
285
|
-
|
|
286
|
-
```ts
|
|
287
|
-
await integrations.createFlow(collectionId, {
|
|
288
|
-
appId: 'core-integrations',
|
|
289
|
-
direction: 'inbound',
|
|
290
|
-
name: 'Import products from 1WorldSync',
|
|
291
|
-
status: 'active',
|
|
292
|
-
schedule: '0 * * * *',
|
|
293
|
-
targetEntity: 'product',
|
|
294
|
-
config: {
|
|
295
|
-
connector: { appId: 'oneworldsync', preset: 'import-products' },
|
|
296
|
-
// Maps the connector's shaped documents (not the raw provider records) onto the product.
|
|
297
|
-
fieldMappings: [ { targetPath: 'name', sourcePath: 'name', transformType: 'direct' } ],
|
|
298
|
-
},
|
|
299
|
-
})
|
|
300
|
-
```
|
|
301
|
-
|
|
302
|
-
---
|
|
303
|
-
|
|
304
|
-
## From the console
|
|
305
|
-
|
|
306
|
-
```ts
|
|
307
|
-
import { integrations, secrets } from '@proveanything/smartlinks'
|
|
308
|
-
|
|
309
|
-
const { connectors } = await integrations.listConnectors(collectionId)
|
|
310
|
-
// → [{ appId, name, version, connector, installation: { connection, settings }, secrets: { 'provider-key': { set, hint } } }]
|
|
311
|
-
|
|
312
|
-
await secrets.put(collectionId, 'provider-key', value, { appId }) // a secret field
|
|
313
|
-
await integrations.saveConnectorInstallation(collectionId, appId, { connection: { environment: 'production' } })
|
|
314
|
-
const test = await integrations.runConnectorStep(collectionId, appId, 'testConnection')
|
|
315
|
-
const target = await integrations.runConnectorStep(collectionId, appId, 'describeTarget')
|
|
316
|
-
const events = await integrations.runConnectorStep(collectionId, appId, 'describeEvents') // loyalty / engagement providers
|
|
317
|
-
const { records } = await integrations.runConnectorStep(collectionId, appId, 'sample', { limit: 5 })
|
|
318
|
-
const shaped = await integrations.runConnectorStep(collectionId, appId, 'shape', { records }) // dry run
|
|
319
|
-
const wire = await integrations.runConnectorStep(collectionId, appId, 'serialize', { documents }) // dry run
|
|
320
|
-
```
|
|
321
|
-
|
|
322
|
-
`send` and `fetch` only run inside a flow. A flow run's summary carries `sent` (outbound) or
|
|
323
|
-
`created` / `updated` / `skipped` (inbound), and every run, inbound or outbound, is in the run history.
|
|
324
|
-
|
|
325
|
-
---
|
|
326
|
-
|
|
327
|
-
## Panels
|
|
328
|
-
|
|
329
|
-
A connector can contribute screens to the integrations console (an API explorer, say). Each
|
|
330
|
-
`panels[]` entry names a component exported from the app's widgets bundle; the console mounts it
|
|
331
|
-
host-side and passes it the collection and the installation. Panels are admin-only and call the
|
|
332
|
-
connector's own functions.
|
|
333
|
-
|
|
334
|
-
---
|
|
335
|
-
|
|
336
|
-
## Who can build connectors
|
|
337
|
-
|
|
338
|
-
First-party connectors run in-process, like any app server function. Third-party connectors will run
|
|
339
|
-
in the isolated runner (see [server-functions.md](server-functions.md)) once that opens up; the contract
|
|
340
|
-
does not change.
|
|
1
|
+
# Integration connectors
|
|
2
|
+
|
|
3
|
+
A **connector** is an app that knows one external provider (a PIM, a content network, a shop
|
|
4
|
+
platform, a loyalty or CRM system…). It is installed on a collection like any app, versioned and released like any app, and
|
|
5
|
+
it adds a manifest `connector` block plus a handful of [server functions](server-functions.md) the
|
|
6
|
+
integration engine and the integrations console call by convention.
|
|
7
|
+
|
|
8
|
+
Three layers, each with one job:
|
|
9
|
+
|
|
10
|
+
| Layer | Holds |
|
|
11
|
+
|---|---|
|
|
12
|
+
| **Engine** (the API) | [Integration flows](integrations.md), runs and their logs, triggers (events, schedule, Run now), the secret store, **the mapping** (each customer's field mappings and rules), generic transports (HTTP, Event Hub). No provider code. |
|
|
13
|
+
| **Integrations console** (a core app) | The one admin screen: installed connectors, connect forms, settings, the mapper with AI assist, flows, runs. |
|
|
14
|
+
| **Connector apps** | One per provider: an **adapter**. Talks to the provider and converts between its format and a clean, provider-neutral document. |
|
|
15
|
+
|
|
16
|
+
**A connector adapts; core maps.** A connector never holds a customer's mapping. It turns the
|
|
17
|
+
provider's records into provider-neutral documents (`shape`), or provider-ready payloads out of
|
|
18
|
+
documents core has already mapped (`serialize`). Every customer's mapping, routing and rules live in
|
|
19
|
+
core, so they work the same for every provider and can be replayed and traced. This is the line
|
|
20
|
+
SnapLogic (endpoint Snaps vs the Mapper), MuleSoft and Airbyte all draw.
|
|
21
|
+
|
|
22
|
+
A flow that only posts JSON to a webhook or an Event Hub needs no connector; it uses the engine's
|
|
23
|
+
generic transports (see [integrations.md](integrations.md)).
|
|
24
|
+
|
|
25
|
+
**No AI at run time.** The AI helps an admin *author* a mapping in the console (from the connector's
|
|
26
|
+
target schema plus real sample records); a person approves it, and it is saved as data and runs
|
|
27
|
+
deterministically. A connector's `shape` and `serialize` must be deterministic too.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## The manifest block
|
|
32
|
+
|
|
33
|
+
```jsonc
|
|
34
|
+
// app.manifest.json
|
|
35
|
+
"connector": {
|
|
36
|
+
"provider": "oneworldsync",
|
|
37
|
+
"title": "1WorldSync",
|
|
38
|
+
"directions": ["inbound"],
|
|
39
|
+
"entities": ["product"],
|
|
40
|
+
"connection": {
|
|
41
|
+
"fields": [
|
|
42
|
+
// Optional: SmartLinks' own credentials are the default; a customer may enter theirs.
|
|
43
|
+
{ "name": "appId", "label": "App ID", "secret": "oneworldsync-app-id" },
|
|
44
|
+
{ "name": "secretKey", "label": "Secret key", "secret": "oneworldsync-secret-key" },
|
|
45
|
+
{ "name": "gln", "label": "GLN", "required": true },
|
|
46
|
+
{ "name": "environment", "label": "Environment", "type": "select",
|
|
47
|
+
"options": [ { "value": "preprod", "label": "Pre-production" }, { "value": "production", "label": "Production" } ] }
|
|
48
|
+
]
|
|
49
|
+
},
|
|
50
|
+
"settings": { // JSON Schema → a form in the console
|
|
51
|
+
"type": "object",
|
|
52
|
+
"properties": { "targetMarket": { "type": "string", "default": "GB" } }
|
|
53
|
+
},
|
|
54
|
+
"presets": [
|
|
55
|
+
{ "id": "import-products", "title": "Import products", "direction": "inbound", "entity": "product",
|
|
56
|
+
"flow": { "config": { "fieldMappings": [ /* a suggested mapping the customer can edit */ ] } } }
|
|
57
|
+
],
|
|
58
|
+
"functions": { // names of this app's server functions
|
|
59
|
+
"testConnection": "testConnection",
|
|
60
|
+
"describeTarget": "describeTarget",
|
|
61
|
+
"sample": "sample",
|
|
62
|
+
"shape": "shape",
|
|
63
|
+
"fetch": "fetch"
|
|
64
|
+
},
|
|
65
|
+
"panels": [ { "id": "explorer", "title": "Explorer", "component": "Explorer" } ],
|
|
66
|
+
"aiGuide": "connector-ai-guide.md"
|
|
67
|
+
},
|
|
68
|
+
"functions": {
|
|
69
|
+
"files": { "js": { "umd": "functions.umd.js" } },
|
|
70
|
+
"definitions": [
|
|
71
|
+
{ "name": "testConnection", "trigger": { "type": "http", "method": "POST" },
|
|
72
|
+
"visibility": "admin", "authority": "collection",
|
|
73
|
+
"capabilities": ["network:content1-api.1worldsync.com", "secrets:oneworldsync-app-id", "secrets:oneworldsync-secret-key"] }
|
|
74
|
+
// …one definition per function named above
|
|
75
|
+
]
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Types: `AppConnectorDeclaration` and friends in `@proveanything/smartlinks` (`src/types/appManifest.ts`).
|
|
80
|
+
|
|
81
|
+
| Field | Meaning |
|
|
82
|
+
|---|---|
|
|
83
|
+
| `provider` | Stable provider key (`syndigo`, `oneworldsync`). |
|
|
84
|
+
| `title` | Provider display name; defaults to the app name. |
|
|
85
|
+
| `directions` | `inbound` (provider → SmartLinks), `outbound` (SmartLinks → provider), or both. |
|
|
86
|
+
| `entities` | SmartLinks entities it maps: `product`, and for people-shaped providers `interaction`, `contact`, `event` (see [Loyalty and CRM connectors](#loyalty-and-crm-connectors)). |
|
|
87
|
+
| `connection.fields[]` | The console's **Connect** form. A field with `secret` is stored as this app's secret of that name on the collection (write-only; never returned to the browser). Other fields are saved in the installation's connection settings. |
|
|
88
|
+
| `settings` | JSON Schema for installation settings; the console renders it as a form. |
|
|
89
|
+
| `presets[]` | Ready-made flows ("Products → provider"). A preset's `flow.config.fieldMappings` is a **suggested** mapping: the connector's knowledge of the provider, which the customer accepts or edits in core. |
|
|
90
|
+
| `functions` | Which of this app's server functions implement each step (below). Fill in only what applies. |
|
|
91
|
+
| `panels[]` | Provider-specific screens the console mounts (an API explorer, a status page). |
|
|
92
|
+
| `aiGuide` | A guide the mapping assistant reads: the provider's quirks, required fields, good defaults. |
|
|
93
|
+
| `recordId` | Path of the provider's own id in a fetched record (e.g. `gtin`). Inbound records are landed and matched by it, so a re-import updates the same product instead of creating another. Default `id`; a flow can override it (`config.upsert.externalIdPath`). |
|
|
94
|
+
| `limits` | `{ requestsPerSecond }` toward the provider, enforced across every SmartLinks worker; `{ maxEvents, maxAttributes }` when the provider caps registered events — a flow's `eventMapping` is checked against them when saved. |
|
|
95
|
+
| `actions[]` | Provider operations invoked on demand (redeem, enrol, issue…): name, title, function, `visibility`, input schema. See [Actions](#actions-provider-operations-on-demand). |
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## The functions
|
|
100
|
+
|
|
101
|
+
Each step names one of the app's server functions. Declare them as **http POST, `visibility:
|
|
102
|
+
"admin"`, `authority: "collection"`**: the engine calls them with no signed-in user (a product edit, a
|
|
103
|
+
schedule), and the console calls the same functions while an admin configures the flow. The input
|
|
104
|
+
arrives as `event.body`; return the result as JSON. Capabilities still cap what each one can do:
|
|
105
|
+
declare the provider's hosts (`network:<host>`) and the secrets it reads (`secrets:<name>`).
|
|
106
|
+
|
|
107
|
+
| Step | Called by | Input (`event.body`) | Returns |
|
|
108
|
+
|---|---|---|---|
|
|
109
|
+
| `testConnection` | Connect form, **Test** | `{ installation }` | `ConnectorTestResult` `{ ok, message }` |
|
|
110
|
+
| `describeTarget` | Mapper opens | `{ installation }` | `ConnectorTargetSchema` `{ fields: [{ path, label, type, required, enum }] }`: the provider's fields *for this customer*. Outbound mappings target these paths. |
|
|
111
|
+
| `sample` | Mapper, AI assist, dry run | `{ installation, limit }` | `ConnectorRecords` `{ records }`: real provider records |
|
|
112
|
+
| `fetch` | Every inbound run | `ConnectorFetchInput` `{ installation, cursor, limit }` | `ConnectorRecords` `{ records, nextCursor }`: one page per call |
|
|
113
|
+
| `shape` | Every inbound run (and sample dry runs) | `ConnectorShapeInput` `{ installation, records }` | `ConnectorShapeResult` `{ documents, skipped }`: one provider-neutral document per record, same order; `null` skips one |
|
|
114
|
+
| `serialize` | Every outbound run | `ConnectorSerializeInput` `{ installation, documents, entities, targetOptions, eventType? }`: documents core mapped to `describeTarget` paths; `entities` = the SmartLinks record each came from (id, name); `targetOptions` = per-field options the mapping carries (e.g. locale, unit); a deletion arrives as `{ id, _deleted: true }` | `ConnectorSerializeResult` `{ payloads, skipped }`: one provider payload per document, same order; `null` skips one |
|
|
115
|
+
| `send` | Every outbound run | `ConnectorSendInput` `{ installation, payloads, eventType? }` | `ConnectorSendResult` `{ results: [{ index, ok, status, error, externalId, retryable?, retryAfterSeconds? }] }` — `retryable: false` for a provider validation error (dead at once), `true` / a 5xx or 429 status for anything worth retrying |
|
|
116
|
+
| `describeEvents` | Mapper opens; a flow with an `eventMapping` is saved | `{ installation }` | `ConnectorEventsSchema` `{ events: [{ name, title, attributes: [{ name, type, required }] }], limits: { maxEvents, maxAttributes } }`: the event names the provider has registered for this customer |
|
|
117
|
+
| `lookup` | An entitlement check (`docs/entitlements.md`) | `ConnectorLookupInput` `{ installation, identity: { externalId, email, phone } \| { externalId, emailHash, phoneHash }, want }` | `ConnectorLookupResult` `{ found, memberId, tier, points, status, attributes, asOf }`. Throw for an outage — never `found: false` |
|
|
118
|
+
|
|
119
|
+
**How a run flows:**
|
|
120
|
+
- **Inbound:** `fetch` (paged; every record is landed as received) → `shape` → core maps the documents
|
|
121
|
+
with the flow's mapping → upsert, matched by `recordId`. Unchanged records aren't processed again; a
|
|
122
|
+
changed flow configuration replays the landed records without fetching.
|
|
123
|
+
- **Outbound:** gather the SmartLinks records → core maps them to the provider's fields → `serialize`
|
|
124
|
+
→ `send` (or a generic transport). A record identical to its last successful delivery isn't sent again.
|
|
125
|
+
- Large runs are processed in short slices that resume after an interruption; there's no record cap.
|
|
126
|
+
|
|
127
|
+
**What happens when a step is left out:**
|
|
128
|
+
- **No `shape`:** core maps the provider records as they come.
|
|
129
|
+
- **No `serialize`:** the mapped documents are sent as they are.
|
|
130
|
+
- **No `send`:** the flow's own transport is used: HTTP (`config.connection`), or Azure Event Hub with
|
|
131
|
+
`config.transport: 'eventhub'` and `config.eventHub`.
|
|
132
|
+
- **No `describeTarget`:** the mapper offers free-text target paths.
|
|
133
|
+
|
|
134
|
+
**What `shape` should produce:** plain, predictable JSON a person can map without reading the
|
|
135
|
+
provider's documentation. Flatten the provider's envelopes, name things plainly, keep codes alongside
|
|
136
|
+
their meanings, and put repeated sections (ingredients, allergens, nutrients, images) in arrays. The
|
|
137
|
+
same provider record always gives the same document.
|
|
138
|
+
|
|
139
|
+
### `installation`
|
|
140
|
+
|
|
141
|
+
Every step receives the installation it serves:
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
interface ConnectorInstallation {
|
|
145
|
+
collectionId: string
|
|
146
|
+
appId: string // the connector app
|
|
147
|
+
flowId?: string | null // when called for a flow
|
|
148
|
+
connection: Record<string, unknown> // non-secret Connect-form fields
|
|
149
|
+
settings: Record<string, unknown> // installation settings, with the flow's overrides on top
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Secrets are never in it. Read them in the function with `ctx.secrets.get('<name>')`, which needs the
|
|
154
|
+
matching `secrets:<name>` capability. A secret the customer set on their collection wins; otherwise the
|
|
155
|
+
connector's own app-level secret of that name (set in Forge) is used, so a connector can ship with
|
|
156
|
+
SmartLinks' credentials as the default.
|
|
157
|
+
|
|
158
|
+
### Example: `fetch` and `shape`
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
// src/functions/index.ts (the app's functions bundle)
|
|
162
|
+
import type { ConnectorFetchInput, ConnectorRecords, ConnectorShapeInput, ConnectorShapeResult } from '@proveanything/smartlinks'
|
|
163
|
+
|
|
164
|
+
export async function fetch(ctx: any, event: any): Promise<ConnectorRecords> {
|
|
165
|
+
const { installation, cursor, limit = 50 } = event.body as ConnectorFetchInput
|
|
166
|
+
const appId = await ctx.secrets.get('provider-app-id')
|
|
167
|
+
const key = await ctx.secrets.get('provider-secret-key')
|
|
168
|
+
const res = await ctx.fetch(buildSignedUrl(installation, { cursor, limit }, appId, key))
|
|
169
|
+
const page = await res.json()
|
|
170
|
+
return { records: page.items, nextCursor: page.next ?? null }
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export async function shape(_ctx: any, event: any): Promise<ConnectorShapeResult> {
|
|
174
|
+
const { records } = event.body as ConnectorShapeInput
|
|
175
|
+
return {
|
|
176
|
+
documents: records.map((r: any) => ({
|
|
177
|
+
gtin: r.item?.gtin,
|
|
178
|
+
name: r.item?.tradeItemDescription?.[0]?.value,
|
|
179
|
+
brand: r.item?.brandName,
|
|
180
|
+
ingredients: (r.item?.ingredients ?? []).map((i: any) => ({ name: i.name, percent: i.percentage })),
|
|
181
|
+
})),
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
## Loyalty and CRM connectors
|
|
189
|
+
|
|
190
|
+
A provider that is about **people** rather than products uses the same contract with three more
|
|
191
|
+
pieces; nothing provider-specific lives in core. Full contract: `loyalty-connector-authoring.md` in the
|
|
192
|
+
platform's design docs; the shapes are in [integrations.md](integrations.md#event-sourced-flows-and-the-envelope).
|
|
193
|
+
|
|
194
|
+
| Flow | What core does | What the connector does |
|
|
195
|
+
|---|---|---|
|
|
196
|
+
| **Earn** (us → them) | an outbound flow with `sourceEntity: 'interaction' \| 'contact' \| 'event'` sends one **envelope** per event, through the flow's identity policy and `eventMapping`, signed, with retries and a run log | `describeEvents` (what the provider has registered), `serialize` (envelope → the provider's event payload), `send` if the generic HTTP destination can't make the call |
|
|
197
|
+
| **Profile** (them → us) | `ctx.sl.members.apply(document)` matches the contact and writes member id, tier, points, tags, external ids; or an inbound flow with `targetEntity: 'contact'` | an **HTTP-trigger function** receiving the provider's member webhook (verify their signature, then apply); or `fetch` + `shape` for a poll |
|
|
198
|
+
| **Gate / display** (on demand) | `entitlements.check` — claims → cached `lookup` → the contact's own loyalty fields → fallback; Valkey cache keyed on an identity hash, brand-set TTL | `lookup`: one identity → tier / points / status |
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
// A member webhook: the provider tells us about a person; we land it on the contact.
|
|
202
|
+
export default async function memberWebhook(event: any, ctx: any) {
|
|
203
|
+
verifyProviderSignature(event, await ctx.secrets.get('webhookSecret'))
|
|
204
|
+
const m = event.body
|
|
205
|
+
return ctx.sl.members.apply({
|
|
206
|
+
identity: { externalId: m.memberId, email: m.email },
|
|
207
|
+
contact: { firstName: m.firstName, lastName: m.lastName }, // fills empty fields only
|
|
208
|
+
loyalty: { memberId: m.memberId, tier: m.tier, points: m.points, status: m.status, asOf: m.updatedAt },
|
|
209
|
+
tags: { add: [`tier:${m.tier}`] },
|
|
210
|
+
}, { provider: 'acme-loyalty' }) // → { contactId, op: 'created' | 'updated' | 'unchanged' }
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Declare it `{ "trigger": { "type": "http" }, "capabilities": ["sl:members:write", "secrets:webhookSecret"] }`;
|
|
215
|
+
`sl:members:write` is approved by the admin on install. The envelope's `identity.externalIds[<provider>]`
|
|
216
|
+
is the member id this wrote back, so **Earn** can address the member by the provider's own id.
|
|
217
|
+
|
|
218
|
+
A connector should ship an `eventMapping` **preset** with the provider's standard event names and an
|
|
219
|
+
`aiGuide` paragraph, so the console's assistant proposes a customer's mapping in the provider's terms.
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## Actions: provider operations on demand
|
|
224
|
+
|
|
225
|
+
Steps are called by the engine. **Actions** are called when someone asks: redeem a reward, enrol a
|
|
226
|
+
member, issue a coupon, raise a ticket, send a receipt, check a balance. A connector declares them in
|
|
227
|
+
the manifest and implements each as a server function; the platform decides who may call them and
|
|
228
|
+
runs them with the connector's credentials, telling the function who asked.
|
|
229
|
+
|
|
230
|
+
```jsonc
|
|
231
|
+
"connector": {
|
|
232
|
+
"actions": [
|
|
233
|
+
{ "name": "redeem", "title": "Redeem a reward", "function": "redeemReward", "visibility": "member",
|
|
234
|
+
"input": { "type": "object", "required": ["rewardId"], "properties": { "rewardId": { "type": "string" } } } },
|
|
235
|
+
{ "name": "enrol", "title": "Join the programme", "function": "enrolMember", "visibility": "member" },
|
|
236
|
+
{ "name": "coupon-status", "title": "Coupon status", "function": "couponStatus", "visibility": "public" },
|
|
237
|
+
{ "name": "adjust-points", "title": "Adjust points", "function": "adjustPoints" } // admin only (the default)
|
|
238
|
+
]
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
| `visibility` | Who may invoke it |
|
|
243
|
+
|---|---|
|
|
244
|
+
| `admin` (default) | the console and admin API; any app function |
|
|
245
|
+
| `member` | also a signed-in person through the Hub / a site |
|
|
246
|
+
| `public` | also anyone |
|
|
247
|
+
|
|
248
|
+
The function receives `event.body = { input, installation, caller: { userId, contactId, role, surface } }`
|
|
249
|
+
and returns JSON (or a `Response`). `input.required` fields from the declared schema are checked before
|
|
250
|
+
the function runs (400 with `missing[]`); fuller validation is the connector's job.
|
|
251
|
+
|
|
252
|
+
```ts
|
|
253
|
+
export async function redeemReward(ctx: any, event: any) {
|
|
254
|
+
const { input, caller } = event.body
|
|
255
|
+
if (!caller.contactId) return new Response(JSON.stringify({ error: 'no_contact' }), { status: 400 })
|
|
256
|
+
const memberId = (await ctx.sl.contacts.forCaller())?.customFields?.loyaltyMemberId // written by Profile
|
|
257
|
+
const res = await ctx.fetch(`${api}/members/${memberId}/redemptions`, { method: 'POST', headers: auth(await ctx.secrets.get('apiKey')), body: JSON.stringify({ reward: input.rewardId }) })
|
|
258
|
+
return res.json()
|
|
259
|
+
}
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
Calling an action:
|
|
263
|
+
|
|
264
|
+
```ts
|
|
265
|
+
// From the Hub or a site (visibility public / member):
|
|
266
|
+
const { actions } = await integrations.listConnectorActions(collectionId, 'acme-loyalty')
|
|
267
|
+
const out = await integrations.callConnectorAction(collectionId, 'acme-loyalty', 'redeem', { rewardId: 'R-5' })
|
|
268
|
+
|
|
269
|
+
// From another app's server function (any visibility; needs sl:connectors:write):
|
|
270
|
+
const out = await ctx.sl.connectors.call('acme-loyalty', 'redeem', { rewardId: 'R-5' })
|
|
271
|
+
|
|
272
|
+
// From the console (any visibility):
|
|
273
|
+
await integrations.runConnectorAction(collectionId, 'acme-loyalty', 'adjust-points', { memberId, delta: 100 })
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
---
|
|
277
|
+
|
|
278
|
+
## Where configuration lives
|
|
279
|
+
|
|
280
|
+
| What | Where |
|
|
281
|
+
|---|---|
|
|
282
|
+
| Connect-form values (non-secret), installation settings | The connector app's config on the collection, in its admin-only zone (`admin.connector.connection`, `admin.connector.settings`). |
|
|
283
|
+
| Credentials | The connector app's secrets, by the names its fields declare (the customer's on their collection, else the connector's default). |
|
|
284
|
+
| One flow's use of the connector | The flow: `config.connector = { appId, preset?, settings? }` (per-flow settings override the installation's), plus `config.fieldMappings`, which core runs. |
|
|
285
|
+
|
|
286
|
+
```ts
|
|
287
|
+
await integrations.createFlow(collectionId, {
|
|
288
|
+
appId: 'core-integrations',
|
|
289
|
+
direction: 'inbound',
|
|
290
|
+
name: 'Import products from 1WorldSync',
|
|
291
|
+
status: 'active',
|
|
292
|
+
schedule: '0 * * * *',
|
|
293
|
+
targetEntity: 'product',
|
|
294
|
+
config: {
|
|
295
|
+
connector: { appId: 'oneworldsync', preset: 'import-products' },
|
|
296
|
+
// Maps the connector's shaped documents (not the raw provider records) onto the product.
|
|
297
|
+
fieldMappings: [ { targetPath: 'name', sourcePath: 'name', transformType: 'direct' } ],
|
|
298
|
+
},
|
|
299
|
+
})
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
---
|
|
303
|
+
|
|
304
|
+
## From the console
|
|
305
|
+
|
|
306
|
+
```ts
|
|
307
|
+
import { integrations, secrets } from '@proveanything/smartlinks'
|
|
308
|
+
|
|
309
|
+
const { connectors } = await integrations.listConnectors(collectionId)
|
|
310
|
+
// → [{ appId, name, version, connector, installation: { connection, settings }, secrets: { 'provider-key': { set, hint } } }]
|
|
311
|
+
|
|
312
|
+
await secrets.put(collectionId, 'provider-key', value, { appId }) // a secret field
|
|
313
|
+
await integrations.saveConnectorInstallation(collectionId, appId, { connection: { environment: 'production' } })
|
|
314
|
+
const test = await integrations.runConnectorStep(collectionId, appId, 'testConnection')
|
|
315
|
+
const target = await integrations.runConnectorStep(collectionId, appId, 'describeTarget')
|
|
316
|
+
const events = await integrations.runConnectorStep(collectionId, appId, 'describeEvents') // loyalty / engagement providers
|
|
317
|
+
const { records } = await integrations.runConnectorStep(collectionId, appId, 'sample', { limit: 5 })
|
|
318
|
+
const shaped = await integrations.runConnectorStep(collectionId, appId, 'shape', { records }) // dry run
|
|
319
|
+
const wire = await integrations.runConnectorStep(collectionId, appId, 'serialize', { documents }) // dry run
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
`send` and `fetch` only run inside a flow. A flow run's summary carries `sent` (outbound) or
|
|
323
|
+
`created` / `updated` / `skipped` (inbound), and every run, inbound or outbound, is in the run history.
|
|
324
|
+
|
|
325
|
+
---
|
|
326
|
+
|
|
327
|
+
## Panels
|
|
328
|
+
|
|
329
|
+
A connector can contribute screens to the integrations console (an API explorer, say). Each
|
|
330
|
+
`panels[]` entry names a component exported from the app's widgets bundle; the console mounts it
|
|
331
|
+
host-side and passes it the collection and the installation. Panels are admin-only and call the
|
|
332
|
+
connector's own functions.
|
|
333
|
+
|
|
334
|
+
---
|
|
335
|
+
|
|
336
|
+
## Who can build connectors
|
|
337
|
+
|
|
338
|
+
First-party connectors run in-process, like any app server function. Third-party connectors will run
|
|
339
|
+
in the isolated runner (see [server-functions.md](server-functions.md)) once that opens up; the contract
|
|
340
|
+
does not change.
|