diff --git a/package.json b/package.json index 5201878..036edbb 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/Onfido.ts b/src/Onfido.ts index 56b4b9e..6ac6c52 100644 --- a/src/Onfido.ts +++ b/src/Onfido.ts @@ -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"; @@ -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, @@ -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); } } diff --git a/src/index.ts b/src/index.ts index 207d9e4..5683c14 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; diff --git a/src/resources/Autofill.ts b/src/resources/Autofill.ts new file mode 100644 index 0000000..3cf45aa --- /dev/null +++ b/src/resources/Autofill.ts @@ -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 { + constructor(axiosInstance: AxiosInstance) { + super("extractions", axiosInstance); + } + + public async perform(documentId: string): Promise { + return this.request({ + method: Method.POST, + body: { documentId } + }); + } +} diff --git a/test/resources/Autofill.test.ts b/test/resources/Autofill.test.ts new file mode 100644 index 0000000..e0e6ac9 --- /dev/null +++ b/test/resources/Autofill.test.ts @@ -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 { + 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); +});