UNPKG

@teerai/sdk

Version:

Official TypeScript SDK for Teer API - Track and manage AI model usage

2 lines 4.39 kB
var k="0.1.2";var l=class i extends Error{constructor(e){super(e),this.name="TeerError",Object.setPrototypeOf(this,i.prototype);}},g=class i extends l{constructor({message:e,status:t,statusText:r,data:c,headers:n,request:p}){super(`${t} ${r}: ${e}`),this.name="TeerAPIError",this.status=t,this.statusText=r,this.data=c,this.headers=n,this.request=p,Object.setPrototypeOf(this,i.prototype);}get isServerError(){return this.status>=500&&this.status<600}get isClientError(){return this.status>=400&&this.status<500}get isRateLimitError(){return this.status===429}},f=class i extends l{constructor({timeoutMs:e,request:t}){super(`Request timed out after ${e}ms`),this.name="TeerTimeoutError",this.timeoutMs=e,this.request=t,Object.setPrototypeOf(this,i.prototype);}},h=class i extends l{constructor({cause:e,request:t}){super(`Network error: ${e.message}`),this.name="TeerNetworkError",this.cause=e,this.request=t,Object.setPrototypeOf(this,i.prototype);}};var u=class{constructor(e,t="",r){this.client=e;this.basePath=t;this.resourceBaseUrl=r;}getPath(e=""){return e?this.basePath?`${this.basePath}/${e}`:e:this.basePath}async request(e,t){try{let{method:r,path:c,data:n}=e,p=this.getPath(c);return await this.client.makeRequest(p,r,n,this.resourceBaseUrl,t)}catch(r){throw r instanceof Error&&!(r instanceof l)&&(r.name==="TypeError"||r.name==="NetworkError"||r.message.includes("network"))?new h({cause:r,request:{method:e.method,path:this.getPath(e.path),baseUrl:this.resourceBaseUrl||this.client.baseURL}}):r}}};var y=class extends u{constructor(e){let t=e.defaultRequestConfig.trackURL;super(e,"ingest",t);}async send(e,t){return this.request({method:"POST",path:"",data:e},t)}};var R=class extends u{constructor(e){let t=e.defaultRequestConfig.trackURL;super(e,"billing/meter-events",t);}async create(e,t){return this.request({method:"POST",path:"",data:e},t)}},E=class extends u{constructor(e){let t=e.defaultRequestConfig.trackURL;super(e,"billing",t),this.meterEvents=new R(e);}};var _="https://api.teer.ai",K="https://track.teer.ai",L=1e4,S=3,I=300,d=class d{constructor(e,t={}){this._apiKey=e;let r=t.baseURL||_,c=t.trackURL||K;this._baseURL=r,this._trackURL=c,this._defaultRequestConfig={baseURL:r,trackURL:c,timeoutMs:t.timeoutMs||L,maxRetries:t.maxRetries||S,retryDelayMs:t.retryDelayMs||I,customFetch:t.customFetch},this._fetchImpl=t.customFetch??((n,p)=>fetch(n,p)),this.ingest=new y(this),this.billing=new E(this);}get apiKey(){return this._apiKey}get baseURL(){return this._baseURL}get trackURL(){return this._trackURL}get defaultRequestConfig(){return {...this._defaultRequestConfig}}async makeRequest(e,t,r,c,n={}){let p=n.timeoutMs??this._defaultRequestConfig.timeoutMs??L,B=n.maxRetries??this._defaultRequestConfig.maxRetries??S,D=n.retryDelayMs??this._defaultRequestConfig.retryDelayMs??I,C=c||this._baseURL,F=`${C}/${d.namespace}/${e}`,M={"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"User-Agent":`@teerai/sdk/${d.sdkVersion}`},U={method:t,path:e,baseUrl:C,headers:M},b=null;for(let q=0;q<=B;q++)try{if(q>0){let s=D*Math.pow(2,q-1)*(.5+Math.random()*.5);await new Promise(x=>setTimeout(x,s));}let a=new AbortController,O=setTimeout(()=>a.abort(),p),j={method:t,headers:M,body:r?JSON.stringify(r):void 0,signal:a.signal};try{let s=await this._fetchImpl(F,j);if(clearTimeout(O),!s.ok){let o,T="",w=s.headers.get("content-type");if(w&&w.includes("application/json"))try{o=await s.json(),T=o.message||o.error||JSON.stringify(o);}catch{o=await s.text(),T=o;}else o=await s.text(),T=o;let m=new g({message:T,status:s.status,statusText:s.statusText,data:o,headers:s.headers,request:U});if(m.isServerError||m.isRateLimitError){if(m.isRateLimitError){let v=s.headers.get("Retry-After");if(v){let P=parseInt(v,10);isNaN(P)||await new Promise(N=>setTimeout(N,P*1e3));}}b=m;continue}throw m}let x=s.headers.get("content-type");return x&&x.includes("application/json")?await s.json():null}catch(s){throw clearTimeout(O),s instanceof DOMException&&s.name==="AbortError"?new f({timeoutMs:p,request:U}):s}}catch(a){if(b=a,a instanceof f||a instanceof h||a instanceof g&&a.isServerError)continue;throw a}if(b)throw b;return null}};d.sdkVersion=k,d.namespace="v1",d.baseURL=_;var A=d; export{E as BillingResource,y as IngestResource,R as MeterEventsResource,u as Resource,A as Teer,g as TeerAPIError,l as TeerError,h as TeerNetworkError,f as TeerTimeoutError};