|
| 1 | +/** |
| 2 | + * Example 03 — Provider + Adapter (MOST IMPORTANT) |
| 3 | + * |
| 4 | + * Shows the core relayfile pattern: a Provider handles auth while an |
| 5 | + * Adapter normalizes data and writes back through the provider. |
| 6 | + * |
| 7 | + * Flow: |
| 8 | + * Webhook → NangoProvider.handleWebhook() normalizes the event |
| 9 | + * → GitHubAdapter maps it to a VFS path |
| 10 | + * → Adapter writes back through NangoProvider.proxy() |
| 11 | + * |
| 12 | + * This example simulates the full cycle with mock webhook data. |
| 13 | + */ |
| 14 | + |
| 15 | +import { NangoProvider } from "@relayfile/provider-nango"; |
| 16 | +import type { NormalizedWebhook, ProxyResponse } from "@relayfile/provider-nango"; |
| 17 | + |
| 18 | +// ── Config ────────────────────────────────────────────────────────── |
| 19 | +const NANGO_SECRET_KEY = process.env.NANGO_SECRET_KEY ?? "nango-mock-secret-key"; |
| 20 | +const CONNECTION_ID = process.env.NANGO_CONNECTION_ID ?? "conn_github_demo"; |
| 21 | + |
| 22 | +// ── Mock adapter ──────────────────────────────────────────────────── |
| 23 | +// In production you would import GitHubAdapter from @relayfile/adapter-github. |
| 24 | +// This minimal adapter demonstrates the contract between adapter and provider. |
| 25 | + |
| 26 | +interface AdapterWritebackRequest { |
| 27 | + connectionId: string; |
| 28 | + path: string; |
| 29 | + payload: Record<string, unknown>; |
| 30 | +} |
| 31 | + |
| 32 | +class GitHubAdapterStub { |
| 33 | + constructor(private provider: NangoProvider) {} |
| 34 | + |
| 35 | + /** |
| 36 | + * Convert a normalized webhook into a VFS path. |
| 37 | + * Real adapters do richer mapping; this shows the shape. |
| 38 | + */ |
| 39 | + mapWebhookToPath(webhook: NormalizedWebhook): string { |
| 40 | + const { provider, objectType, objectId, eventType } = webhook; |
| 41 | + // Example: /github/repos/acme/api/issues/42 |
| 42 | + return `/${provider}/${objectType}/${objectId}`; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Write back to the external API through the provider. |
| 47 | + * The provider handles OAuth — the adapter just describes the request. |
| 48 | + */ |
| 49 | + async writeback(request: AdapterWritebackRequest): Promise<ProxyResponse> { |
| 50 | + console.log(`[Adapter] Writing back to ${request.path}`); |
| 51 | + return this.provider.proxy({ |
| 52 | + method: "POST", |
| 53 | + endpoint: request.path, |
| 54 | + connectionId: request.connectionId, |
| 55 | + body: request.payload, |
| 56 | + // baseUrl omitted — provider resolves from connection |
| 57 | + }); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// ── Mock webhook payload ──────────────────────────────────────────── |
| 62 | +// Simulates a Nango forward-webhook for a GitHub pull_request event. |
| 63 | +const mockWebhookPayload = { |
| 64 | + type: "forward", |
| 65 | + connectionId: CONNECTION_ID, |
| 66 | + providerConfigKey: "github", |
| 67 | + payload: { |
| 68 | + action: "opened", |
| 69 | + pull_request: { |
| 70 | + number: 99, |
| 71 | + title: "Add relayfile examples", |
| 72 | + html_url: "https://github.com/acme/api/pull/99", |
| 73 | + user: { login: "agent-bot" }, |
| 74 | + }, |
| 75 | + repository: { |
| 76 | + full_name: "acme/api", |
| 77 | + }, |
| 78 | + }, |
| 79 | +}; |
| 80 | + |
| 81 | +async function main() { |
| 82 | + const nango = new NangoProvider({ |
| 83 | + secretKey: NANGO_SECRET_KEY, |
| 84 | + }); |
| 85 | + const adapter = new GitHubAdapterStub(nango); |
| 86 | + |
| 87 | + console.log("Provider:", nango.name); |
| 88 | + |
| 89 | + // ── Step 1: Normalize the incoming webhook ──────────────────────── |
| 90 | + console.log("\n--- Step 1: Normalize webhook ---"); |
| 91 | + const webhook = await nango.handleWebhook(mockWebhookPayload); |
| 92 | + console.log("Provider:", webhook.provider); |
| 93 | + console.log("Event:", webhook.eventType); |
| 94 | + console.log("Object:", webhook.objectType, webhook.objectId); |
| 95 | + console.log("Connection:", webhook.connectionId); |
| 96 | + |
| 97 | + // ── Step 2: Adapter maps webhook to VFS path ───────────────────── |
| 98 | + console.log("\n--- Step 2: Map to VFS path ---"); |
| 99 | + const vfsPath = adapter.mapWebhookToPath(webhook); |
| 100 | + console.log("VFS path:", vfsPath); |
| 101 | + |
| 102 | + // ── Step 3: Adapter writes back through provider ────────────────── |
| 103 | + console.log("\n--- Step 3: Writeback through provider ---"); |
| 104 | + try { |
| 105 | + const response = await adapter.writeback({ |
| 106 | + connectionId: CONNECTION_ID, |
| 107 | + path: "/repos/acme/api/pulls/99/comments", |
| 108 | + payload: { body: "Thanks for the PR! Reviewing now." }, |
| 109 | + }); |
| 110 | + console.log("Writeback status:", response.status); |
| 111 | + console.log("Writeback data:", JSON.stringify(response.data, null, 2)); |
| 112 | + } catch (err) { |
| 113 | + // Expected when running without real credentials |
| 114 | + console.log("Writeback failed (expected without real credentials):", (err as Error).message); |
| 115 | + } |
| 116 | + |
| 117 | + // ── Step 4: Show the full flow ──────────────────────────────────── |
| 118 | + console.log("\n--- Full flow summary ---"); |
| 119 | + console.log("1. Webhook received from Nango (GitHub push event)"); |
| 120 | + console.log("2. Provider normalized it into a NormalizedWebhook"); |
| 121 | + console.log("3. Adapter mapped it to VFS path:", vfsPath); |
| 122 | + console.log("4. Adapter wrote back through provider.proxy()"); |
| 123 | + console.log("5. Provider injected OAuth token and forwarded to GitHub API"); |
| 124 | +} |
| 125 | + |
| 126 | +main().catch((err) => { |
| 127 | + console.error("Error:", err); |
| 128 | + process.exit(1); |
| 129 | +}); |
0 commit comments