@fingerprint/node-sdk 7.0.0-test.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/readme.md ADDED
@@ -0,0 +1,191 @@
1
+ <p align="center">
2
+ <a href="https://fingerprint.com">
3
+ <picture>
4
+ <source media="(prefers-color-scheme: dark)" srcset="https://fingerprintjs.github.io/home/resources/logo_light.svg" />
5
+ <source media="(prefers-color-scheme: light)" srcset="https://fingerprintjs.github.io/home/resources/logo_dark.svg" />
6
+ <img src="https://fingerprintjs.github.io/home/resources/logo_dark.svg" alt="Fingerprint logo" width="312px" />
7
+ </picture>
8
+ </a>
9
+ </p>
10
+ <p align="center">
11
+ <a href="https://github.com/fingerprintjs/node-sdk/actions/workflows/build.yml"><img src="https://github.com/fingerprintjs/node-sdk/actions/workflows/build.yml/badge.svg" alt="Build status"></a>
12
+ <a href="https://fingerprintjs.github.io/node-sdk/coverage"><img src="https://fingerprintjs.github.io/node-sdk/coverage/badges.svg" alt="coverage"></a>
13
+ <a href="https://www.npmjs.com/package/@fingerprint/node-sdk"><img src="https://img.shields.io/npm/v/@fingerprint/node-sdk.svg" alt="Current NPM version"></a>
14
+ <a href="https://www.npmjs.com/package/@fingerprint/node-sdk"><img src="https://img.shields.io/npm/dm/@fingerprint/node-sdk.svg" alt="Monthly downloads from NPM"></a>
15
+ <a href="https://discord.gg/39EpE2neBg"><img src="https://img.shields.io/discord/852099967190433792?style=logo&label=Discord&logo=Discord&logoColor=white" alt="Discord server"></a>
16
+ </p>
17
+
18
+ # Fingerprint Server Node.js SDK
19
+
20
+ [Fingerprint](https://fingerprint.com) is a device intelligence platform offering industry-leading accuracy.
21
+
22
+ The Fingerprint Server Node SDK is an easy way to interact with the Fingerprint [Server API](https://dev.fingerprint.com/reference/pro-server-api) from your Node application. You can search, update, or delete identification events.
23
+
24
+ ## Requirements
25
+
26
+ TypeScript support:
27
+
28
+ - TypeScript 4.5.5 or higher
29
+
30
+ Supported runtimes:
31
+
32
+ - Node.js 18 LTS or higher (we support all [Node LTS releases before end-of-life](https://nodejs.dev/en/about/releases/)).
33
+ - Deno and Bun might work but are not actively tested.
34
+ - "Edge" runtimes might work with some modifications but are not actively tested. <details>
35
+ <summary>See "edge" runtimes compatibility</summary>
36
+
37
+ This SDK can be made compatible with JavaScript "edge" runtimes that do not support all Node APIs, for example, [Vercel Edge Runtime](https://edge-runtime.vercel.app/), or [Cloudflare Workers](https://developers.cloudflare.com/workers/).
38
+
39
+ To make it work, replace the SDK's built-in `fetch` function (which relies on Node APIs) with the runtime's native `fetch` function. Pass the function into the constructor with proper binding:
40
+
41
+ ```js
42
+ const client = new FingerprintServerApiClient({
43
+ region: Region.EU,
44
+ apiKey: apiKey,
45
+ fetch: fetch.bind(globalThis),
46
+ })
47
+ ```
48
+
49
+ </details>
50
+
51
+ ## How to install
52
+
53
+ Install the package using your favorite package manager:
54
+
55
+ - NPM:
56
+
57
+ ```sh
58
+ npm i @fingerprint/node-sdk
59
+ ```
60
+
61
+ - Yarn:
62
+ ```sh
63
+ yarn add @fingerprint/node-sdk
64
+ ```
65
+ - pnpm:
66
+ ```sh
67
+ pnpm i @fingerprint/node-sdk
68
+ ```
69
+
70
+ ## Getting started
71
+
72
+ Initialize the client instance and use it to make API requests. You need to specify your Fingerprint [Secret API key](https://dev.fingerprint.com/docs/quick-start-guide#4-get-smart-signals-to-your-server) and the region of your Fingerprint workspace.
73
+
74
+ ```ts
75
+ import {
76
+ FingerprintServerApiClient,
77
+ Region,
78
+ } from '@fingerprint/node-sdk'
79
+
80
+ const client = new FingerprintServerApiClient({
81
+ apiKey: '<SECRET_API_KEY>',
82
+ region: Region.Global,
83
+ })
84
+
85
+ // Get visit history of a specific visitor
86
+ client.searchEvents({ visitor_id: '<visitorId>' }).then((visitorHistory) => {
87
+ console.log(visitorHistory)
88
+ })
89
+
90
+ // Get a specific identification event
91
+ client.getEvent('<eventId>').then((event) => {
92
+ console.log(event)
93
+ })
94
+
95
+ // Get an event with a ruleset evaluation
96
+ client.getEvent('<eventId>', { ruleset_id: '<rulesetId>' }).then((event) => {
97
+ console.log(event.rule_action?.type) // 'allow' or 'block'
98
+ })
99
+
100
+ // Search for identification events
101
+ client
102
+ .searchEvents({
103
+ limit: 10,
104
+ // pagination_key: previousSearchResult.pagination_key,
105
+ suspect: true,
106
+ })
107
+ .then((events) => {
108
+ console.log(events)
109
+ })
110
+ ```
111
+
112
+ See the [Examples](https://github.com/fingerprintjs/node-sdk/tree/main/example) folder for more detailed examples.
113
+
114
+ ### Error handling
115
+
116
+ The Server API methods can throw `RequestError`.
117
+ When handling errors, you can check for it like this:
118
+
119
+ ```typescript
120
+ import {
121
+ RequestError,
122
+ FingerprintServerApiClient,
123
+ TooManyRequestsError,
124
+ } from '@fingerprint/node-sdk'
125
+
126
+ const client = new FingerprintServerApiClient({
127
+ apiKey: '<SECRET_API_KEY>',
128
+ region: Region.Global,
129
+ })
130
+
131
+ // Handling getEvent errors
132
+ try {
133
+ const event = await client.getEvent(eventId)
134
+ console.log(JSON.stringify(event, null, 2))
135
+ } catch (error) {
136
+ if (error instanceof RequestError) {
137
+ console.log(error.responseBody) // Access parsed response body
138
+ console.log(error.response) // You can also access the raw response
139
+ console.log(`error ${error.statusCode}: `, error.message)
140
+ } else {
141
+ console.log('unknown error: ', error)
142
+ }
143
+ }
144
+ ```
145
+
146
+ ### Webhooks
147
+
148
+ #### Webhook types
149
+
150
+ When handling [Webhooks](https://dev.fingerprint.com/reference/posteventwebhook#/) coming from Fingerprint, you can cast the payload as the built-in `Event` type:
151
+
152
+ ```ts
153
+ import { Event } from '@fingerprint/node-sdk'
154
+
155
+ const event = eventWebhookBody as unknown as Event
156
+ ```
157
+
158
+ #### Webhook signature validation
159
+
160
+ Customers on the Enterprise plan can enable [Webhook signatures](https://dev.fingerprint.com/docs/webhooks-security) to cryptographically verify the authenticity of incoming webhooks.
161
+ This SDK provides a utility method for verifying the HMAC signature of the incoming webhook request.
162
+
163
+ To learn more, see [example/validateWebhookSignature.mjs](example/validateWebhookSignature.mjs) or read the [API Reference](https://fingerprintjs.github.io/node-sdk/functions/isValidWebhookSignature.html).
164
+
165
+ ### Sealed results
166
+
167
+ Customers on the Enterprise plan can enable [Sealed results](https://dev.fingerprint.com/docs/sealed-client-results) to receive the full device intelligence result on the client and unseal it on the server. This SDK provides utility methods for decoding sealed results.
168
+
169
+ To learn more, see [example/unsealResult.mjs](https://github.com/fingerprintjs/node-sdk/tree/main/example/unsealResult.mjs) or the [API Reference](https://fingerprintjs.github.io/node-sdk/functions/unsealEventsResponse.html).
170
+
171
+ ### Deleting visitor data
172
+
173
+ Customers on the Enterprise plan can [Delete all data associated with a specific visitor](https://dev.fingerprint.com/reference/deletevisitordata) to comply with privacy regulations. See [example/deleteVisitor.mjs](https://github.com/fingerprintjs/node-sdk/tree/main/example/deleteVisitor.mjs) or the [API Reference](https://fingerprintjs.github.io/node-sdk/classes/FingerprintServerApiClient.html#deleteVisitorData).
174
+
175
+ ## API Reference
176
+
177
+ See the full [API reference](https://fingerprintjs.github.io/node-sdk/).
178
+
179
+ ## Semantic versioning
180
+
181
+ * Changes to **types** in this repository are considered non-breaking and are usually released as PATCH or MINOR semver changes (otherwise every schema addition would be a major version upgrade).
182
+ * It is highly recommended that you lock your package version to a specific PATCH release and upgrade with the expectation that types may be upgraded between any release.
183
+ * The runtime (non-type-related) public API of the Node SDK still follows semver strictly.
184
+
185
+ ## Support and feedback
186
+
187
+ To report problems, ask questions, or provide feedback, please use [Issues](https://github.com/fingerprintjs/node-sdk/issues). If you need private support, you can email us at [oss-support@fingerprint.com](mailto:oss-support@fingerprint.com).
188
+
189
+ ## License
190
+
191
+ This project is licensed under the [MIT license](https://github.com/fingerprintjs/node-sdk/tree/main/LICENSE).
@@ -0,0 +1,51 @@
1
+ import { ErrorResponse } from '../types'
2
+
3
+ export class SdkError extends Error {
4
+ constructor(
5
+ message: string,
6
+ readonly response?: Response,
7
+ cause?: Error
8
+ ) {
9
+ super(message, { cause })
10
+ this.name = this.constructor.name
11
+ }
12
+ }
13
+
14
+ export class RequestError<Code extends number = number, Body = unknown> extends SdkError {
15
+ // HTTP Status code
16
+ readonly statusCode: Code
17
+
18
+ // API error code
19
+ readonly errorCode: string
20
+
21
+ // API error response
22
+ readonly responseBody: Body
23
+
24
+ // Raw HTTP response
25
+ override readonly response: Response
26
+
27
+ constructor(message: string, body: Body, statusCode: Code, errorCode: string, response: Response) {
28
+ super(message, response)
29
+ this.responseBody = body
30
+ this.response = response
31
+ this.errorCode = errorCode
32
+ this.statusCode = statusCode
33
+ }
34
+
35
+ static unknown(response: Response) {
36
+ return new RequestError('Unknown error', undefined, response.status, response.statusText, response)
37
+ }
38
+
39
+ static fromErrorResponse(body: ErrorResponse, response: Response) {
40
+ return new RequestError(body.error.message, body, response.status, body.error.code, response)
41
+ }
42
+ }
43
+
44
+ /**
45
+ * Error that indicate that the request was throttled.
46
+ * */
47
+ export class TooManyRequestsError extends RequestError<429, ErrorResponse> {
48
+ constructor(body: ErrorResponse, response: Response) {
49
+ super(body.error.message, body, 429, body.error.code, response)
50
+ }
51
+ }
@@ -0,0 +1,13 @@
1
+ import { ErrorResponse } from '../types'
2
+
3
+ export function isErrorResponse(value: unknown): value is ErrorResponse {
4
+ return Boolean(
5
+ value &&
6
+ typeof value === 'object' &&
7
+ 'error' in value &&
8
+ typeof value.error === 'object' &&
9
+ value.error &&
10
+ 'code' in value.error &&
11
+ 'message' in value.error
12
+ )
13
+ }
@@ -0,0 +1,7 @@
1
+ export function toError(e: unknown): Error {
2
+ if (e && typeof e === 'object' && 'message' in e) {
3
+ return e as Error
4
+ }
5
+
6
+ return new Error(String(e))
7
+ }
@@ -0,0 +1,32 @@
1
+ import { DecryptionKey } from '../sealedResults'
2
+
3
+ export class UnsealError extends Error {
4
+ constructor(
5
+ readonly key: DecryptionKey,
6
+ readonly error?: Error
7
+ ) {
8
+ let msg = `Unable to decrypt sealed data`
9
+
10
+ if (error) {
11
+ msg = msg.concat(`: ${error.message}`)
12
+ }
13
+
14
+ super(msg)
15
+ this.name = 'UnsealError'
16
+ }
17
+ }
18
+
19
+ export class UnsealAggregateError extends Error {
20
+ constructor(readonly errors: UnsealError[]) {
21
+ super('Unable to decrypt sealed data')
22
+ this.name = 'UnsealAggregateError'
23
+ }
24
+
25
+ addError(error: UnsealError) {
26
+ this.errors.push(error)
27
+ }
28
+
29
+ toString() {
30
+ return this.errors.map((e) => e.toString()).join('\n')
31
+ }
32
+ }