@zimmerstack/connect 0.7.0 → 0.9.1
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/AGENTS.md +305 -25
- package/README.md +572 -56
- package/dist/cjs/client.d.ts +528 -13
- package/dist/cjs/client.js +778 -46
- package/dist/cjs/doctor.d.ts +86 -0
- package/dist/cjs/doctor.js +689 -0
- package/dist/cjs/errors.d.ts +6 -2
- package/dist/cjs/errors.js +7 -2
- package/dist/cjs/generated.d.ts +2873 -203
- package/dist/cjs/generated.js +222 -8
- package/dist/cjs/grid.d.ts +6 -1
- package/dist/cjs/grid.js +8 -0
- package/dist/cjs/index.d.ts +14 -5
- package/dist/cjs/index.js +41 -1
- package/dist/cjs/key.d.ts +39 -0
- package/dist/cjs/key.js +85 -0
- package/dist/cjs/money.d.ts +81 -0
- package/dist/cjs/money.js +172 -0
- package/dist/cjs/notices.d.ts +44 -0
- package/dist/cjs/notices.js +64 -0
- package/dist/cjs/shownOnce.d.ts +42 -0
- package/dist/cjs/shownOnce.js +83 -0
- package/dist/cjs/webhooks.d.ts +182 -3
- package/dist/cjs/webhooks.js +75 -1
- package/dist/esm/client.d.ts +528 -13
- package/dist/esm/client.js +776 -46
- package/dist/esm/doctor.d.ts +86 -0
- package/dist/esm/doctor.js +685 -0
- package/dist/esm/errors.d.ts +6 -2
- package/dist/esm/errors.js +7 -2
- package/dist/esm/generated.d.ts +2873 -203
- package/dist/esm/generated.js +221 -7
- package/dist/esm/grid.d.ts +6 -1
- package/dist/esm/grid.js +7 -0
- package/dist/esm/index.d.ts +14 -5
- package/dist/esm/index.js +8 -3
- package/dist/esm/key.d.ts +39 -0
- package/dist/esm/key.js +81 -0
- package/dist/esm/mcp/cli.js +95 -11
- package/dist/esm/mcp/reference.generated.js +5741 -1008
- package/dist/esm/mcp/server.d.ts +14 -6
- package/dist/esm/mcp/server.js +451 -25
- package/dist/esm/money.d.ts +81 -0
- package/dist/esm/money.js +161 -0
- package/dist/esm/notices.d.ts +44 -0
- package/dist/esm/notices.js +56 -0
- package/dist/esm/shownOnce.d.ts +42 -0
- package/dist/esm/shownOnce.js +77 -0
- package/dist/esm/webhooks.d.ts +182 -3
- package/dist/esm/webhooks.js +69 -0
- package/package.json +2 -2
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Money as the Connect API speaks it, without a float and without "divide by 100".
|
|
3
|
+
*
|
|
4
|
+
* The API sends and takes a price in MAJOR units (rupees, euros, yen) as a decimal string with the
|
|
5
|
+
* currency's own number of decimals: "4500.50" in rupees, "89.50" in euros, "15000" in yen. `decimals`
|
|
6
|
+
* comes with every such answer (`GET /property`, `GET /catalog`, `GET /rates`): pass it to these helpers
|
|
7
|
+
* and they do the rest. Strings in, strings out; the arithmetic is on whole numbers, so 0.1 + 0.2 is 0.30.
|
|
8
|
+
*
|
|
9
|
+
* Two integers are easy to confuse, so each has its own helper:
|
|
10
|
+
* - a currency's own MINOR UNIT (ISO 4217): cents for the euro, paise for the rupee, and for the yen the
|
|
11
|
+
* yen itself. This is what most ledgers store. `amountFromMinorUnits`, `amountToMinorUnits`.
|
|
12
|
+
* - the API's `…Minor` fields (`rateMinor`, `standingMinor`): HUNDREDTHS of the major unit in EVERY
|
|
13
|
+
* currency, so 15000 yen reads 1500000. `amountFromHundredths` turns one into the string.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Any amount as the string the API reads and writes: exactly the currency's decimals, rounded half up
|
|
17
|
+
* when it has more (the API does the same, and names what it rounded in the answer's `rounded` list).
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* toAmount(4500.5, 2); // "4500.50"
|
|
21
|
+
* toAmount("4500.505", 2); // "4500.51"
|
|
22
|
+
* toAmount(15000, 0); // "15000"
|
|
23
|
+
*/
|
|
24
|
+
export declare function toAmount(value: number | string, decimals: number): string;
|
|
25
|
+
/**
|
|
26
|
+
* From a whole number of the currency's own minor unit (cents, paise; for a currency without decimals,
|
|
27
|
+
* the unit itself) to the API's string. Use it when your ledger stores integers.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* amountFromMinorUnits(450050, 2); // "4500.50" (paise)
|
|
31
|
+
* amountFromMinorUnits(15000, 0); // "15000" (yen have no smaller unit)
|
|
32
|
+
*/
|
|
33
|
+
export declare function amountFromMinorUnits(units: number | bigint, decimals: number): string;
|
|
34
|
+
/**
|
|
35
|
+
* From the API's string to a whole number of the currency's own minor unit, for a ledger that stores
|
|
36
|
+
* integers. An amount with more decimals than the currency has is rounded half up first.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* amountToMinorUnits("4500.50", 2); // 450050
|
|
40
|
+
* amountToMinorUnits("15000", 0); // 15000
|
|
41
|
+
*/
|
|
42
|
+
export declare function amountToMinorUnits(amount: number | string, decimals: number): number;
|
|
43
|
+
/**
|
|
44
|
+
* From one of the API's `…Minor` fields (`rateMinor`, `defaultRateMinor`, `standingMinor`) to the string.
|
|
45
|
+
* Those fields are HUNDREDTHS of the major unit in every currency, whatever the currency's own decimals:
|
|
46
|
+
* 15000 yen is `rateMinor: 1500000`. Prefer the string beside the field (`rate`); use this where an older
|
|
47
|
+
* answer or your own stored copy has only the integer.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* amountFromHundredths(450050, 2); // "4500.50"
|
|
51
|
+
* amountFromHundredths(1500000, 0); // "15000"
|
|
52
|
+
*/
|
|
53
|
+
export declare function amountFromHundredths(minor: number, decimals: number): string;
|
|
54
|
+
/** The sum of two amounts, in the currency's decimals. */
|
|
55
|
+
export declare function addAmounts(a: number | string, b: number | string, decimals: number): string;
|
|
56
|
+
/** `a` minus `b`, in the currency's decimals. */
|
|
57
|
+
export declare function subtractAmounts(a: number | string, b: number | string, decimals: number): string;
|
|
58
|
+
/**
|
|
59
|
+
* An amount times a factor (nights, a quantity, a percentage as "1.18"), rounded half up to the currency's
|
|
60
|
+
* decimals once, at the end.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* multiplyAmount("4500.50", 3, 2); // "13501.50" three nights
|
|
64
|
+
* multiplyAmount("89.50", "1.10", 2); // "98.45" ten per cent on top
|
|
65
|
+
*/
|
|
66
|
+
export declare function multiplyAmount(amount: number | string, factor: number | string, decimals: number): string;
|
|
67
|
+
/** -1 when `a` is less than `b`, 1 when it is more, 0 when they are the same amount ("4500.5" and "4500.50" are). */
|
|
68
|
+
export declare function compareAmounts(a: number | string, b: number | string): -1 | 0 | 1;
|
|
69
|
+
/**
|
|
70
|
+
* An amount for a person to read, in their own way of writing numbers: the currency's symbol or code, its
|
|
71
|
+
* decimals, the locale's separators. `locale` defaults to the runtime's.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* formatAmount("4500.50", { currency: "INR", decimals: 2, locale: "en-IN" }); // "₹4,500.50"
|
|
75
|
+
* formatAmount("15000", { currency: "JPY", decimals: 0, locale: "ja-JP" }); // "¥15,000"
|
|
76
|
+
*/
|
|
77
|
+
export declare function formatAmount(amount: number | string, options: {
|
|
78
|
+
currency: string;
|
|
79
|
+
decimals: number;
|
|
80
|
+
locale?: string;
|
|
81
|
+
}): string;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Money as the Connect API speaks it, without a float and without "divide by 100".
|
|
4
|
+
*
|
|
5
|
+
* The API sends and takes a price in MAJOR units (rupees, euros, yen) as a decimal string with the
|
|
6
|
+
* currency's own number of decimals: "4500.50" in rupees, "89.50" in euros, "15000" in yen. `decimals`
|
|
7
|
+
* comes with every such answer (`GET /property`, `GET /catalog`, `GET /rates`): pass it to these helpers
|
|
8
|
+
* and they do the rest. Strings in, strings out; the arithmetic is on whole numbers, so 0.1 + 0.2 is 0.30.
|
|
9
|
+
*
|
|
10
|
+
* Two integers are easy to confuse, so each has its own helper:
|
|
11
|
+
* - a currency's own MINOR UNIT (ISO 4217): cents for the euro, paise for the rupee, and for the yen the
|
|
12
|
+
* yen itself. This is what most ledgers store. `amountFromMinorUnits`, `amountToMinorUnits`.
|
|
13
|
+
* - the API's `…Minor` fields (`rateMinor`, `standingMinor`): HUNDREDTHS of the major unit in EVERY
|
|
14
|
+
* currency, so 15000 yen reads 1500000. `amountFromHundredths` turns one into the string.
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.toAmount = toAmount;
|
|
18
|
+
exports.amountFromMinorUnits = amountFromMinorUnits;
|
|
19
|
+
exports.amountToMinorUnits = amountToMinorUnits;
|
|
20
|
+
exports.amountFromHundredths = amountFromHundredths;
|
|
21
|
+
exports.addAmounts = addAmounts;
|
|
22
|
+
exports.subtractAmounts = subtractAmounts;
|
|
23
|
+
exports.multiplyAmount = multiplyAmount;
|
|
24
|
+
exports.compareAmounts = compareAmounts;
|
|
25
|
+
exports.formatAmount = formatAmount;
|
|
26
|
+
const MICRO_DIGITS = 6;
|
|
27
|
+
const PLAIN = /^(-?)(\d+)(?:\.(\d+))?$/;
|
|
28
|
+
function assertDecimals(decimals) {
|
|
29
|
+
if (!Number.isInteger(decimals) || decimals < 0 || decimals > 6) {
|
|
30
|
+
throw new RangeError(`decimals must be the currency's own count of decimal places (0, 2 or 3; it is on GET /property), not ${String(decimals)}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** A sent value as sign, digits and how many of them sit after the point. A number loses its float noise first, as the API does. */
|
|
34
|
+
function parse(value) {
|
|
35
|
+
let text;
|
|
36
|
+
if (typeof value === "bigint")
|
|
37
|
+
text = value.toString();
|
|
38
|
+
else if (typeof value === "number") {
|
|
39
|
+
if (!Number.isFinite(value) || Math.abs(value) >= 1e15)
|
|
40
|
+
throw new RangeError(`${String(value)} is not an amount`);
|
|
41
|
+
const fixed = value.toFixed(MICRO_DIGITS);
|
|
42
|
+
text = fixed.replace(/0+$/, "").replace(/\.$/, "");
|
|
43
|
+
}
|
|
44
|
+
else
|
|
45
|
+
text = String(value).trim();
|
|
46
|
+
const m = PLAIN.exec(text);
|
|
47
|
+
if (!m)
|
|
48
|
+
throw new RangeError(`"${text.slice(0, 40)}" is not an amount: write digits with an optional decimal point ("4500.50"), with no separator, symbol or space`);
|
|
49
|
+
const fraction = m[3] ?? "";
|
|
50
|
+
const digits = BigInt(m[2] + fraction);
|
|
51
|
+
return { negative: m[1] === "-" && digits !== 0n, digits, scale: fraction.length };
|
|
52
|
+
}
|
|
53
|
+
/** digits × 10^-scale, rounded half up (away from zero) to `decimals` places, as an integer count of the smallest unit. */
|
|
54
|
+
function toUnits(value, decimals) {
|
|
55
|
+
let units;
|
|
56
|
+
if (value.scale <= decimals)
|
|
57
|
+
units = value.digits * 10n ** BigInt(decimals - value.scale);
|
|
58
|
+
else {
|
|
59
|
+
const cut = 10n ** BigInt(value.scale - decimals);
|
|
60
|
+
units = (value.digits + cut / 2n) / cut;
|
|
61
|
+
}
|
|
62
|
+
return value.negative ? -units : units;
|
|
63
|
+
}
|
|
64
|
+
function fromUnits(units, decimals) {
|
|
65
|
+
const negative = units < 0n;
|
|
66
|
+
const text = (negative ? -units : units).toString().padStart(decimals + 1, "0");
|
|
67
|
+
const whole = decimals === 0 ? text : text.slice(0, -decimals);
|
|
68
|
+
return `${negative ? "-" : ""}${whole}${decimals === 0 ? "" : "." + text.slice(-decimals)}`;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Any amount as the string the API reads and writes: exactly the currency's decimals, rounded half up
|
|
72
|
+
* when it has more (the API does the same, and names what it rounded in the answer's `rounded` list).
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* toAmount(4500.5, 2); // "4500.50"
|
|
76
|
+
* toAmount("4500.505", 2); // "4500.51"
|
|
77
|
+
* toAmount(15000, 0); // "15000"
|
|
78
|
+
*/
|
|
79
|
+
function toAmount(value, decimals) {
|
|
80
|
+
assertDecimals(decimals);
|
|
81
|
+
return fromUnits(toUnits(parse(value), decimals), decimals);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* From a whole number of the currency's own minor unit (cents, paise; for a currency without decimals,
|
|
85
|
+
* the unit itself) to the API's string. Use it when your ledger stores integers.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* amountFromMinorUnits(450050, 2); // "4500.50" (paise)
|
|
89
|
+
* amountFromMinorUnits(15000, 0); // "15000" (yen have no smaller unit)
|
|
90
|
+
*/
|
|
91
|
+
function amountFromMinorUnits(units, decimals) {
|
|
92
|
+
assertDecimals(decimals);
|
|
93
|
+
if (typeof units === "number" && !Number.isSafeInteger(units))
|
|
94
|
+
throw new RangeError(`${String(units)} is not a whole number of minor units`);
|
|
95
|
+
return fromUnits(BigInt(units), decimals);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* From the API's string to a whole number of the currency's own minor unit, for a ledger that stores
|
|
99
|
+
* integers. An amount with more decimals than the currency has is rounded half up first.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* amountToMinorUnits("4500.50", 2); // 450050
|
|
103
|
+
* amountToMinorUnits("15000", 0); // 15000
|
|
104
|
+
*/
|
|
105
|
+
function amountToMinorUnits(amount, decimals) {
|
|
106
|
+
assertDecimals(decimals);
|
|
107
|
+
const units = toUnits(parse(amount), decimals);
|
|
108
|
+
const asNumber = Number(units);
|
|
109
|
+
if (!Number.isSafeInteger(asNumber))
|
|
110
|
+
throw new RangeError("The amount is too large to hold as a whole number of minor units");
|
|
111
|
+
return asNumber;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* From one of the API's `…Minor` fields (`rateMinor`, `defaultRateMinor`, `standingMinor`) to the string.
|
|
115
|
+
* Those fields are HUNDREDTHS of the major unit in every currency, whatever the currency's own decimals:
|
|
116
|
+
* 15000 yen is `rateMinor: 1500000`. Prefer the string beside the field (`rate`); use this where an older
|
|
117
|
+
* answer or your own stored copy has only the integer.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* amountFromHundredths(450050, 2); // "4500.50"
|
|
121
|
+
* amountFromHundredths(1500000, 0); // "15000"
|
|
122
|
+
*/
|
|
123
|
+
function amountFromHundredths(minor, decimals) {
|
|
124
|
+
assertDecimals(decimals);
|
|
125
|
+
if (!Number.isSafeInteger(minor))
|
|
126
|
+
throw new RangeError(`${String(minor)} is not a whole number of hundredths`);
|
|
127
|
+
return fromUnits(toUnits({ negative: minor < 0, digits: BigInt(Math.abs(minor)), scale: 2 }, decimals), decimals);
|
|
128
|
+
}
|
|
129
|
+
/** The sum of two amounts, in the currency's decimals. */
|
|
130
|
+
function addAmounts(a, b, decimals) {
|
|
131
|
+
assertDecimals(decimals);
|
|
132
|
+
return fromUnits(toUnits(parse(a), decimals) + toUnits(parse(b), decimals), decimals);
|
|
133
|
+
}
|
|
134
|
+
/** `a` minus `b`, in the currency's decimals. */
|
|
135
|
+
function subtractAmounts(a, b, decimals) {
|
|
136
|
+
assertDecimals(decimals);
|
|
137
|
+
return fromUnits(toUnits(parse(a), decimals) - toUnits(parse(b), decimals), decimals);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* An amount times a factor (nights, a quantity, a percentage as "1.18"), rounded half up to the currency's
|
|
141
|
+
* decimals once, at the end.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* multiplyAmount("4500.50", 3, 2); // "13501.50" three nights
|
|
145
|
+
* multiplyAmount("89.50", "1.10", 2); // "98.45" ten per cent on top
|
|
146
|
+
*/
|
|
147
|
+
function multiplyAmount(amount, factor, decimals) {
|
|
148
|
+
assertDecimals(decimals);
|
|
149
|
+
const a = parse(amount);
|
|
150
|
+
const f = parse(factor);
|
|
151
|
+
return fromUnits(toUnits({ negative: a.negative !== f.negative, digits: a.digits * f.digits, scale: a.scale + f.scale }, decimals), decimals);
|
|
152
|
+
}
|
|
153
|
+
/** -1 when `a` is less than `b`, 1 when it is more, 0 when they are the same amount ("4500.5" and "4500.50" are). */
|
|
154
|
+
function compareAmounts(a, b) {
|
|
155
|
+
const x = toUnits(parse(a), MICRO_DIGITS);
|
|
156
|
+
const y = toUnits(parse(b), MICRO_DIGITS);
|
|
157
|
+
return x < y ? -1 : x > y ? 1 : 0;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* An amount for a person to read, in their own way of writing numbers: the currency's symbol or code, its
|
|
161
|
+
* decimals, the locale's separators. `locale` defaults to the runtime's.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* formatAmount("4500.50", { currency: "INR", decimals: 2, locale: "en-IN" }); // "₹4,500.50"
|
|
165
|
+
* formatAmount("15000", { currency: "JPY", decimals: 0, locale: "ja-JP" }); // "¥15,000"
|
|
166
|
+
*/
|
|
167
|
+
function formatAmount(amount, options) {
|
|
168
|
+
const text = toAmount(amount, options.decimals);
|
|
169
|
+
const format = new Intl.NumberFormat(options.locale, { style: "currency", currency: options.currency, minimumFractionDigits: options.decimals, maximumFractionDigits: options.decimals });
|
|
170
|
+
// A decimal string is formatted digit for digit (no float in between) on every runtime this package supports.
|
|
171
|
+
return format.format(text);
|
|
172
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type NoticeCode, type WriteAccepted, type WriteNotice, type WritePreview } from "./generated.js";
|
|
2
|
+
/**
|
|
3
|
+
* Notices: what a write did NOT do, or will not go on doing. Every answer of `zs.availability.set`,
|
|
4
|
+
* `zs.rates.set` and `zs.restrictions.set`, and every dry run of them, may carry `notices`: one plain
|
|
5
|
+
* sentence each, with a stable `code`, a `level` (warning or info), what it is about and what to do.
|
|
6
|
+
*
|
|
7
|
+
* Branch on `code`; show `message` and `action` to the person who saved. A notice is never a refusal:
|
|
8
|
+
* the write was accepted. A code this version does not know was added later: show it all the same.
|
|
9
|
+
*/
|
|
10
|
+
/** Anything that can carry notices: an accepted write, a dry run's preview. */
|
|
11
|
+
export interface NoticeCarrier {
|
|
12
|
+
notices?: WriteNotice[];
|
|
13
|
+
noticesOmitted?: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Every notice of one answer, or of all the answers of a write the client split into several requests:
|
|
17
|
+
* warnings first, each once. Pass what `set` or `dryRun` returned.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const accepted = await zs.restrictions.set([{ ratePlanId: 901, dateFrom: night, dateTo: night, stopSell: true }]);
|
|
21
|
+
* for (const notice of noticesOf(accepted)) showToThePerson(notice.level, notice.message, notice.action);
|
|
22
|
+
*/
|
|
23
|
+
export declare function noticesOf(answers: NoticeCarrier | readonly (NoticeCarrier | null | undefined)[] | null | undefined): WriteNotice[];
|
|
24
|
+
/** Only the warnings: what you sent does not do the whole job (something still sells, nothing is sent, a rule of the hotel will undo it). */
|
|
25
|
+
export declare function warningsOf(answers: NoticeCarrier | readonly (NoticeCarrier | null | undefined)[] | null | undefined): WriteNotice[];
|
|
26
|
+
/** How many notices were left out of the answers past the 20 each lists (`noticesOmitted`). 0 when none were. */
|
|
27
|
+
export declare function noticesOmittedFrom(answers: NoticeCarrier | readonly (NoticeCarrier | null | undefined)[] | null | undefined): number;
|
|
28
|
+
/**
|
|
29
|
+
* The first notice with this code, or undefined. `code` is one of the contract's (`NOTICES` lists them
|
|
30
|
+
* with what each means), so a typo does not compile.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* const stillSelling = findNotice(accepted, "ROOM_STILL_ON_SALE");
|
|
34
|
+
* if (stillSelling) offerToCloseTheRoomType(stillSelling.roomTypeId, stillSelling.otherRatePlanIds);
|
|
35
|
+
*/
|
|
36
|
+
export declare function findNotice(answers: NoticeCarrier | readonly (NoticeCarrier | null | undefined)[] | null | undefined, code: NoticeCode): WriteNotice | undefined;
|
|
37
|
+
/** What a notice code means, in the contract's own sentence; undefined for a code added after this version. */
|
|
38
|
+
export declare function noticeMeaning(code: string): string | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Is this answer a dry run's preview (nothing was written) and not an accepted write? `zs.rates.dryRun`
|
|
41
|
+
* and `zs.rates.set` already answer two different types; this is for an answer that came through
|
|
42
|
+
* `zs.request("PUT /rates", …)`, whose type is either.
|
|
43
|
+
*/
|
|
44
|
+
export declare function isWritePreview(answer: WriteAccepted | WritePreview | null | undefined): answer is WritePreview;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.noticesOf = noticesOf;
|
|
4
|
+
exports.warningsOf = warningsOf;
|
|
5
|
+
exports.noticesOmittedFrom = noticesOmittedFrom;
|
|
6
|
+
exports.findNotice = findNotice;
|
|
7
|
+
exports.noticeMeaning = noticeMeaning;
|
|
8
|
+
exports.isWritePreview = isWritePreview;
|
|
9
|
+
const generated_ts_1 = require("./generated.js");
|
|
10
|
+
const listOf = (answers) => (Array.isArray(answers) ? answers : [answers]).filter((a) => a !== null && a !== undefined && typeof a === "object");
|
|
11
|
+
/**
|
|
12
|
+
* Every notice of one answer, or of all the answers of a write the client split into several requests:
|
|
13
|
+
* warnings first, each once. Pass what `set` or `dryRun` returned.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const accepted = await zs.restrictions.set([{ ratePlanId: 901, dateFrom: night, dateTo: night, stopSell: true }]);
|
|
17
|
+
* for (const notice of noticesOf(accepted)) showToThePerson(notice.level, notice.message, notice.action);
|
|
18
|
+
*/
|
|
19
|
+
function noticesOf(answers) {
|
|
20
|
+
const seen = new Set();
|
|
21
|
+
const all = [];
|
|
22
|
+
for (const answer of listOf(answers)) {
|
|
23
|
+
for (const notice of answer.notices ?? []) {
|
|
24
|
+
// The same notice on two parts of one split write is said once.
|
|
25
|
+
const key = JSON.stringify([notice.code, notice.roomTypeId, notice.ratePlanIds, notice.dateFrom, notice.dateTo]);
|
|
26
|
+
if (seen.has(key))
|
|
27
|
+
continue;
|
|
28
|
+
seen.add(key);
|
|
29
|
+
all.push(notice);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return [...all.filter((n) => n.level === "warning"), ...all.filter((n) => n.level !== "warning")];
|
|
33
|
+
}
|
|
34
|
+
/** Only the warnings: what you sent does not do the whole job (something still sells, nothing is sent, a rule of the hotel will undo it). */
|
|
35
|
+
function warningsOf(answers) {
|
|
36
|
+
return noticesOf(answers).filter((n) => n.level === "warning");
|
|
37
|
+
}
|
|
38
|
+
/** How many notices were left out of the answers past the 20 each lists (`noticesOmitted`). 0 when none were. */
|
|
39
|
+
function noticesOmittedFrom(answers) {
|
|
40
|
+
return listOf(answers).reduce((sum, a) => sum + (typeof a.noticesOmitted === "number" ? a.noticesOmitted : 0), 0);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The first notice with this code, or undefined. `code` is one of the contract's (`NOTICES` lists them
|
|
44
|
+
* with what each means), so a typo does not compile.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* const stillSelling = findNotice(accepted, "ROOM_STILL_ON_SALE");
|
|
48
|
+
* if (stillSelling) offerToCloseTheRoomType(stillSelling.roomTypeId, stillSelling.otherRatePlanIds);
|
|
49
|
+
*/
|
|
50
|
+
function findNotice(answers, code) {
|
|
51
|
+
return noticesOf(answers).find((n) => n.code === code);
|
|
52
|
+
}
|
|
53
|
+
/** What a notice code means, in the contract's own sentence; undefined for a code added after this version. */
|
|
54
|
+
function noticeMeaning(code) {
|
|
55
|
+
return generated_ts_1.NOTICES.find((n) => n.code === code)?.means;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Is this answer a dry run's preview (nothing was written) and not an accepted write? `zs.rates.dryRun`
|
|
59
|
+
* and `zs.rates.set` already answer two different types; this is for an answer that came through
|
|
60
|
+
* `zs.request("PUT /rates", …)`, whose type is either.
|
|
61
|
+
*/
|
|
62
|
+
function isWritePreview(answer) {
|
|
63
|
+
return answer !== null && answer !== undefined && answer.dryRun === true;
|
|
64
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ConnectLinkCreated, HotelKeyCreated, HotelKeyRotated, WebhookEndpointCreated, WebhookSecretRotated } from "./generated.js";
|
|
2
|
+
/**
|
|
3
|
+
* Values the API shows ONCE: a key it minted or rotated, a connect link's address, a webhook signing
|
|
4
|
+
* secret. Each is in the answer of the call that made it and nowhere else: ZimmerStack keeps a hash,
|
|
5
|
+
* never the value, and no call reads it back.
|
|
6
|
+
*
|
|
7
|
+
* Two things lose one. Printing the answer (a log line, an error report), which leaves the value where
|
|
8
|
+
* it should never be; and a retry: the same request sent again under its idempotency key (the client does
|
|
9
|
+
* this by itself after a lost answer) is answered from a stored copy, and that copy holds null in the
|
|
10
|
+
* value's place. `shownOnce` and `withoutShownOnce` are for those two.
|
|
11
|
+
*/
|
|
12
|
+
export type ShownOnceAnswer = HotelKeyCreated | HotelKeyRotated | ConnectLinkCreated | WebhookEndpointCreated | WebhookSecretRotated;
|
|
13
|
+
/** Which value an answer shows once. */
|
|
14
|
+
export type ShownOnceKind = "hotel-key" | "rotated-hotel-key" | "connect-link" | "webhook-secret" | "rotated-webhook-secret";
|
|
15
|
+
/** The answer carried no value: it is a replay of an earlier request. `nextStep` says what to do; nothing here holds a secret. */
|
|
16
|
+
export declare class ZimmerStackShownOnceError extends Error {
|
|
17
|
+
/** Which value is gone. */
|
|
18
|
+
readonly kind: ShownOnceKind;
|
|
19
|
+
/** The way back, in a sentence that names the call. */
|
|
20
|
+
readonly nextStep: string;
|
|
21
|
+
constructor(kind: ShownOnceKind);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The value an answer shows once: the key of `zs.hotelKeys.create` and `.rotate`, the `url` of
|
|
25
|
+
* `zs.connectLinks.create`, the `secret` of `zs.webhookEndpoints.create` and `.rotateSecret`.
|
|
26
|
+
*
|
|
27
|
+
* It never hands back null or an empty string for you to store by mistake: when the answer is a replay
|
|
28
|
+
* and the value is gone, it throws `ZimmerStackShownOnceError`, whose `nextStep` names the call that
|
|
29
|
+
* gets you a new one. Store what it returns in your server's settings or secret store at once; never in
|
|
30
|
+
* code, a log line, an error report or a chat.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* const minted = await zs.hotelKeys.create(propertyId, { name: "Night audit job", scopes: ["ari:read"] });
|
|
34
|
+
* await secrets.put(`zimmerstack/hotel-${propertyId}`, shownOnce(minted));
|
|
35
|
+
* console.log(withoutShownOnce(minted)); // safe to log: the key is not in it
|
|
36
|
+
*/
|
|
37
|
+
export declare function shownOnce(answer: ShownOnceAnswer): string;
|
|
38
|
+
/**
|
|
39
|
+
* A copy of the answer with the shown-once value taken out (null in its place), for a log line, an
|
|
40
|
+
* audit record or an error report. The answer you pass is not changed.
|
|
41
|
+
*/
|
|
42
|
+
export declare function withoutShownOnce<T extends ShownOnceAnswer>(answer: T): T;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ZimmerStackShownOnceError = void 0;
|
|
4
|
+
exports.shownOnce = shownOnce;
|
|
5
|
+
exports.withoutShownOnce = withoutShownOnce;
|
|
6
|
+
const FIELD = {
|
|
7
|
+
"hotel-key": "key",
|
|
8
|
+
"rotated-hotel-key": "key",
|
|
9
|
+
"connect-link": "url",
|
|
10
|
+
"webhook-secret": "secret",
|
|
11
|
+
"rotated-webhook-secret": "secret",
|
|
12
|
+
};
|
|
13
|
+
/** What to do when the value is gone, by kind: the way back the API offers. */
|
|
14
|
+
const WAY_BACK = {
|
|
15
|
+
"hotel-key": "Rotate the key (zs.hotelKeys.rotate(propertyId, keyId)): the rotation's answer carries a new one, and the old one stops after 24 hours.",
|
|
16
|
+
"rotated-hotel-key": "Revoke the new key (zs.hotelKeys.revoke) and mint one (zs.hotelKeys.create): the key it replaced keeps answering for 24 hours meanwhile.",
|
|
17
|
+
"connect-link": "Make a new link (zs.connectLinks.create with a new idempotency key): it withdraws this one.",
|
|
18
|
+
"webhook-secret": "Rotate the endpoint's secret (zs.webhookEndpoints.rotateSecret(endpoint.id)): the rotation's answer carries a new one.",
|
|
19
|
+
"rotated-webhook-secret": "Rotate the endpoint's secret again (zs.webhookEndpoints.rotateSecret): the old secret keeps signing for 24 hours meanwhile.",
|
|
20
|
+
};
|
|
21
|
+
function kindOf(answer) {
|
|
22
|
+
if (answer === null || typeof answer !== "object")
|
|
23
|
+
return undefined;
|
|
24
|
+
const a = answer;
|
|
25
|
+
if ("url" in a && "link" in a)
|
|
26
|
+
return "connect-link";
|
|
27
|
+
if ("key" in a && "retired" in a)
|
|
28
|
+
return "rotated-hotel-key";
|
|
29
|
+
if ("key" in a && "view" in a)
|
|
30
|
+
return "hotel-key";
|
|
31
|
+
if ("secret" in a && "endpoint" in a)
|
|
32
|
+
return "webhook-secret";
|
|
33
|
+
if ("secret" in a && "secretVersion" in a)
|
|
34
|
+
return "rotated-webhook-secret";
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
/** The answer carried no value: it is a replay of an earlier request. `nextStep` says what to do; nothing here holds a secret. */
|
|
38
|
+
class ZimmerStackShownOnceError extends Error {
|
|
39
|
+
/** Which value is gone. */
|
|
40
|
+
kind;
|
|
41
|
+
/** The way back, in a sentence that names the call. */
|
|
42
|
+
nextStep;
|
|
43
|
+
constructor(kind) {
|
|
44
|
+
super(`This answer is a replay of a request ZimmerStack already answered, so it does not carry the ${FIELD[kind]}: that is shown once, and no copy is kept to show again. If you stored it the first time, use that. If the first answer was lost: ${WAY_BACK[kind]}`);
|
|
45
|
+
this.name = "ZimmerStackShownOnceError";
|
|
46
|
+
this.kind = kind;
|
|
47
|
+
this.nextStep = WAY_BACK[kind];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.ZimmerStackShownOnceError = ZimmerStackShownOnceError;
|
|
51
|
+
/**
|
|
52
|
+
* The value an answer shows once: the key of `zs.hotelKeys.create` and `.rotate`, the `url` of
|
|
53
|
+
* `zs.connectLinks.create`, the `secret` of `zs.webhookEndpoints.create` and `.rotateSecret`.
|
|
54
|
+
*
|
|
55
|
+
* It never hands back null or an empty string for you to store by mistake: when the answer is a replay
|
|
56
|
+
* and the value is gone, it throws `ZimmerStackShownOnceError`, whose `nextStep` names the call that
|
|
57
|
+
* gets you a new one. Store what it returns in your server's settings or secret store at once; never in
|
|
58
|
+
* code, a log line, an error report or a chat.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* const minted = await zs.hotelKeys.create(propertyId, { name: "Night audit job", scopes: ["ari:read"] });
|
|
62
|
+
* await secrets.put(`zimmerstack/hotel-${propertyId}`, shownOnce(minted));
|
|
63
|
+
* console.log(withoutShownOnce(minted)); // safe to log: the key is not in it
|
|
64
|
+
*/
|
|
65
|
+
function shownOnce(answer) {
|
|
66
|
+
const kind = kindOf(answer);
|
|
67
|
+
if (!kind)
|
|
68
|
+
throw new TypeError("shownOnce takes the answer of zs.hotelKeys.create, zs.hotelKeys.rotate, zs.connectLinks.create, zs.webhookEndpoints.create or zs.webhookEndpoints.rotateSecret.");
|
|
69
|
+
const value = answer[FIELD[kind]];
|
|
70
|
+
if (typeof value !== "string" || value === "")
|
|
71
|
+
throw new ZimmerStackShownOnceError(kind);
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* A copy of the answer with the shown-once value taken out (null in its place), for a log line, an
|
|
76
|
+
* audit record or an error report. The answer you pass is not changed.
|
|
77
|
+
*/
|
|
78
|
+
function withoutShownOnce(answer) {
|
|
79
|
+
const kind = kindOf(answer);
|
|
80
|
+
if (!kind)
|
|
81
|
+
return answer;
|
|
82
|
+
return { ...answer, [FIELD[kind]]: null };
|
|
83
|
+
}
|