@tallyui/connector-medusa 1.0.0 → 3.0.0-next.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Paul Kilmurray and TallyUI contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # TallyUI Medusa connector
2
+
3
+ ## Errors
4
+
5
+ Requests with expired or rejected credentials reject with `ConnectorUnauthorizedError` (`code: 'unauthorized'`), defined in `@tallyui/core` and re-exported by this connector. The app should sign in again.
6
+
7
+ Sign-in itself rejects with `SignInError`.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,38 @@
1
- import { ProductTraits, CollectionSync, ReplicationAdapter, TallyConnector } from '@tallyui/core';
1
+ import * as _tallyui_core from '@tallyui/core';
2
+ import { ProductTraits, CollectionSync, ReplicationAdapter, StockReconcileAdapter, SyncContext, StoreSettingsChoice, StoreSettings, CustomerInput, Customer, TallyConnector, ConnectorAuth, ServerCapabilities } from '@tallyui/core';
3
+ export { ConnectorUnauthorizedError } from '@tallyui/core';
2
4
  import { RxJsonSchema } from 'rxdb';
3
5
 
6
+ /** A store API variant's `calculated_price`: the fields the connector reads. Amounts are major units. */
7
+ interface MedusaCalculatedPrice {
8
+ calculated_amount: number | null;
9
+ original_amount: number | null;
10
+ currency_code: string;
11
+ is_calculated_price_tax_inclusive: boolean;
12
+ is_original_price_tax_inclusive: boolean;
13
+ calculated_price?: {
14
+ price_list_id?: string | null;
15
+ price_list_type?: string | null;
16
+ };
17
+ }
18
+ /** A stored variant. `calculated_price`: an object when priced, `null` when not sold here, undefined in base-only mode. */
19
+ interface MedusaVariantDocument {
20
+ id: string;
21
+ prices?: Array<{
22
+ amount: number;
23
+ currency_code: string;
24
+ price_list_id?: string | null;
25
+ }>;
26
+ calculated_price?: MedusaCalculatedPrice | null;
27
+ [field: string]: unknown;
28
+ }
29
+ /** A stored product; typed for the fields the pricing code reads. */
30
+ interface MedusaProductDocument {
31
+ id: string;
32
+ status?: string;
33
+ variants?: MedusaVariantDocument[];
34
+ [field: string]: unknown;
35
+ }
4
36
  /**
5
37
  * MedusaJS v2 Product RxDB schema.
6
38
  * Mirrors the Medusa Admin API product shape.
@@ -21,11 +53,11 @@ declare const medusaProductSchema: RxJsonSchema<any>;
21
53
  *
22
54
  * Key mapping differences from WooCommerce:
23
55
  * - Product name is `title` (not `name`)
24
- * - Price lives on `variants[0].prices[0].amount` (cents, integer)
56
+ * - Price lives on `variants[0].prices[0].amount` (major units: 12 = 12.00)
25
57
  * - Images use `url` (not `src`)
26
58
  * - SKU/barcode live on variants, not the product
27
59
  * - Categories use `name`, tags use `value`
28
- * - Stock is per-variant via `inventory_quantity`
60
+ * - Stock is per-variant: `inventory_quantity` (Store API) or inventory levels (Admin API)
29
61
  */
30
62
  declare const medusaProductTraits: ProductTraits;
31
63
 
@@ -38,34 +70,136 @@ declare const medusaProductTraits: ProductTraits;
38
70
  */
39
71
  declare const medusaProductSync: CollectionSync;
40
72
 
73
+ /**
74
+ * A pass pages through every product with updated_at >= `updated_at`, in id
75
+ * order, `offset` rows in. The pass-start mark becomes the next lower bound.
76
+ */
41
77
  type MedusaProductCheckpoint = {
42
78
  offset: number;
43
79
  updated_at: string;
80
+ pass_max?: string;
81
+ pass_mark?: string;
82
+ pass_count?: number;
44
83
  };
45
84
  /**
46
85
  * Replication adapter for Medusa v2 products.
47
86
  *
48
- * Implements pull (offset-based pagination with updated_at filtering) and
49
- * push (POST to update individual products via Admin API). Designed for
50
- * use with RxDB's replicateRxCollection.
87
+ * Pull-only (id-ordered offset pages within an updated_at window) for RxDB's
88
+ * replicateRxCollection. Catalogue data is server-owned; the POS never
89
+ * writes products.
51
90
  */
52
91
  declare const medusaProductReplication: ReplicationAdapter<any, MedusaProductCheckpoint>;
53
92
 
54
93
  /**
55
- * MedusaJS v2 connector for Tally UI.
94
+ * Variant feed for Medusa products (ADR-060, amendment 8).
95
+ *
96
+ * On Medusa 2.21 a price-only edit bumps the variant's `updated_at` but not
97
+ * the product's, through both the admin's batch route and the single-variant
98
+ * route, so the product feed never sees it. This pull adapter pages changed
99
+ * variants with the same pass and offset design as `medusaProductReplication`
100
+ * and re-delivers their parent products, re-fetched with the id reconcile's
101
+ * `fetchByIds` in the same document shape, into the `products` collection.
102
+ * It runs as a sub-adapter of the connector's combined `replication.products`
103
+ * adapter (`combinePullAdapters`), not as its own replication. Parents that
104
+ * no longer exist are skipped; deletions are the id reconcile's job.
105
+ *
106
+ * On a fresh install `seedCheckpoint` starts the cursor at the variant mark,
107
+ * read before the product feed's first pass, so the catalogue downloads once.
108
+ * An upgrade (a stored product checkpoint, none for this feed) is not seeded:
109
+ * its first pass re-delivers every product that has variants, once. That is
110
+ * deliberate: it heals any price change the product feed missed before this
111
+ * feed existed.
112
+ *
113
+ * `variantPageSize` (the variant page `limit`) is for tests and tuning.
114
+ */
115
+ declare const createMedusaVariantFeedReplication: (variantPageSize?: number) => ReplicationAdapter<any, MedusaProductCheckpoint>;
116
+
117
+ /**
118
+ * Stock reconciler for Medusa v2 products (ADR-060). Inventory changes never
119
+ * touch the product, so replication misses them. Pages are keyed by
120
+ * inventory item id; each value is its location levels' quantities.
121
+ */
122
+ declare const medusaStockReconcile: StockReconcileAdapter;
123
+
124
+ /**
125
+ * Reads the store's currency, tax inclusivity and location tax rate, and the
126
+ * publishable-key channel, from Medusa's admin API only (TV4b). Read-only
127
+ * (ADR-048). Region, then country, then channel: the first ambiguity that
128
+ * needs a choice reports every choice known at that point, so the app asks
129
+ * once.
130
+ */
131
+ declare const medusaStoreSettings: (context: SyncContext, choice?: StoreSettingsChoice) => Promise<StoreSettings>;
132
+
133
+ type MedusaCustomer = {
134
+ id: string;
135
+ deleted_at?: string | null;
136
+ } & Record<string, unknown>;
137
+ type CustomerInit = {
138
+ signal?: AbortSignal;
139
+ fetch?: typeof fetch;
140
+ };
141
+ declare function toCustomer(raw: MedusaCustomer): Customer;
142
+ declare function searchMedusaCustomers(baseUrl: string, headers: Record<string, string>, query: string, init?: CustomerInit & {
143
+ limit?: number;
144
+ }): Promise<Customer[]>;
145
+ declare function createMedusaCustomer(baseUrl: string, headers: Record<string, string>, input: CustomerInput, init?: CustomerInit): Promise<Customer>;
146
+ declare function getMedusaCustomer(baseUrl: string, headers: Record<string, string>, id: string, init?: CustomerInit): Promise<Customer | null>;
147
+
148
+ /**
149
+ * Fills each variant's `calculated_price` from the store API, priced with
150
+ * `context.pricingContext` (`region_id`, `publishable_key`): what Medusa
151
+ * charges at checkout, sale price lists and their dates included (ADR-060
152
+ * amendment 8, D2b). A variant the store API does not list (a draft, or a
153
+ * product outside the key's sales channel) gets `null`: priced mode, not
154
+ * sellable here. Without a pricing context the documents come back
155
+ * unchanged (base-only mode, no `calculated_price` key). A failed store
156
+ * request throws, so the pull retries and never delivers a half-priced
157
+ * document.
158
+ */
159
+ declare function withCalculatedPrices<Doc extends MedusaProductDocument>(docs: Doc[], context: SyncContext): Promise<Doc[]>;
160
+
161
+ /** The calculated-price check's interval: price-list edits and sale start/end bump no timestamp (ADR-060 amendment 8). */
162
+ declare const MEDUSA_CALCULATED_PRICE_RECONCILE_INTERVAL_MS: number;
163
+
164
+ declare const medusaSecretKeyAuth: ConnectorAuth;
165
+ declare const medusaSignIn: NonNullable<ConnectorAuth['signIn']>;
166
+ /** Re-reads the store's `order.create` capability (ADR-062) for a restored session, with the connector's own headers. */
167
+ declare const medusaCapabilities: (context: SyncContext) => Promise<ServerCapabilities | undefined>;
168
+ declare const medusaSearchCustomers: (context: SyncContext, query: string, options?: {
169
+ limit?: number;
170
+ }) => Promise<_tallyui_core.Customer[]>;
171
+ declare const medusaCreateCustomer: (context: SyncContext, input: CustomerInput) => Promise<_tallyui_core.Customer>;
172
+ declare const medusaGetCustomer: (context: SyncContext, id: string) => Promise<_tallyui_core.Customer | null>;
173
+ declare const medusaAdminUserAuth: ConnectorAuth;
174
+ /**
175
+ * MedusaJS v2 connector for Tally UI, with a secret API key. Build one per store session, anew on each
176
+ * sign-in or store change: it owns the reconcile feed, whose queued work must never reach another store's database (#307).
56
177
  *
57
178
  * Connects to Medusa backends via the Admin API.
58
179
  * Products are stored in RxDB using a schema that mirrors the Medusa API shape.
59
180
  *
60
181
  * ```ts
61
- * import { medusaConnector } from '@tallyui/connector-medusa';
182
+ * import { createMedusaConnector } from '@tallyui/connector-medusa';
62
183
  * import { ConnectorProvider } from '@tallyui/core';
63
184
  *
64
- * <ConnectorProvider connector={medusaConnector}>
185
+ * const connector = useMemo(() => createMedusaConnector(), [backendUrl]);
186
+ * <ConnectorProvider connector={connector}>
65
187
  * <App />
66
188
  * </ConnectorProvider>
67
189
  * ```
68
190
  */
191
+ declare function createMedusaConnector(): TallyConnector;
192
+ /** Medusa connector using an admin user's Bearer JWT; like createMedusaConnector(), one per store session with its own feed. */
193
+ declare const createMedusaAdminUserConnector: () => TallyConnector;
194
+ /**
195
+ * @deprecated One instance for the whole app: a store switch can leak queued reconcile work across stores.
196
+ * Use createMedusaConnector() per store session. Removed in 4.0.
197
+ */
69
198
  declare const medusaConnector: TallyConnector;
199
+ /**
200
+ * @deprecated One instance for the whole app: a store switch can leak queued reconcile work across stores.
201
+ * Use createMedusaAdminUserConnector() per store session. Removed in 4.0.
202
+ */
203
+ declare const medusaAdminUserConnector: TallyConnector;
70
204
 
71
- export { medusaConnector, medusaProductReplication, medusaProductSchema, medusaProductSync, medusaProductTraits };
205
+ export { MEDUSA_CALCULATED_PRICE_RECONCILE_INTERVAL_MS, type MedusaCalculatedPrice, type MedusaProductDocument, type MedusaVariantDocument, createMedusaAdminUserConnector, createMedusaConnector, createMedusaCustomer, createMedusaVariantFeedReplication, getMedusaCustomer, medusaAdminUserAuth, medusaAdminUserConnector, medusaCapabilities, medusaConnector, medusaCreateCustomer, medusaGetCustomer, medusaProductReplication, medusaProductSchema, medusaProductSync, medusaProductTraits, medusaSearchCustomers, medusaSecretKeyAuth, medusaSignIn, medusaStockReconcile, medusaStoreSettings, searchMedusaCustomers, toCustomer, withCalculatedPrices };