| 1 | import { Response, Method, RequestBody } from "./interface/Client"; |
| 2 | |
| 3 | export default class Client { |
| 4 | constructor( |
| 5 | apiKey: string, |
| 6 | dev = false, |
| 7 | isBrowser = typeof window !== "undefined" |
| 8 | ) { |
| 9 | this.#isBrowser = isBrowser; |
| 10 | this.host = `http${ |
| 11 | dev ? "://localhost:8080" : "s://api.bytez.com" |
| 12 | }/models/v2/`; |
| 13 | this.headers = { |
| 14 | lang: "javascript", |
| 15 | Authorization: `Key ${apiKey}`, |
| 16 | "content-type": "application/json" |
| 17 | }; |
| 18 | |
| 19 | if (isBrowser === false) { |
| 20 | import("stream").then(module => { |
| 21 | this.#Readable = module.Readable ?? module.default?.Readable; |
| 22 | }); |
| 23 | // make undici invisible to webpack's static analysis |
| 24 | new Function('return import("undici")')().then(({ Agent }) => { |
| 25 | this.#dispatcher = new Agent({ |
| 26 | keepAliveTimeout: this.#timeout, |
| 27 | keepAliveMaxTimeout: this.#timeout, |
| 28 | connectTimeout: this.#timeout, |
| 29 | headersTimeout: this.#timeout, |
| 30 | bodyTimeout: this.#timeout |
| 31 | }); |
| 32 | }); |
| 33 | } |
| 34 | } |
| 35 | #Readable: any; |
| 36 | #isBrowser: boolean; |
| 37 | #dispatcher?: any; |
| 38 | #timeout = 15 * 60e3; |
| 39 | host = ""; |
| 40 | headers = {}; |
| 41 | async request( |
| 42 | path: string, |
| 43 | method?: Method, |
| 44 | body?: RequestBody, |
| 45 | providerKey?: string |
| 46 | ) { |
| 47 | try { |
| 48 | const res = await fetch(this.host + path, { |
| 49 | method, |
| 50 | headers: |
| 51 | providerKey === undefined |
| 52 | ? this.headers |
| 53 | : { ...this.headers, ["provider-key"]: providerKey }, |
| 54 | // @ts-expect-error dispatcher is undici-only |
| 55 | dispatcher: this.#dispatcher, |
| 56 | signal: AbortSignal.timeout(this.#timeout), |
| 57 | body: body ? JSON.stringify(body) : undefined |
| 58 | }); |
| 59 | |
| 60 | if ( |
nothing calls this directly
no outgoing calls
no test coverage detected