@zimmerstack/connect 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +107 -0
- package/LICENSE +21 -0
- package/README.md +239 -2
- package/dist/cjs/client.d.ts +260 -0
- package/dist/cjs/client.js +612 -0
- package/dist/cjs/errors.d.ts +46 -0
- package/dist/cjs/errors.js +51 -0
- package/dist/cjs/generated.d.ts +1153 -0
- package/dist/cjs/generated.js +135 -0
- package/dist/cjs/grid.d.ts +105 -0
- package/dist/cjs/grid.js +135 -0
- package/dist/cjs/index.d.ts +16 -0
- package/dist/cjs/index.js +30 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/webhooks.d.ts +60 -0
- package/dist/cjs/webhooks.js +103 -0
- package/dist/esm/client.d.ts +260 -0
- package/dist/esm/client.js +608 -0
- package/dist/esm/errors.d.ts +46 -0
- package/dist/esm/errors.js +46 -0
- package/dist/esm/generated.d.ts +1153 -0
- package/dist/esm/generated.js +132 -0
- package/dist/esm/grid.d.ts +105 -0
- package/dist/esm/grid.js +127 -0
- package/dist/esm/index.d.ts +16 -0
- package/dist/esm/index.js +11 -0
- package/dist/esm/mcp/cli.d.ts +2 -0
- package/dist/esm/mcp/cli.js +86 -0
- package/dist/esm/mcp/key.d.ts +9 -0
- package/dist/esm/mcp/key.js +31 -0
- package/dist/esm/mcp/reference.d.ts +11 -0
- package/dist/esm/mcp/reference.generated.d.ts +33 -0
- package/dist/esm/mcp/reference.generated.js +2084 -0
- package/dist/esm/mcp/reference.js +137 -0
- package/dist/esm/mcp/server.d.ts +29 -0
- package/dist/esm/mcp/server.js +223 -0
- package/dist/esm/webhooks.d.ts +60 -0
- package/dist/esm/webhooks.js +98 -0
- package/package.json +61 -4
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// GENERATED by scripts/generate.mjs from the Connect API's public contract (ZimmerStack Connect API v1).
|
|
3
|
+
// Do not edit: change the contract, then run `pnpm --dir apps/connect-sdk run generate`.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.OPERATIONS = exports.MODIFIER_TAKES_EFFECT = exports.SCOPES = exports.ERROR_CODES = exports.LIMITS = exports.PROPERTY_HEADER = exports.IDEMPOTENCY_HEADER = exports.API_PREFIX = exports.API_ORIGIN = exports.SDK_VERSION = void 0;
|
|
6
|
+
/** This package's version, sent as the user agent so ZimmerStack can tell which client a request came from. */
|
|
7
|
+
exports.SDK_VERSION = "0.2.0";
|
|
8
|
+
/** Where the API lives. The key is sent nowhere else. */
|
|
9
|
+
exports.API_ORIGIN = "https://api.zimmerstack.com";
|
|
10
|
+
exports.API_PREFIX = "/api/v1/connect-api/v1";
|
|
11
|
+
exports.IDEMPOTENCY_HEADER = "x-idempotency-key";
|
|
12
|
+
/** A software vendor's company key names the hotel on every request in this header (GET /properties lists them). */
|
|
13
|
+
exports.PROPERTY_HEADER = "x-zs-property";
|
|
14
|
+
/** The published limits. A property's own figures are on GET /property (limits). */
|
|
15
|
+
exports.LIMITS = {
|
|
16
|
+
"requestsPerMinute": 120,
|
|
17
|
+
"writesPerMinute": 30,
|
|
18
|
+
"propertyRequestsPerMinute": 300,
|
|
19
|
+
"propertyWritesPerMinute": 60,
|
|
20
|
+
"propertyCellsPerMinute": 20000,
|
|
21
|
+
"ipRequestsPerMinute": 240,
|
|
22
|
+
"maxUpdatesPerRequest": 1000,
|
|
23
|
+
"maxCellsPerRequest": 5000,
|
|
24
|
+
"maxBodyBytes": 1048576,
|
|
25
|
+
"horizonDays": 365,
|
|
26
|
+
"bookingsPerMinute": 20,
|
|
27
|
+
"propertyBookingsPerMinute": 40,
|
|
28
|
+
"propertyBookingsPerDay": 500,
|
|
29
|
+
"companyRequestsPerMinute": 1200,
|
|
30
|
+
"maxNightsPerBooking": 90
|
|
31
|
+
};
|
|
32
|
+
/** Every stable error code (`error.code`). Messages are for people and may change; branch on these. */
|
|
33
|
+
exports.ERROR_CODES = [
|
|
34
|
+
"API_KEY_INVALID",
|
|
35
|
+
"API_KEY_REVOKED",
|
|
36
|
+
"API_KEY_EXPIRED",
|
|
37
|
+
"CONNECT_API_NOT_CONFIGURED",
|
|
38
|
+
"IP_NOT_ALLOWED",
|
|
39
|
+
"SCOPE_MISSING",
|
|
40
|
+
"PROPERTY_UNAVAILABLE",
|
|
41
|
+
"CHANNEL_MANAGER_DISABLED",
|
|
42
|
+
"PMS_NOT_AVAILABLE",
|
|
43
|
+
"VALIDATION",
|
|
44
|
+
"RANGE_INVALID",
|
|
45
|
+
"HORIZON",
|
|
46
|
+
"TOO_MANY_UPDATES",
|
|
47
|
+
"TOO_MANY_CELLS",
|
|
48
|
+
"UNKNOWN_ROOM_TYPE",
|
|
49
|
+
"UNKNOWN_RATE_PLAN",
|
|
50
|
+
"CURRENCY_MISMATCH",
|
|
51
|
+
"RATE_LIMITED",
|
|
52
|
+
"NOTHING_ACCEPTED",
|
|
53
|
+
"BATCH_NOT_FOUND",
|
|
54
|
+
"IDEMPOTENCY_KEY_INVALID",
|
|
55
|
+
"IDEMPOTENCY_KEY_REUSED",
|
|
56
|
+
"IDEMPOTENCY_IN_FLIGHT",
|
|
57
|
+
"STAY_INVALID",
|
|
58
|
+
"UNKNOWN_ROOM_TYPE_FOR_PLAN",
|
|
59
|
+
"OCCUPANCY_EXCEEDED",
|
|
60
|
+
"NO_AVAILABILITY",
|
|
61
|
+
"EXTERNAL_REFERENCE_REUSED",
|
|
62
|
+
"PAYMENT_NOT_ALLOWED",
|
|
63
|
+
"PAYMENT_EXCEEDS_TOTAL",
|
|
64
|
+
"BOOKING_NOT_FOUND",
|
|
65
|
+
"BOOKING_NOT_CANCELLABLE",
|
|
66
|
+
"MODULE_NOT_ENTITLED",
|
|
67
|
+
"SANDBOX_ONLY",
|
|
68
|
+
"ROOM_TYPE_NOT_FOUND",
|
|
69
|
+
"RATE_PLAN_NOT_FOUND",
|
|
70
|
+
"CATALOG_CONFLICT",
|
|
71
|
+
"CATALOG_BUSY",
|
|
72
|
+
"CHANNEL_MANAGER_ERROR",
|
|
73
|
+
"PROPERTY_REQUIRED",
|
|
74
|
+
"PROPERTY_NOT_FOUND",
|
|
75
|
+
"ARI_NOT_AUTHORITATIVE",
|
|
76
|
+
"ROUTE_NOT_FOUND",
|
|
77
|
+
"PAYLOAD_TOO_LARGE",
|
|
78
|
+
"AUTH_BACKEND_UNAVAILABLE",
|
|
79
|
+
"BOOKING_NOT_MODIFIABLE",
|
|
80
|
+
"SANDBOX_NOT_FOUND",
|
|
81
|
+
"SANDBOX_INCOMPLETE",
|
|
82
|
+
"WRITES_FROZEN",
|
|
83
|
+
"WRITES_SUSPENDED",
|
|
84
|
+
"SHIELD_UNAVAILABLE",
|
|
85
|
+
"CATALOG_LIMIT_REACHED",
|
|
86
|
+
"TOO_MANY_NEW_ENTITIES",
|
|
87
|
+
"RATE_OUT_OF_BAND",
|
|
88
|
+
"CHANNEL_NOT_FOUND",
|
|
89
|
+
"CHANNEL_SHARED",
|
|
90
|
+
"MODIFIER_OUT_OF_BAND"
|
|
91
|
+
];
|
|
92
|
+
/** What a key may do. The hotel's owner (or, for a vendor's key, the vendor) chose them when the key was made. */
|
|
93
|
+
exports.SCOPES = [
|
|
94
|
+
"ari:read",
|
|
95
|
+
"ari:write",
|
|
96
|
+
"catalog:write",
|
|
97
|
+
"channel-modifiers:write",
|
|
98
|
+
"channel-bookings:read",
|
|
99
|
+
"bookings:read",
|
|
100
|
+
"bookings:write",
|
|
101
|
+
"bookings:payments"
|
|
102
|
+
];
|
|
103
|
+
/** When a channel's price modifier takes effect. Show it where a person saves one. */
|
|
104
|
+
exports.MODIFIER_TAKES_EFFECT = "A modifier applies to prices sent to the channel after the change. Prices already on the channel stay as they are until those nights are sent again.";
|
|
105
|
+
/** idempotent: the server replays the stored answer when the idempotency key repeats, so the client may resend the request after a network failure. */
|
|
106
|
+
exports.OPERATIONS = {
|
|
107
|
+
"GET /properties": { idempotent: false, bodyRequired: false },
|
|
108
|
+
"GET /property": { idempotent: false, bodyRequired: false },
|
|
109
|
+
"GET /catalog": { idempotent: false, bodyRequired: false },
|
|
110
|
+
"PUT /catalog/room-types": { idempotent: true, bodyRequired: true },
|
|
111
|
+
"DELETE /catalog/room-types/{roomTypeId}": { idempotent: false, bodyRequired: false },
|
|
112
|
+
"PUT /catalog/rate-plans": { idempotent: true, bodyRequired: true },
|
|
113
|
+
"DELETE /catalog/rate-plans/{ratePlanId}": { idempotent: false, bodyRequired: false },
|
|
114
|
+
"GET /availability": { idempotent: false, bodyRequired: false },
|
|
115
|
+
"PUT /availability": { idempotent: true, bodyRequired: true },
|
|
116
|
+
"GET /rates": { idempotent: false, bodyRequired: false },
|
|
117
|
+
"PUT /rates": { idempotent: true, bodyRequired: true },
|
|
118
|
+
"GET /restrictions": { idempotent: false, bodyRequired: false },
|
|
119
|
+
"PUT /restrictions": { idempotent: true, bodyRequired: true },
|
|
120
|
+
"GET /sync": { idempotent: false, bodyRequired: false },
|
|
121
|
+
"GET /bookings": { idempotent: false, bodyRequired: false },
|
|
122
|
+
"POST /bookings": { idempotent: true, bodyRequired: true },
|
|
123
|
+
"GET /bookings/{bookingId}": { idempotent: false, bodyRequired: false },
|
|
124
|
+
"POST /bookings/{bookingId}/cancel": { idempotent: false, bodyRequired: false },
|
|
125
|
+
"GET /channels": { idempotent: false, bodyRequired: false },
|
|
126
|
+
"PUT /channels/{channelId}/modifier": { idempotent: true, bodyRequired: true },
|
|
127
|
+
"POST /rates/resend": { idempotent: true, bodyRequired: false },
|
|
128
|
+
"GET /channel-bookings": { idempotent: false, bodyRequired: false },
|
|
129
|
+
"GET /channel-bookings/{bookingId}": { idempotent: false, bodyRequired: false },
|
|
130
|
+
"GET /sandbox/bookings": { idempotent: false, bodyRequired: false },
|
|
131
|
+
"POST /sandbox/bookings": { idempotent: true, bodyRequired: true },
|
|
132
|
+
"POST /sandbox/bookings/{bookingId}/cancel": { idempotent: false, bodyRequired: false },
|
|
133
|
+
"POST /sandbox/bookings/{bookingId}/modify": { idempotent: false, bodyRequired: true },
|
|
134
|
+
"GET /batches/{batchId}": { idempotent: false, bodyRequired: false },
|
|
135
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { type AvailabilityRead, type Catalog, type RatesRead, type RestrictionsRead } from "./generated.js";
|
|
2
|
+
/**
|
|
3
|
+
* The rate grid as a screen wants it: rows are room types with their rate plans, columns are nights.
|
|
4
|
+
* Everything here is pure (no request, no clock), so it can be tested and reused on its own.
|
|
5
|
+
*/
|
|
6
|
+
/** The six stay rules of a rate plan on a night; null = no rule set for that field. */
|
|
7
|
+
export type Restrictions = Omit<RestrictionsRead["cells"][number], "ratePlanId" | "date">;
|
|
8
|
+
export interface Grid {
|
|
9
|
+
from: string;
|
|
10
|
+
to: string;
|
|
11
|
+
/** ISO 4217. Every price in the grid is in it. */
|
|
12
|
+
currency: string;
|
|
13
|
+
/** One column per night, YYYY-MM-DD in the hotel's own timezone. */
|
|
14
|
+
dates: string[];
|
|
15
|
+
rows: Array<{
|
|
16
|
+
roomType: {
|
|
17
|
+
id: number;
|
|
18
|
+
name: string;
|
|
19
|
+
};
|
|
20
|
+
/** date → rooms of this type left to sell */
|
|
21
|
+
available: Record<string, number>;
|
|
22
|
+
plans: Array<{
|
|
23
|
+
ratePlan: {
|
|
24
|
+
id: number;
|
|
25
|
+
name: string;
|
|
26
|
+
mealPlan: Catalog["ratePlans"][number]["mealPlan"];
|
|
27
|
+
};
|
|
28
|
+
/** date → nightly price in MAJOR units (4500 = ₹4,500.00), before any channel's modifier */
|
|
29
|
+
rate: Record<string, number>;
|
|
30
|
+
/** date → the stay rules */
|
|
31
|
+
restrictions: Record<string, Restrictions>;
|
|
32
|
+
}>;
|
|
33
|
+
}>;
|
|
34
|
+
}
|
|
35
|
+
/** What a person changed on the screen: single nights, only the ones that changed. */
|
|
36
|
+
export interface GridEdits {
|
|
37
|
+
availability?: Array<{
|
|
38
|
+
roomTypeId: number;
|
|
39
|
+
date: string;
|
|
40
|
+
available: number;
|
|
41
|
+
}>;
|
|
42
|
+
/** `rate` in MAJOR units. To close a night send `stopSell`, never a zero price. */
|
|
43
|
+
rates?: Array<{
|
|
44
|
+
ratePlanId: number;
|
|
45
|
+
date: string;
|
|
46
|
+
rate: number;
|
|
47
|
+
}>;
|
|
48
|
+
restrictions?: Array<{
|
|
49
|
+
ratePlanId: number;
|
|
50
|
+
date: string;
|
|
51
|
+
} & {
|
|
52
|
+
[K in keyof Restrictions]?: NonNullable<Restrictions[K]>;
|
|
53
|
+
}>;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Every night from `from` to `to`, both included. Dates are plain calendar strings: the arithmetic runs
|
|
57
|
+
* in UTC, so the server's timezone can never move a night.
|
|
58
|
+
*/
|
|
59
|
+
export declare function eachDate(from: string, to: string): string[];
|
|
60
|
+
/** `days` after `date` (negative goes back), as a calendar date. */
|
|
61
|
+
export declare function addDays(date: string, days: number): string;
|
|
62
|
+
/** Put the catalog and the three reads together into rows and columns. */
|
|
63
|
+
export declare function buildGrid(input: {
|
|
64
|
+
from: string;
|
|
65
|
+
to: string;
|
|
66
|
+
catalog: Catalog;
|
|
67
|
+
availability: AvailabilityRead;
|
|
68
|
+
rates: RatesRead;
|
|
69
|
+
restrictions: RestrictionsRead;
|
|
70
|
+
}): Grid;
|
|
71
|
+
export interface Range<V> {
|
|
72
|
+
id: number;
|
|
73
|
+
dateFrom: string;
|
|
74
|
+
dateTo: string;
|
|
75
|
+
value: V;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Fold single-night edits into the fewest ranges: consecutive nights of one row that carry the same
|
|
79
|
+
* value become one `{ dateFrom, dateTo }`. Thirty nights at one price are one update, not thirty. When a
|
|
80
|
+
* night is given twice, the later one wins.
|
|
81
|
+
*/
|
|
82
|
+
export declare function toRanges<V>(cells: ReadonlyArray<{
|
|
83
|
+
id: number;
|
|
84
|
+
date: string;
|
|
85
|
+
value: V;
|
|
86
|
+
}>): Array<Range<V>>;
|
|
87
|
+
/**
|
|
88
|
+
* Split updates so one request stays inside what the API takes: at most 1,000 updates and 5,000 cells
|
|
89
|
+
* (a cell is one row on one night). A single update larger than a request is cut into shorter ranges.
|
|
90
|
+
*/
|
|
91
|
+
export declare function chunk<U extends {
|
|
92
|
+
dateFrom: string;
|
|
93
|
+
dateTo: string;
|
|
94
|
+
}>(updates: readonly U[], maxUpdates?: number, maxCells?: number): U[][];
|
|
95
|
+
/** A channel's price modifier: ordered steps applied to every price sent to that channel. */
|
|
96
|
+
export type ModifierStep = {
|
|
97
|
+
action: "increase" | "decrease";
|
|
98
|
+
type: "percent" | "fixed";
|
|
99
|
+
value: number;
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* What a channel is sent for a price, in MAJOR units: the steps applied in order. Your grid holds the
|
|
103
|
+
* price before any modifier (that is what the API returns); use this to show "on Booking.com" beside it.
|
|
104
|
+
*/
|
|
105
|
+
export declare function channelPrice(rate: number, steps: readonly ModifierStep[]): number;
|
package/dist/cjs/grid.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.eachDate = eachDate;
|
|
4
|
+
exports.addDays = addDays;
|
|
5
|
+
exports.buildGrid = buildGrid;
|
|
6
|
+
exports.toRanges = toRanges;
|
|
7
|
+
exports.chunk = chunk;
|
|
8
|
+
exports.channelPrice = channelPrice;
|
|
9
|
+
const generated_ts_1 = require("./generated.js");
|
|
10
|
+
const DAY = 86_400_000;
|
|
11
|
+
const DATE = /^\d{4}-\d{2}-\d{2}$/;
|
|
12
|
+
function ms(date) {
|
|
13
|
+
const t = DATE.test(date) ? Date.parse(date + "T00:00:00Z") : NaN;
|
|
14
|
+
if (Number.isNaN(t))
|
|
15
|
+
throw new RangeError(`"${date}" is not a calendar date (YYYY-MM-DD)`);
|
|
16
|
+
return t;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Every night from `from` to `to`, both included. Dates are plain calendar strings: the arithmetic runs
|
|
20
|
+
* in UTC, so the server's timezone can never move a night.
|
|
21
|
+
*/
|
|
22
|
+
function eachDate(from, to) {
|
|
23
|
+
const out = [];
|
|
24
|
+
for (let t = ms(from), end = ms(to); t <= end; t += DAY)
|
|
25
|
+
out.push(new Date(t).toISOString().slice(0, 10));
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
/** `days` after `date` (negative goes back), as a calendar date. */
|
|
29
|
+
function addDays(date, days) {
|
|
30
|
+
return new Date(ms(date) + days * DAY).toISOString().slice(0, 10);
|
|
31
|
+
}
|
|
32
|
+
/** Put the catalog and the three reads together into rows and columns. */
|
|
33
|
+
function buildGrid(input) {
|
|
34
|
+
const rows = input.catalog.roomTypes.map((roomType) => ({
|
|
35
|
+
roomType: { id: roomType.id, name: roomType.name },
|
|
36
|
+
available: {},
|
|
37
|
+
plans: input.catalog.ratePlans
|
|
38
|
+
.filter((plan) => plan.roomTypeId === roomType.id)
|
|
39
|
+
.map((plan) => ({ ratePlan: { id: plan.id, name: plan.name, mealPlan: plan.mealPlan }, rate: {}, restrictions: {} })),
|
|
40
|
+
}));
|
|
41
|
+
const rowOf = new Map(rows.map((row) => [row.roomType.id, row]));
|
|
42
|
+
const planOf = new Map(rows.flatMap((row) => row.plans.map((plan) => [plan.ratePlan.id, plan])));
|
|
43
|
+
for (const cell of input.availability.cells) {
|
|
44
|
+
const row = rowOf.get(cell.roomTypeId);
|
|
45
|
+
if (row)
|
|
46
|
+
row.available[cell.date] = cell.available;
|
|
47
|
+
}
|
|
48
|
+
for (const cell of input.rates.cells) {
|
|
49
|
+
const plan = planOf.get(cell.ratePlanId);
|
|
50
|
+
if (plan)
|
|
51
|
+
plan.rate[cell.date] = cell.rateMinor / 100; // read in minor units, shown and written in major
|
|
52
|
+
}
|
|
53
|
+
for (const { ratePlanId, date, ...rules } of input.restrictions.cells) {
|
|
54
|
+
const plan = planOf.get(ratePlanId);
|
|
55
|
+
if (plan)
|
|
56
|
+
plan.restrictions[date] = rules;
|
|
57
|
+
}
|
|
58
|
+
return { from: input.from, to: input.to, currency: input.catalog.currency, dates: eachDate(input.from, input.to), rows };
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Fold single-night edits into the fewest ranges: consecutive nights of one row that carry the same
|
|
62
|
+
* value become one `{ dateFrom, dateTo }`. Thirty nights at one price are one update, not thirty. When a
|
|
63
|
+
* night is given twice, the later one wins.
|
|
64
|
+
*/
|
|
65
|
+
function toRanges(cells) {
|
|
66
|
+
const latest = new Map();
|
|
67
|
+
for (const cell of cells)
|
|
68
|
+
latest.set(`${cell.id}|${cell.date}`, cell);
|
|
69
|
+
const sorted = [...latest.values()].sort((a, b) => a.id - b.id || (a.date < b.date ? -1 : a.date > b.date ? 1 : 0));
|
|
70
|
+
const out = [];
|
|
71
|
+
for (const cell of sorted) {
|
|
72
|
+
const last = out[out.length - 1];
|
|
73
|
+
if (last && last.id === cell.id && same(last.value, cell.value) && ms(cell.date) - ms(last.dateTo) === DAY) {
|
|
74
|
+
last.dateTo = cell.date;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
out.push({ id: cell.id, dateFrom: cell.date, dateTo: cell.date, value: cell.value });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
/** Equal values, whatever order an object's fields were written in. */
|
|
83
|
+
function same(a, b) {
|
|
84
|
+
return stable(a) === stable(b);
|
|
85
|
+
}
|
|
86
|
+
function stable(value) {
|
|
87
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
88
|
+
return JSON.stringify(value);
|
|
89
|
+
const entries = Object.entries(value).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
90
|
+
return JSON.stringify(entries);
|
|
91
|
+
}
|
|
92
|
+
const nights = (range) => Math.max(1, Math.round((ms(range.dateTo) - ms(range.dateFrom)) / DAY) + 1);
|
|
93
|
+
/**
|
|
94
|
+
* Split updates so one request stays inside what the API takes: at most 1,000 updates and 5,000 cells
|
|
95
|
+
* (a cell is one row on one night). A single update larger than a request is cut into shorter ranges.
|
|
96
|
+
*/
|
|
97
|
+
function chunk(updates, maxUpdates = generated_ts_1.LIMITS.maxUpdatesPerRequest, maxCells = generated_ts_1.LIMITS.maxCellsPerRequest) {
|
|
98
|
+
const pieces = [];
|
|
99
|
+
for (const update of updates) {
|
|
100
|
+
let from = update.dateFrom;
|
|
101
|
+
while (nights({ dateFrom: from, dateTo: update.dateTo }) > maxCells) {
|
|
102
|
+
const to = addDays(from, maxCells - 1);
|
|
103
|
+
pieces.push({ ...update, dateFrom: from, dateTo: to });
|
|
104
|
+
from = addDays(to, 1);
|
|
105
|
+
}
|
|
106
|
+
pieces.push(from === update.dateFrom ? update : { ...update, dateFrom: from });
|
|
107
|
+
}
|
|
108
|
+
const out = [];
|
|
109
|
+
let current = [];
|
|
110
|
+
let cells = 0;
|
|
111
|
+
for (const piece of pieces) {
|
|
112
|
+
if (current.length > 0 && (current.length >= maxUpdates || cells + nights(piece) > maxCells)) {
|
|
113
|
+
out.push(current);
|
|
114
|
+
current = [];
|
|
115
|
+
cells = 0;
|
|
116
|
+
}
|
|
117
|
+
current.push(piece);
|
|
118
|
+
cells += nights(piece);
|
|
119
|
+
}
|
|
120
|
+
if (current.length > 0)
|
|
121
|
+
out.push(current);
|
|
122
|
+
return out;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* What a channel is sent for a price, in MAJOR units: the steps applied in order. Your grid holds the
|
|
126
|
+
* price before any modifier (that is what the API returns); use this to show "on Booking.com" beside it.
|
|
127
|
+
*/
|
|
128
|
+
function channelPrice(rate, steps) {
|
|
129
|
+
let minor = Math.round(rate * 100);
|
|
130
|
+
for (const step of steps) {
|
|
131
|
+
const sign = step.action === "decrease" ? -1 : 1;
|
|
132
|
+
minor = step.type === "percent" ? minor * (1 + (sign * step.value) / 100) : minor + sign * Math.round(step.value * 100);
|
|
133
|
+
}
|
|
134
|
+
return Math.round(minor) / 100;
|
|
135
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zimmerstack/connect: the ZimmerStack Connect API from your server.
|
|
3
|
+
*
|
|
4
|
+
* Server only. The key can change a hotel's prices on every channel: it never goes to a browser, a
|
|
5
|
+
* mobile app, a repository or a log line.
|
|
6
|
+
*/
|
|
7
|
+
export { ZimmerStack } from "./client.js";
|
|
8
|
+
export type { ClientEvent, ReadOptions, RequestArgs, WriteOptions, ZimmerStackOptions } from "./client.js";
|
|
9
|
+
export { ZimmerStackError, ZimmerStackUsageError } from "./errors.js";
|
|
10
|
+
export type { LocalErrorCode } from "./errors.js";
|
|
11
|
+
export { addDays, buildGrid, channelPrice, chunk, eachDate, toRanges } from "./grid.js";
|
|
12
|
+
export type { Grid, GridEdits, ModifierStep, Range, Restrictions } from "./grid.js";
|
|
13
|
+
export { verifyWebhook, ZimmerStackWebhookError } from "./webhooks.js";
|
|
14
|
+
export type { WebhookEvent } from "./webhooks.js";
|
|
15
|
+
export { ERROR_CODES, LIMITS, MODIFIER_TAKES_EFFECT, SCOPES, SDK_VERSION } from "./generated.js";
|
|
16
|
+
export type * from "./generated.js";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SDK_VERSION = exports.SCOPES = exports.MODIFIER_TAKES_EFFECT = exports.LIMITS = exports.ERROR_CODES = exports.ZimmerStackWebhookError = exports.verifyWebhook = exports.toRanges = exports.eachDate = exports.chunk = exports.channelPrice = exports.buildGrid = exports.addDays = exports.ZimmerStackUsageError = exports.ZimmerStackError = exports.ZimmerStack = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @zimmerstack/connect: the ZimmerStack Connect API from your server.
|
|
6
|
+
*
|
|
7
|
+
* Server only. The key can change a hotel's prices on every channel: it never goes to a browser, a
|
|
8
|
+
* mobile app, a repository or a log line.
|
|
9
|
+
*/
|
|
10
|
+
var client_ts_1 = require("./client.js");
|
|
11
|
+
Object.defineProperty(exports, "ZimmerStack", { enumerable: true, get: function () { return client_ts_1.ZimmerStack; } });
|
|
12
|
+
var errors_ts_1 = require("./errors.js");
|
|
13
|
+
Object.defineProperty(exports, "ZimmerStackError", { enumerable: true, get: function () { return errors_ts_1.ZimmerStackError; } });
|
|
14
|
+
Object.defineProperty(exports, "ZimmerStackUsageError", { enumerable: true, get: function () { return errors_ts_1.ZimmerStackUsageError; } });
|
|
15
|
+
var grid_ts_1 = require("./grid.js");
|
|
16
|
+
Object.defineProperty(exports, "addDays", { enumerable: true, get: function () { return grid_ts_1.addDays; } });
|
|
17
|
+
Object.defineProperty(exports, "buildGrid", { enumerable: true, get: function () { return grid_ts_1.buildGrid; } });
|
|
18
|
+
Object.defineProperty(exports, "channelPrice", { enumerable: true, get: function () { return grid_ts_1.channelPrice; } });
|
|
19
|
+
Object.defineProperty(exports, "chunk", { enumerable: true, get: function () { return grid_ts_1.chunk; } });
|
|
20
|
+
Object.defineProperty(exports, "eachDate", { enumerable: true, get: function () { return grid_ts_1.eachDate; } });
|
|
21
|
+
Object.defineProperty(exports, "toRanges", { enumerable: true, get: function () { return grid_ts_1.toRanges; } });
|
|
22
|
+
var webhooks_ts_1 = require("./webhooks.js");
|
|
23
|
+
Object.defineProperty(exports, "verifyWebhook", { enumerable: true, get: function () { return webhooks_ts_1.verifyWebhook; } });
|
|
24
|
+
Object.defineProperty(exports, "ZimmerStackWebhookError", { enumerable: true, get: function () { return webhooks_ts_1.ZimmerStackWebhookError; } });
|
|
25
|
+
var generated_ts_1 = require("./generated.js");
|
|
26
|
+
Object.defineProperty(exports, "ERROR_CODES", { enumerable: true, get: function () { return generated_ts_1.ERROR_CODES; } });
|
|
27
|
+
Object.defineProperty(exports, "LIMITS", { enumerable: true, get: function () { return generated_ts_1.LIMITS; } });
|
|
28
|
+
Object.defineProperty(exports, "MODIFIER_TAKES_EFFECT", { enumerable: true, get: function () { return generated_ts_1.MODIFIER_TAKES_EFFECT; } });
|
|
29
|
+
Object.defineProperty(exports, "SCOPES", { enumerable: true, get: function () { return generated_ts_1.SCOPES; } });
|
|
30
|
+
Object.defineProperty(exports, "SDK_VERSION", { enumerable: true, get: function () { return generated_ts_1.SDK_VERSION; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "commonjs" }
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { ChannelBooking } from "./generated.js";
|
|
2
|
+
/**
|
|
3
|
+
* Verifying a webhook ZimmerStack sent you (Standard Webhooks: the `webhook-id`, `webhook-timestamp` and
|
|
4
|
+
* `webhook-signature` headers). Uses Web Crypto, so it runs on Node and on edge runtimes alike.
|
|
5
|
+
*/
|
|
6
|
+
/** What every delivery carries. Deliveries are at-least-once: dedupe on `id`, order on `occurredAt`. */
|
|
7
|
+
export interface WebhookEvent {
|
|
8
|
+
/** "evt_<uuid>", the same on every retry and on every endpoint. */
|
|
9
|
+
id: string;
|
|
10
|
+
/** channel.booking.created · channel.booking.modified · channel.booking.cancelled · channel.sync.* · zimmerstack.ping */
|
|
11
|
+
type: string;
|
|
12
|
+
apiVersion: string;
|
|
13
|
+
occurredAt: string;
|
|
14
|
+
/** "test" for events from a sandbox or the "send test" button. */
|
|
15
|
+
mode: "live" | "test";
|
|
16
|
+
property: {
|
|
17
|
+
id: number;
|
|
18
|
+
name: string;
|
|
19
|
+
currency: string;
|
|
20
|
+
timezone: string;
|
|
21
|
+
[key: string]: unknown;
|
|
22
|
+
};
|
|
23
|
+
data: {
|
|
24
|
+
booking?: ChannelBooking;
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
};
|
|
27
|
+
/** Dotted paths that changed, on a modification ("stay.checkOut"). */
|
|
28
|
+
changes?: string[] | null;
|
|
29
|
+
/** What those fields held before, on a modification. */
|
|
30
|
+
previous?: Record<string, unknown> | null;
|
|
31
|
+
truncated?: boolean;
|
|
32
|
+
}
|
|
33
|
+
export declare class ZimmerStackWebhookError extends Error {
|
|
34
|
+
/** Why the delivery was refused. Answer 400 and do not act on the body. */
|
|
35
|
+
readonly reason: "missing_headers" | "stale_timestamp" | "bad_signature" | "bad_body";
|
|
36
|
+
constructor(reason: ZimmerStackWebhookError["reason"], message: string);
|
|
37
|
+
}
|
|
38
|
+
type HeaderBag = {
|
|
39
|
+
get(name: string): string | null;
|
|
40
|
+
} | Record<string, string | string[] | undefined>;
|
|
41
|
+
/**
|
|
42
|
+
* Check a delivery and return its event. Throws `ZimmerStackWebhookError` when the delivery is not
|
|
43
|
+
* ZimmerStack's, is older than five minutes, or does not parse.
|
|
44
|
+
*
|
|
45
|
+
* `rawBody` must be the request body exactly as it arrived (the bytes, or the text before any JSON
|
|
46
|
+
* parsing): a body that was parsed and written out again will not match its signature.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* // Next.js route handler
|
|
50
|
+
* export async function POST(req: Request) {
|
|
51
|
+
* const event = await verifyWebhook(await req.text(), req.headers, process.env.ZIMMERSTACK_WEBHOOK_SECRET!);
|
|
52
|
+
* // … dedupe on event.id, then act on event.type
|
|
53
|
+
* return new Response(null, { status: 204 });
|
|
54
|
+
* }
|
|
55
|
+
*/
|
|
56
|
+
export declare function verifyWebhook(rawBody: string | Uint8Array, headers: HeaderBag, secret: string, options?: {
|
|
57
|
+
toleranceSeconds?: number;
|
|
58
|
+
now?: number;
|
|
59
|
+
}): Promise<WebhookEvent>;
|
|
60
|
+
export {};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ZimmerStackWebhookError = void 0;
|
|
4
|
+
exports.verifyWebhook = verifyWebhook;
|
|
5
|
+
class ZimmerStackWebhookError extends Error {
|
|
6
|
+
/** Why the delivery was refused. Answer 400 and do not act on the body. */
|
|
7
|
+
reason;
|
|
8
|
+
constructor(reason, message) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "ZimmerStackWebhookError";
|
|
11
|
+
this.reason = reason;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.ZimmerStackWebhookError = ZimmerStackWebhookError;
|
|
15
|
+
function header(headers, name) {
|
|
16
|
+
if (typeof headers.get === "function")
|
|
17
|
+
return headers.get(name) ?? undefined;
|
|
18
|
+
const bag = headers;
|
|
19
|
+
const value = bag[name] ?? bag[Object.keys(bag).find((key) => key.toLowerCase() === name) ?? ""];
|
|
20
|
+
return Array.isArray(value) ? value[0] : value;
|
|
21
|
+
}
|
|
22
|
+
function fromBase64(text) {
|
|
23
|
+
const raw = atob(text);
|
|
24
|
+
const bytes = new Uint8Array(raw.length);
|
|
25
|
+
for (let i = 0; i < raw.length; i++)
|
|
26
|
+
bytes[i] = raw.charCodeAt(i);
|
|
27
|
+
return bytes;
|
|
28
|
+
}
|
|
29
|
+
/** Compares every byte whatever the first difference, so the time taken says nothing about the signature. */
|
|
30
|
+
function equalBytes(a, b) {
|
|
31
|
+
if (a.length !== b.length)
|
|
32
|
+
return false;
|
|
33
|
+
let diff = 0;
|
|
34
|
+
for (let i = 0; i < a.length; i++)
|
|
35
|
+
diff |= a[i] ^ b[i];
|
|
36
|
+
return diff === 0;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Check a delivery and return its event. Throws `ZimmerStackWebhookError` when the delivery is not
|
|
40
|
+
* ZimmerStack's, is older than five minutes, or does not parse.
|
|
41
|
+
*
|
|
42
|
+
* `rawBody` must be the request body exactly as it arrived (the bytes, or the text before any JSON
|
|
43
|
+
* parsing): a body that was parsed and written out again will not match its signature.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* // Next.js route handler
|
|
47
|
+
* export async function POST(req: Request) {
|
|
48
|
+
* const event = await verifyWebhook(await req.text(), req.headers, process.env.ZIMMERSTACK_WEBHOOK_SECRET!);
|
|
49
|
+
* // … dedupe on event.id, then act on event.type
|
|
50
|
+
* return new Response(null, { status: 204 });
|
|
51
|
+
* }
|
|
52
|
+
*/
|
|
53
|
+
async function verifyWebhook(rawBody, headers, secret, options = {}) {
|
|
54
|
+
const id = header(headers, "webhook-id");
|
|
55
|
+
const timestamp = header(headers, "webhook-timestamp");
|
|
56
|
+
const signature = header(headers, "webhook-signature");
|
|
57
|
+
if (!id || !timestamp || !signature)
|
|
58
|
+
throw new ZimmerStackWebhookError("missing_headers", "The webhook-id, webhook-timestamp and webhook-signature headers are required");
|
|
59
|
+
if (!secret)
|
|
60
|
+
throw new ZimmerStackWebhookError("bad_signature", "No webhook secret was given to verify against");
|
|
61
|
+
const sent = Number(timestamp);
|
|
62
|
+
const now = options.now ?? Math.floor(Date.now() / 1000);
|
|
63
|
+
if (!Number.isFinite(sent) || Math.abs(now - sent) > (options.toleranceSeconds ?? 300)) {
|
|
64
|
+
throw new ZimmerStackWebhookError("stale_timestamp", "The delivery's timestamp is outside the allowed window");
|
|
65
|
+
}
|
|
66
|
+
const encoder = new TextEncoder();
|
|
67
|
+
const body = typeof rawBody === "string" ? encoder.encode(rawBody) : rawBody;
|
|
68
|
+
const prefix = encoder.encode(`${id}.${timestamp}.`);
|
|
69
|
+
const content = new Uint8Array(prefix.length + body.length);
|
|
70
|
+
content.set(prefix, 0);
|
|
71
|
+
content.set(body, prefix.length);
|
|
72
|
+
let expected;
|
|
73
|
+
try {
|
|
74
|
+
const key = await crypto.subtle.importKey("raw", fromBase64(secret.trim().replace(/^whsec_/, "")), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
75
|
+
expected = new Uint8Array(await crypto.subtle.sign("HMAC", key, content));
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
throw new ZimmerStackWebhookError("bad_signature", "The webhook secret is not a valid whsec_ secret");
|
|
79
|
+
}
|
|
80
|
+
// During a secret rotation the header carries one signature per secret, space-separated.
|
|
81
|
+
const matches = signature.split(/\s+/).some((part) => {
|
|
82
|
+
const [version, value] = part.split(",");
|
|
83
|
+
if (version !== "v1" || !value)
|
|
84
|
+
return false;
|
|
85
|
+
try {
|
|
86
|
+
return equalBytes(fromBase64(value), expected);
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
if (!matches)
|
|
93
|
+
throw new ZimmerStackWebhookError("bad_signature", "The delivery's signature does not match");
|
|
94
|
+
try {
|
|
95
|
+
const event = JSON.parse(typeof rawBody === "string" ? rawBody : new TextDecoder().decode(rawBody));
|
|
96
|
+
if (!event || typeof event !== "object" || typeof event.id !== "string" || typeof event.type !== "string")
|
|
97
|
+
throw new Error("not an event");
|
|
98
|
+
return event;
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
throw new ZimmerStackWebhookError("bad_body", "The delivery is signed correctly but its body is not an event");
|
|
102
|
+
}
|
|
103
|
+
}
|