From 6771726d356b84b7c63414d7198bbcb8ddf113cb Mon Sep 17 00:00:00 2001 From: remorses Date: Thu, 20 Apr 2023 17:26:21 +0200 Subject: [PATCH 1/7] removed unused dependencies, use Object, throw errors --- module/genericsUtils.ts | 73 ++++++++++++++++++++++---------------- module/index.ts | 6 ++-- module/package-lock.json | 76 ++++++++++++++++++++++++++++++++++++++-- module/package.json | 7 ++-- 4 files changed, 123 insertions(+), 39 deletions(-) diff --git a/module/genericsUtils.ts b/module/genericsUtils.ts index bb384fe..c13f2ac 100644 --- a/module/genericsUtils.ts +++ b/module/genericsUtils.ts @@ -1,4 +1,4 @@ -import axios, {AxiosError} from 'axios' +import { fetch } from 'native-fetch' const { v4: uuidv4 } = require('uuid') let endpoint: string = 'https://api.banana.dev/' @@ -11,8 +11,9 @@ if ("BANANA_URL" in process.env){ } } +type Object = { [key: string]: any } -export async function runMain(apiKey: string, modelKey: string, modelInputs: object = {}): Promise{ +export async function runMain(apiKey: string, modelKey: string, modelInputs: Object = {}): Promise{ const startOut = await startAPI(apiKey, modelKey, modelInputs) if (startOut["finished"] == true){ const res = { @@ -37,18 +38,18 @@ export async function runMain(apiKey: string, modelKey: string, modelInputs: obj } -export async function startMain(apiKey: string, modelKey: string, modelInputs: object = {}): Promise{ +export async function startMain(apiKey: string, modelKey: string, modelInputs: Object = {}): Promise{ const jsonOut = await startAPI(apiKey, modelKey, modelInputs, true) return jsonOut["callID"] } -export async function checkMain(apiKey: string, callID: string): Promise{ +export async function checkMain(apiKey: string, callID: string): Promise{ const jsonOut = await checkAPI(apiKey, callID) return jsonOut } -const startAPI = async (apiKey: string, modelKey: string, modelInputs: object, startOnly: boolean = false): Promise => { +const startAPI = async (apiKey: string, modelKey: string, modelInputs: Object, startOnly: boolean = false): Promise => { const urlStart = endpoint.concat("start/v4/") const payload = { "id": uuidv4(), @@ -59,22 +60,41 @@ const startAPI = async (apiKey: string, modelKey: string, modelInputs: object, s "startOnly": startOnly, } - const response = await axios.post(urlStart, payload).catch(err => { - if (err.response) { - throw `server error: status code ${err.response.status}` - } else if (err.request) { - throw 'server error: endpoint busy or not available.' - } else { - console.log(err) - throw "Misc axios error. Please email erik@banana.dev with above error" + const response = await fetch(urlStart, { + method: 'POST', + body: JSON.stringify(payload), + headers: { + 'Content-Type': 'application/json' } }) - const jsonOut = response.data + const jsonOut = await getBananaJsonOutput(response) + + return jsonOut +} + +async function getBananaJsonOutput(response: Response) { + const text = await response.text() + let jsonOut: any = null + try { + jsonOut = JSON.parse(text) + } catch { + throw new Error(`Could not parse response from server: ${text}`) + } - if (jsonOut.message.toLowerCase().includes("error")){ - throw jsonOut.message + if (!response.ok) { + if (jsonOut.response) { + throw new Error(`${response.status}: server error: status code ${jsonOut.response.status}`) + } else if (jsonOut.request) { + throw new Error( `${response.status}: server error: endpoint busy or not available.`) + } else { + console.log(jsonOut) + throw new Error("${response.status}: misc error. Please email erik@banana.dev with above error") + } } + if (jsonOut.message.toLowerCase().includes("error")){ + throw new Error(jsonOut.message) + } return jsonOut } @@ -88,21 +108,14 @@ const checkAPI = async (apiKey: string, callID: string): Promise => { "apiKey" : apiKey, "callID" : callID } - - const response = await axios.post(urlCheck, payload).catch(err => { - if (err.response) { - throw `server error: status code ${err.response.status}` - } else if (err.request) { - throw 'server error: endpoint busy or not available.' - } else { - console.log(err) - throw "Misc axios error. Please email erik@banana.dev with above error" + const response = await fetch(urlCheck, { + method: 'POST', + body: JSON.stringify(payload), + headers: { + 'Content-Type': 'application/json' } }) - const jsonOut = response.data - - if (jsonOut.message.toLowerCase().includes("error")){ - throw jsonOut.message - } + + const jsonOut = await getBananaJsonOutput(response) return jsonOut } diff --git a/module/index.ts b/module/index.ts index 259cf19..89af7a3 100644 --- a/module/index.ts +++ b/module/index.ts @@ -1,6 +1,8 @@ import genericsUtils = require("./genericsUtils") -export async function run(apiKey: string, modelKey: string, modelInputs: object = {}): Promise{ +type Object = { [key: string]: any } + +export async function run(apiKey: string, modelKey: string, modelInputs: object = {}): Promise{ const out = await genericsUtils.runMain( apiKey = apiKey, modelKey = modelKey, @@ -16,7 +18,7 @@ export async function start(apiKey: string, modelKey: string, modelInputs: objec return callID } -export async function check(apiKey: string, callID: string): Promise{ +export async function check(apiKey: string, callID: string): Promise{ const jsonOut = await genericsUtils.checkMain(apiKey, callID) return jsonOut } \ No newline at end of file diff --git a/module/package-lock.json b/module/package-lock.json index fa5d7b8..07cf94f 100644 --- a/module/package-lock.json +++ b/module/package-lock.json @@ -1,16 +1,17 @@ { "name": "@banana-dev/banana-dev", - "version": "3.0.0", + "version": "4.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@banana-dev/banana-dev", - "version": "3.0.0", + "version": "4.0.0", "license": "ISC", "dependencies": { "axios": "^0.21.0", "image-to-base64": "^2.1.1", + "native-fetch": "^4.0.2", "utf8": "^3.0.0", "uuid": "^8.3.2", "valid-url": "^1.0.9" @@ -33,6 +34,18 @@ "follow-redirects": "^1.14.0" } }, + "node_modules/busboy": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", + "integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==", + "peer": true, + "dependencies": { + "streamsearch": "^1.1.0" + }, + "engines": { + "node": ">=10.16.0" + } + }, "node_modules/follow-redirects": { "version": "1.14.9", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz", @@ -60,6 +73,14 @@ "node-fetch": "^2.6.0" } }, + "node_modules/native-fetch": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/native-fetch/-/native-fetch-4.0.2.tgz", + "integrity": "sha512-4QcVlKFtv2EYVS5MBgsGX5+NWKtbDbIECdUXDBGDMAZXq3Jkv9zf+y8iS7Ub8fEdga3GpYeazp9gauNqXHJOCg==", + "peerDependencies": { + "undici": "*" + } + }, "node_modules/node-fetch": { "version": "2.6.7", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", @@ -79,11 +100,32 @@ } } }, + "node_modules/streamsearch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", + "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==", + "peer": true, + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/tr46": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", "integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=" }, + "node_modules/undici": { + "version": "5.22.0", + "resolved": "https://registry.npmjs.org/undici/-/undici-5.22.0.tgz", + "integrity": "sha512-fR9RXCc+6Dxav4P9VV/sp5w3eFiSdOjJYsbtWfd4s5L5C4ogyuVpdKIVHeW0vV1MloM65/f7W45nR9ZxwVdyiA==", + "peer": true, + "dependencies": { + "busboy": "^1.6.0" + }, + "engines": { + "node": ">=14.0" + } + }, "node_modules/utf8": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/utf8/-/utf8-3.0.0.tgz", @@ -132,6 +174,15 @@ "follow-redirects": "^1.14.0" } }, + "busboy": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", + "integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==", + "peer": true, + "requires": { + "streamsearch": "^1.1.0" + } + }, "follow-redirects": { "version": "1.14.9", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz", @@ -145,6 +196,12 @@ "node-fetch": "^2.6.0" } }, + "native-fetch": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/native-fetch/-/native-fetch-4.0.2.tgz", + "integrity": "sha512-4QcVlKFtv2EYVS5MBgsGX5+NWKtbDbIECdUXDBGDMAZXq3Jkv9zf+y8iS7Ub8fEdga3GpYeazp9gauNqXHJOCg==", + "requires": {} + }, "node-fetch": { "version": "2.6.7", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", @@ -153,11 +210,26 @@ "whatwg-url": "^5.0.0" } }, + "streamsearch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", + "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==", + "peer": true + }, "tr46": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", "integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=" }, + "undici": { + "version": "5.22.0", + "resolved": "https://registry.npmjs.org/undici/-/undici-5.22.0.tgz", + "integrity": "sha512-fR9RXCc+6Dxav4P9VV/sp5w3eFiSdOjJYsbtWfd4s5L5C4ogyuVpdKIVHeW0vV1MloM65/f7W45nR9ZxwVdyiA==", + "peer": true, + "requires": { + "busboy": "^1.6.0" + } + }, "utf8": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/utf8/-/utf8-3.0.0.tgz", diff --git a/module/package.json b/module/package.json index d694be4..5086b22 100644 --- a/module/package.json +++ b/module/package.json @@ -10,11 +10,8 @@ "author": "Banana", "license": "ISC", "dependencies": { - "axios": "^0.21.0", - "image-to-base64": "^2.1.1", - "utf8": "^3.0.0", - "uuid": "^8.3.2", - "valid-url": "^1.0.9" + "native-fetch": "^4.0.2", + "uuid": "^8.3.2" }, "devDependencies": { "@types/node": "^17.0.13" From 2a0312bf4b48e0ec9d1b6ee5a4ce13bd8cf60177 Mon Sep 17 00:00:00 2001 From: remorses Date: Thu, 20 Apr 2023 17:27:27 +0200 Subject: [PATCH 2/7] removed module folder --- module/genericsUtils.ts => genericsUtils.ts | 0 module/index.ts => index.ts | 0 module/package-lock.json => package-lock.json | 0 module/package.json => package.json | 0 module/tsconfig.json => tsconfig.json | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename module/genericsUtils.ts => genericsUtils.ts (100%) rename module/index.ts => index.ts (100%) rename module/package-lock.json => package-lock.json (100%) rename module/package.json => package.json (100%) rename module/tsconfig.json => tsconfig.json (100%) diff --git a/module/genericsUtils.ts b/genericsUtils.ts similarity index 100% rename from module/genericsUtils.ts rename to genericsUtils.ts diff --git a/module/index.ts b/index.ts similarity index 100% rename from module/index.ts rename to index.ts diff --git a/module/package-lock.json b/package-lock.json similarity index 100% rename from module/package-lock.json rename to package-lock.json diff --git a/module/package.json b/package.json similarity index 100% rename from module/package.json rename to package.json diff --git a/module/tsconfig.json b/tsconfig.json similarity index 100% rename from module/tsconfig.json rename to tsconfig.json From 722a62494fea5810d2bb621c7f4d8f25be11ee33 Mon Sep 17 00:00:00 2001 From: remorses Date: Thu, 20 Apr 2023 17:27:54 +0200 Subject: [PATCH 3/7] increase version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5086b22..0087960 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@banana-dev/banana-dev", - "version": "4.0.0", + "version": "4.1.0", "description": "A typescript-friendly node client to interact with Banana's machine learning inference APIs", "main": "dist/index.js", "types": "dist/index.d.ts", From d30f02d42bb2cd19f278edb241a6edac561ab14c Mon Sep 17 00:00:00 2001 From: remorses Date: Thu, 20 Apr 2023 17:37:07 +0200 Subject: [PATCH 4/7] regenerated lock file --- index.ts | 2 +- package-lock.json | 151 +--------------------------------------------- 2 files changed, 4 insertions(+), 149 deletions(-) diff --git a/index.ts b/index.ts index 89af7a3..83900a7 100644 --- a/index.ts +++ b/index.ts @@ -21,4 +21,4 @@ export async function start(apiKey: string, modelKey: string, modelInputs: objec export async function check(apiKey: string, callID: string): Promise{ const jsonOut = await genericsUtils.checkMain(apiKey, callID) return jsonOut -} \ No newline at end of file +} diff --git a/package-lock.json b/package-lock.json index 07cf94f..a12c18d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,20 +1,16 @@ { "name": "@banana-dev/banana-dev", - "version": "4.0.0", + "version": "4.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@banana-dev/banana-dev", - "version": "4.0.0", + "version": "4.1.0", "license": "ISC", "dependencies": { - "axios": "^0.21.0", - "image-to-base64": "^2.1.1", "native-fetch": "^4.0.2", - "utf8": "^3.0.0", - "uuid": "^8.3.2", - "valid-url": "^1.0.9" + "uuid": "^8.3.2" }, "devDependencies": { "@types/node": "^17.0.13" @@ -26,14 +22,6 @@ "integrity": "sha512-UxDxWn7dl97rKVeVS61vErvw086aCYhDLyvRQZ5Rk65rZKepaFdm53GeqXaKBuOhED4e9uWq34IC3TdSdJJ2Gw==", "dev": true }, - "node_modules/axios": { - "version": "0.21.4", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz", - "integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==", - "dependencies": { - "follow-redirects": "^1.14.0" - } - }, "node_modules/busboy": { "version": "1.6.0", "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", @@ -46,33 +34,6 @@ "node": ">=10.16.0" } }, - "node_modules/follow-redirects": { - "version": "1.14.9", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz", - "integrity": "sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==", - "funding": [ - { - "type": "individual", - "url": "https://github.com/sponsors/RubenVerborgh" - } - ], - "engines": { - "node": ">=4.0" - }, - "peerDependenciesMeta": { - "debug": { - "optional": true - } - } - }, - "node_modules/image-to-base64": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/image-to-base64/-/image-to-base64-2.2.0.tgz", - "integrity": "sha512-Z+aMwm/91UOQqHhrz7Upre2ytKhWejZlWV/JxUTD1sT7GWWKFDJUEV5scVQKnkzSgPHFuQBUEWcanO+ma0PSVw==", - "dependencies": { - "node-fetch": "^2.6.0" - } - }, "node_modules/native-fetch": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/native-fetch/-/native-fetch-4.0.2.tgz", @@ -81,25 +42,6 @@ "undici": "*" } }, - "node_modules/node-fetch": { - "version": "2.6.7", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", - "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", - "dependencies": { - "whatwg-url": "^5.0.0" - }, - "engines": { - "node": "4.x || >=6.0.0" - }, - "peerDependencies": { - "encoding": "^0.1.0" - }, - "peerDependenciesMeta": { - "encoding": { - "optional": true - } - } - }, "node_modules/streamsearch": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", @@ -109,11 +51,6 @@ "node": ">=10.0.0" } }, - "node_modules/tr46": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=" - }, "node_modules/undici": { "version": "5.22.0", "resolved": "https://registry.npmjs.org/undici/-/undici-5.22.0.tgz", @@ -126,11 +63,6 @@ "node": ">=14.0" } }, - "node_modules/utf8": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/utf8/-/utf8-3.0.0.tgz", - "integrity": "sha512-E8VjFIQ/TyQgp+TZfS6l8yp/xWppSAHzidGiRrqe4bK4XP9pTRyKFgGJpO3SN7zdX4DeomTrwaseCHovfpFcqQ==" - }, "node_modules/uuid": { "version": "8.3.2", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", @@ -138,25 +70,6 @@ "bin": { "uuid": "dist/bin/uuid" } - }, - "node_modules/valid-url": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/valid-url/-/valid-url-1.0.9.tgz", - "integrity": "sha1-HBRHm0DxOXp1eC8RXkCGRHQzogA=" - }, - "node_modules/webidl-conversions": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", - "integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=" - }, - "node_modules/whatwg-url": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", - "integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=", - "dependencies": { - "tr46": "~0.0.3", - "webidl-conversions": "^3.0.0" - } } }, "dependencies": { @@ -166,14 +79,6 @@ "integrity": "sha512-UxDxWn7dl97rKVeVS61vErvw086aCYhDLyvRQZ5Rk65rZKepaFdm53GeqXaKBuOhED4e9uWq34IC3TdSdJJ2Gw==", "dev": true }, - "axios": { - "version": "0.21.4", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz", - "integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==", - "requires": { - "follow-redirects": "^1.14.0" - } - }, "busboy": { "version": "1.6.0", "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", @@ -183,44 +88,18 @@ "streamsearch": "^1.1.0" } }, - "follow-redirects": { - "version": "1.14.9", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz", - "integrity": "sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==" - }, - "image-to-base64": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/image-to-base64/-/image-to-base64-2.2.0.tgz", - "integrity": "sha512-Z+aMwm/91UOQqHhrz7Upre2ytKhWejZlWV/JxUTD1sT7GWWKFDJUEV5scVQKnkzSgPHFuQBUEWcanO+ma0PSVw==", - "requires": { - "node-fetch": "^2.6.0" - } - }, "native-fetch": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/native-fetch/-/native-fetch-4.0.2.tgz", "integrity": "sha512-4QcVlKFtv2EYVS5MBgsGX5+NWKtbDbIECdUXDBGDMAZXq3Jkv9zf+y8iS7Ub8fEdga3GpYeazp9gauNqXHJOCg==", "requires": {} }, - "node-fetch": { - "version": "2.6.7", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", - "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", - "requires": { - "whatwg-url": "^5.0.0" - } - }, "streamsearch": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==", "peer": true }, - "tr46": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=" - }, "undici": { "version": "5.22.0", "resolved": "https://registry.npmjs.org/undici/-/undici-5.22.0.tgz", @@ -230,34 +109,10 @@ "busboy": "^1.6.0" } }, - "utf8": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/utf8/-/utf8-3.0.0.tgz", - "integrity": "sha512-E8VjFIQ/TyQgp+TZfS6l8yp/xWppSAHzidGiRrqe4bK4XP9pTRyKFgGJpO3SN7zdX4DeomTrwaseCHovfpFcqQ==" - }, "uuid": { "version": "8.3.2", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==" - }, - "valid-url": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/valid-url/-/valid-url-1.0.9.tgz", - "integrity": "sha1-HBRHm0DxOXp1eC8RXkCGRHQzogA=" - }, - "webidl-conversions": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", - "integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=" - }, - "whatwg-url": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", - "integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=", - "requires": { - "tr46": "~0.0.3", - "webidl-conversions": "^3.0.0" - } } } } From e5a89a02a73a207ed8506b2b46e73cb1cb31df58 Mon Sep 17 00:00:00 2001 From: remorses Date: Thu, 20 Apr 2023 17:40:11 +0200 Subject: [PATCH 5/7] added BananaError --- genericsUtils.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/genericsUtils.ts b/genericsUtils.ts index c13f2ac..412079c 100644 --- a/genericsUtils.ts +++ b/genericsUtils.ts @@ -78,26 +78,35 @@ async function getBananaJsonOutput(response: Response) { try { jsonOut = JSON.parse(text) } catch { - throw new Error(`Could not parse response from server: ${text}`) + throw new BananaError(`Could not parse response from server: ${text}`, ) } if (!response.ok) { if (jsonOut.response) { - throw new Error(`${response.status}: server error: status code ${jsonOut.response.status}`) + throw new BananaError(`${response.status}: server error: status code ${jsonOut.response.status}`, jsonOut) } else if (jsonOut.request) { - throw new Error( `${response.status}: server error: endpoint busy or not available.`) + throw new BananaError( `${response.status}: server error: endpoint busy or not available.`, jsonOut) } else { console.log(jsonOut) - throw new Error("${response.status}: misc error. Please email erik@banana.dev with above error") + throw new BananaError(`${response.status}: misc error. Please email erik@banana.dev with above error`, jsonOut) } } if (jsonOut.message.toLowerCase().includes("error")){ - throw new Error(jsonOut.message) + throw new BananaError(jsonOut.message, jsonOut) } return jsonOut } +class BananaError extends Error { + jsonOutput: any + constructor(message: string, jsonOutput: any = null) { + super(message) + this.name = "BananaError" + this.jsonOutput = jsonOutput + } +} + const checkAPI = async (apiKey: string, callID: string): Promise => { const urlCheck = endpoint.concat("check/v4/") From d4353e35cf5bd33a8e43b3ad4028b111b78e5bf3 Mon Sep 17 00:00:00 2001 From: remorses Date: Thu, 20 Apr 2023 17:43:15 +0200 Subject: [PATCH 6/7] add banana error export --- genericsUtils.ts | 2 +- index.ts | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/genericsUtils.ts b/genericsUtils.ts index 412079c..6b39a9a 100644 --- a/genericsUtils.ts +++ b/genericsUtils.ts @@ -98,7 +98,7 @@ async function getBananaJsonOutput(response: Response) { return jsonOut } -class BananaError extends Error { +export class BananaError extends Error { jsonOutput: any constructor(message: string, jsonOutput: any = null) { super(message) diff --git a/index.ts b/index.ts index 83900a7..05ae694 100644 --- a/index.ts +++ b/index.ts @@ -1,9 +1,9 @@ -import genericsUtils = require("./genericsUtils") +import {checkMain, runMain, startMain, BananaError} from "./genericsUtils" type Object = { [key: string]: any } export async function run(apiKey: string, modelKey: string, modelInputs: object = {}): Promise{ - const out = await genericsUtils.runMain( + const out = await runMain( apiKey = apiKey, modelKey = modelKey, modelInputs=modelInputs) @@ -11,7 +11,7 @@ export async function run(apiKey: string, modelKey: string, modelInputs: object } export async function start(apiKey: string, modelKey: string, modelInputs: object = {}): Promise{ - const callID = await genericsUtils.startMain( + const callID = await startMain( apiKey = apiKey, modelKey = modelKey, modelInputs=modelInputs) @@ -19,6 +19,8 @@ export async function start(apiKey: string, modelKey: string, modelInputs: objec } export async function check(apiKey: string, callID: string): Promise{ - const jsonOut = await genericsUtils.checkMain(apiKey, callID) + const jsonOut = await checkMain(apiKey, callID) return jsonOut } + +export { BananaError } \ No newline at end of file From 2a1d532b99b140872f8f6b52eb7d7a2cdef4838c Mon Sep 17 00:00:00 2001 From: remorses Date: Thu, 20 Apr 2023 17:47:16 +0200 Subject: [PATCH 7/7] renamed json --- genericsUtils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/genericsUtils.ts b/genericsUtils.ts index 6b39a9a..d638929 100644 --- a/genericsUtils.ts +++ b/genericsUtils.ts @@ -99,11 +99,11 @@ async function getBananaJsonOutput(response: Response) { } export class BananaError extends Error { - jsonOutput: any - constructor(message: string, jsonOutput: any = null) { + json: Object + constructor(message: string, json: any = null) { super(message) this.name = "BananaError" - this.jsonOutput = jsonOutput + this.json = json } }