@tallyui/core 0.2.0 → 3.0.0-next.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/LICENSE +21 -0
- package/dist/chunk-C4PWPOKO.js +18 -0
- package/dist/commands-BW4YYgRG.d.ts +306 -0
- package/dist/index.d.ts +636 -8
- package/dist/index.js +468 -4
- package/dist/rxdb/index.d.ts +37 -0
- package/dist/rxdb/index.js +26 -0
- package/dist/server/index.d.ts +213 -0
- package/dist/server/index.js +591 -0
- package/package.json +33 -3
- package/src/commands.ts +32 -0
- package/src/context/connector-context.tsx +78 -4
- package/src/index.ts +53 -0
- package/src/known-warnings.ts +77 -0
- package/src/money.ts +142 -0
- package/src/replication/combine.ts +77 -0
- package/src/replication/reconcile-feed.ts +116 -0
- package/src/rxdb/index.ts +4 -0
- package/src/rxdb/read-fresh.ts +32 -0
- package/src/rxdb/watch-fresh.ts +19 -0
- package/src/rxdb-remote-version-mismatch.ts +30 -0
- package/src/server/__fixtures__/fingerprint-vectors.json +139 -0
- package/src/server/__fixtures__/order-create-v3.json +97 -0
- package/src/server/batch-outcome.ts +19 -0
- package/src/server/batch.ts +112 -0
- package/src/server/command-result.ts +138 -0
- package/src/server/fingerprint.ts +18 -0
- package/src/server/fiscal-figures.ts +84 -0
- package/src/server/index.ts +20 -0
- package/src/server/money.ts +34 -0
- package/src/server/order-payload-shape.ts +112 -0
- package/src/server/register-figures.ts +32 -0
- package/src/server/register-outcome.ts +12 -0
- package/src/server/register-payload-shape.ts +86 -0
- package/src/server/rejection-codes.ts +49 -0
- package/src/server/total-warnings.ts +10 -0
- package/src/server/versions.ts +9 -0
- package/src/sign-in.ts +21 -0
- package/src/stock-overlay.ts +35 -0
- package/src/store-settings.ts +29 -0
- package/src/sync-notice.ts +26 -0
- package/src/types/commands.ts +236 -0
- package/src/types/connector.ts +127 -0
- package/src/types/customers.ts +26 -0
- package/src/types/index.ts +23 -1
- package/src/types/money.ts +33 -0
- package/src/types/reconcile.ts +73 -0
- package/src/types/replication.ts +12 -0
- package/src/types/stock.ts +18 -0
- package/src/types/store-settings.ts +27 -0
- package/src/types/traits/product.ts +62 -6
- package/src/unauthorized.ts +13 -0
- package/src/utils/compare-ids.ts +16 -0
- package/src/variants.ts +14 -0
- package/src/__tests__/connector-context.test.tsx +0 -100
- package/src/types/replication.test-d.ts +0 -20
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { e as CommandError, l as OrderCreatePayload, d as CommandEnvelope, q as RegisterCommandEnvelope, A as AnyCommandEnvelope, f as CommandResult, B as BatchTooLargeBody, c as CommandBatchResponse, C as CommandWarning, r as RegisterCommandResult } from '../commands-BW4YYgRG.js';
|
|
2
|
+
export { M as MAX_COMMANDS_PER_BATCH, O as OrderCreateDisplay, n as OrderCreateTaxRate } from '../commands-BW4YYgRG.js';
|
|
3
|
+
|
|
4
|
+
/** ISO 4217 minor-unit exponent as resolved by Intl (which rejects malformed codes); depends on the runtime's ICU data. */
|
|
5
|
+
declare function currencyDecimals(currency: string): number;
|
|
6
|
+
/** Converts a safe integer minor amount to an exact major-unit decimal string. */
|
|
7
|
+
declare function minorToMajor(minor: number, decimals: number): string;
|
|
8
|
+
/** Rounds a major-unit decimal to minor units, half away from zero, without float scaling. */
|
|
9
|
+
declare function majorToMinor(value: string | number, decimals: number): number;
|
|
10
|
+
|
|
11
|
+
/** JSON with recursively sorted object keys; arrays keep their order. */
|
|
12
|
+
declare function canonicalJson(value: unknown): string;
|
|
13
|
+
/** Lower-case hex SHA-256 of the canonical command content. */
|
|
14
|
+
declare function commandFingerprint(envelope: {
|
|
15
|
+
type: string;
|
|
16
|
+
version: number;
|
|
17
|
+
payload: unknown;
|
|
18
|
+
}): string;
|
|
19
|
+
|
|
20
|
+
/** Shape errors of an order.create payload, e.g. ['lines: expected a non-empty array',
|
|
21
|
+
* 'payments[0].method: expected a string']; [] when the shape is valid, at most ten. Checks
|
|
22
|
+
* presence, types, the `customerId` (64) and `sessionId` (36) bounds, and U+0000 in any string;
|
|
23
|
+
* numbers are finite numbers, and value ranges are the planner's job. A plugin runs it before its
|
|
24
|
+
* replay lookup; every other length bound is payloadBoundErrors', after that lookup. The NUL check
|
|
25
|
+
* runs last, so the earlier checks' errors keep their order. */
|
|
26
|
+
declare function payloadShapeErrors(payload: unknown): string[];
|
|
27
|
+
/** Length-bound errors of an order.create payload, e.g. ['lines[0].title: expected at most 255
|
|
28
|
+
* characters']; [] when every string is within its bound, at most ten. Bounds in UTF-16 code units:
|
|
29
|
+
* `customer.email` 254, every other string 255 (`customerId` and `sessionId` keep theirs in
|
|
30
|
+
* payloadShapeErrors); only strings are checked. A plugin calls it through precheckCommand, after
|
|
31
|
+
* its replay lookup, so a bound tightened later never turns an applied command's resend into
|
|
32
|
+
* `invalid_payload`. */
|
|
33
|
+
declare function payloadBoundErrors(payload: unknown): string[];
|
|
34
|
+
|
|
35
|
+
/** @deprecated Use `CommandError` from `@tallyui/core`; kept for plugins migrating from local copies. */
|
|
36
|
+
type CommandErrorWithData = CommandError;
|
|
37
|
+
/** @deprecated Use `OrderCreatePayload` from `@tallyui/core`; kept for plugins migrating from local copies. */
|
|
38
|
+
type OrderCreatePayloadV3 = OrderCreatePayload;
|
|
39
|
+
declare function fiscalFiguresErrors(payload: OrderCreatePayloadV3): string[];
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The till's capability: the order.create versions `toOrderCreateEnvelope` can produce. It is not what a server
|
|
43
|
+
* supports (#297). A server passes its own lists to `precheckCommand`, and its /info advertises them, gated by its
|
|
44
|
+
* own check (Front desk, 2026-09-30): 4 (#286) is 3 with every discountMinor tax-exclusive, and a plugin
|
|
45
|
+
* advertises it only once it handles it.
|
|
46
|
+
*/
|
|
47
|
+
declare const SUPPORTED_ORDER_CREATE_VERSIONS: readonly number[];
|
|
48
|
+
/** The till's capability for the contract versions shared by all five register commands; a server passes its own. */
|
|
49
|
+
declare const SUPPORTED_REGISTER_VERSIONS: readonly number[];
|
|
50
|
+
|
|
51
|
+
/** An envelope validateBatch accepted: every field's shape is checked, and its version is any positive
|
|
52
|
+
* safe integer (precheckCommand decides which versions this server supports). */
|
|
53
|
+
type ValidatedCommandEnvelope = (Omit<CommandEnvelope<Record<string, unknown>>, 'version'> & {
|
|
54
|
+
type: 'order.create';
|
|
55
|
+
version: number;
|
|
56
|
+
}) | RegisterCommandEnvelope<Record<string, unknown>>;
|
|
57
|
+
/** Validates every envelope before any command is claimed. */
|
|
58
|
+
declare function validateBatch(body: unknown): {
|
|
59
|
+
ok: true;
|
|
60
|
+
commands: ValidatedCommandEnvelope[];
|
|
61
|
+
} | {
|
|
62
|
+
ok: false;
|
|
63
|
+
status: 400;
|
|
64
|
+
message: string;
|
|
65
|
+
} | {
|
|
66
|
+
ok: false;
|
|
67
|
+
status: 413;
|
|
68
|
+
message: string;
|
|
69
|
+
body: BatchTooLargeBody;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* The rejection a batch returns for this command before any executor runs, or undefined
|
|
73
|
+
* when the command goes on to the plugin's executor. A plugin's batch handler calls
|
|
74
|
+
* validateBatch once, then, for each command in order: the shape check (payloadShapeErrors:
|
|
75
|
+
* types and NUL) first, before any database access, because a NUL in clientOrderId would make
|
|
76
|
+
* the lookup itself fail; then the plugin's own replay and collision lookups; then
|
|
77
|
+
* precheckCommand (versions, string lengths via payloadBoundErrors, fiscal figures); then the
|
|
78
|
+
* claim, in the executor (order.create; register commands follow ADR-068). It pushes any result
|
|
79
|
+
* one of those steps returns and moves on.
|
|
80
|
+
*
|
|
81
|
+
* The replay lookup goes before precheckCommand so an already-applied command always replays
|
|
82
|
+
* as `duplicate`, even once a later @tallyui/core tightens what precheckCommand accepts.
|
|
83
|
+
*
|
|
84
|
+
* `supported` is the SERVER's own list (what its /info advertises), never core's constants, which are the
|
|
85
|
+
* till's capability (#297). The version check, the `unsupported_version` message and its `data` all use it:
|
|
86
|
+
* `precheckCommand(envelope, { orderCreate: [1, 2, 3], register: [1] })`.
|
|
87
|
+
*/
|
|
88
|
+
declare function precheckCommand(envelope: Pick<AnyCommandEnvelope | ValidatedCommandEnvelope, 'id' | 'type' | 'version' | 'payload'>, supported: {
|
|
89
|
+
orderCreate: readonly number[];
|
|
90
|
+
register: readonly number[];
|
|
91
|
+
}): CommandResult | undefined;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* A plugin's batch handler stops at the first in-progress or transient command and returns that
|
|
95
|
+
* outcome for the whole batch; otherwise it returns 200 with every result in order.
|
|
96
|
+
*/
|
|
97
|
+
type BatchOutcome = {
|
|
98
|
+
status: 200;
|
|
99
|
+
body: CommandBatchResponse;
|
|
100
|
+
} | {
|
|
101
|
+
status: 409;
|
|
102
|
+
body: {
|
|
103
|
+
code: 'in_progress';
|
|
104
|
+
id: string;
|
|
105
|
+
};
|
|
106
|
+
} | {
|
|
107
|
+
status: 503;
|
|
108
|
+
body: {
|
|
109
|
+
code: 'transient';
|
|
110
|
+
id: string;
|
|
111
|
+
message: 'Temporary failure, retry later.';
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
declare function inProgressOutcome(id: string): BatchOutcome;
|
|
115
|
+
/** The message is fixed so a server never leaks its internal error (log that separately). */
|
|
116
|
+
declare function transientOutcome(id: string): BatchOutcome;
|
|
117
|
+
|
|
118
|
+
type OrderRejectionCode = 'unknown_variant' | 'invalid_quantity' | 'underpaid' | 'unsupported_currency';
|
|
119
|
+
type Rejection = {
|
|
120
|
+
code: OrderRejectionCode;
|
|
121
|
+
message: string;
|
|
122
|
+
};
|
|
123
|
+
declare function totalWarnings(expectedMinor: number, serverMinor: number): CommandWarning[];
|
|
124
|
+
|
|
125
|
+
/** parseCommandResult's refusal; its message names the first bad field. A plugin maps it
|
|
126
|
+
* to its own invalid-data error (e.g. Medusa's MedusaError INVALID_DATA). */
|
|
127
|
+
declare class CommandResultError extends Error {
|
|
128
|
+
constructor(message: string);
|
|
129
|
+
}
|
|
130
|
+
/** Validates a CommandResult (e.g. one read back from the ledger). Throws
|
|
131
|
+
* CommandResultError naming the first bad field. */
|
|
132
|
+
declare function parseCommandResult(value: unknown): CommandResult;
|
|
133
|
+
|
|
134
|
+
/** Shape errors before a register command claims a ledger row. Unknown top-level keys are allowed. */
|
|
135
|
+
declare function registerPayloadErrors(type: string, payload: unknown): string[];
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* A session's expected figures per method: the counted float in cash, plus each order's payments, plus paid_in
|
|
139
|
+
* and minus paid_out movements that are not voided. The plugin chooses which orders count (for Medusa: completed,
|
|
140
|
+
* archived and cancelled POS orders, so a received sale stays counted after an admin archives or cancels it) and
|
|
141
|
+
* passes each order's recorded POS payments; this function only sums.
|
|
142
|
+
*/
|
|
143
|
+
declare function deriveSessionFigures(input: {
|
|
144
|
+
countedFloatMinor: number;
|
|
145
|
+
orders: {
|
|
146
|
+
payments: {
|
|
147
|
+
method: string;
|
|
148
|
+
amountMinor: number;
|
|
149
|
+
}[];
|
|
150
|
+
}[];
|
|
151
|
+
movements: {
|
|
152
|
+
id: string;
|
|
153
|
+
type: 'paid_in' | 'paid_out' | 'no_sale' | 'void';
|
|
154
|
+
amountMinor: number;
|
|
155
|
+
voids: string | null;
|
|
156
|
+
}[];
|
|
157
|
+
}): {
|
|
158
|
+
expected: Record<string, number>;
|
|
159
|
+
salesCount: number;
|
|
160
|
+
};
|
|
161
|
+
declare function deriveVariance(counted: Record<string, number>, expected: Record<string, number>): Record<string, number>;
|
|
162
|
+
|
|
163
|
+
type RegisterConflictCode = 'register_session_already_open' | 'register_session_closed' | 'register_closure_exists' | 'register_closure_number_invalid';
|
|
164
|
+
type RegisterOutcome = {
|
|
165
|
+
kind: 'ok';
|
|
166
|
+
register: RegisterCommandResult;
|
|
167
|
+
} | {
|
|
168
|
+
kind: 'conflict';
|
|
169
|
+
code: RegisterConflictCode;
|
|
170
|
+
data?: Record<string, unknown>;
|
|
171
|
+
} | {
|
|
172
|
+
kind: 'invalid';
|
|
173
|
+
message: string;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
/** Every error.code a plugin may put on a rejected result (ADR-038 and its amendments, ADR-068),
|
|
177
|
+
* except `register_approval_required`, which arrives with registers c2c. */
|
|
178
|
+
type CommandRejectionCode = 'invalid_payload' | 'unsupported_version' | 'idempotency_mismatch' | 'store_configuration' | 'platform_error' | 'insufficient_stock' | 'unsupported_tax_mode' | 'internal_error' | OrderRejectionCode | RegisterConflictCode;
|
|
179
|
+
/**
|
|
180
|
+
* The rejected result for a platform-native error the plugin has judged permanent (a replay would fail
|
|
181
|
+
* the same way, e.g. a Vendure ErrorResult code) and the contract has no code for. `platform_error` is
|
|
182
|
+
* for `order.create` only, for now: a rejected register command halts that register's queue until c2c,
|
|
183
|
+
* so register refusals use the ADR-068 codes.
|
|
184
|
+
*
|
|
185
|
+
* Return it only when the platform made no durable change for this command: the plugin threw so its
|
|
186
|
+
* transaction rolled back, or it compensated. Otherwise the plugin applies the command (with a warning
|
|
187
|
+
* where one fits), or compensates the partial write and then stays transient (ADR-039). The till offers
|
|
188
|
+
* Retry for `platform_error` and requeue resends under a new command id, and a transient result resends
|
|
189
|
+
* the same id after the ledger claim is released, so either would duplicate a partly written sale.
|
|
190
|
+
*
|
|
191
|
+
* It is stored in the ledger and replayed as recorded; the order shows under "Needs attention" with
|
|
192
|
+
* Retry. An error the plugin can't classify stays transient (503, retried), never platform_error.
|
|
193
|
+
*/
|
|
194
|
+
declare function platformErrorResult(id: string, platformCode: string, platformMessage: string): CommandResult;
|
|
195
|
+
/**
|
|
196
|
+
* The rejected result for an exception from the plugin's own code (a programming error, not the
|
|
197
|
+
* database, the network, or an unknown SQLSTATE), raised after a complete rollback or a complete
|
|
198
|
+
* compensation, so that nothing of the sale remains, in the database or outside it. "The plugin's own
|
|
199
|
+
* code" excludes an error thrown from inside the platform SDK, the database client or the HTTP client.
|
|
200
|
+
*
|
|
201
|
+
* It is stored in the ledger and replayed as recorded, because it fails the same way on every retry
|
|
202
|
+
* and a retry loop would hide the bug. The message is generic so no internal detail reaches the till;
|
|
203
|
+
* the correlation id links it to the plugin's log.
|
|
204
|
+
*
|
|
205
|
+
* Database, network, unknown-SQLSTATE and any other unclassifiable errors stay transient (503), never
|
|
206
|
+
* `internal_error`. `order.create` only, as for `platform_error`.
|
|
207
|
+
*
|
|
208
|
+
* See ADR-038's `internal_error` amendment (needs-admin on a failed compensation; on Vendure only
|
|
209
|
+
* before the first write).
|
|
210
|
+
*/
|
|
211
|
+
declare function internalErrorResult(id: string, correlationId: string): CommandResult;
|
|
212
|
+
|
|
213
|
+
export { type BatchOutcome, BatchTooLargeBody, type CommandErrorWithData, type CommandRejectionCode, CommandResultError, type OrderCreatePayloadV3, type OrderRejectionCode, type RegisterConflictCode, type RegisterOutcome, type Rejection, SUPPORTED_ORDER_CREATE_VERSIONS, SUPPORTED_REGISTER_VERSIONS, type ValidatedCommandEnvelope, canonicalJson, commandFingerprint, currencyDecimals, deriveSessionFigures, deriveVariance, fiscalFiguresErrors, inProgressOutcome, internalErrorResult, majorToMinor, minorToMajor, parseCommandResult, payloadBoundErrors, payloadShapeErrors, platformErrorResult, precheckCommand, registerPayloadErrors, totalWarnings, transientOutcome, validateBatch };
|