@qredex/svelte 1.0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Qredex, LTD.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,142 @@
1
+ <!--
2
+ ▄▄▄▄
3
+ ▄█▀▀███▄▄ █▄
4
+ ██ ██ ▄ ██
5
+ ██ ██ ████▄▄█▀█▄ ▄████ ▄█▀█▄▀██ ██▀
6
+ ██ ▄ ██ ██ ██▄█▀ ██ ██ ██▄█▀ ███
7
+ ▀█████▄▄█▀ ▄▀█▄▄▄▄█▀███▄▀█▄▄▄▄██ ██▄
8
+ ▀█
9
+
10
+ Copyright (C) 2026 — 2026, Qredex, LTD. All Rights Reserved.
11
+
12
+ DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
13
+
14
+ This file is part of the Qredex Agent SDK and is licensed under the MIT License. See LICENSE.
15
+ Redistribution and use are permitted under that license.
16
+
17
+ If you need additional information or have any questions, please email: copyright@qredex.com
18
+ -->
19
+
20
+ # @qredex/svelte
21
+
22
+ Thin Svelte bindings for `@qredex/agent`.
23
+
24
+ [![CI](https://github.com/Qredex/qredex-agent/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/Qredex/qredex-agent/actions/workflows/ci.yml)
25
+ [![Release](https://github.com/Qredex/qredex-agent/actions/workflows/release.yml/badge.svg)](https://github.com/Qredex/qredex-agent/actions/workflows/release.yml)
26
+ [![npm version](https://img.shields.io/npm/v/%40qredex%2Fsvelte.svg)](https://www.npmjs.com/package/@qredex/svelte)
27
+ [![license](https://img.shields.io/npm/l/%40qredex%2Fsvelte)](LICENSE)
28
+
29
+ ## Install
30
+
31
+ ```bash
32
+ npm install @qredex/svelte
33
+ ```
34
+
35
+ ## Attribution Flow
36
+
37
+ ```mermaid
38
+ sequenceDiagram
39
+ participant User
40
+ participant Storefront as Your cart UI
41
+ participant Wrapper as useQredexAgent()
42
+ participant Agent as QredexAgent
43
+
44
+ User->>Storefront: Land with ?qdx_intent=iit_xxx
45
+ Storefront->>Wrapper: useQredexAgent()
46
+ Note right of Agent: Captures IIT automatically<br/>No function call needed
47
+
48
+ User->>Storefront: Cart item count changes
49
+ Storefront->>Agent: agent.handleCartChange({ itemCount, previousCount })
50
+ Note right of Agent: Locks IIT to PIT internally when lockable
51
+
52
+ User->>Storefront: Checkout
53
+ Storefront->>Agent: agent.getPurchaseIntentToken()
54
+ Note right of Storefront: Send PIT with the order to your backend
55
+
56
+ User->>Storefront: Cart is cleared
57
+ Storefront->>Agent: agent.handleCartEmpty()
58
+
59
+ opt No cart-empty step after checkout
60
+ Storefront->>Agent: agent.handlePaymentSuccess()
61
+ end
62
+ ```
63
+
64
+ Call `useQredexAgent()`, then forward merchant cart state with `agent.handleCartChange(...)`, read the PIT with `agent.getPurchaseIntentToken()`, and clear attribution with `agent.handleCartEmpty()`. Only call `agent.handlePaymentSuccess()` if your platform has no cart-empty step after checkout.
65
+
66
+ ## Recommended Integration
67
+
68
+ Use `useQredexAgent()` inside the existing cart surface you already own. The wrapper stays headless.
69
+
70
+ ```svelte
71
+ <script lang="ts">
72
+ import { useQredexAgent } from '@qredex/svelte';
73
+
74
+ export let itemCount = 0;
75
+
76
+ const { agent, state } = useQredexAgent();
77
+ let previousCount = itemCount;
78
+
79
+ $: if (itemCount !== previousCount) {
80
+ agent.handleCartChange({
81
+ itemCount,
82
+ previousCount,
83
+ });
84
+
85
+ previousCount = itemCount;
86
+ }
87
+
88
+ async function clearCart() {
89
+ await fetch('/api/cart/clear', {
90
+ method: 'POST',
91
+ });
92
+
93
+ agent.handleCartEmpty();
94
+ }
95
+
96
+ async function submitOrder() {
97
+ const pit = $state.pit ?? agent.getPurchaseIntentToken();
98
+
99
+ await fetch('/api/orders', {
100
+ method: 'POST',
101
+ headers: {
102
+ 'Content-Type': 'application/json',
103
+ },
104
+ body: JSON.stringify({
105
+ orderId: 'order-123',
106
+ qredex_pit: pit,
107
+ }),
108
+ });
109
+
110
+ await clearCart();
111
+ }
112
+ </script>
113
+
114
+ <div>
115
+ <span>Qredex status: {$state.locked ? 'locked' : 'waiting'}</span>
116
+ <button on:click={clearCart}>Clear cart</button>
117
+ <button disabled={!$state.hasPIT} on:click={submitOrder}>
118
+ Send PIT to backend
119
+ </button>
120
+ </div>
121
+ ```
122
+
123
+ ## What To Call When
124
+
125
+ | Merchant event | Call | Why |
126
+ |---|---|---|
127
+ | Cart becomes non-empty | `agent.handleCartChange({ itemCount, previousCount })` | Gives Qredex the live cart state so IIT can lock to PIT |
128
+ | Cart changes while still non-empty | `agent.handleCartChange(...)` | Safe retry path on the next merchant-reported non-empty cart event if a previous lock failed |
129
+ | Clear cart action | `clearCart() -> agent.handleCartEmpty()` | Clears IIT/PIT from the live session |
130
+ | Need PIT for order submission | `$state.pit` or `agent.getPurchaseIntentToken()` | Attach PIT to the checkout payload |
131
+ | Checkout completes without a cart-empty step | `agent.handlePaymentSuccess()` | Optional explicit cleanup path |
132
+
133
+ ## API Surface
134
+
135
+ | Export | Use |
136
+ |---|---|
137
+ | `useQredexAgent()` | Primary Svelte composable. Returns `{ agent, state }` |
138
+ | `useQredex()` | Deprecated alias for `useQredexAgent()` |
139
+ | `createQredexStateStore()` | State store only, without the convenience composable |
140
+ | `getQredexAgent()` | Direct access to the singleton runtime |
141
+ | `initQredex()` | Explicit browser init when needed |
142
+ | `QredexAgent` | Re-export of the core agent |
@@ -0,0 +1,36 @@
1
+ /**
2
+ * ▄▄▄▄
3
+ * ▄█▀▀███▄▄ █▄
4
+ * ██ ██ ▄ ██
5
+ * ██ ██ ████▄▄█▀█▄ ▄████ ▄█▀█▄▀██ ██▀
6
+ * ██ ▄ ██ ██ ██▄█▀ ██ ██ ██▄█▀ ███
7
+ * ▀█████▄▄█▀ ▄▀█▄▄▄▄█▀███▄▀█▄▄▄▄██ ██▄
8
+ * ▀█
9
+ *
10
+ * Copyright (C) 2026 — 2026, Qredex, LTD. All Rights Reserved.
11
+ *
12
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
13
+ *
14
+ * This file is part of the Qredex Agent SDK and is licensed under the MIT License. See LICENSE.
15
+ * Redistribution and use are permitted under that license.
16
+ *
17
+ * If you need additional information or have any questions, please email: copyright@qredex.com
18
+ */
19
+ import { type Readable } from 'svelte/store';
20
+ import CoreQredexAgent, { type AgentConfig } from '@qredex/agent';
21
+ export type QredexState = ReturnType<typeof CoreQredexAgent.getState>;
22
+ export interface QredexComposable {
23
+ agent: typeof CoreQredexAgent;
24
+ state: Readable<QredexState>;
25
+ }
26
+ export declare function getQredexAgent(): typeof CoreQredexAgent;
27
+ export declare function initQredex(config?: AgentConfig): typeof CoreQredexAgent;
28
+ export declare function useQredexAgent(config?: AgentConfig): QredexComposable;
29
+ export declare function createQredexStateStore(config?: AgentConfig): Readable<QredexState>;
30
+ /**
31
+ * @deprecated Use useQredexAgent() instead.
32
+ */
33
+ export declare function useQredex(config?: AgentConfig): QredexComposable;
34
+ export { CoreQredexAgent as QredexAgent };
35
+ export * from '@qredex/agent';
36
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAGH,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,eAAe,EAAE,EAAE,KAAK,WAAW,EAAE,MAAM,eAAe,CAAC;AAElE,MAAM,MAAM,WAAW,GAAG,UAAU,CAAC,OAAO,eAAe,CAAC,QAAQ,CAAC,CAAC;AACtE,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,OAAO,eAAe,CAAC;IAC9B,KAAK,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;CAC9B;AAgBD,wBAAgB,cAAc,IAAI,OAAO,eAAe,CAEvD;AAED,wBAAgB,UAAU,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,eAAe,CAMvE;AAED,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,gBAAgB,CASrE;AAED,wBAAgB,sBAAsB,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,QAAQ,CAAC,WAAW,CAAC,CAmBlF;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,gBAAgB,CAEhE;AAED,OAAO,EAAE,eAAe,IAAI,WAAW,EAAE,CAAC;AAC1C,cAAc,eAAe,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,76 @@
1
+ /**
2
+ * ▄▄▄▄
3
+ * ▄█▀▀███▄▄ █▄
4
+ * ██ ██ ▄ ██
5
+ * ██ ██ ████▄▄█▀█▄ ▄████ ▄█▀█▄▀██ ██▀
6
+ * ██ ▄ ██ ██ ██▄█▀ ██ ██ ██▄█▀ ███
7
+ * ▀█████▄▄█▀ ▄▀█▄▄▄▄█▀███▄▀█▄▄▄▄██ ██▄
8
+ * ▀█
9
+ *
10
+ * Copyright (C) 2026 — 2026, Qredex, LTD. All Rights Reserved.
11
+ *
12
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
13
+ *
14
+ * This file is part of the Qredex Agent SDK and is licensed under the MIT License. See LICENSE.
15
+ * Redistribution and use are permitted under that license.
16
+ *
17
+ * If you need additional information or have any questions, please email: copyright@qredex.com
18
+ */
19
+ import { onMount } from 'svelte';
20
+ import { readable } from 'svelte/store';
21
+ import CoreQredexAgent from '@qredex/agent';
22
+ const SERVER_STATE = {
23
+ hasIIT: false,
24
+ hasPIT: false,
25
+ iit: null,
26
+ pit: null,
27
+ cartState: 'unknown',
28
+ locked: false,
29
+ timestamp: 0,
30
+ };
31
+ function canUseBrowser() {
32
+ return typeof window !== 'undefined';
33
+ }
34
+ export function getQredexAgent() {
35
+ return CoreQredexAgent;
36
+ }
37
+ export function initQredex(config) {
38
+ if (canUseBrowser() && (config !== undefined || !CoreQredexAgent.isInitialized())) {
39
+ CoreQredexAgent.init(config);
40
+ }
41
+ return CoreQredexAgent;
42
+ }
43
+ export function useQredexAgent(config) {
44
+ onMount(() => {
45
+ initQredex(config);
46
+ });
47
+ return {
48
+ agent: CoreQredexAgent,
49
+ state: createQredexStateStore(config),
50
+ };
51
+ }
52
+ export function createQredexStateStore(config) {
53
+ return readable(SERVER_STATE, (set) => {
54
+ if (!canUseBrowser()) {
55
+ return () => undefined;
56
+ }
57
+ initQredex(config);
58
+ set(CoreQredexAgent.getState());
59
+ const handler = () => {
60
+ set(CoreQredexAgent.getState());
61
+ };
62
+ CoreQredexAgent.onStateChanged(handler);
63
+ return () => {
64
+ CoreQredexAgent.offStateChanged(handler);
65
+ };
66
+ });
67
+ }
68
+ /**
69
+ * @deprecated Use useQredexAgent() instead.
70
+ */
71
+ export function useQredex(config) {
72
+ return useQredexAgent(config);
73
+ }
74
+ export { CoreQredexAgent as QredexAgent };
75
+ export * from '@qredex/agent';
76
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAiB,MAAM,cAAc,CAAC;AACvD,OAAO,eAAqC,MAAM,eAAe,CAAC;AAQlE,MAAM,YAAY,GAAgB;IAChC,MAAM,EAAE,KAAK;IACb,MAAM,EAAE,KAAK;IACb,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,SAAS,EAAE,SAAS;IACpB,MAAM,EAAE,KAAK;IACb,SAAS,EAAE,CAAC;CACb,CAAC;AAEF,SAAS,aAAa;IACpB,OAAO,OAAO,MAAM,KAAK,WAAW,CAAC;AACvC,CAAC;AAED,MAAM,UAAU,cAAc;IAC5B,OAAO,eAAe,CAAC;AACzB,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,MAAoB;IAC7C,IAAI,aAAa,EAAE,IAAI,CAAC,MAAM,KAAK,SAAS,IAAI,CAAC,eAAe,CAAC,aAAa,EAAE,CAAC,EAAE,CAAC;QAClF,eAAe,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC/B,CAAC;IAED,OAAO,eAAe,CAAC;AACzB,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,MAAoB;IACjD,OAAO,CAAC,GAAG,EAAE;QACX,UAAU,CAAC,MAAM,CAAC,CAAC;IACrB,CAAC,CAAC,CAAC;IAEH,OAAO;QACL,KAAK,EAAE,eAAe;QACtB,KAAK,EAAE,sBAAsB,CAAC,MAAM,CAAC;KACtC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,sBAAsB,CAAC,MAAoB;IACzD,OAAO,QAAQ,CAAc,YAAY,EAAE,CAAC,GAAG,EAAE,EAAE;QACjD,IAAI,CAAC,aAAa,EAAE,EAAE,CAAC;YACrB,OAAO,GAAG,EAAE,CAAC,SAAS,CAAC;QACzB,CAAC;QAED,UAAU,CAAC,MAAM,CAAC,CAAC;QACnB,GAAG,CAAC,eAAe,CAAC,QAAQ,EAAE,CAAC,CAAC;QAEhC,MAAM,OAAO,GAAG,GAAG,EAAE;YACnB,GAAG,CAAC,eAAe,CAAC,QAAQ,EAAE,CAAC,CAAC;QAClC,CAAC,CAAC;QAEF,eAAe,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;QAExC,OAAO,GAAG,EAAE;YACV,eAAe,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;QAC3C,CAAC,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,SAAS,CAAC,MAAoB;IAC5C,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC;AAChC,CAAC;AAED,OAAO,EAAE,eAAe,IAAI,WAAW,EAAE,CAAC;AAC1C,cAAc,eAAe,CAAC"}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@qredex/svelte",
3
+ "version": "1.0.0",
4
+ "description": "Svelte wrapper for Qredex Agent",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "./dist/index.js",
8
+ "module": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "README.md",
19
+ "LICENSE"
20
+ ],
21
+ "scripts": {
22
+ "build": "tsc -p tsconfig.json"
23
+ },
24
+ "dependencies": {
25
+ "@qredex/agent": "^1.0.0"
26
+ },
27
+ "devDependencies": {
28
+ "@qredex/agent": "file:../.."
29
+ },
30
+ "peerDependencies": {
31
+ "svelte": "^4.0.0 || ^5.0.0"
32
+ },
33
+ "license": "MIT",
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+https://github.com/Qredex/qredex-agent.git",
40
+ "directory": "packages/svelte"
41
+ },
42
+ "homepage": "https://github.com/Qredex/qredex-agent/tree/main/packages/svelte#readme",
43
+ "bugs": {
44
+ "url": "https://github.com/Qredex/qredex-agent/issues"
45
+ }
46
+ }