@mailoo/market 1.35.10 → 1.35.12
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 +70 -0
- package/dist/wholesale-price-list/index.cjs +1068 -0
- package/dist/wholesale-price-list/index.cjs.map +1 -0
- package/dist/wholesale-price-list/index.d.cts +327 -0
- package/dist/wholesale-price-list/index.d.ts +327 -0
- package/dist/wholesale-price-list/index.js +1006 -0
- package/dist/wholesale-price-list/index.js.map +1 -0
- package/package.json +12 -2
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared wholesale price-list generator (MAI-110 v1 / AMS-25).
|
|
3
|
+
*
|
|
4
|
+
* Entry: `renderPriceListXlsx(input)` from
|
|
5
|
+
* `@mailoo/market/wholesale-price-list` (Node / Route Handler only).
|
|
6
|
+
*
|
|
7
|
+
* v1: one unit-price column, no quantity tiers, no min-order column.
|
|
8
|
+
* Quantity breaks / MOQ are deferred (AMS-28).
|
|
9
|
+
*/
|
|
10
|
+
/** Built-in label locales. Other `locale` strings still work; labels fall back to en. */
|
|
11
|
+
type PriceListBuiltinLocale = 'hy' | 'ru' | 'en' | 'de' | 'es';
|
|
12
|
+
type PriceListHeader = {
|
|
13
|
+
siteName: string;
|
|
14
|
+
/**
|
|
15
|
+
* Absolute logo URL, or null. SVG is accepted — the generator rasterizes
|
|
16
|
+
* to PNG for xlsx embed (and for Sheets when bytes are available).
|
|
17
|
+
*/
|
|
18
|
+
logoUrl: string | null;
|
|
19
|
+
/** Optional pre-fetched logo bytes (svg / png / jpeg / webp). */
|
|
20
|
+
logoBytes?: Uint8Array | null;
|
|
21
|
+
/**
|
|
22
|
+
* Contact block as a single pre-formatted string (may contain newlines).
|
|
23
|
+
* Pre-Form: from `MARKET_PRICE_LIST_CONTACTS`. Mailoo admin: see package README —
|
|
24
|
+
* MARKET config has no contacts field yet.
|
|
25
|
+
*/
|
|
26
|
+
contacts: string;
|
|
27
|
+
/** ISO-8601 instant. */
|
|
28
|
+
generatedAt: string;
|
|
29
|
+
/**
|
|
30
|
+
* IANA time zone for the “Generated: …” display (e.g. `Asia/Yerevan`).
|
|
31
|
+
* Omitted → format in UTC.
|
|
32
|
+
*/
|
|
33
|
+
timeZone?: string | null;
|
|
34
|
+
};
|
|
35
|
+
type PriceListRow = {
|
|
36
|
+
productId: string;
|
|
37
|
+
/** null = product has no variants (one row for the product). */
|
|
38
|
+
variantId: string | null;
|
|
39
|
+
category: string | null;
|
|
40
|
+
product: string;
|
|
41
|
+
variant: string | null;
|
|
42
|
+
packaging: string | null;
|
|
43
|
+
/**
|
|
44
|
+
* Unit price in the price measurement unit.
|
|
45
|
+
* `null` / negative / NaN / non-finite → cell shows localized “on request”;
|
|
46
|
+
* Sum stays blank (row still renders; does not fail the whole file).
|
|
47
|
+
*
|
|
48
|
+
* AMS-28 (not in v1): row currently has no `measurementUnit` column —
|
|
49
|
+
* packaging / unit labels stay in `packaging` only.
|
|
50
|
+
*/
|
|
51
|
+
unitPrice: number | null;
|
|
52
|
+
/** ISO 4217; null = unknown (still summed in a single Total when alone). */
|
|
53
|
+
currency: string | null;
|
|
54
|
+
/** Absolute storefront PDP (`?variant=` when variantId set). */
|
|
55
|
+
productUrl: string;
|
|
56
|
+
previewMediaId: string | null;
|
|
57
|
+
/**
|
|
58
|
+
* Absolute cover URL. Used for Sheets `=IMAGE` only when anonymously
|
|
59
|
+
* fetchable (not Mailoo `…/images/public/content?id=` which needs an API key).
|
|
60
|
+
* Prefer `previewImageBytes` / `loadPreviewImage` for xlsx embed.
|
|
61
|
+
*/
|
|
62
|
+
previewImageUrl: string | null;
|
|
63
|
+
/** Optional pre-fetched cover bytes for xlsx embed. */
|
|
64
|
+
previewImageBytes?: Uint8Array | null;
|
|
65
|
+
};
|
|
66
|
+
type PriceListInput = {
|
|
67
|
+
/** One language per file; row/header strings are already localized by the caller. */
|
|
68
|
+
locale: string;
|
|
69
|
+
header: PriceListHeader;
|
|
70
|
+
rows: PriceListRow[];
|
|
71
|
+
/** Optional overrides of built-in hy/ru/en/de/es strings. */
|
|
72
|
+
labels?: Partial<Record<PriceListLabelKey, string>>;
|
|
73
|
+
/**
|
|
74
|
+
* Fetch cover bytes when `previewImageBytes` is omitted.
|
|
75
|
+
* Return null to leave the photo blank.
|
|
76
|
+
* When omitted, the package tries an anonymous GET of `previewImageUrl`
|
|
77
|
+
* (skips Mailoo API-keyed embed URLs).
|
|
78
|
+
*/
|
|
79
|
+
loadPreviewImage?: (args: {
|
|
80
|
+
previewMediaId: string | null;
|
|
81
|
+
previewImageUrl: string | null;
|
|
82
|
+
}) => Promise<Uint8Array | null>;
|
|
83
|
+
/**
|
|
84
|
+
* Fetch logo bytes when `logoBytes` omitted but `logoUrl` set.
|
|
85
|
+
* When omitted, the package tries an anonymous GET of `logoUrl`.
|
|
86
|
+
*/
|
|
87
|
+
loadLogo?: (logoUrl: string) => Promise<Uint8Array | null>;
|
|
88
|
+
/** Square thumbnail edge in px (default 120). */
|
|
89
|
+
photoSizePx?: number;
|
|
90
|
+
};
|
|
91
|
+
type PriceListLabelKey = 'title' | 'photo' | 'category' | 'product' | 'variant' | 'packaging' | 'price' | 'onRequest' | 'link' | 'linkText' | 'quantity' | 'sum' | 'total' | 'generated' | 'filenamePrefix';
|
|
92
|
+
type PriceListColumnKey = 'photo' | 'category' | 'product' | 'variant' | 'packaging' | 'price' | 'link' | 'quantity' | 'sum' | 'currencyHidden';
|
|
93
|
+
type PriceListLayout = {
|
|
94
|
+
locale: string;
|
|
95
|
+
photoSizePx: number;
|
|
96
|
+
/** Fixed data-row height in Excel points (~96 pt ≈ 130 px). */
|
|
97
|
+
dataRowHeightPt: number;
|
|
98
|
+
photoColWidthChars: number;
|
|
99
|
+
/** 1-based Excel row of visible column headers. */
|
|
100
|
+
headerRow: number;
|
|
101
|
+
firstDataRow: number;
|
|
102
|
+
totalRow: number;
|
|
103
|
+
columns: Array<{
|
|
104
|
+
key: PriceListColumnKey;
|
|
105
|
+
header: string;
|
|
106
|
+
index: number;
|
|
107
|
+
letter: string;
|
|
108
|
+
}>;
|
|
109
|
+
priceColLetter: string;
|
|
110
|
+
quantityColLetter: string;
|
|
111
|
+
sumColLetter: string;
|
|
112
|
+
currencyColLetter: string;
|
|
113
|
+
linkColLetter: string;
|
|
114
|
+
};
|
|
115
|
+
type PriceListSheetModel = {
|
|
116
|
+
title: string;
|
|
117
|
+
locale: string;
|
|
118
|
+
layout: PriceListLayout;
|
|
119
|
+
cells: Array<{
|
|
120
|
+
a1: string;
|
|
121
|
+
value: string | number | null;
|
|
122
|
+
formula?: string;
|
|
123
|
+
}>;
|
|
124
|
+
previewImageUrls: Array<string | null>;
|
|
125
|
+
logoUrl: string | null;
|
|
126
|
+
};
|
|
127
|
+
type RenderPriceListXlsxResult = {
|
|
128
|
+
buffer: Buffer;
|
|
129
|
+
filename: string;
|
|
130
|
+
contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
|
131
|
+
layout: PriceListLayout;
|
|
132
|
+
sheetModel: PriceListSheetModel;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
/** ~120 px square → Excel column width in characters (approx). */
|
|
136
|
+
declare const PRICE_LIST_DEFAULT_PHOTO_PX = 120;
|
|
137
|
+
/** Fixed data-row height so rows never jump without a photo (~130 px). */
|
|
138
|
+
declare const PRICE_LIST_DATA_ROW_HEIGHT_PT = 96;
|
|
139
|
+
declare const PRICE_LIST_PHOTO_COL_CHARS = 18;
|
|
140
|
+
/** Rows above the column header: logo/name, contacts, generated. */
|
|
141
|
+
declare const PRICE_LIST_BANNER_ROW_COUNT = 3;
|
|
142
|
+
declare function excelColumnLetter(index1Based: number): string;
|
|
143
|
+
declare function validatePriceListInput(input: PriceListInput): void;
|
|
144
|
+
declare function buildPriceListLayout(input: PriceListInput): PriceListLayout;
|
|
145
|
+
|
|
146
|
+
declare function resolvePriceListLabels(locale: string, overrides?: Partial<Record<PriceListLabelKey, string>>): Record<PriceListLabelKey, string>;
|
|
147
|
+
declare function formatGeneratedLabel(template: string, generatedAt: string, locale: string, timeZone?: string | null): string;
|
|
148
|
+
/**
|
|
149
|
+
* Calendar date for the filename in `timeZone` (IANA), falling back to UTC.
|
|
150
|
+
* Parts are taken from Intl so midnight near zone offsets (e.g. Asia/Yerevan)
|
|
151
|
+
* lands on the correct local YYYY-MM-DD.
|
|
152
|
+
*/
|
|
153
|
+
declare function priceListFilenameDate(generatedAt: string, timeZone?: string | null): string;
|
|
154
|
+
declare function priceListFilename(locale: string, generatedAt: string, prefix: string, timeZone?: string | null): string;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Soft-coerce unit prices for wholesale export.
|
|
158
|
+
* Negative / NaN / non-finite → null (“on request”) so one bad row cannot
|
|
159
|
+
* crash the whole workbook.
|
|
160
|
+
*/
|
|
161
|
+
declare function coerceUnitPrice(value: number | null): number | null;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Best-effort fetch of absolute http(s) image bytes.
|
|
165
|
+
* Used as the package default when callers omit loadPreviewImage / loadLogo.
|
|
166
|
+
* Fail-soft: returns null on any network/HTTP error (do not throw).
|
|
167
|
+
*
|
|
168
|
+
* Do **not** rely on this for Mailoo `…/images/public/content?id=` URLs —
|
|
169
|
+
* those require X-API-Key. Callers must load via media id / API key instead.
|
|
170
|
+
*/
|
|
171
|
+
declare function fetchAbsoluteUrlBytes(url: string): Promise<Uint8Array | null>;
|
|
172
|
+
/**
|
|
173
|
+
* Mailoo library image embed URLs need an API key — Google Sheets =IMAGE
|
|
174
|
+
* cannot send headers. Never put these in =IMAGE(); upload to Drive instead.
|
|
175
|
+
* Avatars (`/api/v1/public/avatars/…`) are anonymously readable and are OK.
|
|
176
|
+
*/
|
|
177
|
+
declare function isMailooApiKeyedImageUrl(url: string): boolean;
|
|
178
|
+
/** Extract `id` from Mailoo `…/images/public/content?id=` URLs. */
|
|
179
|
+
declare function mediaIdFromMailooPublicImageUrl(url: string): string | null;
|
|
180
|
+
/** URL safe to hand to Sheets =IMAGE (anonymous GET). */
|
|
181
|
+
declare function isAnonymousHttpImageUrl(url: string): boolean;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Line sum: quantity × unit price.
|
|
185
|
+
* Blank / non-positive quantity → blank. Non-numeric price (on request) → blank.
|
|
186
|
+
*/
|
|
187
|
+
declare function buildLineSumFormula(args: {
|
|
188
|
+
layout: PriceListLayout;
|
|
189
|
+
/** 1-based Excel row for this data line. */
|
|
190
|
+
row: number;
|
|
191
|
+
}): string;
|
|
192
|
+
declare function buildTotalSumFormula(args: {
|
|
193
|
+
layout: PriceListLayout;
|
|
194
|
+
firstDataRow: number;
|
|
195
|
+
lastDataRow: number;
|
|
196
|
+
}): string;
|
|
197
|
+
/**
|
|
198
|
+
* Per-currency total when the sheet mixes ISO codes.
|
|
199
|
+
* `currency` is written into the formula as a quoted literal.
|
|
200
|
+
*/
|
|
201
|
+
declare function buildCurrencyTotalSumIfFormula(args: {
|
|
202
|
+
layout: PriceListLayout;
|
|
203
|
+
firstDataRow: number;
|
|
204
|
+
lastDataRow: number;
|
|
205
|
+
currency: string;
|
|
206
|
+
}): string;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Fit image into a square canvas with white padding (contain), PNG output.
|
|
210
|
+
* Used for xlsx embedded thumbnails so every photo cell is the same size.
|
|
211
|
+
* SVG input is accepted (sharp rasterizes via librsvg / built-in).
|
|
212
|
+
*/
|
|
213
|
+
declare function normalizePreviewSquare(input: Uint8Array, sizePx?: number): Promise<Buffer>;
|
|
214
|
+
/**
|
|
215
|
+
* Rasterize logo (SVG / PNG / JPEG / WebP) to a PNG suitable for the header
|
|
216
|
+
* box (~120×40). Fail-fast if sharp cannot decode the bytes.
|
|
217
|
+
*/
|
|
218
|
+
declare function rasterizeLogo(input: Uint8Array, opts?: {
|
|
219
|
+
widthPx?: number;
|
|
220
|
+
heightPx?: number;
|
|
221
|
+
}): Promise<Buffer>;
|
|
222
|
+
/** Google Sheets IMAGE formula, mode 4 = custom size (fixed cell look). */
|
|
223
|
+
declare function googleSheetsImageFormula(url: string, sizePx: number): string;
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Render a wholesale price-list workbook.
|
|
227
|
+
* Single price column; Sum = Quantity × Price; Total at bottom.
|
|
228
|
+
*/
|
|
229
|
+
declare function renderPriceListXlsx(input: PriceListInput): Promise<RenderPriceListXlsxResult>;
|
|
230
|
+
/** @deprecated Use `renderPriceListXlsx`. */
|
|
231
|
+
declare const buildWholesalePriceListXlsx: typeof renderPriceListXlsx;
|
|
232
|
+
|
|
233
|
+
/** Structural Sheets client surface used by this module (no googleapis types). */
|
|
234
|
+
type PriceListGoogleSheetsClient = {
|
|
235
|
+
spreadsheets: {
|
|
236
|
+
create: (args: {
|
|
237
|
+
requestBody: unknown;
|
|
238
|
+
fields?: string;
|
|
239
|
+
}) => Promise<{
|
|
240
|
+
data: {
|
|
241
|
+
spreadsheetId?: string | null;
|
|
242
|
+
spreadsheetUrl?: string | null;
|
|
243
|
+
};
|
|
244
|
+
}>;
|
|
245
|
+
get: (args: {
|
|
246
|
+
spreadsheetId: string;
|
|
247
|
+
fields?: string;
|
|
248
|
+
}) => Promise<{
|
|
249
|
+
data: {
|
|
250
|
+
sheets?: Array<{
|
|
251
|
+
properties?: {
|
|
252
|
+
sheetId?: number | null;
|
|
253
|
+
} | null;
|
|
254
|
+
}> | null;
|
|
255
|
+
};
|
|
256
|
+
}>;
|
|
257
|
+
values: {
|
|
258
|
+
update: (args: {
|
|
259
|
+
spreadsheetId: string;
|
|
260
|
+
range: string;
|
|
261
|
+
valueInputOption?: string;
|
|
262
|
+
requestBody: {
|
|
263
|
+
values: string[][];
|
|
264
|
+
};
|
|
265
|
+
}) => Promise<unknown>;
|
|
266
|
+
};
|
|
267
|
+
batchUpdate: (args: {
|
|
268
|
+
spreadsheetId: string;
|
|
269
|
+
requestBody: {
|
|
270
|
+
requests: unknown[];
|
|
271
|
+
};
|
|
272
|
+
}) => Promise<unknown>;
|
|
273
|
+
};
|
|
274
|
+
};
|
|
275
|
+
/** Structural Drive client surface used by this module (no googleapis types). */
|
|
276
|
+
type PriceListGoogleDriveClient = {
|
|
277
|
+
files: {
|
|
278
|
+
create: (args: unknown) => Promise<{
|
|
279
|
+
data: {
|
|
280
|
+
id?: string | null;
|
|
281
|
+
};
|
|
282
|
+
}>;
|
|
283
|
+
get: (args: {
|
|
284
|
+
fileId: string;
|
|
285
|
+
fields?: string;
|
|
286
|
+
supportsAllDrives?: boolean;
|
|
287
|
+
}) => Promise<{
|
|
288
|
+
data: {
|
|
289
|
+
parents?: string[] | null;
|
|
290
|
+
};
|
|
291
|
+
}>;
|
|
292
|
+
update: (args: unknown) => Promise<unknown>;
|
|
293
|
+
};
|
|
294
|
+
permissions: {
|
|
295
|
+
create: (args: unknown) => Promise<unknown>;
|
|
296
|
+
};
|
|
297
|
+
};
|
|
298
|
+
type PriceListGoogleClients = {
|
|
299
|
+
sheets: PriceListGoogleSheetsClient;
|
|
300
|
+
drive: PriceListGoogleDriveClient;
|
|
301
|
+
};
|
|
302
|
+
type PriceListGoogleSheetsOptions = {
|
|
303
|
+
/** Raw service-account JSON string (or already-parsed object). */
|
|
304
|
+
serviceAccountJson: string | Record<string, unknown>;
|
|
305
|
+
/** Optional Drive folder id; SA must have write access to the folder. */
|
|
306
|
+
driveFolderId?: string | null;
|
|
307
|
+
/** Optional email to share the new spreadsheet with (writer). */
|
|
308
|
+
shareWithEmail?: string | null;
|
|
309
|
+
/** Injected Google clients (tests). When omitted, built from serviceAccountJson. */
|
|
310
|
+
clients?: PriceListGoogleClients;
|
|
311
|
+
};
|
|
312
|
+
type RenderPriceListGoogleSheetResult = {
|
|
313
|
+
spreadsheetId: string;
|
|
314
|
+
spreadsheetUrl: string;
|
|
315
|
+
};
|
|
316
|
+
declare function createPriceListGoogleClients(serviceAccountJson: string | Record<string, unknown>): Promise<PriceListGoogleClients>;
|
|
317
|
+
/**
|
|
318
|
+
* Create a Google Spreadsheet with the same structure as the xlsx export.
|
|
319
|
+
* Photos/logo for =IMAGE:
|
|
320
|
+
* 1) When bytes are available → upload PNG to Drive (`uc?export=view&id=…`)
|
|
321
|
+
* with anyone-reader (Sheets can fetch without an API key).
|
|
322
|
+
* 2) Else only anonymously fetchable http(s) URLs (never Mailoo
|
|
323
|
+
* `…/images/public/content?id=` which requires X-API-Key).
|
|
324
|
+
*/
|
|
325
|
+
declare function renderPriceListGoogleSheet(input: PriceListInput, options: PriceListGoogleSheetsOptions): Promise<RenderPriceListGoogleSheetResult>;
|
|
326
|
+
|
|
327
|
+
export { PRICE_LIST_BANNER_ROW_COUNT, PRICE_LIST_DATA_ROW_HEIGHT_PT, PRICE_LIST_DEFAULT_PHOTO_PX, PRICE_LIST_PHOTO_COL_CHARS, type PriceListBuiltinLocale, type PriceListColumnKey, type PriceListGoogleClients, type PriceListGoogleDriveClient, type PriceListGoogleSheetsClient, type PriceListGoogleSheetsOptions, type PriceListHeader, type PriceListInput, type PriceListLabelKey, type PriceListLayout, type PriceListRow, type PriceListSheetModel, type RenderPriceListGoogleSheetResult, type RenderPriceListXlsxResult, buildCurrencyTotalSumIfFormula, buildLineSumFormula, buildPriceListLayout, buildTotalSumFormula, buildWholesalePriceListXlsx, coerceUnitPrice, createPriceListGoogleClients, excelColumnLetter, fetchAbsoluteUrlBytes, formatGeneratedLabel, googleSheetsImageFormula, isAnonymousHttpImageUrl, isMailooApiKeyedImageUrl, mediaIdFromMailooPublicImageUrl, normalizePreviewSquare, priceListFilename, priceListFilenameDate, rasterizeLogo, renderPriceListGoogleSheet, renderPriceListXlsx, resolvePriceListLabels, validatePriceListInput };
|