@entifix/react-integration 0.20.2 → 0.20.3
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.
|
@@ -36,7 +36,7 @@ export type FlowOutcome = {
|
|
|
36
36
|
* principal. Zustand's `StateStorage` satisfies it.
|
|
37
37
|
*/
|
|
38
38
|
export interface PendingFlowStorage {
|
|
39
|
-
getItem(name: string): string | null |
|
|
39
|
+
getItem(name: string): string | null | PromiseLike<string | null>;
|
|
40
40
|
setItem(name: string, value: string): unknown;
|
|
41
41
|
}
|
|
42
42
|
export interface PendingFlowStoreOptions {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pending-flow-store.d.ts","sourceRoot":"","sources":["../../src/pending/pending-flow-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAGjE,OAAO,EAAa,KAAK,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAExE;;;;;GAKG;AACH,eAAO,MAAM,0BAA0B,IAAI,CAAC;AAE5C,6EAA6E;AAC7E,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,oDAAoD;IACpD,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;IAC1B,mDAAmD;IACnD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,yEAAyE;IACzE,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,uEAAuE;IACvE,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;CAC3B;AAED;;;;GAIG;AACH,MAAM,MAAM,WAAW,GACnB;IAAE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,WAAW,CAAA;CAAE,GAC5D;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,OAAO,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":["import type { DomainEvent, EntifixFailure } from '@entifix/core';\nimport { Effect, Result, Schema } from 'effect';\n\nimport { flowTopic, type PushTopic } from '../reactive/push-channel.js';\n\n/**\n * The shape version of what a store persists. A stored value at any other\n * version is discarded, never migrated: the store is a cache of in-flight\n * work, and losing it costs a re-read\n * ([ADR 0022](../../../../../docs/adr/0022-two-settle-modes-and-the-pending-flow-store.md)).\n */\nexport const PENDING_FLOW_STORE_VERSION = 1;\n\n/** One write the server handed to a flow, waiting for the flow's outcome. */\nexport interface PendingFlow {\n readonly flowId: string;\n /** Where the outcome is pushed: `flow:<flowId>`. */\n readonly topic: PushTopic;\n /** ISO-8601, when the write was answered `202`. */\n readonly createdAt: string;\n /** The flow's `status` link, read when its push may have been missed. */\n readonly status?: string;\n /** What the write meant, for the screen that resumes it. JSON only. */\n readonly intent?: unknown;\n}\n\n/**\n * How a pending flow ended. A `failed` outcome with no `failure` is a flow\n * nobody could account for within the age bound: it is no longer shown as\n * pending, and there is nothing to tell the user why.\n */\nexport type FlowOutcome =\n | { readonly kind: 'confirmed'; readonly event?: DomainEvent }\n | { readonly kind: 'failed'; readonly failure?: EntifixFailure };\n\n/**\n * Where a store persists, supplied by the host and scoped by it to one\n * principal. Zustand's `StateStorage` satisfies it.\n */\nexport interface PendingFlowStorage {\n getItem(name: string): string | null | Promise<string | null>;\n setItem(name: string, value: string): unknown;\n}\n\nexport interface PendingFlowStoreOptions {\n /** Omitted, the store lives in memory and a reload forgets it. */\n readonly storage?: PendingFlowStorage;\n /** The storage key. Defaults to `entifix.pending-flows`. */\n readonly key?: string;\n /**\n * An anonymous scope keeps flow ids and their age only: an intent or a\n * status link could carry personal data or a capability, and nothing scopes\n * an anonymous storage to one person.\n */\n readonly anonymous?: boolean;\n readonly now?: () => Date;\n}\n\nexport interface PendingFlowInput {\n readonly flowId: string;\n readonly status?: string;\n readonly intent?: unknown;\n}\n\nexport interface PendingFlowStore {\n /** Settles once whatever was persisted has been read back. */\n readonly ready: Promise<void>;\n /** The pending flows, oldest first. The same array until something changes. */\n list(): readonly PendingFlow[];\n /**\n * Records a pending flow. Throws a `TypeError`, and records nothing, when\n * the intent is not JSON: a value that cannot be read back must never reach\n * storage.\n */\n add(input: PendingFlowInput): PendingFlow;\n /**\n * Ends a pending flow with its outcome. Answers `true` only for the call\n * that ended it, so a redelivered outcome is told apart from the first.\n */\n settle(flowId: string, outcome: FlowOutcome): boolean;\n /** Runs whenever the list changes. */\n subscribe(listener: () => void): () => void;\n /** Runs once, when the named flow settles. */\n onSettled(\n flowId: string,\n listener: (outcome: FlowOutcome) => void,\n ): () => void;\n}\n\nconst isJson = (value: unknown): boolean => {\n if (value === null) return true;\n switch (typeof value) {\n case 'string':\n case 'boolean':\n return true;\n case 'number':\n return Number.isFinite(value);\n case 'object': {\n if (Array.isArray(value)) return value.every(isJson);\n const prototype = Object.getPrototypeOf(value) as unknown;\n return (\n (prototype === Object.prototype || prototype === null) &&\n Object.values(value).every(isJson)\n );\n }\n default:\n return false;\n }\n};\n\nconst Stored = Schema.Struct({\n v: Schema.Literal(PENDING_FLOW_STORE_VERSION),\n flows: Schema.Array(\n Schema.Struct({\n flowId: Schema.String,\n createdAt: Schema.String,\n status: Schema.optionalKey(Schema.String),\n intent: Schema.optionalKey(Schema.Unknown),\n }),\n ),\n});\n\n// Whatever the storage holds, as pending flows: nothing at all when it is\n// missing, unreadable, or at another version.\nconst decodeStored = (raw: string | null): readonly PendingFlow[] => {\n if (raw === null) return [];\n const decoded = Effect.runSync(\n Effect.result(\n Effect.flatMap(\n Effect.try(() => JSON.parse(raw) as unknown),\n Schema.decodeUnknownEffect(Stored),\n ),\n ),\n );\n if (Result.isFailure(decoded)) return [];\n return decoded.success.flows.map(flow => ({\n ...flow,\n topic: flowTopic(flow.flowId),\n }));\n};\n\n/**\n * The pending-flow store: held in memory, written through to the host's\n * storage as `{ v, flows }`, and read back from it once on creation.\n */\nexport function makePendingFlowStore({\n storage,\n key = 'entifix.pending-flows',\n anonymous = false,\n now = () => new Date(),\n}: PendingFlowStoreOptions = {}): PendingFlowStore {\n let flows: readonly PendingFlow[] = [];\n const listeners = new Set<() => void>();\n const settledListeners = new Map<\n string,\n Set<(outcome: FlowOutcome) => void>\n >();\n let writing: Promise<unknown> = Promise.resolve();\n\n const changed = (next: readonly PendingFlow[]): void => {\n flows = next;\n for (const listener of listeners) listener();\n };\n\n const persist = (): void => {\n if (storage === undefined) return;\n const value = JSON.stringify({ v: PENDING_FLOW_STORE_VERSION, flows });\n // Serialized, so a slow write never lands after a later one. A write that\n // fails costs only the reload-survival of what is pending.\n writing = writing\n .then(() => storage.setItem(key, value))\n .catch(() => undefined);\n };\n\n const ready =\n storage === undefined\n ? Promise.resolve()\n : Promise.resolve()\n .then(() => storage.getItem(key))\n .then(\n raw => decodeStored(raw),\n () => [],\n )\n .then(stored => {\n const held = new Set(flows.map(flow => flow.flowId));\n const restored = stored.filter(flow => !held.has(flow.flowId));\n if (restored.length > 0) changed([...restored, ...flows]);\n });\n\n return {\n ready,\n list: () => flows,\n add({ flowId, status, intent }) {\n if (!anonymous && intent !== undefined && !isJson(intent))\n throw new TypeError(\n `the intent of pending flow '${flowId}' is not JSON, so it cannot be stored`,\n );\n const flow: PendingFlow = {\n flowId,\n topic: flowTopic(flowId),\n createdAt: now().toISOString(),\n ...(anonymous || status === undefined ? {} : { status }),\n ...(anonymous || intent === undefined ? {} : { intent }),\n };\n changed([...flows.filter(held => held.flowId !== flowId), flow]);\n persist();\n return flow;\n },\n settle(flowId, outcome) {\n if (!flows.some(flow => flow.flowId === flowId)) return false;\n changed(flows.filter(flow => flow.flowId !== flowId));\n persist();\n const waiting = settledListeners.get(flowId);\n settledListeners.delete(flowId);\n for (const listener of waiting ?? []) listener(outcome);\n return true;\n },\n subscribe(listener) {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n onSettled(flowId, listener) {\n const waiting = settledListeners.get(flowId) ?? new Set();\n settledListeners.set(flowId, waiting.add(listener));\n return () => {\n waiting.delete(listener);\n };\n },\n };\n}\n"],"names":["Effect","Result","Schema","flowTopic","PENDING_FLOW_STORE_VERSION","isJson","value","Number","isFinite","Array","isArray","every","prototype","Object","getPrototypeOf","values","Stored","Struct","v","Literal","flows","flowId","String","createdAt","status","optionalKey","intent","Unknown","decodeStored","raw","decoded","runSync","result","flatMap","try","JSON","parse","decodeUnknownEffect","isFailure","success","map","flow","topic","makePendingFlowStore","storage","key","anonymous","now","Date","listeners","Set","settledListeners","Map","writing","Promise","resolve","changed","next","listener","persist","undefined","stringify","then","setItem","catch","ready","getItem","stored","held","restored","filter","has","length","list","add","TypeError","toISOString","settle","outcome","some","waiting","get","delete","subscribe","onSettled","set"],"mappings":";AACA,SAASA,MAAM,EAAEC,MAAM,EAAEC,MAAM,QAAQ,SAAS;AAEhD,SAASC,SAAS,QAAwB,8BAA8B;AAExE;;;;;CAKC,GACD,OAAO,MAAMC,6BAA6B,EAAE;AA8E5C,MAAMC,SAAS,CAACC;IACd,IAAIA,UAAU,MAAM,OAAO;IAC3B,OAAQ,OAAOA;QACb,KAAK;QACL,KAAK;YACH,OAAO;QACT,KAAK;YACH,OAAOC,OAAOC,QAAQ,CAACF;QACzB,KAAK;YAAU;gBACb,IAAIG,MAAMC,OAAO,CAACJ,QAAQ,OAAOA,MAAMK,KAAK,CAACN;gBAC7C,MAAMO,YAAYC,OAAOC,cAAc,CAACR;gBACxC,OACE,AAACM,CAAAA,cAAcC,OAAOD,SAAS,IAAIA,cAAc,IAAG,KACpDC,OAAOE,MAAM,CAACT,OAAOK,KAAK,CAACN;YAE/B;QACA;YACE,OAAO;IACX;AACF;AAEA,MAAMW,SAASd,OAAOe,MAAM,CAAC;IAC3BC,GAAGhB,OAAOiB,OAAO,CAACf;IAClBgB,OAAOlB,OAAOO,KAAK,CACjBP,OAAOe,MAAM,CAAC;QACZI,QAAQnB,OAAOoB,MAAM;QACrBC,WAAWrB,OAAOoB,MAAM;QACxBE,QAAQtB,OAAOuB,WAAW,CAACvB,OAAOoB,M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AAYsB,kBAAAA,UAAW,EAAE,CAAEtB,SAASoB;YAC/C,OAAO;QACT;QACAK,WAAUzB,QAAQ;YAChBT,UAAUyB,GAAG,CAAChB;YACd,OAAO;gBACLT,UAAUiC,MAAM,CAACxB;YACnB;QACF;QACA0B,WAAU/D,MAAM,EAAEqC,QAAQ;gBACRP;YAAhB,MAAM6B,WAAU7B,wBAAAA,iBAAiB8B,GAAG,CAAC5D,mBAArB8B,wBAAgC,IAAID;YACpDC,iBAAiBkC,GAAG,CAAChE,QAAQ2D,QAAQN,GAAG,CAAChB;YACzC,OAAO;gBACLsB,QAAQE,MAAM,CAACxB;YACjB;QACF;IACF;AACF"}
|
|
1
|
+
{"version":3,"sources":["../../src/pending/pending-flow-store.ts"],"sourcesContent":["import type { DomainEvent, EntifixFailure } from '@entifix/core';\nimport { Effect, Result, Schema } from 'effect';\n\nimport { flowTopic, type PushTopic } from '../reactive/push-channel.js';\n\n/**\n * The shape version of what a store persists. A stored value at any other\n * version is discarded, never migrated: the store is a cache of in-flight\n * work, and losing it costs a re-read\n * ([ADR 0022](../../../../../docs/adr/0022-two-settle-modes-and-the-pending-flow-store.md)).\n */\nexport const PENDING_FLOW_STORE_VERSION = 1;\n\n/** One write the server handed to a flow, waiting for the flow's outcome. */\nexport interface PendingFlow {\n readonly flowId: string;\n /** Where the outcome is pushed: `flow:<flowId>`. */\n readonly topic: PushTopic;\n /** ISO-8601, when the write was answered `202`. */\n readonly createdAt: string;\n /** The flow's `status` link, read when its push may have been missed. */\n readonly status?: string;\n /** What the write meant, for the screen that resumes it. JSON only. */\n readonly intent?: unknown;\n}\n\n/**\n * How a pending flow ended. A `failed` outcome with no `failure` is a flow\n * nobody could account for within the age bound: it is no longer shown as\n * pending, and there is nothing to tell the user why.\n */\nexport type FlowOutcome =\n | { readonly kind: 'confirmed'; readonly event?: DomainEvent }\n | { readonly kind: 'failed'; readonly failure?: EntifixFailure };\n\n/**\n * Where a store persists, supplied by the host and scoped by it to one\n * principal. Zustand's `StateStorage` satisfies it.\n */\nexport interface PendingFlowStorage {\n getItem(name: string): string | null | PromiseLike<string | null>;\n setItem(name: string, value: string): unknown;\n}\n\nexport interface PendingFlowStoreOptions {\n /** Omitted, the store lives in memory and a reload forgets it. */\n readonly storage?: PendingFlowStorage;\n /** The storage key. Defaults to `entifix.pending-flows`. */\n readonly key?: string;\n /**\n * An anonymous scope keeps flow ids and their age only: an intent or a\n * status link could carry personal data or a capability, and nothing scopes\n * an anonymous storage to one person.\n */\n readonly anonymous?: boolean;\n readonly now?: () => Date;\n}\n\nexport interface PendingFlowInput {\n readonly flowId: string;\n readonly status?: string;\n readonly intent?: unknown;\n}\n\nexport interface PendingFlowStore {\n /** Settles once whatever was persisted has been read back. */\n readonly ready: Promise<void>;\n /** The pending flows, oldest first. The same array until something changes. */\n list(): readonly PendingFlow[];\n /**\n * Records a pending flow. Throws a `TypeError`, and records nothing, when\n * the intent is not JSON: a value that cannot be read back must never reach\n * storage.\n */\n add(input: PendingFlowInput): PendingFlow;\n /**\n * Ends a pending flow with its outcome. Answers `true` only for the call\n * that ended it, so a redelivered outcome is told apart from the first.\n */\n settle(flowId: string, outcome: FlowOutcome): boolean;\n /** Runs whenever the list changes. */\n subscribe(listener: () => void): () => void;\n /** Runs once, when the named flow settles. */\n onSettled(\n flowId: string,\n listener: (outcome: FlowOutcome) => void,\n ): () => void;\n}\n\nconst isJson = (value: unknown): boolean => {\n if (value === null) return true;\n switch (typeof value) {\n case 'string':\n case 'boolean':\n return true;\n case 'number':\n return Number.isFinite(value);\n case 'object': {\n if (Array.isArray(value)) return value.every(isJson);\n const prototype = Object.getPrototypeOf(value) as unknown;\n return (\n (prototype === Object.prototype || prototype === null) &&\n Object.values(value).every(isJson)\n );\n }\n default:\n return false;\n }\n};\n\nconst Stored = Schema.Struct({\n v: Schema.Literal(PENDING_FLOW_STORE_VERSION),\n flows: Schema.Array(\n Schema.Struct({\n flowId: Schema.String,\n createdAt: Schema.String,\n status: Schema.optionalKey(Schema.String),\n intent: Schema.optionalKey(Schema.Unknown),\n }),\n ),\n});\n\n// Whatever the storage holds, as pending flows: nothing at all when it is\n// missing, unreadable, or at another version.\nconst decodeStored = (raw: string | null): readonly PendingFlow[] => {\n if (raw === null) return [];\n const decoded = Effect.runSync(\n Effect.result(\n Effect.flatMap(\n Effect.try(() => JSON.parse(raw) as unknown),\n Schema.decodeUnknownEffect(Stored),\n ),\n ),\n );\n if (Result.isFailure(decoded)) return [];\n return decoded.success.flows.map(flow => ({\n ...flow,\n topic: flowTopic(flow.flowId),\n }));\n};\n\n/**\n * The pending-flow store: held in memory, written through to the host's\n * storage as `{ v, flows }`, and read back from it once on creation.\n */\nexport function makePendingFlowStore({\n storage,\n key = 'entifix.pending-flows',\n anonymous = false,\n now = () => new Date(),\n}: PendingFlowStoreOptions = {}): PendingFlowStore {\n let flows: readonly PendingFlow[] = [];\n const listeners = new Set<() => void>();\n const settledListeners = new Map<\n string,\n Set<(outcome: FlowOutcome) => void>\n >();\n let writing: Promise<unknown> = Promise.resolve();\n\n const changed = (next: readonly PendingFlow[]): void => {\n flows = next;\n for (const listener of listeners) listener();\n };\n\n const persist = (): void => {\n if (storage === undefined) return;\n const value = JSON.stringify({ v: PENDING_FLOW_STORE_VERSION, flows });\n // Serialized, so a slow write never lands after a later one. A write that\n // fails costs only the reload-survival of what is pending.\n writing = writing\n .then(() => storage.setItem(key, value))\n .catch(() => undefined);\n };\n\n const ready =\n storage === undefined\n ? Promise.resolve()\n : Promise.resolve()\n .then(() => storage.getItem(key))\n .then(\n raw => decodeStored(raw),\n () => [],\n )\n .then(stored => {\n const held = new Set(flows.map(flow => flow.flowId));\n const restored = stored.filter(flow => !held.has(flow.flowId));\n if (restored.length > 0) changed([...restored, ...flows]);\n });\n\n return {\n ready,\n list: () => flows,\n add({ flowId, status, intent }) {\n if (!anonymous && intent !== undefined && !isJson(intent))\n throw new TypeError(\n `the intent of pending flow '${flowId}' is not JSON, so it cannot be stored`,\n );\n const flow: PendingFlow = {\n flowId,\n topic: flowTopic(flowId),\n createdAt: now().toISOString(),\n ...(anonymous || status === undefined ? {} : { status }),\n ...(anonymous || intent === undefined ? {} : { intent }),\n };\n changed([...flows.filter(held => held.flowId !== flowId), flow]);\n persist();\n return flow;\n },\n settle(flowId, outcome) {\n if (!flows.some(flow => flow.flowId === flowId)) return false;\n changed(flows.filter(flow => flow.flowId !== flowId));\n persist();\n const waiting = settledListeners.get(flowId);\n settledListeners.delete(flowId);\n for (const listener of waiting ?? []) listener(outcome);\n return true;\n },\n subscribe(listener) {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n onSettled(flowId, listener) {\n const waiting = settledListeners.get(flowId) ?? new Set();\n settledListeners.set(flowId, waiting.add(listener));\n return () => {\n waiting.delete(listener);\n };\n },\n };\n}\n"],"names":["Effect","Result","Schema","flowTopic","PENDING_FLOW_STORE_VERSION","isJson","value","Number","isFinite","Array","isArray","every","prototype","Object","getPrototypeOf","values","Stored","Struct","v","Literal","flows","flowId","String","createdAt","status","optionalKey","intent","Unknown","decodeStored","raw","decoded","runSync","result","flatMap","try","JSON","parse","decodeUnknownEffect","isFailure","success","map","flow","topic","makePendingFlowStore","storage","key","anonymous","now","Date","listeners","Set","settledListeners","Map","writing","Promise","resolve","changed","next","listener","persist","undefined","stringify","then","setItem","catch","ready","getItem","stored","held","restored","filter","has","length","list","add","TypeError","toISOString","settle","outcome","some","waiting","get","delete","subscribe","onSettled","set"],"mappings":";AACA,SAASA,MAAM,EAAEC,MAAM,EAAEC,MAAM,QAAQ,SAAS;AAEhD,SAASC,SAAS,QAAwB,8BAA8B;AAExE;;;;;CAKC,GACD,OAAO,MAAMC,6BAA6B,EAAE;AA8E5C,MAAMC,SAAS,CAACC;IACd,IAAIA,UAAU,MAAM,OAAO;IAC3B,OAAQ,OAAOA;QACb,KAAK;QACL,KAAK;YACH,OAAO;QACT,KAAK;YACH,OAAOC,OAAOC,QAAQ,CAACF;QACzB,KAAK;YAAU;gBACb,IAAIG,MAA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kC,YAAY,CAAC,IAAI;gBAAElC;YAAO;YAExD8B,QAAQ;mBAAIpC,MAAMkD,MAAM,CAACF,CAAAA,OAAQA,KAAK/C,MAAM,KAAKA;gBAASoB;aAAK;YAC/DkB;YACA,OAAOlB;QACT;QACAoC,QAAOxD,MAAM,EAAEyD,OAAO;YACpB,IAAI,CAAC1D,MAAM2D,IAAI,CAACtC,CAAAA,OAAQA,KAAKpB,MAAM,KAAKA,SAAS,OAAO;YACxDmC,QAAQpC,MAAMkD,MAAM,CAAC7B,CAAAA,OAAQA,KAAKpB,MAAM,KAAKA;YAC7CsC;YACA,MAAMqB,UAAU7B,iBAAiB8B,GAAG,CAAC5D;YACrC8B,iBAAiB+B,MAAM,CAAC7D;YACxB,KAAK,MAAMqC,YAAYsB,kBAAAA,UAAW,EAAE,CAAEtB,SAASoB;YAC/C,OAAO;QACT;QACAK,WAAUzB,QAAQ;YAChBT,UAAUyB,GAAG,CAAChB;YACd,OAAO;gBACLT,UAAUiC,MAAM,CAACxB;YACnB;QACF;QACA0B,WAAU/D,MAAM,EAAEqC,QAAQ;gBACRP;YAAhB,MAAM6B,WAAU7B,wBAAAA,iBAAiB8B,GAAG,CAAC5D,mBAArB8B,wBAAgC,IAAID;YACpDC,iBAAiBkC,GAAG,CAAChE,QAAQ2D,QAAQN,GAAG,CAAChB;YACzC,OAAO;gBACLsB,QAAQE,MAAM,CAACxB;YACjB;QACF;IACF;AACF"}
|
package/package.json
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
|
-
"version": "0.20.
|
|
24
|
+
"version": "0.20.3",
|
|
25
25
|
"description": "React hooks that bind entities to data: TanStack Query and Form over the entity adapters, drafts, mutations and live invalidation.",
|
|
26
26
|
"license": "MIT",
|
|
27
27
|
"repository": {
|
|
@@ -65,16 +65,16 @@
|
|
|
65
65
|
"LICENSE"
|
|
66
66
|
],
|
|
67
67
|
"dependencies": {
|
|
68
|
-
"@entifix/business": "0.20.
|
|
69
|
-
"@entifix/
|
|
70
|
-
"@entifix/
|
|
68
|
+
"@entifix/business": "0.20.3",
|
|
69
|
+
"@entifix/core": "0.20.3",
|
|
70
|
+
"@entifix/transactions": "0.20.3"
|
|
71
71
|
},
|
|
72
72
|
"devDependencies": {
|
|
73
73
|
"@tanstack/react-form": "1.33.5",
|
|
74
|
-
"@tanstack/react-query": "^5.
|
|
74
|
+
"@tanstack/react-query": "^5.107.1",
|
|
75
75
|
"effect": "4.0.7",
|
|
76
76
|
"react": "19.3.1",
|
|
77
|
-
"@entifix/testing-unit": "0.20.
|
|
77
|
+
"@entifix/testing-unit": "0.20.3"
|
|
78
78
|
},
|
|
79
79
|
"peerDependencies": {
|
|
80
80
|
"@tanstack/react-form": ">=1.33 <2",
|