Spaces:
Running
Running
| window.Api = { | |
| _apiKey: '', | |
| setApiKey: function (key) { | |
| this._apiKey = key; | |
| sessionStorage.setItem('ocr_api_key', key); | |
| }, | |
| getApiKey: function () { | |
| return this._apiKey || sessionStorage.getItem('ocr_api_key') || ''; | |
| }, | |
| _headers: function () { | |
| var headers = {}; | |
| var key = this.getApiKey(); | |
| if (key) headers['X-API-Key'] = key; | |
| headers['X-Request-ID'] = crypto.randomUUID ? crypto.randomUUID() : Utils.generateId(); | |
| return headers; | |
| }, | |
| checkHealth: function () { | |
| return fetch('/health') | |
| .then(function (resp) { return resp.json(); }); | |
| }, | |
| warmUp: function () { | |
| return fetch('/warm') | |
| .then(function (resp) { return resp.json(); }); | |
| }, | |
| parseBarcode: function (text) { | |
| var headers = this._headers(); | |
| headers['Content-Type'] = 'application/json'; | |
| return fetch('/api/barcode/parse', { | |
| method: 'POST', | |
| headers: headers, | |
| body: JSON.stringify({ text: text }), | |
| }).then(function (resp) { | |
| return resp.json().then(function (data) { | |
| if (!resp.ok) { | |
| var err = new Error(data.error || 'Unknown error'); | |
| err.status = resp.status; | |
| throw err; | |
| } | |
| return data; | |
| }); | |
| }); | |
| }, | |
| scanSerial: function (imageFile) { | |
| var formData = new FormData(); | |
| formData.append('image', imageFile); | |
| var controller = new AbortController(); | |
| var timeoutId = setTimeout(function () { controller.abort(); }, 120000); | |
| return fetch('/api/serial', { | |
| method: 'POST', | |
| headers: this._headers(), | |
| body: formData, | |
| signal: controller.signal, | |
| }).then(function (resp) { | |
| clearTimeout(timeoutId); | |
| return resp.json().then(function (data) { | |
| if (!resp.ok) { | |
| var err = new Error(data.error || 'Unknown error'); | |
| err.status = resp.status; | |
| throw err; | |
| } | |
| return data; | |
| }); | |
| }).catch(function (err) { | |
| clearTimeout(timeoutId); | |
| if (err.name === 'AbortError') { | |
| var timeoutErr = new Error('Request timed out after 120 seconds'); | |
| timeoutErr.status = 0; | |
| throw timeoutErr; | |
| } | |
| throw err; | |
| }); | |
| }, | |
| processDocument: function (frontFile, docType, backFile) { | |
| var formData = new FormData(); | |
| formData.append('front', frontFile); | |
| formData.append('doc_type', docType); | |
| if (backFile) { | |
| formData.append('back', backFile); | |
| } | |
| var controller = new AbortController(); | |
| var timeoutId = setTimeout(function () { controller.abort(); }, 120000); | |
| return fetch('/api/ocr', { | |
| method: 'POST', | |
| headers: this._headers(), | |
| body: formData, | |
| signal: controller.signal, | |
| }).then(function (resp) { | |
| clearTimeout(timeoutId); | |
| return resp.json().then(function (data) { | |
| if (!resp.ok) { | |
| var err = new Error(data.error || 'Unknown error'); | |
| err.status = resp.status; | |
| throw err; | |
| } | |
| return data; | |
| }); | |
| }).catch(function (err) { | |
| clearTimeout(timeoutId); | |
| if (err.name === 'AbortError') { | |
| var timeoutErr = new Error('Request timed out after 120 seconds'); | |
| timeoutErr.status = 0; | |
| throw timeoutErr; | |
| } | |
| throw err; | |
| }); | |
| }, | |
| }; | |