@supadata/js
Version:
TypeScript / JavaScript SDK for Supadata API
3 lines • 4.28 kB
JavaScript
var r=class extends Error{error;details;documentationUrl;constructor(e){super(e.message||"An unexpected error occurred"),this.error=e.error||"internal-error",this.details=e.details||"An unexpected error occurred",this.documentationUrl=e.documentationUrl||"",this.name="SupadataError";}};var h={name:"@supadata/js",version:"1.1.3",description:"TypeScript / JavaScript SDK for Supadata API",homepage:"https://supadata.ai",repository:"https://github.com/supadata-ai/js",main:"./dist/index.cjs",module:"./dist/index.mjs",types:"./dist/index.d.ts",exports:{".":{types:"./dist/index.d.ts",import:"./dist/index.mjs",require:"./dist/index.cjs",default:"./dist/index.mjs"}},scripts:{dev:"tsup --watch",build:"tsup",test:"node --experimental-vm-modules node_modules/jest/bin/jest.js",prepare:"npm run build",format:'prettier --write "src/**/*.{js,ts}"',"format:check":'prettier --check "src/**/*.{js,ts}"'},files:["dist","README.md"],keywords:["supadata","api","sdk","typescript","youtube","transcript","web scraping"],author:"Supadata AI",license:"MIT",devDependencies:{"@types/jest":"^29.5.14","@types/node":"^22.10.10",jest:"^29.7.0","jest-fetch-mock":"^3.0.3",prettier:"^3.4.2","ts-jest":"^29.2.5",typescript:"^5.7.3",tsup:"^8.3.6"}};var y=`supadata-js/${h.version}`,o=class{config;constructor(e){this.config=e;}async fetch(e,t={},i="GET"){let a=`${this.config.baseUrl||"https://api.supadata.ai/v1"}${e.startsWith("/")?e:`/${e}`}`;if(i==="GET"&&Object.keys(t).length>0){let u=new URLSearchParams;Object.entries(t).forEach(([n,b])=>{b!=null&&u.append(n,String(b));}),a+=`?${u.toString()}`;}return this.fetchUrl(a,i,t)}async fetchUrl(e,t="GET",i){let s={method:t,headers:{"x-api-key":this.config.apiKey,"Content-Type":"application/json","User-Agent":y}};t==="POST"&&i&&(s.body=JSON.stringify(i));let a=await fetch(e,s),u=a.headers.get("content-type");if(!a.ok)if(u?.includes("application/json")){let n=await a.json();throw new r(n)}else throw new r({error:"internal-error",message:"Unexpected error response format",details:await a.text()});try{if(!u?.includes("application/json"))throw new r({error:"internal-error",message:"Invalid response format",details:"Expected JSON response but received different content type"});return await a.json()}catch(n){throw new r({error:"internal-error",message:"Failed to parse response",details:n instanceof Error?n.message:"Unknown error"})}}};var p=class extends o{transcript=Object.assign(async e=>this.fetch("/youtube/transcript",e),{batch:async e=>(this.validateBatchLimit(e),this.fetch("/youtube/transcript/batch",e,"POST"))});video=Object.assign(async e=>this.fetch("/youtube/video",e),{batch:async e=>(this.validateBatchLimit(e),this.fetch("/youtube/video/batch",e,"POST"))});channel=Object.assign(async e=>this.fetch("/youtube/channel",e),{videos:async e=>(this.validateLimit(e),this.fetch("/youtube/channel/videos",e))});playlist=Object.assign(async e=>this.fetch("/youtube/playlist",e),{videos:async e=>(this.validateLimit(e),this.fetch("/youtube/playlist/videos",e))});batch={getBatchResults:async e=>{if(!e)throw new r({error:"invalid-request",message:"Missing jobId",details:"The jobId parameter is required to get batch results."});return this.fetch(`/youtube/batch/${e}`)}};translate=async e=>this.fetch("/youtube/transcript/translate",e);validateLimit(e){if(e.limit!=null&&e.limit!=null&&(e.limit<1||e.limit>5e3))throw new r({error:"invalid-request",message:"Invalid limit.",details:"The limit must be between 1 and 5000."})}validateBatchLimit(e){if(e.limit!=null&&e.limit!=null&&(e.limit<1||e.limit>5e3))throw new r({error:"invalid-request",message:"Invalid limit for batch operation.",details:"The limit must be between 1 and 5000."})}};var m=class extends o{async scrape(e){return this.fetch("/web/scrape",{url:e})}async map(e){return this.fetch("/web/map",{url:e})}async crawl(e){return this.fetch("/web/crawl",e,"POST")}async getCrawlResults(e){let t,i=[],s;do t=await(s?this.fetchUrl(s):this.fetch(`/web/crawl/${e}`)),t.pages&&(i=[...i,...t.pages]),s=t.next;while(s);return t}};var g=class{youtube;web;constructor(e){this.youtube=new p(e),this.web=new m(e);}};
export{o as BaseClient,g as Supadata,r as SupadataError,m as WebService,p as YouTubeService};//# sourceMappingURL=index.mjs.map
//# sourceMappingURL=index.mjs.map