@mark1russell7/market-sources 0.2.6 → 0.2.7
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 +39 -2
- package/build/index.d.ts +114 -5
- package/build/index.d.ts.map +1 -1
- package/build/index.js +558 -16
- package/build/index.js.map +1 -1
- package/package.json +5 -3
package/build/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { instantSchema, moneySchema, offerId, offerSchema, parseGtin, priceReadingSchema } from "@mark1russell7/market";
|
|
1
|
+
import { instantSchema, meteredKindOf, meteredObservationSchema, moneySchema, offerId, offerSchema, parseGtin, priceReadingSchema } from "@mark1russell7/market";
|
|
2
|
+
import { canonicalUnit, rationals } from "@mark1russell7/quantity";
|
|
2
3
|
import { z } from "zod";
|
|
3
4
|
import { redactSecrets } from "@mark1russell7/text/redact";
|
|
4
5
|
import { ApiError, readGrant, send, sendWithToken, statusLine as statusLine$1, tokenStore } from "polite-crawler/api";
|
|
@@ -132,6 +133,21 @@ function observe(offer, reading, notes) {
|
|
|
132
133
|
reading: checkedReading.data
|
|
133
134
|
};
|
|
134
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* This function examines a metered offer and its reading with the schemas of `@mark1russell7/market`. It gives
|
|
138
|
+
* the observation, or it adds a note and gives undefined.
|
|
139
|
+
*/
|
|
140
|
+
function observeMetered(offer, reading, notes) {
|
|
141
|
+
const checked = meteredObservationSchema.safeParse({
|
|
142
|
+
offer: compact(offer),
|
|
143
|
+
reading: compact(reading)
|
|
144
|
+
});
|
|
145
|
+
if (!checked.success) {
|
|
146
|
+
notes.push(`The offer ${offer.id} is skipped: the schema refuses it (${issues(checked.error)}).`);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
return checked.data;
|
|
150
|
+
}
|
|
135
151
|
/** This function gives a result with the observations. An offer that the answer repeats counts once. */
|
|
136
152
|
function done(observations, notes) {
|
|
137
153
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -152,6 +168,462 @@ function refused(...problems) {
|
|
|
152
168
|
problems
|
|
153
169
|
};
|
|
154
170
|
}
|
|
171
|
+
// src/azure.ts
|
|
172
|
+
/** The name of the Azure Retail Prices API as a source of metered offers. */
|
|
173
|
+
const AZURE_SOURCE = "azure";
|
|
174
|
+
/** The provider of the location of each offer of the Azure Retail Prices API. */
|
|
175
|
+
const AZURE_PROVIDER = "azure";
|
|
176
|
+
/**
|
|
177
|
+
* The terms of the Azure Retail Prices API. The API needs no key, and its documentation names tools for internal
|
|
178
|
+
* analysis and price comparison. The Microsoft APIs Terms of Use permit copies only for the use of the
|
|
179
|
+
* application, and they forbid redistribution and resale of the data.
|
|
180
|
+
*/
|
|
181
|
+
const AZURE_TERMS = {
|
|
182
|
+
source: AZURE_SOURCE,
|
|
183
|
+
retention: { kind: "keep" },
|
|
184
|
+
deleteWhenEnded: false,
|
|
185
|
+
priceModel: true,
|
|
186
|
+
clause: "Azure Retail Prices REST API overview, read on 2026-11-18, and Microsoft APIs Terms of Use of October 2025, sections 3(b)(4) and 3(b)(14): copies only as the application needs them, and no redistribution or resale"
|
|
187
|
+
};
|
|
188
|
+
/** The address of the Azure Retail Prices API. */
|
|
189
|
+
const AZURE_PRICES_URL = "https://prices.azure.com/api/retail/prices";
|
|
190
|
+
/** The version of the API that the request builder asks for. Only this version gives the prices of savings plans. */
|
|
191
|
+
const AZURE_API_VERSION = "2023-01-01-preview";
|
|
192
|
+
/** The fields of a filter of the Azure Retail Prices API, from its documentation. */
|
|
193
|
+
const AZURE_FILTER_FIELDS = [
|
|
194
|
+
"armRegionName",
|
|
195
|
+
"location",
|
|
196
|
+
"meterId",
|
|
197
|
+
"meterName",
|
|
198
|
+
"productId",
|
|
199
|
+
"skuId",
|
|
200
|
+
"productName",
|
|
201
|
+
"skuName",
|
|
202
|
+
"serviceName",
|
|
203
|
+
"serviceId",
|
|
204
|
+
"serviceFamily",
|
|
205
|
+
"priceType",
|
|
206
|
+
"armSkuName"
|
|
207
|
+
];
|
|
208
|
+
/** The longest value of a field of a filter, in code units. */
|
|
209
|
+
const FILTER_VALUE_LIMIT = 200;
|
|
210
|
+
/**
|
|
211
|
+
* This function gives the URL of a read of the Azure Retail Prices API with a filter. Each field of the filter
|
|
212
|
+
* gives the term `field eq 'value'`, and the terms join with `and`. Without a currency, the API gives US dollars.
|
|
213
|
+
*
|
|
214
|
+
* @throws RangeError when the filter has no field, or a field that the API does not filter. It also throws when a
|
|
215
|
+
* value is empty, has a space at an end, has a control character or has more than 200 characters. A currency that
|
|
216
|
+
* `@mark1russell7/quantity` does not have also gives a `RangeError`.
|
|
217
|
+
*/
|
|
218
|
+
function azurePricesUrl(filter, currency) {
|
|
219
|
+
const fields = AZURE_FILTER_FIELDS;
|
|
220
|
+
const other = Object.keys(filter).find((name) => !fields.includes(name));
|
|
221
|
+
if (other !== void 0) throw new RangeError(`The API does not filter the field ${show(other)}.`);
|
|
222
|
+
const terms = [];
|
|
223
|
+
for (const name of AZURE_FILTER_FIELDS) {
|
|
224
|
+
const value = filter[name];
|
|
225
|
+
if (value === void 0) continue;
|
|
226
|
+
if (typeof value !== "string" || value === "" || value !== value.trim() || value.length > FILTER_VALUE_LIMIT || /[\u0000-\u001f\u007f]/.test(value)) throw new RangeError(`The value of the field ${name} is not a text of 1 to ${FILTER_VALUE_LIMIT} characters with no control character and no space at an end.`);
|
|
227
|
+
terms.push(`${name} eq '${value.replaceAll("'", "''")}'`);
|
|
228
|
+
}
|
|
229
|
+
if (terms.length === 0) throw new RangeError("The filter has no field. A read of each price of Azure is a large crawl.");
|
|
230
|
+
const query = [`api-version=${AZURE_API_VERSION}`];
|
|
231
|
+
if (currency !== void 0) {
|
|
232
|
+
if (!/^[A-Z]{3}$/.test(currency) || !isCurrency(currency)) throw new RangeError(`The currency ${show(currency)} is not a money unit of @mark1russell7/quantity.`);
|
|
233
|
+
query.push(`currencyCode=${encodeURIComponent(`'${currency}'`)}`);
|
|
234
|
+
}
|
|
235
|
+
query.push(`$filter=${encodeURIComponent(terms.join(" and "))}`);
|
|
236
|
+
return `${AZURE_PRICES_URL}?${query.join("&")}`;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* The pattern of a link to the Azure Retail Prices API: `https`, the host `prices.azure.com`, no port or the port 443,
|
|
240
|
+
* the path `/api/retail/prices` and an optional query. The pattern refuses a user name, a password and a fragment.
|
|
241
|
+
*/
|
|
242
|
+
const AZURE_PRICES_LINK = /^https:\/\/prices\.azure\.com(?::443)?\/api\/retail\/prices(?:\?[^#\s]*)?$/;
|
|
243
|
+
/** This function tells if a link goes to the Azure Retail Prices API (`AZURE_PRICES_LINK`). */
|
|
244
|
+
function isAzurePricesUrl(link) {
|
|
245
|
+
return AZURE_PRICES_LINK.test(link);
|
|
246
|
+
}
|
|
247
|
+
/** This function gives the link of the next page of an answer, or null. A link to another address gives null. */
|
|
248
|
+
function azureNextPage(answer) {
|
|
249
|
+
const link = text(field(answer, "NextPageLink"));
|
|
250
|
+
return link !== void 0 && isAzurePricesUrl(link) ? link : null;
|
|
251
|
+
}
|
|
252
|
+
/** The words of an amount in a unit of measure, and their units. A GB of Azure is 2^30 bytes, thus a GiB. */
|
|
253
|
+
const AMOUNT_WORDS = {
|
|
254
|
+
Second: "s",
|
|
255
|
+
Seconds: "s",
|
|
256
|
+
Minute: "min",
|
|
257
|
+
Minutes: "min",
|
|
258
|
+
Hour: "h",
|
|
259
|
+
Hours: "h",
|
|
260
|
+
Day: "d",
|
|
261
|
+
Days: "d",
|
|
262
|
+
Month: "mo",
|
|
263
|
+
Months: "mo",
|
|
264
|
+
Year: "yr",
|
|
265
|
+
Years: "yr",
|
|
266
|
+
KB: "KiB",
|
|
267
|
+
KiB: "KiB",
|
|
268
|
+
MB: "MiB",
|
|
269
|
+
MiB: "MiB",
|
|
270
|
+
GB: "GiB",
|
|
271
|
+
GiB: "GiB",
|
|
272
|
+
TB: "TiB",
|
|
273
|
+
TiB: "TiB"
|
|
274
|
+
};
|
|
275
|
+
/** The words of a time after the slash of a unit of measure, and their units. */
|
|
276
|
+
const PER_WORDS = {
|
|
277
|
+
Second: "s",
|
|
278
|
+
Minute: "min",
|
|
279
|
+
Hour: "h",
|
|
280
|
+
Day: "d",
|
|
281
|
+
Month: "mo",
|
|
282
|
+
Year: "yr"
|
|
283
|
+
};
|
|
284
|
+
/** A unit of measure: a count with `K` or `M`, a word of an amount, and `/` and a time. Only the count is necessary. */
|
|
285
|
+
const UNIT_OF_MEASURE = /^([1-9][0-9]{0,8})([KM]?)(?: ([A-Za-z]+))?(?:\/([A-Za-z]+))?$/;
|
|
286
|
+
/**
|
|
287
|
+
* This function gives the unit of a unit of measure of the API, or undefined when the parser does not know it. A
|
|
288
|
+
* count with no word gives `{count}`, for example `10K` gives 10,000 of `{count}`. A word of data or time gives
|
|
289
|
+
* its unit, and `/Month` multiplies it by a month. Only `1/Hour` and `1/Month` give a time alone.
|
|
290
|
+
*/
|
|
291
|
+
function azureUnit(unitOfMeasure) {
|
|
292
|
+
const match = UNIT_OF_MEASURE.exec(unitOfMeasure);
|
|
293
|
+
if (match === null) return void 0;
|
|
294
|
+
const [, digits = "", multiplier = "", word, per] = match;
|
|
295
|
+
const count = Number(digits) * (multiplier === "K" ? 1e3 : multiplier === "M" ? 1e6 : 1);
|
|
296
|
+
const amount = word === void 0 ? void 0 : AMOUNT_WORDS[word];
|
|
297
|
+
const time = per === void 0 ? void 0 : PER_WORDS[per];
|
|
298
|
+
if (word !== void 0 && amount === void 0 || per !== void 0 && time === void 0) return void 0;
|
|
299
|
+
let unit;
|
|
300
|
+
if (amount !== void 0) unit = time === void 0 ? amount : `${amount}·${time}`;
|
|
301
|
+
else if (time !== void 0 && count === 1) unit = time;
|
|
302
|
+
else unit = time === void 0 ? "{count}" : `{count}·${time}`;
|
|
303
|
+
try {
|
|
304
|
+
meteredKindOf(priceUnit("USD", unit));
|
|
305
|
+
} catch {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
return {
|
|
309
|
+
count,
|
|
310
|
+
unit
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
function priceUnit(currency, unit) {
|
|
314
|
+
return canonicalUnit(unit.includes("·") ? `${currency}/(${unit})` : `${currency}/${unit}`);
|
|
315
|
+
}
|
|
316
|
+
/** This function gives the exact price of one unit, from the price of `count` units, as the nearest number. */
|
|
317
|
+
function perUnit(price, count) {
|
|
318
|
+
return rationals.toNumber(rationals.div(rationals.fromNumber(price), rationals.fromNumber(count)));
|
|
319
|
+
}
|
|
320
|
+
/** The text of a spot term at the end of a SKU name: `Spot`, or `Low Priority` of the older VMs that Azure can end. */
|
|
321
|
+
const SPOT = /(?:^|\s)(?:Spot|Low Priority)$/;
|
|
322
|
+
/** A term of a reservation or of a savings plan: `1 Year`, `3 Years` or `10 Years`. */
|
|
323
|
+
const YEARS = /^([1-9][0-9]?) Years?$/;
|
|
324
|
+
function yearsOf(value) {
|
|
325
|
+
const match = YEARS.exec(text(value) ?? "");
|
|
326
|
+
return match?.[1] === void 0 ? void 0 : Number(match[1]);
|
|
327
|
+
}
|
|
328
|
+
/** The fields of a row that each offer keeps as specs. The spec `pricing` adds the key of the term of the offer. */
|
|
329
|
+
const SPEC_FIELDS = [
|
|
330
|
+
"serviceName",
|
|
331
|
+
"serviceFamily",
|
|
332
|
+
"productName",
|
|
333
|
+
"skuName",
|
|
334
|
+
"armSkuName",
|
|
335
|
+
"location"
|
|
336
|
+
];
|
|
337
|
+
function readRow(item, where, notes) {
|
|
338
|
+
if (!isRecord(item)) {
|
|
339
|
+
notes.push(`The row ${where} is skipped: it is not an object.`);
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
const skuId = text(item["skuId"]);
|
|
343
|
+
const meterId = text(item["meterId"]);
|
|
344
|
+
const meterName = text(item["meterName"]);
|
|
345
|
+
const region = text(item["armRegionName"]) ?? text(item["location"]);
|
|
346
|
+
const product = text(item["productName"]);
|
|
347
|
+
const sku = text(item["skuName"]);
|
|
348
|
+
const type = text(item["type"]);
|
|
349
|
+
if (skuId === void 0 || meterId === void 0 || meterName === void 0 || region === void 0 || product === void 0 || sku === void 0 || type === void 0) {
|
|
350
|
+
const missing = Object.entries({
|
|
351
|
+
skuId,
|
|
352
|
+
meterId,
|
|
353
|
+
meterName,
|
|
354
|
+
armRegionName: region,
|
|
355
|
+
productName: product,
|
|
356
|
+
skuName: sku,
|
|
357
|
+
type
|
|
358
|
+
}).filter(([, value]) => value === void 0);
|
|
359
|
+
notes.push(`The row ${where} is skipped: it has no ${missing.map(([name]) => name).join(", ")}.`);
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
const unitOfMeasure = text(item["unitOfMeasure"]) ?? "";
|
|
363
|
+
const unit = azureUnit(unitOfMeasure);
|
|
364
|
+
if (unit === void 0) {
|
|
365
|
+
notes.push(`The row ${where} is skipped: the parser does not know the unit of measure ${show(unitOfMeasure)}.`);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
const currency = text(item["currencyCode"]);
|
|
369
|
+
if (currency === void 0 || !isCurrency(currency)) {
|
|
370
|
+
notes.push(`The row ${where} is skipped: the currency ${show(item["currencyCode"])} is not a money unit of @mark1russell7/quantity.`);
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
const start = decimal(item["tierMinimumUnits"]);
|
|
374
|
+
const price = decimal(item["retailPrice"]);
|
|
375
|
+
if (start === void 0 || price === void 0) {
|
|
376
|
+
const name = start === void 0 ? "tierMinimumUnits" : "retailPrice";
|
|
377
|
+
notes.push(`The row ${where} is skipped: its ${name} ${show(item[name])} is not a decimal of zero or more.`);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
const specs = [];
|
|
381
|
+
for (const name of SPEC_FIELDS) {
|
|
382
|
+
const value = text(item[name]);
|
|
383
|
+
if (value !== void 0) specs.push({
|
|
384
|
+
name,
|
|
385
|
+
value
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
const spot = SPOT.test(sku);
|
|
389
|
+
return {
|
|
390
|
+
where,
|
|
391
|
+
region,
|
|
392
|
+
skuId,
|
|
393
|
+
meterId,
|
|
394
|
+
meterName,
|
|
395
|
+
unitOfMeasure,
|
|
396
|
+
unit,
|
|
397
|
+
currency,
|
|
398
|
+
start,
|
|
399
|
+
price,
|
|
400
|
+
type,
|
|
401
|
+
title: `${product}, ${sku}`,
|
|
402
|
+
spot,
|
|
403
|
+
specs,
|
|
404
|
+
item
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
function variantsOf(row, notes) {
|
|
408
|
+
const base = row.spot ? { kind: "spot" } : { kind: "on-demand" };
|
|
409
|
+
if (row.type === "DevTestConsumption") return [{
|
|
410
|
+
key: "dev-test",
|
|
411
|
+
term: base,
|
|
412
|
+
title: `${row.title}, Dev/Test`,
|
|
413
|
+
price: row.price
|
|
414
|
+
}];
|
|
415
|
+
if (row.type === "Reservation") {
|
|
416
|
+
const years = yearsOf(row.item["reservationTerm"]);
|
|
417
|
+
if (years === void 0) {
|
|
418
|
+
notes.push(`The row ${row.where} is skipped: its reservationTerm ${show(row.item["reservationTerm"])} is not a count of years.`);
|
|
419
|
+
return [];
|
|
420
|
+
}
|
|
421
|
+
const term = {
|
|
422
|
+
kind: "committed",
|
|
423
|
+
length: {
|
|
424
|
+
value: years,
|
|
425
|
+
unit: "yr"
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
return [{
|
|
429
|
+
key: `reservation-${years}y`,
|
|
430
|
+
term,
|
|
431
|
+
title: `${row.title}, ${years}-year reservation`,
|
|
432
|
+
price: 0,
|
|
433
|
+
upfront: row.price
|
|
434
|
+
}];
|
|
435
|
+
}
|
|
436
|
+
if (row.type !== "Consumption") {
|
|
437
|
+
notes.push(`The row ${row.where} is skipped: its type ${show(row.type)} is not a type of the documentation.`);
|
|
438
|
+
return [];
|
|
439
|
+
}
|
|
440
|
+
const variants = [{
|
|
441
|
+
key: "consumption",
|
|
442
|
+
term: base,
|
|
443
|
+
title: row.title,
|
|
444
|
+
price: row.price
|
|
445
|
+
}];
|
|
446
|
+
const plans = row.item["savingsPlan"];
|
|
447
|
+
(list(plans) ?? []).forEach((plan, index) => {
|
|
448
|
+
const years = yearsOf(field(plan, "term"));
|
|
449
|
+
const price = decimal(field(plan, "retailPrice"));
|
|
450
|
+
if (years === void 0 || price === void 0) {
|
|
451
|
+
notes.push(`The savings plan ${row.where}.savingsPlan[${index}] is skipped: it has no term in years or no retailPrice.`);
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
const term = {
|
|
455
|
+
kind: "committed",
|
|
456
|
+
length: {
|
|
457
|
+
value: years,
|
|
458
|
+
unit: "yr"
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
variants.push({
|
|
462
|
+
key: `savings-plan-${years}y`,
|
|
463
|
+
term,
|
|
464
|
+
title: `${row.title}, ${years}-year savings plan`,
|
|
465
|
+
price
|
|
466
|
+
});
|
|
467
|
+
});
|
|
468
|
+
if (plans !== void 0 && plans !== null && list(plans) === void 0) notes.push(`The field savingsPlan of the row ${row.where} is not a list, and the parser does not read it.`);
|
|
469
|
+
return variants;
|
|
470
|
+
}
|
|
471
|
+
function addRow(row, drafts, notes) {
|
|
472
|
+
for (const variant of variantsOf(row, notes)) {
|
|
473
|
+
const sourceId = `${row.region}/${row.skuId}/${variant.key}`;
|
|
474
|
+
let draft = drafts.get(sourceId);
|
|
475
|
+
if (draft === void 0) {
|
|
476
|
+
draft = {
|
|
477
|
+
sourceId,
|
|
478
|
+
region: row.region,
|
|
479
|
+
title: variant.title,
|
|
480
|
+
term: variant.term,
|
|
481
|
+
specs: [...row.specs, {
|
|
482
|
+
name: "pricing",
|
|
483
|
+
value: variant.key
|
|
484
|
+
}],
|
|
485
|
+
currency: row.currency,
|
|
486
|
+
upfront: variant.upfront,
|
|
487
|
+
meters: /* @__PURE__ */ new Map()
|
|
488
|
+
};
|
|
489
|
+
drafts.set(sourceId, draft);
|
|
490
|
+
} else if (draft.currency !== row.currency) {
|
|
491
|
+
notes.push(`The row ${row.where} is skipped: its currency ${row.currency} is not the currency of the offer ${offerId(AZURE_SOURCE, sourceId)}.`);
|
|
492
|
+
continue;
|
|
493
|
+
} else if (variant.upfront !== void 0) {
|
|
494
|
+
notes.push(`The row ${row.where} is skipped: it repeats the reservation ${offerId(AZURE_SOURCE, sourceId)}.`);
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
const key = `${row.meterId}\u0000${row.meterName}\u0000${row.unitOfMeasure}`;
|
|
498
|
+
let meter = draft.meters.get(key);
|
|
499
|
+
if (meter === void 0) {
|
|
500
|
+
meter = {
|
|
501
|
+
meterId: row.meterId,
|
|
502
|
+
meterName: row.meterName,
|
|
503
|
+
unit: row.unit,
|
|
504
|
+
tiers: /* @__PURE__ */ new Map()
|
|
505
|
+
};
|
|
506
|
+
draft.meters.set(key, meter);
|
|
507
|
+
}
|
|
508
|
+
if (row.start !== 0 && row.unit.count !== 1) meter.problem = `a tier starts at ${row.start} of the unit of measure ${row.unitOfMeasure}, and the documentation does not tell the unit of the start`;
|
|
509
|
+
const known = meter.tiers.get(row.start);
|
|
510
|
+
if (known !== void 0 && known !== variant.price) meter.problem = `two rows give the tier at ${row.start} the prices ${known} and ${variant.price}`;
|
|
511
|
+
meter.tiers.set(row.start, variant.price);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
function meterOf(draft, meter, name, id, notes) {
|
|
515
|
+
if (meter.problem !== void 0) {
|
|
516
|
+
notes.push(`The meter ${show(meter.meterName)} of the offer ${id} is skipped: ${meter.problem}.`);
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
const starts = [...meter.tiers.keys()].sort((a, b) => a - b);
|
|
520
|
+
if (starts[0] !== 0) {
|
|
521
|
+
notes.push(`The meter ${show(meter.meterName)} of the offer ${id} is skipped: it has no tier that starts at 0.`);
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
const unitPrice = priceUnit(draft.currency, meter.unit.unit);
|
|
525
|
+
return {
|
|
526
|
+
name,
|
|
527
|
+
tiers: starts.map((start) => ({
|
|
528
|
+
from: {
|
|
529
|
+
value: start,
|
|
530
|
+
unit: meter.unit.unit
|
|
531
|
+
},
|
|
532
|
+
unitPrice: {
|
|
533
|
+
value: perUnit(meter.tiers.get(start) ?? 0, meter.unit.count),
|
|
534
|
+
unit: unitPrice
|
|
535
|
+
}
|
|
536
|
+
}))
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
function observationOf(draft, at, notes) {
|
|
540
|
+
const id = offerId(AZURE_SOURCE, draft.sourceId);
|
|
541
|
+
const names = /* @__PURE__ */ new Map();
|
|
542
|
+
for (const meter of draft.meters.values()) names.set(meter.meterName, (names.get(meter.meterName) ?? 0) + 1);
|
|
543
|
+
const meters = [];
|
|
544
|
+
for (const meter of draft.meters.values()) {
|
|
545
|
+
const read = meterOf(draft, meter, (names.get(meter.meterName) ?? 0) > 1 ? `${meter.meterName} (${meter.meterId})` : meter.meterName, id, notes);
|
|
546
|
+
if (read !== void 0) meters.push(read);
|
|
547
|
+
}
|
|
548
|
+
if (meters.length === 0) {
|
|
549
|
+
notes.push(`The offer ${id} is skipped: it has no meter with a valid price.`);
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
return observeMetered({
|
|
553
|
+
id,
|
|
554
|
+
source: AZURE_SOURCE,
|
|
555
|
+
sourceId: draft.sourceId,
|
|
556
|
+
title: draft.title,
|
|
557
|
+
location: {
|
|
558
|
+
provider: AZURE_PROVIDER,
|
|
559
|
+
region: draft.region
|
|
560
|
+
},
|
|
561
|
+
term: draft.term,
|
|
562
|
+
specs: [...draft.specs]
|
|
563
|
+
}, {
|
|
564
|
+
offerId: id,
|
|
565
|
+
at,
|
|
566
|
+
period: {
|
|
567
|
+
value: 1,
|
|
568
|
+
unit: "mo"
|
|
569
|
+
},
|
|
570
|
+
meters,
|
|
571
|
+
upfront: draft.upfront === void 0 ? void 0 : {
|
|
572
|
+
value: draft.upfront,
|
|
573
|
+
unit: draft.currency
|
|
574
|
+
}
|
|
575
|
+
}, notes);
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* This function reads one page of the Azure Retail Prices API (`GET /api/retail/prices`) at the instant `at`.
|
|
579
|
+
* The prices of one SKU can continue on the next page: `parseAzureRetailPricePages` reads all the pages together.
|
|
580
|
+
*
|
|
581
|
+
* @throws RangeError when `at` is not an instant in UTC in the ISO 8601 form.
|
|
582
|
+
*/
|
|
583
|
+
function parseAzureRetailPrices(answer, at) {
|
|
584
|
+
return parseAzureRetailPricePages([answer], at);
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* This function reads the pages of one read of the Azure Retail Prices API at the instant `at`. Each SKU in a
|
|
588
|
+
* region on a term gives one offer, with a meter for each meter of the SKU. The `next` of the result is the link
|
|
589
|
+
* of the last page.
|
|
590
|
+
*
|
|
591
|
+
* @throws RangeError when `at` is not an instant in UTC in the ISO 8601 form.
|
|
592
|
+
*/
|
|
593
|
+
function parseAzureRetailPricePages(answers, at) {
|
|
594
|
+
checkAt(at);
|
|
595
|
+
if (answers.length === 0) return refused("The read has no page.");
|
|
596
|
+
const notes = [];
|
|
597
|
+
const drafts = /* @__PURE__ */ new Map();
|
|
598
|
+
const rows = [];
|
|
599
|
+
for (const [index, answer] of answers.entries()) {
|
|
600
|
+
const label = answers.length === 1 ? "The answer" : `The page ${index + 1}`;
|
|
601
|
+
if (!isRecord(answer)) return refused(`${label} is not an object.`);
|
|
602
|
+
const items = list(answer["Items"]);
|
|
603
|
+
if (items === void 0) return refused(`The field Items of ${label.toLowerCase()} is not a list.`);
|
|
604
|
+
const prefix = answers.length === 1 ? "" : `page ${index + 1}, `;
|
|
605
|
+
items.forEach((item, row) => {
|
|
606
|
+
const read = readRow(item, `${prefix}Items[${row}]`, notes);
|
|
607
|
+
if (read !== void 0) rows.push(read);
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
for (const row of rows) addRow(row, drafts, notes);
|
|
611
|
+
const observations = [];
|
|
612
|
+
for (const draft of drafts.values()) {
|
|
613
|
+
const observation = observationOf(draft, at, notes);
|
|
614
|
+
if (observation !== void 0) observations.push(observation);
|
|
615
|
+
}
|
|
616
|
+
const last = answers[answers.length - 1];
|
|
617
|
+
const next = azureNextPage(last);
|
|
618
|
+
const link = field(last, "NextPageLink");
|
|
619
|
+
if (next === null && text(link) !== void 0) notes.push(`The link of the next page ${show(link)} does not go to the Azure Retail Prices API, and the result has no link.`);
|
|
620
|
+
return {
|
|
621
|
+
ok: true,
|
|
622
|
+
observations,
|
|
623
|
+
notes,
|
|
624
|
+
next
|
|
625
|
+
};
|
|
626
|
+
}
|
|
155
627
|
// src/bestbuy.ts
|
|
156
628
|
/** The name of the Best Buy Products API and Open Box API as a source of offers. */
|
|
157
629
|
const BEST_BUY_SOURCE = "bestbuy";
|
|
@@ -801,19 +1273,28 @@ function formBody(fields) {
|
|
|
801
1273
|
return Object.entries(fields).map(([name, value]) => `${encodeURIComponent(name)}=${encodeURIComponent(value)}`).join("&");
|
|
802
1274
|
}
|
|
803
1275
|
/**
|
|
804
|
-
* This function reads
|
|
805
|
-
*
|
|
1276
|
+
* This function reads the JSON of an answer. An answer that is not a 2xx gives a problem with its status line,
|
|
1277
|
+
* and no part of its body.
|
|
806
1278
|
*/
|
|
807
|
-
function
|
|
1279
|
+
function readJson(answer, input) {
|
|
808
1280
|
const { reply } = answer;
|
|
809
1281
|
if (reply.status < 200 || reply.status > 299) return refused(redact(`${input.source} answered ${statusLine$1(reply)} to ${input.what}.`, input.secrets ?? []));
|
|
810
|
-
let json;
|
|
811
1282
|
try {
|
|
812
|
-
|
|
1283
|
+
return {
|
|
1284
|
+
ok: true,
|
|
1285
|
+
json: JSON.parse(reply.body)
|
|
1286
|
+
};
|
|
813
1287
|
} catch {
|
|
814
1288
|
return refused(`The answer of ${input.source} to ${input.what} is not JSON.`);
|
|
815
1289
|
}
|
|
816
|
-
|
|
1290
|
+
}
|
|
1291
|
+
/**
|
|
1292
|
+
* This function reads an answer with a parser of this package at the instant `at`. An answer that is not a 2xx
|
|
1293
|
+
* gives a problem with its status line, and no part of its body.
|
|
1294
|
+
*/
|
|
1295
|
+
function readAnswer(answer, input, parse, at) {
|
|
1296
|
+
const read = readJson(answer, input);
|
|
1297
|
+
return read.ok ? parse(read.json, at) : read;
|
|
817
1298
|
}
|
|
818
1299
|
/** This function gives the instant of a time in milliseconds since the epoch, in the ISO 8601 form in UTC. */
|
|
819
1300
|
function instantOf(ms) {
|
|
@@ -834,7 +1315,7 @@ const DIGIKEY_US = {
|
|
|
834
1315
|
language: "en",
|
|
835
1316
|
currency: "USD"
|
|
836
1317
|
};
|
|
837
|
-
const SOURCE$
|
|
1318
|
+
const SOURCE$3 = "Digi-Key";
|
|
838
1319
|
/**
|
|
839
1320
|
* This function makes a client of the Digi-Key Product Information API v4. The client gets a token with the
|
|
840
1321
|
* 2-legged OAuth flow (the client credentials grant), and it keeps the token until 1 minute before its end. Each
|
|
@@ -851,7 +1332,7 @@ function createDigiKeyClient(options) {
|
|
|
851
1332
|
const locale = options.locale ?? DIGIKEY_US;
|
|
852
1333
|
const secrets = [clientId, clientSecret];
|
|
853
1334
|
const context = {
|
|
854
|
-
source: SOURCE$
|
|
1335
|
+
source: SOURCE$3,
|
|
855
1336
|
host,
|
|
856
1337
|
minIntervalMs: 500,
|
|
857
1338
|
secrets,
|
|
@@ -877,7 +1358,7 @@ function createDigiKeyClient(options) {
|
|
|
877
1358
|
what: "the token",
|
|
878
1359
|
exchange
|
|
879
1360
|
}, signal);
|
|
880
|
-
return readGrant(reply, SOURCE$
|
|
1361
|
+
return readGrant(reply, SOURCE$3, issuedAt, secrets, redact);
|
|
881
1362
|
}, pace.now);
|
|
882
1363
|
const headers = (token) => ({
|
|
883
1364
|
authorization: `Bearer ${token}`,
|
|
@@ -949,7 +1430,7 @@ const EBAY_PACE_MS = 1e3;
|
|
|
949
1430
|
const EBAY_SEARCH_LIMIT = 200;
|
|
950
1431
|
/** The marketplace of the US. */
|
|
951
1432
|
const EBAY_US = "EBAY_US";
|
|
952
|
-
const SOURCE$
|
|
1433
|
+
const SOURCE$2 = "eBay";
|
|
953
1434
|
const ITEM_ID = /^v1\|\d{1,20}\|\d{1,20}$/;
|
|
954
1435
|
const MARKETPLACE = /^EBAY_[A-Z]{2,4}$/;
|
|
955
1436
|
/**
|
|
@@ -974,7 +1455,7 @@ function createEbayClient(options) {
|
|
|
974
1455
|
basic
|
|
975
1456
|
];
|
|
976
1457
|
const context = {
|
|
977
|
-
source: SOURCE$
|
|
1458
|
+
source: SOURCE$2,
|
|
978
1459
|
host,
|
|
979
1460
|
minIntervalMs: EBAY_PACE_MS,
|
|
980
1461
|
secrets,
|
|
@@ -1000,7 +1481,7 @@ function createEbayClient(options) {
|
|
|
1000
1481
|
what: "the token",
|
|
1001
1482
|
exchange
|
|
1002
1483
|
}, signal);
|
|
1003
|
-
return readGrant(reply, SOURCE$
|
|
1484
|
+
return readGrant(reply, SOURCE$2, issuedAt, secrets, redact);
|
|
1004
1485
|
}, pace.now);
|
|
1005
1486
|
const headers = (token) => ({
|
|
1006
1487
|
authorization: `Bearer ${token}`,
|
|
@@ -1039,6 +1520,66 @@ function createEbayClient(options) {
|
|
|
1039
1520
|
}
|
|
1040
1521
|
};
|
|
1041
1522
|
}
|
|
1523
|
+
// src/azure-client.ts
|
|
1524
|
+
/** The host of the Azure Retail Prices API. */
|
|
1525
|
+
const AZURE_HOST = "prices.azure.com";
|
|
1526
|
+
/**
|
|
1527
|
+
* The client sends one request each 6 seconds at most. The documentation gives no limit. But each answer has the
|
|
1528
|
+
* headers `x-ms-ratelimit-remaining-retailprices-requests` and `x-ms-ratelimit-retailprices-retry-after: 60`.
|
|
1529
|
+
* Thus the client sends 10 requests in 60 seconds at most.
|
|
1530
|
+
*/
|
|
1531
|
+
const AZURE_PACE_MS = 6e3;
|
|
1532
|
+
/** The most pages of one read. Each page has 1,000 prices at most. */
|
|
1533
|
+
const AZURE_PAGE_LIMIT = 10;
|
|
1534
|
+
const SOURCE$1 = "Azure Retail Prices";
|
|
1535
|
+
function checkPages(pages) {
|
|
1536
|
+
if (!Number.isInteger(pages) || pages < 1 || pages > 10) throw new RangeError(`The count of pages is not a whole number from 1 to 10.`);
|
|
1537
|
+
return pages;
|
|
1538
|
+
}
|
|
1539
|
+
/**
|
|
1540
|
+
* This function makes a client of the Azure Retail Prices API. A read follows the link of the next page of each
|
|
1541
|
+
* answer, only to the same API, up to its count of pages. It then reads all the pages together.
|
|
1542
|
+
*/
|
|
1543
|
+
function createAzureClient(options) {
|
|
1544
|
+
const { pace, transport } = options;
|
|
1545
|
+
const read = async (first, pages, what, signal) => {
|
|
1546
|
+
const answers = [];
|
|
1547
|
+
let link = first;
|
|
1548
|
+
while (link !== null && answers.length < pages) {
|
|
1549
|
+
const page = answers.length + 1;
|
|
1550
|
+
const exchange = {
|
|
1551
|
+
method: "GET",
|
|
1552
|
+
url: link,
|
|
1553
|
+
headers: { accept: "application/json" },
|
|
1554
|
+
body: null
|
|
1555
|
+
};
|
|
1556
|
+
const input = {
|
|
1557
|
+
source: SOURCE$1,
|
|
1558
|
+
host: AZURE_HOST,
|
|
1559
|
+
minIntervalMs: AZURE_PACE_MS,
|
|
1560
|
+
what: page === 1 ? what : `${what}, page ${page}`,
|
|
1561
|
+
secrets: [],
|
|
1562
|
+
redact,
|
|
1563
|
+
exchange
|
|
1564
|
+
};
|
|
1565
|
+
const json = readJson(await send(pace, transport, input, signal), input);
|
|
1566
|
+
if (!json.ok) return json;
|
|
1567
|
+
answers.push(json.json);
|
|
1568
|
+
link = azureNextPage(json.json);
|
|
1569
|
+
}
|
|
1570
|
+
return parseAzureRetailPricePages(answers, instantOf(pace.now()));
|
|
1571
|
+
};
|
|
1572
|
+
return {
|
|
1573
|
+
async prices(filter, readOptions = {}, signal) {
|
|
1574
|
+
const url = azurePricesUrl(filter, readOptions.currency);
|
|
1575
|
+
return read(url, checkPages(readOptions.pages ?? 1), "the prices of a filter", signal);
|
|
1576
|
+
},
|
|
1577
|
+
async more(next, pages = 1, signal) {
|
|
1578
|
+
if (typeof next !== "string" || !isAzurePricesUrl(next)) throw new RangeError("The link is not a link of the Azure Retail Prices API.");
|
|
1579
|
+
return read(next, checkPages(pages), "the next page of a read", signal);
|
|
1580
|
+
}
|
|
1581
|
+
};
|
|
1582
|
+
}
|
|
1042
1583
|
// src/bestbuy-client.ts
|
|
1043
1584
|
/** The host of the Best Buy APIs. */
|
|
1044
1585
|
const BEST_BUY_HOST = "api.bestbuy.com";
|
|
@@ -1112,12 +1653,13 @@ function createBestBuyClient(options) {
|
|
|
1112
1653
|
};
|
|
1113
1654
|
}
|
|
1114
1655
|
// src/index.ts
|
|
1115
|
-
/** The terms of each source of this package
|
|
1656
|
+
/** The terms of each source of this package: the sellers in the order of the hand crawl, then Azure. */
|
|
1116
1657
|
const SOURCE_TERMS = [
|
|
1117
1658
|
EBAY_TERMS,
|
|
1118
1659
|
BEST_BUY_TERMS,
|
|
1119
|
-
DIGIKEY_TERMS
|
|
1660
|
+
DIGIKEY_TERMS,
|
|
1661
|
+
AZURE_TERMS
|
|
1120
1662
|
];
|
|
1121
|
-
export { ApiError, BEST_BUY_HOST, BEST_BUY_PACE_MS, BEST_BUY_PAGE_LIMIT, BEST_BUY_SOURCE, BEST_BUY_TERMS, DIGIKEY_HOST, DIGIKEY_PACE_MS, DIGIKEY_SANDBOX_HOST, DIGIKEY_SEARCH_LIMIT, DIGIKEY_SELLER, DIGIKEY_SOURCE, DIGIKEY_TERMS, DIGIKEY_US, EBAY_HOST, EBAY_PACE_MS, EBAY_SANDBOX_HOST, EBAY_SCOPE, EBAY_SEARCH_LIMIT, EBAY_SOURCE, EBAY_TERMS, EBAY_US, SECRET_MARK, SOURCE_TERMS, bestBuyCondition, createBestBuyClient, createDigiKeyClient, createEbayClient, ebayCondition, parseBestBuyOpenBox, parseBestBuyProduct, parseBestBuyProducts, parseDigiKeyKeywordSearch, parseDigiKeyProductDetails, parseEbayItem, parseEbaySearch, redact };
|
|
1663
|
+
export { AZURE_API_VERSION, AZURE_FILTER_FIELDS, AZURE_HOST, AZURE_PACE_MS, AZURE_PAGE_LIMIT, AZURE_PRICES_URL, AZURE_PROVIDER, AZURE_SOURCE, AZURE_TERMS, ApiError, BEST_BUY_HOST, BEST_BUY_PACE_MS, BEST_BUY_PAGE_LIMIT, BEST_BUY_SOURCE, BEST_BUY_TERMS, DIGIKEY_HOST, DIGIKEY_PACE_MS, DIGIKEY_SANDBOX_HOST, DIGIKEY_SEARCH_LIMIT, DIGIKEY_SELLER, DIGIKEY_SOURCE, DIGIKEY_TERMS, DIGIKEY_US, EBAY_HOST, EBAY_PACE_MS, EBAY_SANDBOX_HOST, EBAY_SCOPE, EBAY_SEARCH_LIMIT, EBAY_SOURCE, EBAY_TERMS, EBAY_US, SECRET_MARK, SOURCE_TERMS, azureNextPage, azurePricesUrl, azureUnit, bestBuyCondition, createAzureClient, createBestBuyClient, createDigiKeyClient, createEbayClient, ebayCondition, isAzurePricesUrl, parseAzureRetailPricePages, parseAzureRetailPrices, parseBestBuyOpenBox, parseBestBuyProduct, parseBestBuyProducts, parseDigiKeyKeywordSearch, parseDigiKeyProductDetails, parseEbayItem, parseEbaySearch, redact };
|
|
1122
1664
|
|
|
1123
1665
|
//# sourceMappingURL=index.js.map
|