@dataships/sdk 0.0.0-stage → 2.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 +202 -0
- package/README.md +327 -2
- package/dist/adapters/custom-platform-checkout/index.d.ts +139 -0
- package/dist/adapters/custom-platform-checkout/index.js +304 -0
- package/dist/adapters/custom-platform-checkout/index.js.map +1 -0
- package/dist/adapters/custom-platform-signup/index.d.ts +119 -0
- package/dist/adapters/custom-platform-signup/index.js +288 -0
- package/dist/adapters/custom-platform-signup/index.js.map +1 -0
- package/dist/adapters/salesforce/index.d.ts +100 -0
- package/dist/adapters/salesforce/index.js +278 -0
- package/dist/adapters/salesforce/index.js.map +1 -0
- package/dist/adapters/shopify-checkout/index.d.ts +102 -0
- package/dist/adapters/shopify-checkout/index.js +178 -0
- package/dist/adapters/shopify-checkout/index.js.map +1 -0
- package/dist/beacon-B8v41O8P.d.ts +4 -0
- package/dist/dataships.min.js +6 -0
- package/dist/dataships.min.js.map +1 -0
- package/dist/index.d.ts +185 -0
- package/dist/index.js +713 -0
- package/dist/index.js.map +1 -0
- package/dist/integrity.json +9 -0
- package/dist/locale-DwOMogGu.d.ts +9 -0
- package/dist/retry--aRf_ryF.d.ts +130 -0
- package/dist/server.d.ts +72 -0
- package/dist/server.js +455 -0
- package/dist/server.js.map +1 -0
- package/dist/types-Bfm_pCq7.d.ts +233 -0
- package/package.json +83 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 DATASHIPS LIMITED
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,328 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @dataships/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Transport and session-lifecycle client for the Dataships consent-session API
|
|
4
|
+
(`/v1/consent-session`).
|
|
5
|
+
|
|
6
|
+
The SDK is platform-agnostic. It takes a `ConsentSessionAdapter` that knows how
|
|
7
|
+
to read order/customer context from a host environment (Shopify checkout,
|
|
8
|
+
Salesforce, a custom platform, …) and posts the consent-session envelope to the
|
|
9
|
+
Dataships backend.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
The package is published publicly to npm under the `@dataships` scope:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @dataships/sdk
|
|
17
|
+
# or
|
|
18
|
+
yarn add @dataships/sdk
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Quickstart (Salesforce SFCC storefront)
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { DatashipsClient } from "@dataships/sdk";
|
|
25
|
+
import {
|
|
26
|
+
SalesforceAdapter,
|
|
27
|
+
getConfigFromScriptTag,
|
|
28
|
+
} from "@dataships/sdk/adapters/salesforce";
|
|
29
|
+
|
|
30
|
+
const config = getConfigFromScriptTag();
|
|
31
|
+
|
|
32
|
+
const client = new DatashipsClient({
|
|
33
|
+
// Public key + the JWT the cartridge minted at render; both come from the
|
|
34
|
+
// script tag. The SDK sends the token verbatim and never inspects it.
|
|
35
|
+
auth: { publicKey: config.publicKey, token: config.token! },
|
|
36
|
+
// Any env other than "production" also needs a baseUrl; see "Environments".
|
|
37
|
+
env: config.env,
|
|
38
|
+
app: "my-sfcc-cartridge",
|
|
39
|
+
version: "1.0.0",
|
|
40
|
+
adapter: new SalesforceAdapter({ config }),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const response = await client.upsertSession({ identifierType: "EMAIL" });
|
|
44
|
+
// Render one widget per entry; an empty array means render nothing.
|
|
45
|
+
for (const entry of response.consents) {
|
|
46
|
+
render(entry.channel, entry.consentType, entry.checked, entry.widgetConfig);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const session = { consentSessionId: response.consentSessionId };
|
|
50
|
+
const stop = client.startHeartbeat(session, {
|
|
51
|
+
onError: err => console.warn("heartbeat failed", err),
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
await client.saveConsent({
|
|
55
|
+
channel: "EMAIL",
|
|
56
|
+
consent: true,
|
|
57
|
+
session,
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The Salesforce adapter speaks the `consent-session` protocol (see below) and
|
|
62
|
+
reads its config from the cartridge-rendered `data-*` attributes on the
|
|
63
|
+
Dataships `<script>` tag. Rotate the token with `client.setToken(next)` before
|
|
64
|
+
it expires; a missed rotation surfaces as a 401 `DatashipsError` and stops the
|
|
65
|
+
heartbeat.
|
|
66
|
+
|
|
67
|
+
## Environments
|
|
68
|
+
|
|
69
|
+
Only production has a built-in API host (`https://api.dataships.io`).
|
|
70
|
+
The package ships no internal hostnames, so `env: "staging"` or `env: "local"`
|
|
71
|
+
without a `baseUrl` throws at construction. Pass the host Dataships gives you
|
|
72
|
+
for a non-production environment:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
new DatashipsClient({ env: "staging", baseUrl: STAGING_API_URL, ... });
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Script tag (CDN)
|
|
79
|
+
|
|
80
|
+
For pages without a bundler, `dist/dataships.min.js` is a minified IIFE that
|
|
81
|
+
assigns `window.Dataships` with the browser client, every adapter (not the
|
|
82
|
+
server client) and `version`. `unpkg` and `jsdelivr` serve it for a bare
|
|
83
|
+
package URL.
|
|
84
|
+
|
|
85
|
+
```html
|
|
86
|
+
<script src="https://sdk.dataships.io/sdk/v2/dataships.min.js"></script>
|
|
87
|
+
<script>
|
|
88
|
+
const { DatashipsClient, CustomPlatformCheckoutAdapter } = window.Dataships;
|
|
89
|
+
const client = new DatashipsClient({
|
|
90
|
+
auth: { publicKey, token },
|
|
91
|
+
app: "my-storefront",
|
|
92
|
+
version: "1.0.0",
|
|
93
|
+
adapter: new CustomPlatformCheckoutAdapter({ collectionStep: "CHECKOUT_INFORMATION" }),
|
|
94
|
+
});
|
|
95
|
+
</script>
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Versions and CDN URLs
|
|
99
|
+
|
|
100
|
+
- `https://sdk.dataships.io/sdk/v2/` follows the latest 2.x release and is the
|
|
101
|
+
URL to use. A new major gets a new alias, so a page never moves majors on its own.
|
|
102
|
+
- `https://sdk.dataships.io/sdk/2.0.0/` is an exact release and never changes.
|
|
103
|
+
- `https://sdk.dataships.io/sdk/next/` follows the latest release candidate.
|
|
104
|
+
|
|
105
|
+
Every exact release publishes `integrity.json` beside its scripts, with the
|
|
106
|
+
SHA-384 of each one. To use subresource integrity, pin the exact version (an
|
|
107
|
+
alias moves with every release, so its hash would stop matching):
|
|
108
|
+
|
|
109
|
+
```html
|
|
110
|
+
<script
|
|
111
|
+
src="https://sdk.dataships.io/sdk/2.0.0/dataships.min.js"
|
|
112
|
+
integrity="sha384-…"
|
|
113
|
+
crossorigin="anonymous"></script>
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Quickstart (custom platform / headless storefront)
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
import { DatashipsClient } from "@dataships/sdk";
|
|
120
|
+
import { CustomPlatformCheckoutAdapter } from "@dataships/sdk/adapters/custom-platform-checkout";
|
|
121
|
+
|
|
122
|
+
const adapter = new CustomPlatformCheckoutAdapter({
|
|
123
|
+
collectionStep: "CHECKOUT_INFORMATION", // or "THANK_YOU" on the confirmation page
|
|
124
|
+
locale: navigator.language, // coerced to a SupportedLocale
|
|
125
|
+
storageScope: publicKey, // keys the datashipsSessionId in localStorage
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const client = new DatashipsClient({
|
|
129
|
+
// The merchant's server mints the short-lived ES256 JWT and injects it into the page.
|
|
130
|
+
auth: { publicKey, token },
|
|
131
|
+
env: "production",
|
|
132
|
+
app: "my-storefront",
|
|
133
|
+
version: "1.0.0",
|
|
134
|
+
adapter,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// Anonymous first, then again once the shopper types an email.
|
|
138
|
+
let response = await client.upsertSession({ identifierType: "EMAIL" });
|
|
139
|
+
response = await client.upsertSession({ identifierType: "EMAIL", identifier: typedEmail });
|
|
140
|
+
|
|
141
|
+
const session = { consentSessionId: response.consentSessionId };
|
|
142
|
+
const stop = client.startHeartbeat(session);
|
|
143
|
+
await client.saveConsent({ channel: "EMAIL", consent: true, session });
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
The adapter posts `integration: { type: "CUSTOM_PLATFORM_CHECKOUT", collectionStep,
|
|
147
|
+
datashipsSessionId, isFastCheckout }`.
|
|
148
|
+
The signup adapter (`CustomPlatformSignupAdapter`, `@dataships/sdk/adapters/custom-platform-signup`)
|
|
149
|
+
posts `integration: { type: "CUSTOM_PLATFORM_SIGNUP", datashipsSessionId }` for a merchant-built
|
|
150
|
+
form: same beacon lifecycle under its own storage key, one id per form fill, `resetDatashipsSessionId()`
|
|
151
|
+
after the form is submitted; identifiers come from a `user` literal or resolver. The `datashipsSessionId` beacon is minted on the
|
|
152
|
+
checkout step, carried to the thank-you page through `localStorage` (scoped by
|
|
153
|
+
`storageScope`), and rotated when a new checkout starts; pass your own
|
|
154
|
+
`datashipsSessionId` to own that lifecycle instead. A thank-you upsert for a checkout
|
|
155
|
+
that already showed the widget answers 204, which the SDK resolves to an empty
|
|
156
|
+
`consents`.
|
|
157
|
+
|
|
158
|
+
Completing the flow — the placed order landing on every session opened under the
|
|
159
|
+
beacon and processing being queued — is `client.submitSession()`, backed by
|
|
160
|
+
`POST /v1/consent-session/submit`:
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
await client.submitSession({
|
|
164
|
+
user: { firstName: "Jane", lastName: "Doe" }, // both required; contact details never travel here
|
|
165
|
+
details: { id: "ORD-1001", total: 149.9, billingCountryCode: "IE", submittedAt: new Date() },
|
|
166
|
+
});
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
The beacon defaults to the adapter's own and is discarded once the submit is
|
|
170
|
+
accepted; pass `datashipsSessionId` to complete a beacon you carried yourself.
|
|
171
|
+
The identifier is what the widget collected on the upsert; the submit carries
|
|
172
|
+
only the shopper's names, and the SDK refuses a blank one before sending.
|
|
173
|
+
The backend is idempotent and answers 200 even when the beacon matches no
|
|
174
|
+
session. `details` is the flow's payload merged under `integration` next to the
|
|
175
|
+
adapter's `type`: for `CUSTOM_PLATFORM_CHECKOUT` that is the order above
|
|
176
|
+
(`total` as a number in the integration's currency, `billingCountryCode` ISO
|
|
177
|
+
3166-1 alpha-2). The server client exposes the same call with
|
|
178
|
+
`datashipsSessionId` required, for a merchant backend completing from its
|
|
179
|
+
order-created hook.
|
|
180
|
+
|
|
181
|
+
## Quickstart (Shopify checkout UI extension)
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
import { DatashipsClient } from "@dataships/sdk";
|
|
185
|
+
import { ShopifyCheckoutAdapter } from "@dataships/sdk/adapters/shopify-checkout";
|
|
186
|
+
|
|
187
|
+
const adapter = new ShopifyCheckoutAdapter({
|
|
188
|
+
collectionStep: "CHECKOUT_INFORMATION", // or "THANK_YOU"
|
|
189
|
+
datashipsOrderUuid, // the checkout's cart-attribute UUID, when it has one
|
|
190
|
+
locale: shopify.localization.language.value.isoCode,
|
|
191
|
+
checkoutToken: shopify.checkoutToken.value,
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
const client = new DatashipsClient({
|
|
195
|
+
// Shopify session tokens live about a minute: fetch one per request.
|
|
196
|
+
auth: { publicKey, getToken: () => shopify.sessionToken.get() },
|
|
197
|
+
env: "production",
|
|
198
|
+
app: "my-checkout-extension",
|
|
199
|
+
version: "1.0.0",
|
|
200
|
+
adapter,
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const response = await client.upsertSession({ identifierType: "EMAIL", identifier: email });
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
`getToken` is called before every request, retries and heartbeats included,
|
|
207
|
+
and is exclusive with `token` (`setToken()` throws under it). Update facts that
|
|
208
|
+
change on the page with `adapter.setCheckoutDetails({ orderId, wallet, … })`
|
|
209
|
+
and `adapter.setCollectionStep(...)`.
|
|
210
|
+
|
|
211
|
+
In the checkout editor, run the same flow with no credentials and no network
|
|
212
|
+
call:
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
const client = new DatashipsClient({
|
|
216
|
+
app: "my-checkout-extension",
|
|
217
|
+
version: "1.0.0",
|
|
218
|
+
adapter: new ShopifyCheckoutAdapter({
|
|
219
|
+
collectionStep: "CHECKOUT_INFORMATION",
|
|
220
|
+
previewMode: true,
|
|
221
|
+
}),
|
|
222
|
+
});
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## Unsafe: the server key in the browser
|
|
226
|
+
|
|
227
|
+
A merchant with no server at all cannot mint tokens. As an explicit, accepted-risk
|
|
228
|
+
escape hatch the browser client takes the server key under its own name:
|
|
229
|
+
|
|
230
|
+
```ts
|
|
231
|
+
const client = new DatashipsClient({
|
|
232
|
+
auth: { unsafeServerKey: "ds_sk_live_…" }, // UNSAFE: exposed to every visitor
|
|
233
|
+
env: "production",
|
|
234
|
+
app: "my-storefront",
|
|
235
|
+
version: "1.0.0",
|
|
236
|
+
adapter,
|
|
237
|
+
});
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
The key is sent alone as `X-Api-Key`. Anyone who views the page source can open
|
|
241
|
+
sessions, save consent and submit orders as that merchant until the key is
|
|
242
|
+
revoked (ask Dataships support to revoke it); revocation is the only remedy. The SDK refuses a public key under this name, warns once in the
|
|
243
|
+
console at construction, appends `unsafe-server-key` to `X-Dataships-App` so the
|
|
244
|
+
backend can tell these calls from genuine server-to-server ones, and `setToken()`
|
|
245
|
+
throws. Prefer `auth: { publicKey, token }` wherever a token can be minted.
|
|
246
|
+
|
|
247
|
+
## Server-to-server (merchant backend)
|
|
248
|
+
|
|
249
|
+
The server key never leaves the merchant's servers. A separate entry keeps it
|
|
250
|
+
out of storefront bundles: `@dataships/sdk/server` refuses to construct where
|
|
251
|
+
`window` exists, and the browser client refuses server keys.
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
import { DatashipsServerClient } from "@dataships/sdk/server";
|
|
255
|
+
|
|
256
|
+
const client = new DatashipsServerClient({
|
|
257
|
+
auth: { serverKey: process.env.DATASHIPS_SERVER_KEY! },
|
|
258
|
+
app: "my-backend",
|
|
259
|
+
version: "1.0.0",
|
|
260
|
+
adapter, // any consent-session adapter supplying the integration context
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
// The connection IP is your server's, so the user's IP is mandatory.
|
|
264
|
+
const response = await client.upsertSession({
|
|
265
|
+
identifierType: "EMAIL",
|
|
266
|
+
identifier: "user@example.com",
|
|
267
|
+
userIp: request.ip,
|
|
268
|
+
});
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
The server client exposes `upsertSession`, `saveConsent`, `heartbeat` (one
|
|
272
|
+
beat; schedule your own cadence), `submitSession` (with an explicit
|
|
273
|
+
`datashipsSessionId`), `triggerVerificationCode` and `submitVerificationCode`,
|
|
274
|
+
with the same responses and `DatashipsError` as the browser client.
|
|
275
|
+
|
|
276
|
+
## Adapters
|
|
277
|
+
|
|
278
|
+
| Adapter | Import | Use case |
|
|
279
|
+
| --- | --- | --- |
|
|
280
|
+
| Shopify checkout UI extensions | `@dataships/sdk/adapters/shopify-checkout` | Checkout UI extensions (`SHOPIFY_CHECKOUT`). No DOM: the extension passes the collection step, `datashipsOrderUuid` and checkout details from the Shopify checkout API. Authenticate with `auth: { publicKey, getToken: () => shopify.sessionToken.get() }` so every request carries a fresh session token. Used by the Dataships Shopify app. |
|
|
281
|
+
| Salesforce (SFCC) | `@dataships/sdk/adapters/salesforce` | Reads `data-*` attributes off the SFCC cartridge `<script>` tag. Used by the Dataships SFCC cartridge. |
|
|
282
|
+
| Custom platform checkout | `@dataships/sdk/adapters/custom-platform-checkout` | Bespoke / headless storefronts with no native integration (`CUSTOM_PLATFORM`). Every input comes from the integrator; the adapter owns the per-checkout `datashipsSessionId`. Used by `@dataships/elements`. |
|
|
283
|
+
| Custom platform signup | `@dataships/sdk/adapters/custom-platform-signup` | Merchant-built signup forms (newsletter, account creation) on platforms with no native integration. Posts `CUSTOM_PLATFORM_SIGNUP` with the per-form-fill `datashipsSessionId`; the merchant supplies the user (`email`, `phone`, name) as a literal or a resolver. |
|
|
284
|
+
|
|
285
|
+
### Consent-session protocol
|
|
286
|
+
|
|
287
|
+
Every adapter drives `/v1/consent-session`. The client authenticates with
|
|
288
|
+
`auth: { publicKey, token }` or `auth: { publicKey, getToken }` (`X-Api-Key` +
|
|
289
|
+
`Authorization: Bearer`). `upsertSession({ identifierType, identifier?, userIp?, format? })`
|
|
290
|
+
posts the `{ identifier, identifierType, locale, userIp?, format?, integration: { type, … } }`
|
|
291
|
+
envelope to `POST /v1/consent-session` and returns
|
|
292
|
+
`{ consentSessionId, consents[], userVerificationRequired, lastHeartbeat, identifier }`:
|
|
293
|
+
one entry per channel that renders (`channel`, `consentType`, `checked`, and
|
|
294
|
+
`widgetConfig` with the nullable copy fields), plus the identifier the session
|
|
295
|
+
holds so a widget can refill a field it lost. `format` picks how the copy comes
|
|
296
|
+
back: `json` (the API default) for plain strings you format yourself, `html` for
|
|
297
|
+
`text` and `consentDescription` with links and emphasis applied. In `json` copy,
|
|
298
|
+
`{{MARKETING_PREFERENCES}}` is the marketing modal link showing
|
|
299
|
+
`widgetConfig.placeholderValues.MARKETING_PREFERENCES`, which the API resolves
|
|
300
|
+
to the author's text or the served language's "Click here". Save-consent, heartbeat and the
|
|
301
|
+
OTP calls live under `/v1/consent-session/*`. Pass
|
|
302
|
+
`{ consentSessionId: response.consentSessionId }` as the session for
|
|
303
|
+
`startHeartbeat` / `saveConsent`. A 204 No Content on upsert resolves to an
|
|
304
|
+
empty `consents` as well.
|
|
305
|
+
|
|
306
|
+
In preview mode (`previewMode: true` on the adapter) the client makes no
|
|
307
|
+
network call and needs no `auth`. `upsertSession` returns a synthetic response
|
|
308
|
+
for the requested identifier type: `fakeEmailUpsertResponse` (one EMAIL
|
|
309
|
+
consent) for EMAIL, `fakePhoneUpsertResponse` (one SMS consent,
|
|
310
|
+
`userVerificationRequired: false`) for PHONE. The other calls
|
|
311
|
+
resolve without a request: `triggerVerificationCode` reports untriggered and
|
|
312
|
+
`submitVerificationCode` resolves verified.
|
|
313
|
+
|
|
314
|
+
Errors surface as `DatashipsError { status, code, reason, challenge, body }`.
|
|
315
|
+
401 / 403 are never retried; a 422 `COUNTRY_NOT_FOUND` on upsert resolves to
|
|
316
|
+
an empty `consents` instead of throwing. A 409 is retried when it is a held
|
|
317
|
+
checkout lock (`PESSIMISTIC_LOCK_EXCEPTION`) but not when the session has
|
|
318
|
+
already been processed (`CONSENT_SESSION_ALREADY_PROCESSED`, exposed as
|
|
319
|
+
`error.isSessionProcessed`), which also stops the heartbeat.
|
|
320
|
+
|
|
321
|
+
## Contributing
|
|
322
|
+
|
|
323
|
+
See `CONTRIBUTING.md` in the repository for local development, prereleases
|
|
324
|
+
and the release process.
|
|
325
|
+
|
|
326
|
+
## License
|
|
327
|
+
|
|
328
|
+
Licensed under the [Apache License, Version 2.0](LICENSE).
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { C as ConsentSessionAdapter, I as IdentifierType, U as UpsertSessionResponse, S as SupportedLocale, a as ConsentSessionIdentifier } from '../../types-Bfm_pCq7.js';
|
|
2
|
+
export { D as DATASHIPS_SESSION_ID_MAX_LENGTH } from '../../beacon-B8v41O8P.js';
|
|
3
|
+
export { c as coerceLocale } from '../../locale-DwOMogGu.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Where in the checkout the widget is being shown. Mirrors core-api's
|
|
7
|
+
* `WidgetType` as accepted by the `CUSTOM_PLATFORM_CHECKOUT` integration:
|
|
8
|
+
*
|
|
9
|
+
* - `CHECKOUT_INFORMATION` — the pre-purchase step where the shopper enters
|
|
10
|
+
* their contact details.
|
|
11
|
+
* - `THANK_YOU` — the order confirmation page shown right after purchase.
|
|
12
|
+
* - `ORDER_STATUS` — a later revisit of a placed order (reload of the
|
|
13
|
+
* confirmation page, "view your order" link). Never collects consent: the
|
|
14
|
+
* API answers 204 and the SDK resolves an empty `consents`.
|
|
15
|
+
*/
|
|
16
|
+
type CustomPlatformCollectionStep = "CHECKOUT_INFORMATION" | "THANK_YOU" | "ORDER_STATUS";
|
|
17
|
+
/**
|
|
18
|
+
* Integration-specific fields the custom-platform adapter contributes to the
|
|
19
|
+
* consent-session envelope, merged under `integration`. The client injects
|
|
20
|
+
* `type: "CUSTOM_PLATFORM_CHECKOUT"`; the identifier and `locale` travel at the
|
|
21
|
+
* top level; the merchant is identified by the `X-Api-Key` header.
|
|
22
|
+
*
|
|
23
|
+
* `datashipsSessionId` is the SDK-generated beacon for one checkout: sent on
|
|
24
|
+
* every upsert and echoed on order submission, which is how the order is
|
|
25
|
+
* mapped back onto the EMAIL / PHONE sibling sessions. The order itself never
|
|
26
|
+
* travels on the upsert.
|
|
27
|
+
*/
|
|
28
|
+
interface CustomPlatformCheckoutSessionContext {
|
|
29
|
+
collectionStep: CustomPlatformCollectionStep;
|
|
30
|
+
datashipsSessionId: string;
|
|
31
|
+
/**
|
|
32
|
+
* Whether the shopper is in a fast / express checkout (Apple Pay, Shop Pay,
|
|
33
|
+
* PayPal Express, …) that skips the checkout information step. The backend
|
|
34
|
+
* then shows the widget on the thank-you page even though a checkout
|
|
35
|
+
* session already exists.
|
|
36
|
+
*/
|
|
37
|
+
isFastCheckout: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** `integration.type` discriminator core-api routes to the custom-platform checkout lifecycle. */
|
|
41
|
+
declare const CUSTOM_PLATFORM_CHECKOUT_INTEGRATION_TYPE = "CUSTOM_PLATFORM_CHECKOUT";
|
|
42
|
+
|
|
43
|
+
interface CustomPlatformCheckoutAdapterOptions {
|
|
44
|
+
/** Which checkout step the page represents. Change it later with {@link CustomPlatformCheckoutAdapter.setCollectionStep}. */
|
|
45
|
+
collectionStep: CustomPlatformCollectionStep;
|
|
46
|
+
/**
|
|
47
|
+
* The shopper's language. Any BCP 47 / POSIX style string is accepted
|
|
48
|
+
* (`en`, `en-GB`, `de_DE`); the first segment is used when it maps to a
|
|
49
|
+
* {@link SupportedLocale}, otherwise `"en"`.
|
|
50
|
+
*/
|
|
51
|
+
locale?: string;
|
|
52
|
+
/** Server-known email, when the page already has it (order confirmation). */
|
|
53
|
+
email?: string | null;
|
|
54
|
+
/** Server-known E.164 phone, when the page already has it. */
|
|
55
|
+
phone?: string | null;
|
|
56
|
+
/** See {@link CustomPlatformCheckoutSessionContext.isFastCheckout}. Defaults to false. */
|
|
57
|
+
isFastCheckout?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Bring your own checkout beacon. When set the adapter never touches
|
|
60
|
+
* `localStorage`; the integrator owns the lifecycle (one id per checkout,
|
|
61
|
+
* echoed on order submission). At most 100 characters.
|
|
62
|
+
*/
|
|
63
|
+
datashipsSessionId?: string;
|
|
64
|
+
/**
|
|
65
|
+
* Scopes the `localStorage` entry that carries the beacon across the
|
|
66
|
+
* checkout → confirmation navigation, so two merchants (or two stores) on
|
|
67
|
+
* one origin do not share a checkout. Typically the merchant's public key.
|
|
68
|
+
*/
|
|
69
|
+
storageScope?: string;
|
|
70
|
+
/**
|
|
71
|
+
* When true the client never calls the API and serves synthetic email data
|
|
72
|
+
* instead, so the widget can be styled without credentials. Defaults to
|
|
73
|
+
* false.
|
|
74
|
+
*/
|
|
75
|
+
previewMode?: boolean;
|
|
76
|
+
/** Override the window for tests. Defaults to `globalThis.window`. */
|
|
77
|
+
window?: Window;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Consent-session adapter for merchants on a platform Dataships has no native
|
|
81
|
+
* integration with (the `CUSTOM_PLATFORM` merchant integration): a bespoke
|
|
82
|
+
* storefront, a headless build, a platform without an app store. Drives the
|
|
83
|
+
* `CUSTOM_PLATFORM_CHECKOUT` upsert type on `POST /v1/consent-session`.
|
|
84
|
+
*
|
|
85
|
+
* Unlike the Salesforce and Shopify adapters there is no platform surface to
|
|
86
|
+
* read from, so every input is supplied by the integrator: the checkout step,
|
|
87
|
+
* the locale, the identifiers the page already knows. The adapter's own job is
|
|
88
|
+
* the `datashipsSessionId` beacon that ties the checkout-information upsert,
|
|
89
|
+
* the thank-you upsert and the later order submission together.
|
|
90
|
+
*
|
|
91
|
+
* Beacon lifecycle (when {@link CustomPlatformCheckoutAdapterOptions.datashipsSessionId}
|
|
92
|
+
* is not supplied):
|
|
93
|
+
* - First use on `CHECKOUT_INFORMATION`: a fresh UUID, stored under the scope.
|
|
94
|
+
* - `THANK_YOU` / `ORDER_STATUS`: the stored id is reused so the backend
|
|
95
|
+
* resolves the session opened at checkout (and suppresses the widget when it
|
|
96
|
+
* was already asked there). A confirmation reached without a stored entry —
|
|
97
|
+
* cleared storage, another device, a fast checkout landing straight on the
|
|
98
|
+
* thank-you page — mints a fresh id: best-effort correlation by design.
|
|
99
|
+
* - A later `CHECKOUT_INFORMATION` after a `THANK_YOU` / `ORDER_STATUS` use is
|
|
100
|
+
* a new checkout: the stored id is discarded and a fresh one minted, so a
|
|
101
|
+
* completed (processed) session is never upserted again.
|
|
102
|
+
* - {@link resetDatashipsSessionId} forces a fresh id, e.g. right after the
|
|
103
|
+
* order was submitted.
|
|
104
|
+
*/
|
|
105
|
+
declare class CustomPlatformCheckoutAdapter implements ConsentSessionAdapter<CustomPlatformCheckoutSessionContext> {
|
|
106
|
+
#private;
|
|
107
|
+
readonly name: string;
|
|
108
|
+
readonly sessionApi: "consent-session";
|
|
109
|
+
readonly integrationType = "CUSTOM_PLATFORM_CHECKOUT";
|
|
110
|
+
readonly previewMode: boolean;
|
|
111
|
+
constructor(opts: CustomPlatformCheckoutAdapterOptions);
|
|
112
|
+
/** Synthetic upsert response served in {@link previewMode}, matching the requested identifier type. */
|
|
113
|
+
getPreviewUpsertResponse(identifierType?: IdentifierType): UpsertSessionResponse;
|
|
114
|
+
getLocale(): SupportedLocale;
|
|
115
|
+
get collectionStep(): CustomPlatformCollectionStep;
|
|
116
|
+
/** Move the same adapter to another step, e.g. a single-page checkout reaching its confirmation view. */
|
|
117
|
+
setCollectionStep(step: CustomPlatformCollectionStep): void;
|
|
118
|
+
get isFastCheckout(): boolean;
|
|
119
|
+
setFastCheckout(isFastCheckout: boolean): void;
|
|
120
|
+
/**
|
|
121
|
+
* Remember an identifier the shopper typed so later calls (a refresh, a
|
|
122
|
+
* remount) resolve it without the caller passing it again. An explicit
|
|
123
|
+
* `upsertSession({ identifier })` still takes precedence.
|
|
124
|
+
*/
|
|
125
|
+
setIdentifier(identifierType: IdentifierType, value: string | null): void;
|
|
126
|
+
getIdentifier(identifierType: IdentifierType): ConsentSessionIdentifier;
|
|
127
|
+
/** The beacon this checkout is known by; resolved (and persisted) on first use. */
|
|
128
|
+
getDatashipsSessionId(): string;
|
|
129
|
+
/**
|
|
130
|
+
* Discard the current beacon so the next call mints a fresh one. Call it once
|
|
131
|
+
* the order has been submitted, so the next checkout on this browser opens a
|
|
132
|
+
* new session instead of touching the processed one. No-op for an
|
|
133
|
+
* integrator-owned id.
|
|
134
|
+
*/
|
|
135
|
+
resetDatashipsSessionId(): void;
|
|
136
|
+
getSessionContext(): Promise<CustomPlatformCheckoutSessionContext>;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export { CUSTOM_PLATFORM_CHECKOUT_INTEGRATION_TYPE, CustomPlatformCheckoutAdapter, type CustomPlatformCheckoutAdapterOptions, type CustomPlatformCheckoutSessionContext, type CustomPlatformCollectionStep };
|