Skip to content

Commit

Permalink
Adding Autofill endpoint. (#21)
Browse files Browse the repository at this point in the history
* Adding Autofill endpoint.

* MR comments adjustments.

* Tests for autofill endpoint.

* Fixes after MR.

* Bumped version, changed test data slightly.
  • Loading branch information
kulak-at authored Jul 15, 2020
1 parent 11e088d commit 12612db
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onfido/api",
"version": "1.3.0",
"version": "1.4.0",
"description": "Node.js library for the Onfido API",
"keywords": [
"onfido",
Expand Down
3 changes: 3 additions & 0 deletions src/Onfido.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios, { AxiosInstance } from "axios";
import { Addresses } from "./resources/Addresses";
import { Applicants } from "./resources/Applicants";
import { Autofill } from "./resources/Autofill";
import { Checks } from "./resources/Checks";
import { Documents } from "./resources/Documents";
import { LivePhotos } from "./resources/LivePhotos";
Expand Down Expand Up @@ -41,6 +42,7 @@ export class Onfido {
public readonly address: Addresses;
public readonly webhook: Webhooks;
public readonly sdkToken: SdkTokens;
public readonly autofill: Autofill;

constructor({
apiToken,
Expand Down Expand Up @@ -77,5 +79,6 @@ export class Onfido {
this.address = new Addresses(this.axiosInstance);
this.webhook = new Webhooks(this.axiosInstance);
this.sdkToken = new SdkTokens(this.axiosInstance);
this.autofill = new Autofill(this.axiosInstance);
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export { Check, CheckRequest } from "./resources/Checks";
export { Report } from "./resources/Reports";
export { Webhook, WebhookRequest } from "./resources/Webhooks";
export { SdkTokenRequest } from "./resources/SdkTokens";
export { Autofill, ExtractionResult } from "./resources/Autofill";
47 changes: 47 additions & 0 deletions src/resources/Autofill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { AxiosInstance } from "axios";
import { Method, Resource } from "../Resource";

export type ExtractionResult = {
documentId: string;
documentClassification: {
issuingCountry: string;
documentType: string;
issuingState?: string;
};
extractedData: {
documentNumber?: string;
firstName?: string;
lastName?: string;
middleName?: string;
fullName?: string;
gender?: string;
dateOfBirth?: string;
dateOfExpiry?: string;
nationality?: string;
mrzLine1?: string;
mrzLine2?: string;
mrzLine3?: string;
addressLine1?: string;
addressLine2?: string;
addressLine3?: string;
addressLine4?: string;
addressLine5?: string;
};
};

type AutofillResource = {
documentId: string;
};

export class Autofill extends Resource<AutofillResource> {
constructor(axiosInstance: AxiosInstance) {
super("extractions", axiosInstance);
}

public async perform(documentId: string): Promise<ExtractionResult> {
return this.request({
method: Method.POST,
body: { documentId }
});
}
}
54 changes: 54 additions & 0 deletions test/resources/Autofill.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import nock from "nock";
import { Onfido } from "onfido-node";

const onfido = new Onfido({ apiToken: "api_token" });

// Fake data, taken from documentation.
const exampleAutofillJson = {
document_id: "21345-xxx",
document_classification: {
issuing_country: "FRA",
document_type: "national_identity_card"
},
extracted_data: {
date_of_birth: "1990-07-21",
date_of_expiry: "2025-07-07",
document_number: "200407512345",
first_name: "AMANDINE CHANTAL",
gender: "Female",
last_name: "MAVARINE",
mrz_line1: "IDFRAMAVARINE<<<<<<<<<<<<<<<<<075123",
mrz_line2: "2000000000000AMANDINE<CHANT9007219F5",
nationality: "FRA"
}
};

const exampleAutofillResult = {
documentId: "21345-xxx",
documentClassification: {
issuingCountry: "FRA",
documentType: "national_identity_card"
},
extractedData: {
dateOfBirth: "1990-07-21",
dateOfExpiry: "2025-07-07",
documentNumber: "200407512345",
firstName: "AMANDINE CHANTAL",
gender: "Female",
lastName: "MAVARINE",
mrzLine1: "IDFRAMAVARINE<<<<<<<<<<<<<<<<<075123",
mrzLine2: "2000000000000AMANDINE<CHANT9007219F5",
nationality: "FRA"
}
};

it("performs autofill", async () => {
nock("https://api.onfido.com/v3")
.post("/extractions/", {
document_id: "21345-xxx"
})
.reply(201, exampleAutofillJson);

const result = await onfido.autofill.perform("21345-xxx");
expect(result).toEqual(exampleAutofillResult);
});

0 comments on commit 12612db

Please sign in to comment.