@proveanything/smartlinks 2.1.34 → 2.1.37

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.
@@ -73,7 +73,8 @@ export declare namespace containers {
73
73
  * ```typescript
74
74
  * const { containers: holding } = await containers.findForItem('coll_123', {
75
75
  * itemType: 'proof',
76
- * itemId: 'proof-uuid',
76
+ * itemId: 'unit-0007',
77
+ * productId: 'drill',
77
78
  * })
78
79
  * ```
79
80
  */
@@ -99,7 +99,8 @@ export var containers;
99
99
  * ```typescript
100
100
  * const { containers: holding } = await containers.findForItem('coll_123', {
101
101
  * itemType: 'proof',
102
- * itemId: 'proof-uuid',
102
+ * itemId: 'unit-0007',
103
+ * productId: 'drill',
103
104
  * })
104
105
  * ```
105
106
  */
@@ -44,6 +44,7 @@ export { tags } from "./tags.js";
44
44
  export { order } from "./order.js";
45
45
  export { app } from "./appObjects.js";
46
46
  export { bookings } from "./bookings.js";
47
+ export { shop } from "./shop.js";
47
48
  export { attestations } from "./attestations.js";
48
49
  export { containers } from "./containers.js";
49
50
  export { lots } from "./lots.js";
package/dist/api/index.js CHANGED
@@ -47,6 +47,7 @@ export { tags } from "./tags.js";
47
47
  export { order } from "./order.js";
48
48
  export { app } from "./appObjects.js";
49
49
  export { bookings } from "./bookings.js";
50
+ export { shop } from "./shop.js";
50
51
  export { attestations } from "./attestations.js";
51
52
  export { containers } from "./containers.js";
52
53
  export { lots } from "./lots.js";
@@ -0,0 +1,106 @@
1
+ import type { ShopPublicConfig, ShopProduct, ShopCheckoutInput, ShopCheckoutResult, ShopBuyerOrder, ShopPage, ShopSettingsView, ShopSettingsPatch, ShopConnectStatus, ShopPrice, ShopPriceInput, ShopPriceUpdate, ShopPriceListParams, ShopAdminOrder, ShopOrderListParams, ShopShipInput, ShopRefundInput } from '../types/shop.js';
2
+ export declare namespace shop {
3
+ /**
4
+ * What a shop front shows: currency, the seller's details (UK law: shown before paying), shipping rates,
5
+ * policies, and `ready` (false until the shop can take payments).
6
+ */
7
+ function config(collectionId: string): Promise<ShopPublicConfig>;
8
+ /**
9
+ * What is for sale, in the shop's currency, with stock (`inStock`, `available`) and whether the buyer must
10
+ * sign in first (`needsSignIn`: a proof or access goes to an account).
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const { items } = await shop.products(collectionId)
15
+ * items.map((p) => `${p.name}: ${formatMoney(p.amount, p.currency)}${p.inStock ? '' : ' (sold out)'}`)
16
+ * ```
17
+ */
18
+ function products(collectionId: string, params?: {
19
+ productIds?: string[];
20
+ }): Promise<{
21
+ currency: string;
22
+ items: ShopProduct[];
23
+ }>;
24
+ /**
25
+ * Start paying: the server prices the basket, holds the stock and returns Stripe's checkout page. Send the
26
+ * buyer to `checkoutUrl`. Signed-in buyers' orders are theirs (`shop.myOrders`).
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * const { checkoutUrl } = await shop.checkout(collectionId, {
31
+ * lines: [{ productId: 'soap', quantity: 2 }],
32
+ * shippingRateId: 'std',
33
+ * successUrl: `${location.origin}/thanks`, // ?session_id= is added
34
+ * cancelUrl: `${location.origin}/basket`,
35
+ * })
36
+ * location.href = checkoutUrl
37
+ * ```
38
+ * @throws `SHOP_NOT_READY`, `NOT_FOR_SALE`, `OUT_OF_STOCK`, `SHIPPING_REQUIRED`, `SIGN_IN_REQUIRED`,
39
+ * `DIGITAL_CONSENT_REQUIRED`, `INVALID_INPUT` (e.g. a return page on another site), `PAYMENT_UNAVAILABLE` (nothing was charged)
40
+ */
41
+ function checkout(collectionId: string, input: ShopCheckoutInput): Promise<ShopCheckoutResult>;
42
+ /**
43
+ * The thank-you page: the order a Stripe checkout paid for, from the `session_id` on the success URL.
44
+ * Its `status` is `pending-payment` for a moment while the payment is confirmed: check again shortly.
45
+ * `downloads` has links for bought files once it's paid.
46
+ */
47
+ function orderBySession(collectionId: string, sessionId: string): Promise<ShopBuyerOrder>;
48
+ /** The signed-in buyer's orders, newest first, with their download links. @throws `SIGN_IN_REQUIRED` */
49
+ function myOrders(collectionId: string, params?: {
50
+ limit?: number;
51
+ offset?: number;
52
+ }): Promise<ShopPage<ShopBuyerOrder>>;
53
+ /** Format minor units as money for the person: `formatMoney(1299, 'GBP')` → "£12.99". */
54
+ function formatMoney(amount: number, currency: string, locale?: string): string;
55
+ /** The seller's side: settings, Stripe, prices and orders (collection admins). */
56
+ namespace admin {
57
+ /** The settings, what's still needed to take payments (`readiness`), and what SmartLinks takes (`fee`, read-only). */
58
+ function settings(collectionId: string): Promise<ShopSettingsView>;
59
+ /** Change settings: merged into what's stored; `shipping.rates` is replaced as a whole. */
60
+ function updateSettings(collectionId: string, patch: ShopSettingsPatch): Promise<ShopSettingsView>;
61
+ /**
62
+ * Connect the seller's Stripe account (a new one, set up on Stripe's own pages). Send the seller to `url`;
63
+ * Stripe brings them back to `returnUrl` (done or not: check `connectStatus`) or `refreshUrl` (the link ran out).
64
+ */
65
+ function connect(collectionId: string, input: {
66
+ returnUrl: string;
67
+ refreshUrl: string;
68
+ }): Promise<{
69
+ url: string;
70
+ expiresAt: string;
71
+ }>;
72
+ /** Whether Stripe is connected, and whether it can take payments and pay out yet. */
73
+ function connectStatus(collectionId: string): Promise<ShopConnectStatus>;
74
+ function listPrices(collectionId: string, params?: ShopPriceListParams): Promise<ShopPage<ShopPrice>>;
75
+ /**
76
+ * Put a product (or variant) on sale, or change its price in that currency (one price per product per
77
+ * currency). @throws `INVALID_INPUT`, `APP_NOT_INSTALLED`
78
+ */
79
+ function setPrice(collectionId: string, input: ShopPriceInput): Promise<ShopPrice>;
80
+ function updatePrice(collectionId: string, priceId: string, changes: ShopPriceUpdate): Promise<ShopPrice>;
81
+ /** Take it off sale for good (or set `active: false` to pause). */
82
+ function removePrice(collectionId: string, priceId: string): Promise<{
83
+ deleted: true;
84
+ }>;
85
+ /** Shop orders, newest first. Unpaid and expired checkouts are left out unless asked for. */
86
+ function listOrders(collectionId: string, params?: ShopOrderListParams): Promise<ShopPage<ShopAdminOrder>>;
87
+ function getOrder(collectionId: string, orderId: string): Promise<ShopAdminOrder>;
88
+ /**
89
+ * Send some or all of what's left, with tracking. The held stock leaves; the buyer's "shipped" email
90
+ * goes out if the shop has one. @throws `NOT_SHIPPABLE`, `NOTHING_TO_SHIP`
91
+ */
92
+ function ship(collectionId: string, orderId: string, input?: ShopShipInput): Promise<ShopAdminOrder>;
93
+ /**
94
+ * Refund through Stripe: lines, shipping, a goodwill amount, or everything left. Unsent stock goes back;
95
+ * digital delivery is withdrawn. @throws `NOT_REFUNDABLE`, `NOTHING_TO_REFUND`
96
+ */
97
+ function refund(collectionId: string, orderId: string, input?: ShopRefundInput): Promise<ShopAdminOrder>;
98
+ /** An app that delivers a line itself (`deliveryConfig.kind: 'app'`) says it's done. */
99
+ function markDelivered(collectionId: string, orderId: string, itemId: string, input?: {
100
+ note?: string;
101
+ reference?: string;
102
+ }): Promise<ShopAdminOrder>;
103
+ /** Cancel a checkout that hasn't been paid (its stock is released). @throws `NOT_PENDING` */
104
+ function cancelCheckout(collectionId: string, orderId: string): Promise<ShopAdminOrder>;
105
+ }
106
+ }
@@ -0,0 +1,171 @@
1
+ // src/api/shop.ts
2
+ // Basic commerce — see docs/shop.md. A shop front (any page, any site) uses the public calls; the Shop app and
3
+ // Studio use `shop.admin`. Prices, totals and "paid" are never taken from the browser: the server prices the
4
+ // basket, and only Stripe's webhook marks an order paid.
5
+ import { request, post, put, patch, del } from '../http.js';
6
+ const pub = (collectionId) => `/public/collection/${encodeURIComponent(collectionId)}/shop`;
7
+ const adm = (collectionId) => `/admin/collection/${encodeURIComponent(collectionId)}/shop`;
8
+ function qs(params) {
9
+ if (!params)
10
+ return '';
11
+ const parts = [];
12
+ for (const [k, v] of Object.entries(params)) {
13
+ if (v === undefined || v === null || v === '')
14
+ continue;
15
+ parts.push(`${encodeURIComponent(k)}=${encodeURIComponent(Array.isArray(v) ? v.join(',') : String(v))}`);
16
+ }
17
+ return parts.length ? `?${parts.join('&')}` : '';
18
+ }
19
+ export var shop;
20
+ (function (shop) {
21
+ /**
22
+ * What a shop front shows: currency, the seller's details (UK law: shown before paying), shipping rates,
23
+ * policies, and `ready` (false until the shop can take payments).
24
+ */
25
+ async function config(collectionId) {
26
+ return request(`${pub(collectionId)}/config`);
27
+ }
28
+ shop.config = config;
29
+ /**
30
+ * What is for sale, in the shop's currency, with stock (`inStock`, `available`) and whether the buyer must
31
+ * sign in first (`needsSignIn`: a proof or access goes to an account).
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * const { items } = await shop.products(collectionId)
36
+ * items.map((p) => `${p.name}: ${formatMoney(p.amount, p.currency)}${p.inStock ? '' : ' (sold out)'}`)
37
+ * ```
38
+ */
39
+ async function products(collectionId, params) {
40
+ return request(`${pub(collectionId)}/products${qs({ productIds: params === null || params === void 0 ? void 0 : params.productIds })}`);
41
+ }
42
+ shop.products = products;
43
+ /**
44
+ * Start paying: the server prices the basket, holds the stock and returns Stripe's checkout page. Send the
45
+ * buyer to `checkoutUrl`. Signed-in buyers' orders are theirs (`shop.myOrders`).
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * const { checkoutUrl } = await shop.checkout(collectionId, {
50
+ * lines: [{ productId: 'soap', quantity: 2 }],
51
+ * shippingRateId: 'std',
52
+ * successUrl: `${location.origin}/thanks`, // ?session_id= is added
53
+ * cancelUrl: `${location.origin}/basket`,
54
+ * })
55
+ * location.href = checkoutUrl
56
+ * ```
57
+ * @throws `SHOP_NOT_READY`, `NOT_FOR_SALE`, `OUT_OF_STOCK`, `SHIPPING_REQUIRED`, `SIGN_IN_REQUIRED`,
58
+ * `DIGITAL_CONSENT_REQUIRED`, `INVALID_INPUT` (e.g. a return page on another site), `PAYMENT_UNAVAILABLE` (nothing was charged)
59
+ */
60
+ async function checkout(collectionId, input) {
61
+ return post(`${pub(collectionId)}/checkout`, input);
62
+ }
63
+ shop.checkout = checkout;
64
+ /**
65
+ * The thank-you page: the order a Stripe checkout paid for, from the `session_id` on the success URL.
66
+ * Its `status` is `pending-payment` for a moment while the payment is confirmed: check again shortly.
67
+ * `downloads` has links for bought files once it's paid.
68
+ */
69
+ async function orderBySession(collectionId, sessionId) {
70
+ return request(`${pub(collectionId)}/orders/by-session/${encodeURIComponent(sessionId)}`);
71
+ }
72
+ shop.orderBySession = orderBySession;
73
+ /** The signed-in buyer's orders, newest first, with their download links. @throws `SIGN_IN_REQUIRED` */
74
+ async function myOrders(collectionId, params) {
75
+ return request(`${pub(collectionId)}/orders/mine${qs(params)}`);
76
+ }
77
+ shop.myOrders = myOrders;
78
+ /** Format minor units as money for the person: `formatMoney(1299, 'GBP')` → "£12.99". */
79
+ function formatMoney(amount, currency, locale) {
80
+ try {
81
+ return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(amount / 100);
82
+ }
83
+ catch (_a) {
84
+ return `${(amount / 100).toFixed(2)} ${currency}`;
85
+ }
86
+ }
87
+ shop.formatMoney = formatMoney;
88
+ /** The seller's side: settings, Stripe, prices and orders (collection admins). */
89
+ let admin;
90
+ (function (admin) {
91
+ /** The settings, what's still needed to take payments (`readiness`), and what SmartLinks takes (`fee`, read-only). */
92
+ async function settings(collectionId) {
93
+ return request(`${adm(collectionId)}/settings`);
94
+ }
95
+ admin.settings = settings;
96
+ /** Change settings: merged into what's stored; `shipping.rates` is replaced as a whole. */
97
+ async function updateSettings(collectionId, patch) {
98
+ return put(`${adm(collectionId)}/settings`, patch);
99
+ }
100
+ admin.updateSettings = updateSettings;
101
+ /**
102
+ * Connect the seller's Stripe account (a new one, set up on Stripe's own pages). Send the seller to `url`;
103
+ * Stripe brings them back to `returnUrl` (done or not: check `connectStatus`) or `refreshUrl` (the link ran out).
104
+ */
105
+ async function connect(collectionId, input) {
106
+ return post(`${adm(collectionId)}/connect`, input);
107
+ }
108
+ admin.connect = connect;
109
+ /** Whether Stripe is connected, and whether it can take payments and pay out yet. */
110
+ async function connectStatus(collectionId) {
111
+ return request(`${adm(collectionId)}/connect/status`);
112
+ }
113
+ admin.connectStatus = connectStatus;
114
+ async function listPrices(collectionId, params) {
115
+ return request(`${adm(collectionId)}/prices${qs(params)}`);
116
+ }
117
+ admin.listPrices = listPrices;
118
+ /**
119
+ * Put a product (or variant) on sale, or change its price in that currency (one price per product per
120
+ * currency). @throws `INVALID_INPUT`, `APP_NOT_INSTALLED`
121
+ */
122
+ async function setPrice(collectionId, input) {
123
+ return post(`${adm(collectionId)}/prices`, input);
124
+ }
125
+ admin.setPrice = setPrice;
126
+ async function updatePrice(collectionId, priceId, changes) {
127
+ return patch(`${adm(collectionId)}/prices/${encodeURIComponent(priceId)}`, changes);
128
+ }
129
+ admin.updatePrice = updatePrice;
130
+ /** Take it off sale for good (or set `active: false` to pause). */
131
+ async function removePrice(collectionId, priceId) {
132
+ return del(`${adm(collectionId)}/prices/${encodeURIComponent(priceId)}`);
133
+ }
134
+ admin.removePrice = removePrice;
135
+ /** Shop orders, newest first. Unpaid and expired checkouts are left out unless asked for. */
136
+ async function listOrders(collectionId, params) {
137
+ return request(`${adm(collectionId)}/orders${qs(params)}`);
138
+ }
139
+ admin.listOrders = listOrders;
140
+ async function getOrder(collectionId, orderId) {
141
+ return request(`${adm(collectionId)}/orders/${encodeURIComponent(orderId)}`);
142
+ }
143
+ admin.getOrder = getOrder;
144
+ /**
145
+ * Send some or all of what's left, with tracking. The held stock leaves; the buyer's "shipped" email
146
+ * goes out if the shop has one. @throws `NOT_SHIPPABLE`, `NOTHING_TO_SHIP`
147
+ */
148
+ async function ship(collectionId, orderId, input = {}) {
149
+ return post(`${adm(collectionId)}/orders/${encodeURIComponent(orderId)}/ship`, input);
150
+ }
151
+ admin.ship = ship;
152
+ /**
153
+ * Refund through Stripe: lines, shipping, a goodwill amount, or everything left. Unsent stock goes back;
154
+ * digital delivery is withdrawn. @throws `NOT_REFUNDABLE`, `NOTHING_TO_REFUND`
155
+ */
156
+ async function refund(collectionId, orderId, input = {}) {
157
+ return post(`${adm(collectionId)}/orders/${encodeURIComponent(orderId)}/refund`, input);
158
+ }
159
+ admin.refund = refund;
160
+ /** An app that delivers a line itself (`deliveryConfig.kind: 'app'`) says it's done. */
161
+ async function markDelivered(collectionId, orderId, itemId, input = {}) {
162
+ return post(`${adm(collectionId)}/orders/${encodeURIComponent(orderId)}/lines/${encodeURIComponent(itemId)}/delivered`, input);
163
+ }
164
+ admin.markDelivered = markDelivered;
165
+ /** Cancel a checkout that hasn't been paid (its stock is released). @throws `NOT_PENDING` */
166
+ async function cancelCheckout(collectionId, orderId) {
167
+ return post(`${adm(collectionId)}/orders/${encodeURIComponent(orderId)}/expire`, {});
168
+ }
169
+ admin.cancelCheckout = cancelCheckout;
170
+ })(admin = shop.admin || (shop.admin = {}));
171
+ })(shop || (shop = {}));
@@ -81,6 +81,23 @@ export declare namespace wallet {
81
81
  ok: true;
82
82
  pass: WalletPass;
83
83
  }>;
84
+ /**
85
+ * The same lock-screen message on every active pass for one thing, e.g. everyone holding a ticket for an event
86
+ * (admin).
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * await wallet.messageAll('coll_123', { type: 'event', id: eventId }, { header: 'Doors open', body: 'Doors open at 7pm' })
91
+ * ```
92
+ */
93
+ function messageAll(collectionId: string, ref: WalletPassRef, msg: {
94
+ header?: string;
95
+ body: string;
96
+ }): Promise<{
97
+ ok: true;
98
+ passes: number;
99
+ sent: number;
100
+ }>;
84
101
  /** A lock-screen message on the pass (admin). */
85
102
  function message(collectionId: string, passId: string, msg: {
86
103
  header?: string;
@@ -78,6 +78,19 @@ export var wallet;
78
78
  return post(`${adm(collectionId)}/passes/${encodeURIComponent(passId)}/void`, { reason });
79
79
  }
80
80
  wallet.voidPass = voidPass;
81
+ /**
82
+ * The same lock-screen message on every active pass for one thing, e.g. everyone holding a ticket for an event
83
+ * (admin).
84
+ *
85
+ * @example
86
+ * ```typescript
87
+ * await wallet.messageAll('coll_123', { type: 'event', id: eventId }, { header: 'Doors open', body: 'Doors open at 7pm' })
88
+ * ```
89
+ */
90
+ async function messageAll(collectionId, ref, msg) {
91
+ return post(`${adm(collectionId)}/message`, Object.assign({ ref }, msg));
92
+ }
93
+ wallet.messageAll = messageAll;
81
94
  /** A lock-screen message on the pass (admin). */
82
95
  async function message(collectionId, passId, msg) {
83
96
  return post(`${adm(collectionId)}/passes/${encodeURIComponent(passId)}/message`, msg);