@datadome/react-native-core-datadome
v0.1.0-beta.1
Published
React Native wrapper around the native CoreDataDome SDKs for Android and iOS.
Readme
@datadome/react-native-core-datadome
React Native SDK for DataDome bot protection, built on the native CoreDataDome SDKs for Android and iOS. It prepares your request headers and validates your HTTP responses against DataDome, presenting a challenge to the user when one is required.
Beta. This package is a pre-release. The API may change before 1.0.
Requirements
| Requirement | Version | | --- | --- | | React Native | 0.85 or later | | React | 19 or later | | Architecture | New Architecture (enabled by default since React Native 0.76) | | iOS | 15.0 or later | | Android | minSdk 24 (Android 7.0) or later |
Installation
npm install @datadome/react-native-core-datadome
# or
yarn add @datadome/react-native-core-datadomeiOS
cd ios && pod installpod install downloads the native DataDome framework, so it needs network
access the first time. Set your app's deployment target to iOS 15.0 or higher.
Android
No extra setup. The native DataDome SDK is resolved from Maven Central.
Usage
import { DataDome, ResponseValidationStatus } from '@datadome/react-native-core-datadome';
const dataDome = new DataDome({ clientKey: 'YOUR_CLIENT_KEY' });
async function request(url: string): Promise<Response> {
// 1. Add the DataDome headers to your own.
const response = await fetch(url, {
headers: await dataDome.prepareRequestHeaders({}, url),
});
// 2. Convert the response headers to a plain object.
const headers: { [key: string]: string } = {};
response.headers.forEach((value, key) => {
headers[key] = value;
});
// 3. Validate the response. The body is read only if DataDome needs it.
const status = await dataDome.validateResponse(
{ status: response.status, headers, body: () => response.clone().text() },
url
);
switch (status) {
case ResponseValidationStatus.Allowed:
return response;
case ResponseValidationStatus.NeedRetry:
return request(url); // the challenge was passed, retry the request
case ResponseValidationStatus.Blocked:
throw new Error('Blocked by DataDome');
case ResponseValidationStatus.Error:
throw new Error('DataDome validation error');
}
}Constructing DataDome again with the same configuration reuses the same
client, so it is safe to call in a component body. There is nothing to release.
API
new DataDome(config)
config.clientKey: string— your DataDome client key.config.domain?: string | null— optional origin URL.
On Android, a malformed domain throws from the constructor.
prepareRequestHeaders(headers, url): Promise<{ [key: string]: string }>
Returns headers with everything DataDome requires for a request to url
added, such as the DataDome cookie. Call it for every protected request and send
the request with the result. Pass {} when you have no headers of your own.
validateResponse(response, url): Promise<ResponseValidationStatus>
response.status: number— HTTP status code.response.headers: { [key: string]: string }— response headers.response.body: () => string | null | Promise<string | null>— returns the response body as text, ornullwhen there is none. Called only when DataDome needs the body. If it throws,validateResponserejects with that error.url: string— the request URL.
Resolves to a ResponseValidationStatus:
| Member | Meaning |
| --- | --- |
| Allowed | Proceed normally. |
| NeedRetry | A challenge was passed; retry the request. |
| Blocked | The user is blocked; do not retry. |
| Error | Validation failed. |
getCookie(url): Promise<DataDomeCookie | null>
Returns the stored DataDome cookie ({ name, value }) for url, or null.
Errors
The methods above reject with these codes:
| Code | Platforms | When |
| --- | --- | --- |
| ERR_INVALID_URL | Android, iOS | url could not be parsed. |
| ERR_INVALID_INSTANCE | Android, iOS | The native module was torn down under a live client. |
| ERR_DATADOME | Android | A DataDome call failed. |
Platform differences
prepareRequestHeaders: whenheadershas noCookiekey, Android adds one and iOS does not.- The
domainpassed tonew DataDomeis validated on Android only.
License
MIT
