@tallyui/core 3.9.1 → 3.9.2
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/dist/chunk-UFWXJAMF.js +185 -0
- package/dist/{commands-BPNfbeF9.d.ts → command-result-MfXME8sU.d.ts} +10 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +6 -2
- package/dist/server/index.d.ts +3 -12
- package/dist/server/index.js +4 -167
- package/package.json +1 -1
- package/src/index.ts +4 -0
- package/dist/chunk-C4PWPOKO.js +0 -18
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// src/commands.ts
|
|
2
|
+
var COMMANDS_PATH = "/tally/v1/commands";
|
|
3
|
+
var PROTOCOL_HEADER = "X-Tally-Protocol";
|
|
4
|
+
var PROTOCOL_VERSION = 1;
|
|
5
|
+
var MAX_COMMANDS_PER_BATCH = 50;
|
|
6
|
+
function isCommandBatchResponse(value) {
|
|
7
|
+
return typeof value === "object" && value !== null && "results" in value && Array.isArray(value.results) && value.results.every(
|
|
8
|
+
(result) => typeof result === "object" && result !== null && "id" in result && typeof result.id === "string" && "status" in result && (result.status === "applied" || result.status === "duplicate" || result.status === "rejected")
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// src/server/command-result.ts
|
|
13
|
+
var CommandResultError = class extends Error {
|
|
14
|
+
constructor(message) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "CommandResultError";
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
function parseCommandResult(value) {
|
|
20
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
21
|
+
throw new CommandResultError("Invalid result: expected object");
|
|
22
|
+
}
|
|
23
|
+
const input = value;
|
|
24
|
+
if (typeof input.id !== "string" || input.id.length === 0) {
|
|
25
|
+
throw new CommandResultError("Invalid id");
|
|
26
|
+
}
|
|
27
|
+
if (input.status !== "applied" && input.status !== "duplicate" && input.status !== "rejected") {
|
|
28
|
+
throw new CommandResultError("Invalid status");
|
|
29
|
+
}
|
|
30
|
+
const result = { id: input.id, status: input.status };
|
|
31
|
+
const object = (value2) => typeof value2 === "object" && value2 !== null && (Object.getPrototypeOf(value2) === Object.prototype || Object.getPrototypeOf(value2) === null);
|
|
32
|
+
if (input.serverRefs !== void 0) {
|
|
33
|
+
if (typeof input.serverRefs !== "object" || input.serverRefs === null || Array.isArray(input.serverRefs)) {
|
|
34
|
+
throw new CommandResultError("Invalid serverRefs");
|
|
35
|
+
}
|
|
36
|
+
const refs = input.serverRefs;
|
|
37
|
+
if (typeof refs.orderId !== "string" || refs.orderId.length === 0) {
|
|
38
|
+
throw new CommandResultError("Invalid serverRefs.orderId");
|
|
39
|
+
}
|
|
40
|
+
if (refs.displayId !== void 0 && typeof refs.displayId !== "string") {
|
|
41
|
+
throw new CommandResultError("Invalid serverRefs.displayId");
|
|
42
|
+
}
|
|
43
|
+
if (!Number.isSafeInteger(refs.totalMinor)) {
|
|
44
|
+
throw new CommandResultError("Invalid serverRefs.totalMinor");
|
|
45
|
+
}
|
|
46
|
+
result.serverRefs = { orderId: refs.orderId, totalMinor: refs.totalMinor };
|
|
47
|
+
if (refs.displayId !== void 0) result.serverRefs.displayId = refs.displayId;
|
|
48
|
+
}
|
|
49
|
+
if (input.register !== void 0) {
|
|
50
|
+
if (!object(input.register)) throw new CommandResultError("Invalid register");
|
|
51
|
+
result.register = input.register;
|
|
52
|
+
}
|
|
53
|
+
if (input.refund !== void 0) {
|
|
54
|
+
if (!object(input.refund)) throw new CommandResultError("Invalid refund");
|
|
55
|
+
const refund = input.refund;
|
|
56
|
+
if (!Number.isSafeInteger(refund.totalMinor) || refund.totalMinor < 0) throw new CommandResultError("Invalid refund.totalMinor");
|
|
57
|
+
if (!object(refund.byMethod)) throw new CommandResultError("Invalid refund.byMethod");
|
|
58
|
+
const byMethod = Object.fromEntries(Object.entries(refund.byMethod).map(([key, amount]) => {
|
|
59
|
+
if (!key.length) throw new CommandResultError("Invalid refund.byMethod");
|
|
60
|
+
if (!Number.isSafeInteger(amount) || amount < 0) throw new CommandResultError(`Invalid refund.byMethod.${key}`);
|
|
61
|
+
return [key, amount];
|
|
62
|
+
}));
|
|
63
|
+
if (Object.values(byMethod).reduce((sum, amount) => sum + amount, 0) !== refund.totalMinor) throw new CommandResultError("Invalid refund.byMethod");
|
|
64
|
+
if (!Array.isArray(refund.refunds) || refund.refunds.length === 0) throw new CommandResultError("Invalid refund.refunds");
|
|
65
|
+
const refunds = refund.refunds.map((value2, index) => {
|
|
66
|
+
const field = `refund.refunds[${index}]`;
|
|
67
|
+
if (!object(value2)) throw new CommandResultError(`Invalid ${field}`);
|
|
68
|
+
const entry = value2;
|
|
69
|
+
for (const key of ["id", "paymentId"]) {
|
|
70
|
+
if (typeof entry[key] !== "string" || entry[key].length === 0) throw new CommandResultError(`Invalid ${field}.${key}`);
|
|
71
|
+
}
|
|
72
|
+
if (!Number.isSafeInteger(entry.totalMinor) || entry.totalMinor < 0) throw new CommandResultError(`Invalid ${field}.totalMinor`);
|
|
73
|
+
if (typeof entry.state !== "string" || entry.state.length === 0) throw new CommandResultError(`Invalid ${field}.state`);
|
|
74
|
+
return { id: entry.id, paymentId: entry.paymentId, totalMinor: entry.totalMinor, state: entry.state };
|
|
75
|
+
});
|
|
76
|
+
if (refunds.reduce((sum, entry) => sum + entry.totalMinor, 0) !== refund.totalMinor) throw new CommandResultError("Invalid refund.refunds");
|
|
77
|
+
result.refund = { totalMinor: refund.totalMinor, byMethod, refunds };
|
|
78
|
+
}
|
|
79
|
+
if (result.status === "applied" && result.serverRefs === void 0 && result.register === void 0 && result.refund === void 0) {
|
|
80
|
+
throw new CommandResultError("Invalid serverRefs: required for applied");
|
|
81
|
+
}
|
|
82
|
+
if (input.warnings !== void 0) {
|
|
83
|
+
if (!Array.isArray(input.warnings)) {
|
|
84
|
+
throw new CommandResultError("Invalid warnings");
|
|
85
|
+
}
|
|
86
|
+
result.warnings = input.warnings.map((warning, index) => {
|
|
87
|
+
const field = `warnings[${index}]`;
|
|
88
|
+
if (typeof warning !== "object" || warning === null || Array.isArray(warning)) {
|
|
89
|
+
throw new CommandResultError(`Invalid ${field}`);
|
|
90
|
+
}
|
|
91
|
+
const item = warning;
|
|
92
|
+
if (item.code === "total_mismatch") {
|
|
93
|
+
for (const key of ["expectedMinor", "serverMinor"]) {
|
|
94
|
+
if (!Number.isInteger(item[key])) {
|
|
95
|
+
throw new CommandResultError(`Invalid ${field}.${key}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (item.bridgeMinor != null && !Number.isSafeInteger(item.bridgeMinor)) {
|
|
99
|
+
throw new CommandResultError(`Invalid ${field}.bridgeMinor`);
|
|
100
|
+
}
|
|
101
|
+
return item.bridgeMinor != null ? { code: item.code, expectedMinor: item.expectedMinor, serverMinor: item.serverMinor, bridgeMinor: item.bridgeMinor } : { code: item.code, expectedMinor: item.expectedMinor, serverMinor: item.serverMinor };
|
|
102
|
+
}
|
|
103
|
+
if (item.code === "insufficient_stock") {
|
|
104
|
+
if (typeof item.variantId !== "string" || item.variantId.length === 0) {
|
|
105
|
+
throw new CommandResultError(`Invalid ${field}.variantId`);
|
|
106
|
+
}
|
|
107
|
+
if (!Number.isInteger(item.quantity) || item.quantity < 1) {
|
|
108
|
+
throw new CommandResultError(`Invalid ${field}.quantity`);
|
|
109
|
+
}
|
|
110
|
+
return { code: item.code, variantId: item.variantId, quantity: item.quantity };
|
|
111
|
+
}
|
|
112
|
+
if (item.code === "tax_rate_mismatch") {
|
|
113
|
+
if (!Number.isSafeInteger(item.ratePpm) || item.ratePpm < 0) {
|
|
114
|
+
throw new CommandResultError(`Invalid ${field}.ratePpm`);
|
|
115
|
+
}
|
|
116
|
+
for (const key of ["expectedMinor", "serverMinor"]) {
|
|
117
|
+
if (!Number.isSafeInteger(item[key])) {
|
|
118
|
+
throw new CommandResultError(`Invalid ${field}.${key}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return { code: item.code, ratePpm: item.ratePpm, expectedMinor: item.expectedMinor, serverMinor: item.serverMinor };
|
|
122
|
+
}
|
|
123
|
+
if (item.code === "customer_ignored") {
|
|
124
|
+
if (typeof item.customerId !== "string" || item.customerId.length === 0 || item.customerId.length > 64) {
|
|
125
|
+
throw new CommandResultError(`Invalid ${field}.customerId`);
|
|
126
|
+
}
|
|
127
|
+
return { code: item.code, customerId: item.customerId };
|
|
128
|
+
}
|
|
129
|
+
if (item.code === "register_session_unknown") {
|
|
130
|
+
if (typeof item.sessionId !== "string" || item.sessionId.length === 0 || item.sessionId.length > 64) {
|
|
131
|
+
throw new CommandResultError(`Invalid ${field}.sessionId`);
|
|
132
|
+
}
|
|
133
|
+
return { code: item.code, sessionId: item.sessionId };
|
|
134
|
+
}
|
|
135
|
+
if (item.code === "figures_mismatch") {
|
|
136
|
+
if (!Array.isArray(item.fields) || item.fields.length === 0) throw new CommandResultError(`Invalid ${field}.fields`);
|
|
137
|
+
const seen = [];
|
|
138
|
+
return { code: item.code, fields: item.fields.map((value2, i) => {
|
|
139
|
+
const path = `${field}.fields[${i}]`;
|
|
140
|
+
if (typeof value2 !== "object" || value2 === null || Array.isArray(value2)) throw new CommandResultError(`Invalid ${path}`);
|
|
141
|
+
const entry = value2;
|
|
142
|
+
if (!["subtotalMinor", "taxMinor", "discountMinor"].includes(entry.field) || seen.includes(entry.field)) {
|
|
143
|
+
throw new CommandResultError(`Invalid ${path}.field`);
|
|
144
|
+
}
|
|
145
|
+
seen.push(entry.field);
|
|
146
|
+
if (!Number.isSafeInteger(entry.tillMinor)) throw new CommandResultError(`Invalid ${path}.tillMinor`);
|
|
147
|
+
if (!Number.isSafeInteger(entry.serverMinor) || entry.serverMinor === entry.tillMinor) throw new CommandResultError(`Invalid ${path}.serverMinor`);
|
|
148
|
+
return { field: entry.field, tillMinor: entry.tillMinor, serverMinor: entry.serverMinor };
|
|
149
|
+
}) };
|
|
150
|
+
}
|
|
151
|
+
throw new CommandResultError(`Invalid ${field}.code`);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
if (input.error !== void 0) {
|
|
155
|
+
if (typeof input.error !== "object" || input.error === null || Array.isArray(input.error)) {
|
|
156
|
+
throw new CommandResultError("Invalid error");
|
|
157
|
+
}
|
|
158
|
+
const error = input.error;
|
|
159
|
+
if (typeof error.code !== "string" || error.code.length === 0) {
|
|
160
|
+
throw new CommandResultError("Invalid error.code");
|
|
161
|
+
}
|
|
162
|
+
if (typeof error.message !== "string") {
|
|
163
|
+
throw new CommandResultError("Invalid error.message");
|
|
164
|
+
}
|
|
165
|
+
result.error = { code: error.code, message: error.message };
|
|
166
|
+
if (error.data !== void 0) {
|
|
167
|
+
if (!object(error.data)) throw new CommandResultError("Invalid error.data");
|
|
168
|
+
result.error.data = error.data;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (result.status === "rejected" && result.error === void 0) {
|
|
172
|
+
throw new CommandResultError("Invalid error: required for rejected");
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export {
|
|
178
|
+
COMMANDS_PATH,
|
|
179
|
+
PROTOCOL_HEADER,
|
|
180
|
+
PROTOCOL_VERSION,
|
|
181
|
+
MAX_COMMANDS_PER_BATCH,
|
|
182
|
+
isCommandBatchResponse,
|
|
183
|
+
CommandResultError,
|
|
184
|
+
parseCommandResult
|
|
185
|
+
};
|
|
@@ -470,4 +470,13 @@ declare const MAX_COMMANDS_PER_BATCH = 50;
|
|
|
470
470
|
*/
|
|
471
471
|
declare function isCommandBatchResponse(value: unknown): value is CommandBatchResponse;
|
|
472
472
|
|
|
473
|
-
|
|
473
|
+
/** parseCommandResult's refusal; its message names the first bad field. A plugin maps it
|
|
474
|
+
* to its own invalid-data error (e.g. Medusa's MedusaError INVALID_DATA). */
|
|
475
|
+
declare class CommandResultError extends Error {
|
|
476
|
+
constructor(message: string);
|
|
477
|
+
}
|
|
478
|
+
/** Validates a CommandResult (e.g. one read back from the ledger). Throws
|
|
479
|
+
* CommandResultError naming the first bad field. */
|
|
480
|
+
declare function parseCommandResult(value: unknown): CommandResult;
|
|
481
|
+
|
|
482
|
+
export { type AnyCommandEnvelope as A, type BatchTooLargeBody as B, type CommandWarning as C, type RegisterCommandEnvelope as D, type RegisterCommandResult as E, type RegisterCommandType as F, type RegisterMovementRecordPayload as G, type RegisterMovementVoidPayload as H, type RegisterSessionAlreadyOpenData as I, type RegisterSessionOpenPayload as J, type RegisterSessionSupersededData as K, type RegisterSessionTransitionPayload as L, MAX_COMMANDS_PER_BATCH as M, isCommandBatchResponse as N, type OrderCreateDisplay as O, PROTOCOL_HEADER as P, parseCommandResult as Q, type RegisterClosureSubmitPayload as R, COMMANDS_PATH as a, type CommandBatchRequest as b, type CommandBatchResponse as c, type CommandEnvelope as d, type CommandError as e, type CommandResult as f, CommandResultError as g, type CommandServerRefs as h, type CommandStatus as i, type CommandType as j, type OrderCreateEnvelope as k, type OrderCreateLine as l, type OrderCreatePayload as m, type OrderCreatePayment as n, type OrderCreateTaxRate as o, type OrderRefundAmountMismatchData as p, type OrderRefundEnvelope as q, type OrderRefundLine as r, type OrderRefundNoOpenSessionData as s, type OrderRefundOrderStateData as t, type OrderRefundPayload as u, type OrderRefundQuantityExceedsData as v, type OrderRefundRejectionCode as w, type OrderRefundResult as x, PROTOCOL_VERSION as y, type PaymentMethodKind as z };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { RxReplicationPullStreamItem, RxReplicationWriteToMasterRow, RxJsonSchema } from 'rxdb';
|
|
2
2
|
import { Observable } from 'rxjs';
|
|
3
|
-
import { C as CommandWarning } from './
|
|
4
|
-
export { A as AnyCommandEnvelope, B as BatchTooLargeBody, a as COMMANDS_PATH, b as CommandBatchRequest, c as CommandBatchResponse, d as CommandEnvelope, e as CommandError, f as CommandResult, g as
|
|
3
|
+
import { C as CommandWarning } from './command-result-MfXME8sU.js';
|
|
4
|
+
export { A as AnyCommandEnvelope, B as BatchTooLargeBody, a as COMMANDS_PATH, b as CommandBatchRequest, c as CommandBatchResponse, d as CommandEnvelope, e as CommandError, f as CommandResult, g as CommandResultError, h as CommandServerRefs, i as CommandStatus, j as CommandType, M as MAX_COMMANDS_PER_BATCH, O as OrderCreateDisplay, k as OrderCreateEnvelope, l as OrderCreateLine, m as OrderCreatePayload, n as OrderCreatePayment, o as OrderCreateTaxRate, p as OrderRefundAmountMismatchData, q as OrderRefundEnvelope, r as OrderRefundLine, s as OrderRefundNoOpenSessionData, t as OrderRefundOrderStateData, u as OrderRefundPayload, v as OrderRefundQuantityExceedsData, w as OrderRefundRejectionCode, x as OrderRefundResult, P as PROTOCOL_HEADER, y as PROTOCOL_VERSION, z as PaymentMethodKind, R as RegisterClosureSubmitPayload, D as RegisterCommandEnvelope, E as RegisterCommandResult, F as RegisterCommandType, G as RegisterMovementRecordPayload, H as RegisterMovementVoidPayload, I as RegisterSessionAlreadyOpenData, J as RegisterSessionOpenPayload, K as RegisterSessionSupersededData, L as RegisterSessionTransitionPayload, N as isCommandBatchResponse, Q as parseCommandResult } from './command-result-MfXME8sU.js';
|
|
5
5
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
6
6
|
import { ReactNode } from 'react';
|
|
7
7
|
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
COMMANDS_PATH,
|
|
3
|
+
CommandResultError,
|
|
3
4
|
MAX_COMMANDS_PER_BATCH,
|
|
4
5
|
PROTOCOL_HEADER,
|
|
5
6
|
PROTOCOL_VERSION,
|
|
6
|
-
isCommandBatchResponse
|
|
7
|
-
|
|
7
|
+
isCommandBatchResponse,
|
|
8
|
+
parseCommandResult
|
|
9
|
+
} from "./chunk-UFWXJAMF.js";
|
|
8
10
|
import {
|
|
9
11
|
__export
|
|
10
12
|
} from "./chunk-MLKGABMK.js";
|
|
@@ -1750,6 +1752,7 @@ function toCouponConfigs(codes, coupons) {
|
|
|
1750
1752
|
}
|
|
1751
1753
|
export {
|
|
1752
1754
|
COMMANDS_PATH,
|
|
1755
|
+
CommandResultError,
|
|
1753
1756
|
ConnectorProvider,
|
|
1754
1757
|
ConnectorUnauthorizedError,
|
|
1755
1758
|
CustomerServiceError,
|
|
@@ -1775,6 +1778,7 @@ export {
|
|
|
1775
1778
|
moneyFromDecimalString,
|
|
1776
1779
|
moneyFromMajor,
|
|
1777
1780
|
moneyToMajor,
|
|
1781
|
+
parseCommandResult,
|
|
1778
1782
|
parseInfoCapabilities,
|
|
1779
1783
|
parseLineTax,
|
|
1780
1784
|
parseTaxRounding,
|
package/dist/server/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { e as CommandError,
|
|
2
|
-
export { M as MAX_COMMANDS_PER_BATCH, O as OrderCreateDisplay,
|
|
1
|
+
import { e as CommandError, m as OrderCreatePayload, d as CommandEnvelope, D as RegisterCommandEnvelope, A as AnyCommandEnvelope, f as CommandResult, B as BatchTooLargeBody, c as CommandBatchResponse, C as CommandWarning, E as RegisterCommandResult, w as OrderRefundRejectionCode } from '../command-result-MfXME8sU.js';
|
|
2
|
+
export { g as CommandResultError, M as MAX_COMMANDS_PER_BATCH, O as OrderCreateDisplay, o as OrderCreateTaxRate, Q as parseCommandResult } from '../command-result-MfXME8sU.js';
|
|
3
3
|
|
|
4
4
|
/** ISO 4217 minor-unit exponent as resolved by Intl (which rejects malformed codes); depends on the runtime's ICU data. */
|
|
5
5
|
declare function currencyDecimals(currency: string): number;
|
|
@@ -128,15 +128,6 @@ type Rejection = {
|
|
|
128
128
|
};
|
|
129
129
|
declare function totalWarnings(expectedMinor: number, serverMinor: number): CommandWarning[];
|
|
130
130
|
|
|
131
|
-
/** parseCommandResult's refusal; its message names the first bad field. A plugin maps it
|
|
132
|
-
* to its own invalid-data error (e.g. Medusa's MedusaError INVALID_DATA). */
|
|
133
|
-
declare class CommandResultError extends Error {
|
|
134
|
-
constructor(message: string);
|
|
135
|
-
}
|
|
136
|
-
/** Validates a CommandResult (e.g. one read back from the ledger). Throws
|
|
137
|
-
* CommandResultError naming the first bad field. */
|
|
138
|
-
declare function parseCommandResult(value: unknown): CommandResult;
|
|
139
|
-
|
|
140
131
|
/** Shape errors before a register command claims a ledger row. Unknown top-level keys are allowed. */
|
|
141
132
|
declare function registerPayloadErrors(type: string, payload: unknown): string[];
|
|
142
133
|
|
|
@@ -224,4 +215,4 @@ declare function platformErrorResult(id: string, platformCode: string, platformM
|
|
|
224
215
|
*/
|
|
225
216
|
declare function internalErrorResult(id: string, correlationId: string): CommandResult;
|
|
226
217
|
|
|
227
|
-
export { type BatchOutcome, BatchTooLargeBody, type CommandErrorWithData, type CommandRejectionCode,
|
|
218
|
+
export { type BatchOutcome, BatchTooLargeBody, type CommandErrorWithData, type CommandRejectionCode, type OrderCreatePayloadV3, type OrderRejectionCode, type RegisterConflictCode, type RegisterOutcome, type Rejection, SUPPORTED_ORDER_CREATE_VERSIONS, SUPPORTED_ORDER_REFUND_VERSIONS, SUPPORTED_REGISTER_VERSIONS, type ValidatedCommandEnvelope, canonicalJson, commandFingerprint, currencyDecimals, deriveSessionFigures, deriveVariance, fiscalFiguresErrors, inProgressOutcome, internalErrorResult, majorToMinor, minorToMajor, payloadBoundErrors, payloadShapeErrors, platformErrorResult, precheckCommand, refundPayloadErrors, registerPayloadErrors, totalWarnings, transientOutcome, validateBatch };
|
package/dist/server/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
CommandResultError,
|
|
3
|
+
MAX_COMMANDS_PER_BATCH,
|
|
4
|
+
parseCommandResult
|
|
5
|
+
} from "../chunk-UFWXJAMF.js";
|
|
4
6
|
import "../chunk-MLKGABMK.js";
|
|
5
7
|
|
|
6
8
|
// src/server/money.ts
|
|
@@ -402,171 +404,6 @@ function totalWarnings(expectedMinor, serverMinor) {
|
|
|
402
404
|
return serverMinor === expectedMinor ? [] : [{ code: "total_mismatch", expectedMinor, serverMinor }];
|
|
403
405
|
}
|
|
404
406
|
|
|
405
|
-
// src/server/command-result.ts
|
|
406
|
-
var CommandResultError = class extends Error {
|
|
407
|
-
constructor(message) {
|
|
408
|
-
super(message);
|
|
409
|
-
this.name = "CommandResultError";
|
|
410
|
-
}
|
|
411
|
-
};
|
|
412
|
-
function parseCommandResult(value) {
|
|
413
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
414
|
-
throw new CommandResultError("Invalid result: expected object");
|
|
415
|
-
}
|
|
416
|
-
const input = value;
|
|
417
|
-
if (typeof input.id !== "string" || input.id.length === 0) {
|
|
418
|
-
throw new CommandResultError("Invalid id");
|
|
419
|
-
}
|
|
420
|
-
if (input.status !== "applied" && input.status !== "duplicate" && input.status !== "rejected") {
|
|
421
|
-
throw new CommandResultError("Invalid status");
|
|
422
|
-
}
|
|
423
|
-
const result = { id: input.id, status: input.status };
|
|
424
|
-
const object = (value2) => typeof value2 === "object" && value2 !== null && (Object.getPrototypeOf(value2) === Object.prototype || Object.getPrototypeOf(value2) === null);
|
|
425
|
-
if (input.serverRefs !== void 0) {
|
|
426
|
-
if (typeof input.serverRefs !== "object" || input.serverRefs === null || Array.isArray(input.serverRefs)) {
|
|
427
|
-
throw new CommandResultError("Invalid serverRefs");
|
|
428
|
-
}
|
|
429
|
-
const refs = input.serverRefs;
|
|
430
|
-
if (typeof refs.orderId !== "string" || refs.orderId.length === 0) {
|
|
431
|
-
throw new CommandResultError("Invalid serverRefs.orderId");
|
|
432
|
-
}
|
|
433
|
-
if (refs.displayId !== void 0 && typeof refs.displayId !== "string") {
|
|
434
|
-
throw new CommandResultError("Invalid serverRefs.displayId");
|
|
435
|
-
}
|
|
436
|
-
if (!Number.isSafeInteger(refs.totalMinor)) {
|
|
437
|
-
throw new CommandResultError("Invalid serverRefs.totalMinor");
|
|
438
|
-
}
|
|
439
|
-
result.serverRefs = { orderId: refs.orderId, totalMinor: refs.totalMinor };
|
|
440
|
-
if (refs.displayId !== void 0) result.serverRefs.displayId = refs.displayId;
|
|
441
|
-
}
|
|
442
|
-
if (input.register !== void 0) {
|
|
443
|
-
if (!object(input.register)) throw new CommandResultError("Invalid register");
|
|
444
|
-
result.register = input.register;
|
|
445
|
-
}
|
|
446
|
-
if (input.refund !== void 0) {
|
|
447
|
-
if (!object(input.refund)) throw new CommandResultError("Invalid refund");
|
|
448
|
-
const refund = input.refund;
|
|
449
|
-
if (!Number.isSafeInteger(refund.totalMinor) || refund.totalMinor < 0) throw new CommandResultError("Invalid refund.totalMinor");
|
|
450
|
-
if (!object(refund.byMethod)) throw new CommandResultError("Invalid refund.byMethod");
|
|
451
|
-
const byMethod = Object.fromEntries(Object.entries(refund.byMethod).map(([key, amount]) => {
|
|
452
|
-
if (!key.length) throw new CommandResultError("Invalid refund.byMethod");
|
|
453
|
-
if (!Number.isSafeInteger(amount) || amount < 0) throw new CommandResultError(`Invalid refund.byMethod.${key}`);
|
|
454
|
-
return [key, amount];
|
|
455
|
-
}));
|
|
456
|
-
if (Object.values(byMethod).reduce((sum, amount) => sum + amount, 0) !== refund.totalMinor) throw new CommandResultError("Invalid refund.byMethod");
|
|
457
|
-
if (!Array.isArray(refund.refunds) || refund.refunds.length === 0) throw new CommandResultError("Invalid refund.refunds");
|
|
458
|
-
const refunds = refund.refunds.map((value2, index) => {
|
|
459
|
-
const field = `refund.refunds[${index}]`;
|
|
460
|
-
if (!object(value2)) throw new CommandResultError(`Invalid ${field}`);
|
|
461
|
-
const entry = value2;
|
|
462
|
-
for (const key of ["id", "paymentId"]) {
|
|
463
|
-
if (typeof entry[key] !== "string" || entry[key].length === 0) throw new CommandResultError(`Invalid ${field}.${key}`);
|
|
464
|
-
}
|
|
465
|
-
if (!Number.isSafeInteger(entry.totalMinor) || entry.totalMinor < 0) throw new CommandResultError(`Invalid ${field}.totalMinor`);
|
|
466
|
-
if (typeof entry.state !== "string" || entry.state.length === 0) throw new CommandResultError(`Invalid ${field}.state`);
|
|
467
|
-
return { id: entry.id, paymentId: entry.paymentId, totalMinor: entry.totalMinor, state: entry.state };
|
|
468
|
-
});
|
|
469
|
-
if (refunds.reduce((sum, entry) => sum + entry.totalMinor, 0) !== refund.totalMinor) throw new CommandResultError("Invalid refund.refunds");
|
|
470
|
-
result.refund = { totalMinor: refund.totalMinor, byMethod, refunds };
|
|
471
|
-
}
|
|
472
|
-
if (result.status === "applied" && result.serverRefs === void 0 && result.register === void 0 && result.refund === void 0) {
|
|
473
|
-
throw new CommandResultError("Invalid serverRefs: required for applied");
|
|
474
|
-
}
|
|
475
|
-
if (input.warnings !== void 0) {
|
|
476
|
-
if (!Array.isArray(input.warnings)) {
|
|
477
|
-
throw new CommandResultError("Invalid warnings");
|
|
478
|
-
}
|
|
479
|
-
result.warnings = input.warnings.map((warning, index) => {
|
|
480
|
-
const field = `warnings[${index}]`;
|
|
481
|
-
if (typeof warning !== "object" || warning === null || Array.isArray(warning)) {
|
|
482
|
-
throw new CommandResultError(`Invalid ${field}`);
|
|
483
|
-
}
|
|
484
|
-
const item = warning;
|
|
485
|
-
if (item.code === "total_mismatch") {
|
|
486
|
-
for (const key of ["expectedMinor", "serverMinor"]) {
|
|
487
|
-
if (!Number.isInteger(item[key])) {
|
|
488
|
-
throw new CommandResultError(`Invalid ${field}.${key}`);
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
if (item.bridgeMinor != null && !Number.isSafeInteger(item.bridgeMinor)) {
|
|
492
|
-
throw new CommandResultError(`Invalid ${field}.bridgeMinor`);
|
|
493
|
-
}
|
|
494
|
-
return item.bridgeMinor != null ? { code: item.code, expectedMinor: item.expectedMinor, serverMinor: item.serverMinor, bridgeMinor: item.bridgeMinor } : { code: item.code, expectedMinor: item.expectedMinor, serverMinor: item.serverMinor };
|
|
495
|
-
}
|
|
496
|
-
if (item.code === "insufficient_stock") {
|
|
497
|
-
if (typeof item.variantId !== "string" || item.variantId.length === 0) {
|
|
498
|
-
throw new CommandResultError(`Invalid ${field}.variantId`);
|
|
499
|
-
}
|
|
500
|
-
if (!Number.isInteger(item.quantity) || item.quantity < 1) {
|
|
501
|
-
throw new CommandResultError(`Invalid ${field}.quantity`);
|
|
502
|
-
}
|
|
503
|
-
return { code: item.code, variantId: item.variantId, quantity: item.quantity };
|
|
504
|
-
}
|
|
505
|
-
if (item.code === "tax_rate_mismatch") {
|
|
506
|
-
if (!Number.isSafeInteger(item.ratePpm) || item.ratePpm < 0) {
|
|
507
|
-
throw new CommandResultError(`Invalid ${field}.ratePpm`);
|
|
508
|
-
}
|
|
509
|
-
for (const key of ["expectedMinor", "serverMinor"]) {
|
|
510
|
-
if (!Number.isSafeInteger(item[key])) {
|
|
511
|
-
throw new CommandResultError(`Invalid ${field}.${key}`);
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
return { code: item.code, ratePpm: item.ratePpm, expectedMinor: item.expectedMinor, serverMinor: item.serverMinor };
|
|
515
|
-
}
|
|
516
|
-
if (item.code === "customer_ignored") {
|
|
517
|
-
if (typeof item.customerId !== "string" || item.customerId.length === 0 || item.customerId.length > 64) {
|
|
518
|
-
throw new CommandResultError(`Invalid ${field}.customerId`);
|
|
519
|
-
}
|
|
520
|
-
return { code: item.code, customerId: item.customerId };
|
|
521
|
-
}
|
|
522
|
-
if (item.code === "register_session_unknown") {
|
|
523
|
-
if (typeof item.sessionId !== "string" || item.sessionId.length === 0 || item.sessionId.length > 64) {
|
|
524
|
-
throw new CommandResultError(`Invalid ${field}.sessionId`);
|
|
525
|
-
}
|
|
526
|
-
return { code: item.code, sessionId: item.sessionId };
|
|
527
|
-
}
|
|
528
|
-
if (item.code === "figures_mismatch") {
|
|
529
|
-
if (!Array.isArray(item.fields) || item.fields.length === 0) throw new CommandResultError(`Invalid ${field}.fields`);
|
|
530
|
-
const seen = [];
|
|
531
|
-
return { code: item.code, fields: item.fields.map((value2, i) => {
|
|
532
|
-
const path = `${field}.fields[${i}]`;
|
|
533
|
-
if (typeof value2 !== "object" || value2 === null || Array.isArray(value2)) throw new CommandResultError(`Invalid ${path}`);
|
|
534
|
-
const entry = value2;
|
|
535
|
-
if (!["subtotalMinor", "taxMinor", "discountMinor"].includes(entry.field) || seen.includes(entry.field)) {
|
|
536
|
-
throw new CommandResultError(`Invalid ${path}.field`);
|
|
537
|
-
}
|
|
538
|
-
seen.push(entry.field);
|
|
539
|
-
if (!Number.isSafeInteger(entry.tillMinor)) throw new CommandResultError(`Invalid ${path}.tillMinor`);
|
|
540
|
-
if (!Number.isSafeInteger(entry.serverMinor) || entry.serverMinor === entry.tillMinor) throw new CommandResultError(`Invalid ${path}.serverMinor`);
|
|
541
|
-
return { field: entry.field, tillMinor: entry.tillMinor, serverMinor: entry.serverMinor };
|
|
542
|
-
}) };
|
|
543
|
-
}
|
|
544
|
-
throw new CommandResultError(`Invalid ${field}.code`);
|
|
545
|
-
});
|
|
546
|
-
}
|
|
547
|
-
if (input.error !== void 0) {
|
|
548
|
-
if (typeof input.error !== "object" || input.error === null || Array.isArray(input.error)) {
|
|
549
|
-
throw new CommandResultError("Invalid error");
|
|
550
|
-
}
|
|
551
|
-
const error = input.error;
|
|
552
|
-
if (typeof error.code !== "string" || error.code.length === 0) {
|
|
553
|
-
throw new CommandResultError("Invalid error.code");
|
|
554
|
-
}
|
|
555
|
-
if (typeof error.message !== "string") {
|
|
556
|
-
throw new CommandResultError("Invalid error.message");
|
|
557
|
-
}
|
|
558
|
-
result.error = { code: error.code, message: error.message };
|
|
559
|
-
if (error.data !== void 0) {
|
|
560
|
-
if (!object(error.data)) throw new CommandResultError("Invalid error.data");
|
|
561
|
-
result.error.data = error.data;
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
if (result.status === "rejected" && result.error === void 0) {
|
|
565
|
-
throw new CommandResultError("Invalid error: required for rejected");
|
|
566
|
-
}
|
|
567
|
-
return result;
|
|
568
|
-
}
|
|
569
|
-
|
|
570
407
|
// src/server/register-payload-shape.ts
|
|
571
408
|
function registerPayloadErrors(type, payload) {
|
|
572
409
|
const errors = [];
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -81,3 +81,7 @@ export {
|
|
|
81
81
|
export type { ConnectorProviderProps } from './context/connector-context';
|
|
82
82
|
export * as woocommerceTax from './tax/woocommerce';
|
|
83
83
|
export * as woocommerceCoupons from './coupons/woocommerce';
|
|
84
|
+
|
|
85
|
+
// Browser-safe: command-result.ts imports only types. @tallyui/core/server re-exports these too, but
|
|
86
|
+
// its barrel reaches node:crypto (fingerprint.ts), so browser packages import them from here.
|
|
87
|
+
export { parseCommandResult, CommandResultError } from './server/command-result';
|
package/dist/chunk-C4PWPOKO.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
// src/commands.ts
|
|
2
|
-
var COMMANDS_PATH = "/tally/v1/commands";
|
|
3
|
-
var PROTOCOL_HEADER = "X-Tally-Protocol";
|
|
4
|
-
var PROTOCOL_VERSION = 1;
|
|
5
|
-
var MAX_COMMANDS_PER_BATCH = 50;
|
|
6
|
-
function isCommandBatchResponse(value) {
|
|
7
|
-
return typeof value === "object" && value !== null && "results" in value && Array.isArray(value.results) && value.results.every(
|
|
8
|
-
(result) => typeof result === "object" && result !== null && "id" in result && typeof result.id === "string" && "status" in result && (result.status === "applied" || result.status === "duplicate" || result.status === "rejected")
|
|
9
|
-
);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export {
|
|
13
|
-
COMMANDS_PATH,
|
|
14
|
-
PROTOCOL_HEADER,
|
|
15
|
-
PROTOCOL_VERSION,
|
|
16
|
-
MAX_COMMANDS_PER_BATCH,
|
|
17
|
-
isCommandBatchResponse
|
|
18
|
-
};
|