npm package: @resistantai/resistant-documents-client
Description: Client library to connect to Resistant Documents service.
Author: ResistantAI
Latest version: 3.3.0

# Resistant documents client
This library provides a Typescript client for a Resistant.AI [document forgery analysis service](https://resistant.ai/products/documents/).
For a detailed description of the API please see [API reference docs](https://documents.testing.resistant.ai/docs/v2-preview.html).


## Prerequisites
During the customer onboarding process, you should be provided with the following:

- `CLIENT_ID` : str
- `CLIENT_SECRET` : str

Note: Those credentials are connected with specific environment (e.g. test, production etc.). Together with secret credentials for
non-production environment you should be provided with the following URLs:

- `API_URL`
- `TOKEN_URL`

`TOKEN_URL` is used to authenticate with an identity provider, this library
does not use it. Once authenticated, use `` `Bearer ${accessToken}` `` as API key.


## Installation
Package can be installed using `npm`:
```shell
$ npm add @resistantai/resistant-documents-client
```

## Usage

### Import and init
Package can be imported as
```javascript
import ResistantDocumentsApi from "@resistantai/resistant-documents-client";
```
or in `Node` as
```javascript
const ResistantDocumentsApi = require("@resistantai/resistant-documents-client");
```

Client instance can be then initialized using
```javascript
const client = new ResistantDocumentsApi(() => "YOUR_API_KEY", API_URL);
```
or if you wish to use it against the production environment you can skip the second parameter:
```javascript
const client = new ResistantDocumentsApi(() => "YOUR_API_KEY");
```

### Examples
#### Submit document for analysis and wait for results
```javascript
client.analyze(file).then(results => console.log(results));
```

Information about original file name is not stored in the backed. You can manually store this information
using `query_id` parameter:

```javascript
client.analyze(file, file.name).then(analysisResponse => console.log(analysisResponse));
```
Provided `query_id` is always returned with `AnalysisResponse`.

#### Submit document and fetch results manually
```javascript
client.submit(file, file.name)
    .then(submissionId => client.results(submissionId))
    .then(analysisResponse => console.log(analysisResponse));
```
Analysis is asynchronous, therefore the client needs to poll for the result until it is available. If
desired, it is possible to change number of retries when fetching the result:
```javascript
client.submit(file, file.name)
    .then(submissionId => client.results(submissionId, 10)) // will retry at most 10 times
    .then(analysisResponse => console.log(analysisResponse));
```

#### Submit document and fetch fraud analysis results, parsed content and quality information
Besides analysis result, one can also fetch quality information for the submitted document,
if quality analysis was requested. By default, only fraud analysis is performed. To also request quality
analysis, the submit method accepts an additional argument
```javascript
// will perform quality and fraud analysis
const submissionId = await client.submit(file, PipelineConfiguration.QUALITY_AND_FRAUD)

// will perform only quality analysis
const submissionId = await client.submit(file, PipelineConfiguration.QUALITY_ONLY)

// TODO: what does this do?
const submissionId = await client.submit(file, PipelineConfiguration.CONTENT_AFTER_FRAUD_AFTER_QUALITY) 
```
Analysis, quality and content results can then be fetched as follows:
```javascript
const analysisResponse = await client.results(submissionId); 
const qualityResponse = await client.quality(submissionId); 
const contentResponse = await client.content(submissionId); 
```
Please note that parsing content is available only for selected document types.

#### Download submitted file
Once file has been submitted, it can be downloaded as long as its retention period hasn't expired, after which
it is permanently deleted.
```javascript
const submissionId = await client.submit(file);
const rawFileContent = await client.data(submissionId);
```

[//]: # ()
[//]: # (#### Get URL to display analysis results)

[//]: # (Analysis results can be displayed in GUI even to users that do not possess the API key. To generate a temporary)

[//]: # (link, do the following:)

[//]: # (```javascript)

[//]: # (const submissionId = await client.submit&#40;file&#41;;)

[//]: # ()
[//]: # (// generate URL showing analysis results with 5 minute validity )

[//]: # (const presignResponse = await client.presign&#40;submissionId, 5&#41;;)

[//]: # ()
[//]: # (console.log&#40;presignResponse.presigned_url&#41;)

[//]: # (```)