diff --git a/module/genericsUtils.ts b/genericsUtils.ts similarity index 56% rename from module/genericsUtils.ts rename to genericsUtils.ts index bb384fe..d638929 100644 --- a/module/genericsUtils.ts +++ b/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,25 +60,53 @@ 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 BananaError(`Could not parse response from server: ${text}`, ) + } - if (jsonOut.message.toLowerCase().includes("error")){ - throw jsonOut.message + if (!response.ok) { + if (jsonOut.response) { + throw new BananaError(`${response.status}: server error: status code ${jsonOut.response.status}`, jsonOut) + } else if (jsonOut.request) { + throw new BananaError( `${response.status}: server error: endpoint busy or not available.`, jsonOut) + } else { + console.log(jsonOut) + throw new BananaError(`${response.status}: misc error. Please email erik@banana.dev with above error`, jsonOut) + } } + if (jsonOut.message.toLowerCase().includes("error")){ + throw new BananaError(jsonOut.message, jsonOut) + } return jsonOut } +export class BananaError extends Error { + json: Object + constructor(message: string, json: any = null) { + super(message) + this.name = "BananaError" + this.json = json + } +} + const checkAPI = async (apiKey: string, callID: string): Promise => { const urlCheck = endpoint.concat("check/v4/") @@ -88,21 +117,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/index.ts similarity index 59% rename from module/index.ts rename to index.ts index 259cf19..05ae694 100644 --- a/module/index.ts +++ b/index.ts @@ -1,7 +1,9 @@ -import genericsUtils = require("./genericsUtils") +import {checkMain, runMain, startMain, BananaError} from "./genericsUtils" -export async function run(apiKey: string, modelKey: string, modelInputs: object = {}): Promise{ - const out = await genericsUtils.runMain( +type Object = { [key: string]: any } + +export async function run(apiKey: string, modelKey: string, modelInputs: object = {}): Promise{ + const out = await runMain( apiKey = apiKey, modelKey = modelKey, modelInputs=modelInputs) @@ -9,14 +11,16 @@ 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) return callID } -export async function check(apiKey: string, callID: string): Promise{ - const jsonOut = await genericsUtils.checkMain(apiKey, callID) +export async function check(apiKey: string, callID: string): Promise{ + const jsonOut = await checkMain(apiKey, callID) return jsonOut -} \ No newline at end of file +} + +export { BananaError } \ No newline at end of file diff --git a/module/package-lock.json b/module/package-lock.json deleted file mode 100644 index fa5d7b8..0000000 --- a/module/package-lock.json +++ /dev/null @@ -1,191 +0,0 @@ -{ - "name": "@banana-dev/banana-dev", - "version": "3.0.0", - "lockfileVersion": 2, - "requires": true, - "packages": { - "": { - "name": "@banana-dev/banana-dev", - "version": "3.0.0", - "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" - }, - "devDependencies": { - "@types/node": "^17.0.13" - } - }, - "node_modules/@types/node": { - "version": "17.0.23", - "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.23.tgz", - "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/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/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/tr46": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=" - }, - "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", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", - "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": { - "@types/node": { - "version": "17.0.23", - "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.23.tgz", - "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" - } - }, - "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" - } - }, - "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" - } - }, - "tr46": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=" - }, - "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" - } - } - } -} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..a12c18d --- /dev/null +++ b/package-lock.json @@ -0,0 +1,118 @@ +{ + "name": "@banana-dev/banana-dev", + "version": "4.1.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "@banana-dev/banana-dev", + "version": "4.1.0", + "license": "ISC", + "dependencies": { + "native-fetch": "^4.0.2", + "uuid": "^8.3.2" + }, + "devDependencies": { + "@types/node": "^17.0.13" + } + }, + "node_modules/@types/node": { + "version": "17.0.23", + "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.23.tgz", + "integrity": "sha512-UxDxWn7dl97rKVeVS61vErvw086aCYhDLyvRQZ5Rk65rZKepaFdm53GeqXaKBuOhED4e9uWq34IC3TdSdJJ2Gw==", + "dev": true + }, + "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/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/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/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/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "bin": { + "uuid": "dist/bin/uuid" + } + } + }, + "dependencies": { + "@types/node": { + "version": "17.0.23", + "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.23.tgz", + "integrity": "sha512-UxDxWn7dl97rKVeVS61vErvw086aCYhDLyvRQZ5Rk65rZKepaFdm53GeqXaKBuOhED4e9uWq34IC3TdSdJJ2Gw==", + "dev": true + }, + "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" + } + }, + "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": {} + }, + "streamsearch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", + "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==", + "peer": true + }, + "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" + } + }, + "uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==" + } + } +} diff --git a/module/package.json b/package.json similarity index 73% rename from module/package.json rename to package.json index d694be4..0087960 100644 --- a/module/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", @@ -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" diff --git a/module/tsconfig.json b/tsconfig.json similarity index 100% rename from module/tsconfig.json rename to tsconfig.json