@12-apps/marketplace 1.10.0 → 1.11.0

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.
Files changed (71) hide show
  1. package/ADOPTING.md +36 -0
  2. package/README.md +63 -0
  3. package/dist/99food/index.d.ts +2 -2
  4. package/dist/99food/index.js +2 -2
  5. package/dist/{adapter-CYJzu1c9.d.ts → adapter-CD0SfdVS.d.ts} +124 -1
  6. package/dist/{chunk-PYHNM47K.js → chunk-DCWOQ5PN.js} +2 -2
  7. package/dist/{chunk-WVZ2IXFH.js → chunk-OS4TSIFU.js} +190 -12
  8. package/dist/chunk-OS4TSIFU.js.map +1 -0
  9. package/dist/chunk-SGOTN5PX.js +345 -0
  10. package/dist/chunk-SGOTN5PX.js.map +1 -0
  11. package/dist/{chunk-HEXLCTOG.js → chunk-TVZU4HNT.js} +1 -1
  12. package/dist/chunk-TVZU4HNT.js.map +1 -0
  13. package/dist/{chunk-2BA2EHNF.js → chunk-YWMEIMO3.js} +206 -22
  14. package/dist/chunk-YWMEIMO3.js.map +1 -0
  15. package/dist/{client-B4EQCRD_.d.ts → client-11sncog4.d.ts} +1 -1
  16. package/dist/{engine-DK3iV_Kk.d.ts → engine-C_3RK8wC.d.ts} +116 -4
  17. package/dist/hono/index.d.ts +2 -2
  18. package/dist/hono/index.js +3 -3
  19. package/dist/ifood/index.d.ts +5 -2
  20. package/dist/ifood/index.js +125 -7
  21. package/dist/ifood/index.js.map +1 -1
  22. package/dist/index.d.ts +23 -4
  23. package/dist/index.js +264 -21
  24. package/dist/index.js.map +1 -1
  25. package/dist/keeta/index.d.ts +2 -2
  26. package/dist/keeta/index.js +2 -2
  27. package/dist/manifest/index.d.ts +219 -1
  28. package/dist/manifest/index.js +1 -1
  29. package/dist/manifest/server.d.ts +2 -2
  30. package/dist/manifest/server.js +3 -3
  31. package/dist/rappi/index.d.ts +1 -1
  32. package/dist/rappi/index.js +1 -1
  33. package/dist/server/en-US.d.ts +13 -0
  34. package/dist/server/en-US.js +13 -0
  35. package/dist/server/en-US.js.map +1 -1
  36. package/dist/server/index.d.ts +241 -7
  37. package/dist/server/index.js +3 -3
  38. package/dist/server/pt-BR.d.ts +13 -0
  39. package/dist/server/pt-BR.js +13 -0
  40. package/dist/server/pt-BR.js.map +1 -1
  41. package/package.json +1 -1
  42. package/prisma/marketplace.prisma +41 -0
  43. package/prisma/migrations/20261127090000_marketplace_app_link/migration.sql +81 -0
  44. package/src/core/adapter.ts +21 -0
  45. package/src/core/bulk.ts +43 -8
  46. package/src/core/engine.ts +59 -3
  47. package/src/core/integration.ts +60 -32
  48. package/src/core/link-store.ts +170 -0
  49. package/src/core/linking-port.ts +100 -0
  50. package/src/core/linking.ts +363 -0
  51. package/src/core/merchant-claim.ts +106 -0
  52. package/src/core/ports.ts +3 -0
  53. package/src/ifood/index.ts +4 -0
  54. package/src/ifood/linking.ts +170 -0
  55. package/src/ifood/outbound.ts +10 -3
  56. package/src/index.ts +14 -0
  57. package/src/server/copy.ts +20 -2
  58. package/src/server/en-US.ts +13 -0
  59. package/src/server/index.ts +1 -1
  60. package/src/server/link-routes.ts +140 -0
  61. package/src/server/mcp-link.ts +103 -0
  62. package/src/server/mcp.ts +6 -2
  63. package/src/server/pt-BR.ts +13 -0
  64. package/src/server/routes.ts +5 -2
  65. package/src/server/webhook.ts +86 -0
  66. package/dist/chunk-2BA2EHNF.js.map +0 -1
  67. package/dist/chunk-HEXLCTOG.js.map +0 -1
  68. package/dist/chunk-WVZ2IXFH.js.map +0 -1
  69. package/dist/chunk-ZO4S3LF4.js +0 -254
  70. package/dist/chunk-ZO4S3LF4.js.map +0 -1
  71. /package/dist/{chunk-PYHNM47K.js.map → chunk-DCWOQ5PN.js.map} +0 -0
@@ -79,10 +79,22 @@ async function admit(
79
79
  }
80
80
  }
81
81
 
82
+ interface Admitted {
83
+ clientId: string;
84
+ provider: string;
85
+ payload: unknown;
86
+ target: IngestionTarget;
87
+ }
88
+
82
89
  /** Record and act on one verified delivery. */
83
90
  async function handleWebhook(config: MarketplaceApiConfig, request: WireRequest<unknown>): Promise<WireResponse> {
84
91
  const admitted = await admit(config, request);
85
92
  if ("status" in admitted) return admitted;
93
+ return deliverAdmitted(config, admitted);
94
+ }
95
+
96
+ /** Record and act on a delivery whose store and signature are proven. */
97
+ async function deliverAdmitted(config: MarketplaceApiConfig, admitted: Admitted): Promise<WireResponse> {
86
98
  const { clientId, provider, payload, target } = admitted;
87
99
  const { engine } = config;
88
100
 
@@ -109,6 +121,80 @@ async function handleWebhook(config: MarketplaceApiConfig, request: WireRequest<
109
121
  return { status: 200, body: { received: true, outcome: outcome.kind } };
110
122
  }
111
123
 
124
+ /**
125
+ * The store a delivery to the APP's single address belongs to, and its
126
+ * connection: resolved from the merchant the payload names, then proved by
127
+ * the signature (FUT-5613). The payload is read before it is verified only to
128
+ * find whose key verifies it; nothing of it is acted on until it is.
129
+ */
130
+ async function admitApp(
131
+ config: MarketplaceApiConfig,
132
+ request: WireRequest<unknown>,
133
+ ): Promise<Admitted | WireResponse> {
134
+ const { provider = "" } = request.params;
135
+ if (!config.engine.knows(provider)) return { status: 404, body: { error: "unknown_provider" } };
136
+ const raw = request.request;
137
+ if (!raw) return { status: 400, body: { error: "raw_request_required" } };
138
+ const rawBody = await raw.text();
139
+ const payload = parsedJson(rawBody);
140
+ if (payload === undefined) return { status: 400, body: { error: "invalid_json" } };
141
+ const found = await appConnection(config, provider, payload);
142
+ if ("status" in found) return found;
143
+ // The app's address never takes an unsigned delivery, whatever the store's own setting.
144
+ const signature = config.engine.readSignature(provider, (name) => raw.headers.get(name));
145
+ const secret = found.target.webhookSecret;
146
+ if (!secret || !config.engine.verifySignature(provider, rawBody, signature, secret)) {
147
+ config.logger.warn(`marketplace.webhook: app ${provider} delivery for ${found.clientId} refused`);
148
+ return UNAUTHORIZED;
149
+ }
150
+ return { ...found, provider, payload };
151
+ }
152
+
153
+ /** A JSON body, or undefined when it is not JSON. */
154
+ function parsedJson(rawBody: string): unknown {
155
+ try {
156
+ return JSON.parse(rawBody) as unknown;
157
+ } catch {
158
+ return undefined;
159
+ }
160
+ }
161
+
162
+ /** The store whose live connection holds the merchant a delivery names, or the 401. */
163
+ async function appConnection(
164
+ config: MarketplaceApiConfig,
165
+ provider: string,
166
+ payload: unknown,
167
+ ): Promise<{ clientId: string; target: IngestionTarget } | WireResponse> {
168
+ const merchantId = config.engine.normalize(provider, payload)?.externalMerchantId ?? null;
169
+ const clientId = merchantId === null ? null : await config.engine.tenantFor(provider, merchantId);
170
+ if (!clientId) return UNAUTHORIZED;
171
+ try {
172
+ const target = await config.engine.target(clientId, provider);
173
+ return target ? { clientId, target } : UNAUTHORIZED;
174
+ } catch (error) {
175
+ config.logger.error(`marketplace.webhook: app ${provider} key of ${clientId} unavailable: ${String(error)}`);
176
+ return UNAUTHORIZED;
177
+ }
178
+ }
179
+
180
+ /**
181
+ * `POST /webhooks/marketplace/:provider` — the ONE address the software's own
182
+ * app registers with a marketplace, for every store linked through it
183
+ * (FUT-5613). The store is found by the merchant id the delivery names, and
184
+ * the delivery must carry a valid signature: unsigned is refused here.
185
+ */
186
+ export function appWebhookRoute(config: MarketplaceApiConfig): WireRoute<unknown> {
187
+ return {
188
+ method: "POST",
189
+ path: "/webhooks/marketplace/:provider",
190
+ kind: "webhook",
191
+ handle: async (request) => {
192
+ const admitted = await admitApp(config, request);
193
+ return "status" in admitted ? admitted : deliverAdmitted(config, admitted);
194
+ },
195
+ };
196
+ }
197
+
112
198
  /** The webhook descriptor. Raw body required: signatures are computed over bytes. */
113
199
  export function webhookRoute(config: MarketplaceApiConfig): WireRoute<unknown> {
114
200
  return {