@enface/js
Version:
Biometric researches
152 lines (147 loc) • 4.22 kB
JavaScript
import * as m from './schema/mutations';
import * as s from './schema/subscriptions';
import 'cross-fetch/polyfill';
import * as utils from './utils';
import * as constants from './constants';
export class EnfaceApi {
constructor({
client,
}) {
this.client = client;
}
recognize({
images,
groupId,
}) {
return new Promise(async (resolve, reject) => {
try {
images = await utils.checkImages(images, 1, constants.MIN_IMAGE_SIZE);
} catch (error) {
reject(error);
}
let actionId;
const { client } = this;
client.subscribe({
query: s.RECOGNIZE_UPDATED,
variables: {
groupId,
},
}).subscribe({
next(data) {
const { data: { faceUpdated: { action } } } = data;
if (actionId !== action.sessionId) {
return reject(new Error(`different action sessionId ${action.sessionId}`));
}
client.wsLink.subscriptionClient.close();
resolve(utils.filterObject(action, '__typename'));
},
error(error) {
client.wsLink.subscriptionClient.close();
reject(error);
},
});
try {
const { data: { goFace: { sessionId } } } = await client.mutate({
mutation: m.RECOGNIZE,
variables: {
apiKey: client.apiKey,
files: utils.nameImagesByIndex(images),
},
});
actionId = sessionId;
} catch (error) {
reject(error);
}
});
}
liveness({
images,
groupId,
}) {
return new Promise(async (resolve, reject) => {
try {
images = await utils.checkImages(images, 3, constants.MIN_IMAGE_SIZE);
} catch (error) {
reject(error);
}
let actionId;
const { client } = this;
client.subscribe({
query: s.LIVENESS_UPDATED,
variables: {
groupId,
},
}).subscribe({
next(data) {
const { data: { livenessUpdated: { action } } } = data;
if (actionId !== action.sessionId) {
return reject(new Error(`different action sessionId ${action.sessionId}`));
}
client.wsLink.subscriptionClient.close();
resolve(utils.filterObject(action, '__typename'));
},
error(error) {
client.wsLink.subscriptionClient.close();
reject(error);
},
});
try {
const { data: { goLiveness: { sessionId } } } = await client.mutate({
mutation: m.LIVENESS,
variables: {
apiKey: client.apiKey,
files: utils.nameImagesByIndex(images),
},
});
actionId = sessionId;
} catch (error) {
reject(error);
}
});
}
recognizeLiveness({
images,
groupId,
}) {
return new Promise(async (resolve, reject) => {
try {
images = await utils.checkImages(images, 3, constants.MIN_IMAGE_SIZE);
} catch (error) {
reject(error);
}
let actionId;
const { client } = this;
client.subscribe({
query: s.REC_LIVE_UPDATED,
variables: {
groupId,
},
}).subscribe({
next(data) {
const { data: { liveFaceUpdated: { action } } } = data;
if (actionId !== action.sessionId) {
return reject(new Error(`different action id ${action.sessionId}`));
}
client.wsLink.subscriptionClient.close();
resolve(utils.filterObject(action, '__typename'));
},
error(error) {
client.wsLink.subscriptionClient.close();
reject(error);
},
});
try {
const { data: { goLiveFace: { sessionId } } } = await client.mutate({
mutation: m.RECOGNIZE_LIVENESS,
variables: {
apiKey: client.apiKey,
files: utils.nameImagesByIndex(images),
},
});
actionId = sessionId;
} catch (error) {
reject(error);
}
});
}
}