@microblink/blinkid-imagecapture-in-browser-sdk
Version:
A smart image capturing library for WebAssembly-enabled browsers.
10 lines (8 loc) • 5.27 kB
JavaScript
/*! ****************************************************************************
Copyright (c) Microblink. All rights reserved.
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
***************************************************************************** */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Client={})}(this,(function(e){"use strict";var t;e.ApiType=void 0,(t=e.ApiType||(e.ApiType={})).Cloud="cloud",t.SelfHosted="self-hosted";const s={GENERIC_ERROR:"There was an error during scan action.",UNKNOWN_ERROR:"Oops, something went wrong."};function o(e,...t){const s=function(e,t,s){e=e||"",t=t||"",s=s||"";for(;e.indexOf(t)>-1;)e=e.replace(t,s);return e}([...t].join("/"),"//","/");return(e.endsWith("/")?e.slice(0,-1):e)+(s.startsWith("/")?s:"/"+s)}function i(e,t,s,o){return new Promise(((i,n)=>{const a={method:e,headers:s};o&&(a.body=JSON.stringify(o)),window.fetch(t,a).then((e=>{const t=e.headers.entries(),s={};for(const e of t)s[e[0]]=e[1];const o={httpStatus:e.status,headers:s};e.text().then((e=>{try{const t=JSON.parse(e);o.data=t}catch(t){o.data=e}i(o)})).catch((e=>{o.data=e,n(o)}))})).catch((e=>{n({httpStatus:-1,headers:{},data:e})}))}))}const n={API_ROLE_IS_NOT_FOUND:"API role is not found",AUTHORIZATION_HEADER_IS_NOT_VALID:"Authorization header is not valid",BAD_REQUEST:"Bad request",FORBIDDEN_RECOGNIZER:"Forbidden recognizer",IMAGE_IS_NOT_ABLE_TO_CONVERT_TO_RAW_PIXELS:"Image is not able to convert to raw pixels",IMAGE_IS_NOT_VALID:"Image is not valid",IMAGE_IS_NOT_VALID_BASE64_STRING:"Image is not valid base64 string",IMAGE_SIZE_IS_TOO_BIG:"Image size is too big",INSUFFICIENT_BALANCE:"Insufficient balance",INTERNAL_SERVER_ERROR:"Internal server error",INVALID_LICENSE_KEY:"Invalid license key",NOT_ALLOWED_TO_EXECUTE_REQUESTED_RECOGNIZER:"Not allowed to execute requested recognizer",SERVER_CANCELED_REQUEST:"Server canceled request",SERVER_TOO_BUSY:"Server too busy"},a={Accept:"application/json","Content-Type":"application/json"};class r{constructor(e,t,s){this.apiLocation=e,this.healthcheckEndpoint=t,this.headers=Object.assign(a,s);for(const e in this.headers)e&&this.headers[e]||delete this.headers[e]}getHealthcheckResponse(){return i("GET",o(this.apiLocation,this.healthcheckEndpoint),this.headers)}recognize(e,t,s="POST"){return i(s,o(this.apiLocation,e),this.headers,t)}}const c={API_ROLE_IS_NOT_FOUND:"API role is not found",AUTHORIZATION_HEADER_IS_NOT_VALID:"Authorization header is not valid",BAD_REQUEST:"Bad request",FORBIDDEN_RECOGNIZER:"Forbidden recognizer",IMAGE_IS_NOT_ABLE_TO_CONVERT_TO_RAW_PIXELS:"Image is not able to convert to raw pixels",IMAGE_IS_NOT_VALID:"Image is not valid",IMAGE_IS_NOT_VALID_BASE64_STRING:"Image is not valid base64 string",IMAGE_SIZE_IS_TOO_BIG:"Image size is too big",INTERNAL_SERVER_ERROR:"Internal server error",INVALID_LICENSE_KEY:"Invalid license key",NOT_ALLOWED_TO_EXECUTE_REQUESTED_RECOGNIZER:"Not allowed to execute requested recognizer",SERVER_CANCELED_REQUEST:"Server canceled request",SERVER_TOO_BUSY:"Server too busy"},h={Accept:"application/json","Content-Type":"application/json"};class E{constructor(e,t,s){this.apiLocation=e,this.healthcheckEndpoint=t,this.headers=Object.assign(h,s);for(const e in this.headers)e&&this.headers[e]||delete this.headers[e]}getHealthcheckResponse(){return i("GET",o(this.apiLocation,this.healthcheckEndpoint),this.headers)}recognize(e,t,s="POST"){return i(s,o(this.apiLocation,e),this.headers,t)}}e.Client=class{constructor(t,o){if(!t)throw"API type is not provided!";const i=(null==o?void 0:o.headers)||{},a=(null==o?void 0:o.healthcheckEndpoint)||"/v2/hc",h=(null==o?void 0:o.apiLocation)||"";let d={};switch(t){case e.ApiType.Cloud:d=n,this.service=new r(h||"http://localhost",a,i);break;case e.ApiType.SelfHosted:d=c,this.service=new E(h||"http://localhost",a,i);break;default:throw"Unsupported API type!"}this.errorMessages=Object.assign(s,d,(null==o?void 0:o.messages)||{})}getHealthcheck(){return new Promise(((e,t)=>{this.service.getHealthcheckResponse().then((s=>{200!==s.httpStatus?t({status:!1,response:s}):e({status:!0})})).catch((e=>{t({status:!1,response:e})}))}))}recognize(e,t){return new Promise(((s,o)=>{this.service.recognize(e,t).then((e=>{if(200===e.httpStatus)return void s({status:!0,response:e});const t=e.data.code||"GENERIC_ERROR";o({status:!1,response:e,error:{code:t,message:this.errorMessages[t]}})})).catch((e=>{let t="UNKNOWN_ERROR";(-1===e.httpStatus||100>e.httpStatus%400)&&(t="GENERIC_ERROR");o({status:!1,response:e,error:{code:t,message:this.errorMessages[t]}})}))}))}imageDataToBase64(e){if(!e)throw"Image data is not provided!";if(!e.width||!e.height)throw"Image data is malformed!";return function(e){const t=document.createElement("canvas");t.width=e.width,t.height=e.height;const s=t.getContext("2d");return null==s||s.putImageData(e,0,0),t.toDataURL()}(e)}},e.GenericMessages=s,Object.defineProperty(e,"__esModule",{value:!0})}));