@supadata/js 1.0.5 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -55,8 +55,10 @@ const channel: YoutubeChannel = await supadata.youtube.channel({
55
55
  });
56
56
 
57
57
  // Get a list of video IDs from a YouTube channel
58
- const channelVideos: string[] = await supadata.youtube.channel.videos({
58
+ const channelVideos: VideoIds = await supadata.youtube.channel.videos({
59
59
  id: 'https://youtube.com/@RickAstleyVEVO', // can be url, channel id, handle
60
+ type: 'all', // 'video', 'short', 'all'
61
+ limit: 10,
60
62
  });
61
63
 
62
64
  // Get the metadata of a YouTube playlist
@@ -65,8 +67,9 @@ const playlist: YoutubePlaylist = await supadata.youtube.playlist({
65
67
  });
66
68
 
67
69
  // Get a list of video IDs from a YouTube playlist
68
- const playlistVideos: string[] = await supadata.youtube.playlist.videos({
70
+ const playlistVideos: VideoIds = await supadata.youtube.playlist.videos({
69
71
  id: 'https://www.youtube.com/playlist?list=PLlaN88a7y2_plecYoJxvRFTLHVbIVAOoc', // can be url or playlist id
72
+ limit: 10,
70
73
  });
71
74
 
72
75
  // Scrape web content
package/dist/index.cjs CHANGED
@@ -1,3 +1,3 @@
1
- 'use strict';var t=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 d={403:{error:"invalid-request",message:"Invalid or missing API key",details:"Please ensure you have provided a valid API key"},404:{error:"invalid-request",message:"Endpoint does not exist",details:"The API endpoint you are trying to access does not exist"},429:{error:"limit-exceeded",message:"Limit exceeded",details:"You have exceeded the allowed request rate or quota limits"}},f=(i,e)=>i in d?new t({...d[i],message:d[i].message,details:e||d[i].details}):new t({error:"internal-error",message:"An unexpected error occurred",details:e});var c=class{config;constructor(e){this.config=e;}async fetch(e,r={},s="GET"){let n=`${this.config.baseUrl||"https://api.supadata.ai/v1"}${e.startsWith("/")?e:`/${e}`}`;if(s==="GET"&&Object.keys(r).length>0){let l=new URLSearchParams;Object.entries(r).forEach(([o,g])=>{g!=null&&l.append(o,String(g));}),n+=`?${l.toString()}`;}return this.fetchUrl(n,s,r)}async fetchUrl(e,r="GET",s){let a={method:r,headers:{"x-api-key":this.config.apiKey,"Content-Type":"application/json"}};r==="POST"&&s&&(a.body=JSON.stringify(s));let n=await fetch(e,a),l=n.headers.get("content-type");if(!n.ok){if([403,404,429].includes(n.status)){let o=await n.json();throw f(n.status,o.message)}if(l?.includes("application/json")){let o=await n.json();throw new t(o)}else throw new t({error:"internal-error",message:"Unexpected error response format",details:await n.text()})}try{if(!l?.includes("application/json"))throw new t({error:"internal-error",message:"Invalid response format",details:"Expected JSON response but received different content type"});return await n.json()}catch(o){throw new t({error:"internal-error",message:"Failed to parse response",details:o instanceof Error?o.message:"Unknown error"})}}};var p=class extends c{async transcript(e){return this.fetch("/youtube/transcript",e)}async translate(e){return this.fetch("/youtube/transcript/translate",e)}async video(e){return this.fetch("/youtube/video",e)}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))});validateLimit(e){if(e.limit!=null&&e.limit!=null&&(e.limit<1||e.limit>5e3))throw new t({error:"invalid-request",message:"Invalid limit.",details:"The limit must be between 1 and 5000."})}};var m=class extends c{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 r,s=[],a;do r=await(a?this.fetchUrl(a):this.fetch(`/web/crawl/${e}`)),r.pages&&(s=[...s,...r.pages]),a=r.next;while(a);return r}};var h=class{youtube;web;constructor(e){this.youtube=new p(e),this.web=new m(e);}};
1
+ 'use strict';var t=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 d={403:{error:"invalid-request",message:"Invalid or missing API key",details:"Please ensure you have provided a valid API key"},404:{error:"invalid-request",message:"Endpoint does not exist",details:"The API endpoint you are trying to access does not exist"},429:{error:"limit-exceeded",message:"Limit exceeded",details:"You have exceeded the allowed request rate or quota limits"}},f=(n,e)=>n in d?new t({...d[n],message:d[n].message,details:e||d[n].details}):new t({error:"internal-error",message:"An unexpected error occurred",details:e});var c=class{config;constructor(e){this.config=e;}async fetch(e,r={},s="GET"){let i=`${this.config.baseUrl||"https://api.supadata.ai/v1"}${e.startsWith("/")?e:`/${e}`}`;if(s==="GET"&&Object.keys(r).length>0){let l=new URLSearchParams;Object.entries(r).forEach(([o,g])=>{g!=null&&l.append(o,String(g));}),i+=`?${l.toString()}`;}return this.fetchUrl(i,s,r)}async fetchUrl(e,r="GET",s){let a={method:r,headers:{"x-api-key":this.config.apiKey,"Content-Type":"application/json"}};r==="POST"&&s&&(a.body=JSON.stringify(s));let i=await fetch(e,a),l=i.headers.get("content-type");if(!i.ok){if([403,404,429].includes(i.status)){let o=await i.json();throw f(i.status,o.message)}if(l?.includes("application/json")){let o=await i.json();throw new t(o)}else throw new t({error:"internal-error",message:"Unexpected error response format",details:await i.text()})}try{if(!l?.includes("application/json"))throw new t({error:"internal-error",message:"Invalid response format",details:"Expected JSON response but received different content type"});return await i.json()}catch(o){throw new t({error:"internal-error",message:"Failed to parse response",details:o instanceof Error?o.message:"Unknown error"})}}};var p=class extends c{async transcript(e){return this.fetch("/youtube/transcript",e)}async translate(e){return this.fetch("/youtube/transcript/translate",e)}async video(e){return this.fetch("/youtube/video",e)}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))});validateLimit(e){if(e.limit!=null&&e.limit!=null&&(e.limit<1||e.limit>5e3))throw new t({error:"invalid-request",message:"Invalid limit.",details:"The limit must be between 1 and 5000."})}};var m=class extends c{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 r,s=[],a;do r=await(a?this.fetchUrl(a):this.fetch(`/web/crawl/${e}`)),r.pages&&(s=[...s,...r.pages]),a=r.next;while(a);return r}};var h=class{youtube;web;constructor(e){this.youtube=new p(e),this.web=new m(e);}};
2
2
  exports.BaseClient=c;exports.Supadata=h;exports.SupadataError=t;exports.WebService=m;exports.YouTubeService=p;//# sourceMappingURL=index.cjs.map
3
3
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/types.ts","../src/gateway-error-mapper.ts","../src/client.ts","../src/services/youtube.ts","../src/services/web.ts","../src/index.ts"],"names":["SupadataError","error","GATEWAY_STATUS_ERRORS","mapGatewayError","statusCode","errorText","BaseClient","config","endpoint","params","method","url","queryParams","key","value","body","options","response","contentType","errorData","YouTubeService","WebService","request","jobId","pages","nextUrl","Supadata"],"mappings":"aAoDO,IAAMA,CAAN,CAAA,cAA4B,KAAM,CACvC,MASA,OACA,CAAA,gBAAA,CAEA,WAAYC,CAAAA,CAAAA,CAKT,CACD,KAAA,CAAMA,CAAM,CAAA,OAAA,EAAW,8BAA8B,CACrD,CAAA,IAAA,CAAK,KAAQA,CAAAA,CAAAA,CAAM,KAAS,EAAA,gBAAA,CAC5B,IAAK,CAAA,OAAA,CAAUA,EAAM,OAAW,EAAA,8BAAA,CAChC,IAAK,CAAA,gBAAA,CAAmBA,CAAM,CAAA,gBAAA,EAAoB,EAClD,CAAA,IAAA,CAAK,KAAO,gBACd,CACF,ECtEA,IAAMC,CAAwB,CAAA,CAC5B,GAAK,CAAA,CACH,MAA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AA,CAAA,CAEtCQ,EAAUR,CAAS,CAAA,IAAA,CAAA,MACZQ,CAET,EAAA,OAAOR,CACT,CACF,ECpDO,IAAMS,EAAN,KAAe,CACX,OACA,CAAA,GAAA,CAET,WAAYnB,CAAAA,CAAAA,CAAwB,CAClC,IAAA,CAAK,QAAU,IAAIa,CAAAA,CAAeb,CAAM,CAAA,CACxC,KAAK,GAAM,CAAA,IAAIc,CAAWd,CAAAA,CAAM,EAClC,CACF","file":"index.cjs","sourcesContent":["export interface TranscriptChunk {\n text: string;\n offset: number;\n duration: number;\n lang: string;\n}\n\nexport interface Transcript {\n content: TranscriptChunk[] | string;\n lang: string;\n availableLangs: string[];\n}\n\nexport interface TranslatedTranscript {\n content: TranscriptChunk[] | string;\n lang: string;\n}\n\nexport interface Scrape {\n url: string;\n content: string;\n name: string;\n description: string;\n ogUrl: string;\n countCharacters: number;\n urls: string[];\n}\n\nexport interface SiteMap {\n urls: string[];\n}\n\nexport interface CrawlRequest {\n url: string;\n limit?: number;\n}\n\nexport interface Crawl {\n jobId: string;\n}\n\nexport interface CrawlJob {\n status: 'scraping' | 'completed' | 'failed' | 'cancelled';\n pages?: Scrape[];\n next?: string;\n}\n\nexport interface SupadataConfig {\n apiKey: string;\n baseUrl?: string;\n}\n\nexport class SupadataError extends Error {\n error:\n | 'invalid-request'\n | 'missing-parameters'\n | 'internal-error'\n | 'transcript-unavailable'\n | 'video-not-found'\n | 'video-id-invalid'\n | 'youtube-api-error'\n | 'limit-exceeded';\n details: string;\n documentationUrl: string;\n\n constructor(error: {\n error: SupadataError['error'];\n message?: string;\n details?: string;\n documentationUrl?: string;\n }) {\n super(error.message || 'An unexpected error occurred');\n this.error = error.error || 'internal-error';\n this.details = error.details || 'An unexpected error occurred';\n this.documentationUrl = error.documentationUrl || '';\n this.name = 'SupadataError';\n }\n}\n\nexport interface YoutubeVideo {\n id: string;\n title: string;\n description: string;\n duration: number;\n channel: {\n id: string;\n name: string;\n };\n tags: string[];\n thumbnail: string;\n uploadDate: string;\n viewCount: number;\n likeCount: number;\n transcriptLanguages: string[];\n}\n\nexport interface YoutubeChannel {\n id: string;\n name: string;\n handle: string;\n description: string;\n subscriberCount: number;\n videoCount: number;\n thumbnail: string;\n banner: string;\n}\n\nexport interface YoutubePlaylist {\n id: string;\n title: string;\n videoCount: number;\n viewCount: number;\n lastUpdated: string;\n description: string;\n thumbnail: string;\n}\n","import { SupadataError } from './types';\n\n/*\nThe API gateway returns errors in text/plain content type. \nAs a temporary workaround we're mapping them to SupadataError.\n*/\n\nconst GATEWAY_STATUS_ERRORS = {\n 403: {\n error: 'invalid-request' as const,\n message: 'Invalid or missing API key',\n details: 'Please ensure you have provided a valid API key',\n },\n 404: {\n error: 'invalid-request' as const,\n message: 'Endpoint does not exist',\n details: 'The API endpoint you are trying to access does not exist',\n },\n 429: {\n error: 'limit-exceeded' as const,\n message: 'Limit exceeded',\n details: 'You have exceeded the allowed request rate or quota limits',\n },\n};\n\nexport const mapGatewayError = (\n statusCode: number,\n errorText: string\n): SupadataError => {\n if (statusCode in GATEWAY_STATUS_ERRORS) {\n return new SupadataError({\n ...GATEWAY_STATUS_ERRORS[\n statusCode as keyof typeof GATEWAY_STATUS_ERRORS\n ],\n message:\n GATEWAY_STATUS_ERRORS[statusCode as keyof typeof GATEWAY_STATUS_ERRORS]\n .message,\n details:\n errorText ||\n GATEWAY_STATUS_ERRORS[statusCode as keyof typeof GATEWAY_STATUS_ERRORS]\n .details,\n });\n }\n\n // Default error if status code is not recognized\n return new SupadataError({\n error: 'internal-error',\n message: 'An unexpected error occurred',\n details: errorText,\n });\n};\n","import { SupadataConfig, SupadataError } from './types.js';\nimport { mapGatewayError } from './gateway-error-mapper.js';\n\nexport class BaseClient {\n protected config: SupadataConfig;\n\n constructor(config: SupadataConfig) {\n this.config = config;\n }\n\n protected async fetch<T>(\n endpoint: string,\n params: Record<string, any> = {},\n method: 'GET' | 'POST' = 'GET'\n ): Promise<T> {\n const baseUrl = this.config.baseUrl || 'https://api.supadata.ai/v1';\n let url = `${baseUrl}${\n endpoint.startsWith('/') ? endpoint : `/${endpoint}`\n }`;\n\n if (method === 'GET' && Object.keys(params).length > 0) {\n const queryParams = new URLSearchParams();\n Object.entries(params).forEach(([key, value]) => {\n if (value !== undefined && value !== null) {\n queryParams.append(key, String(value));\n }\n });\n url += `?${queryParams.toString()}`;\n }\n\n return this.fetchUrl<T>(url, method, params);\n }\n\n protected async fetchUrl<T>(\n url: string,\n method: 'GET' | 'POST' = 'GET',\n body?: Record<string, any>\n ): Promise<T> {\n const options: RequestInit = {\n method,\n headers: {\n 'x-api-key': this.config.apiKey,\n 'Content-Type': 'application/json',\n },\n };\n\n if (method === 'POST' && body) {\n options.body = JSON.stringify(body);\n }\n\n const response = await fetch(url, options);\n\n const contentType = response.headers.get('content-type');\n\n if (!response.ok) {\n // First check for gateway-specific status codes\n if ([403, 404, 429].includes(response.status)) {\n const errorData = await response.json();\n throw mapGatewayError(response.status, errorData.message);\n }\n\n // Handle standard API errors\n if (contentType?.includes('application/json')) {\n const errorData = await response.json();\n throw new SupadataError(errorData);\n } else {\n // Fallback for unexpected non-JSON errors\n throw new SupadataError({\n error: 'internal-error',\n message: 'Unexpected error response format',\n details: await response.text(),\n });\n }\n }\n\n try {\n if (!contentType?.includes('application/json')) {\n throw new SupadataError({\n error: 'internal-error',\n message: 'Invalid response format',\n details: 'Expected JSON response but received different content type',\n });\n }\n\n return (await response.json()) as T;\n } catch (error) {\n throw new SupadataError({\n error: 'internal-error',\n message: 'Failed to parse response',\n details: error instanceof Error ? error.message : 'Unknown error',\n });\n }\n }\n}\n","import { BaseClient } from '../client.js';\nimport {\n SupadataError,\n Transcript,\n TranslatedTranscript,\n YoutubeChannel,\n YoutubePlaylist,\n YoutubeVideo,\n} from '../types.js';\n\n/**\n * Ensures exactly one property from the specified keys is provided.\n * @example\n * // Valid: { url: \"...\" } or { videoId: \"...\" }\n * // Invalid: {} or { url: \"...\", videoId: \"...\" }\n */\ntype ExactlyOne<T, Keys extends keyof T> = {\n [K in Keys]: { [P in K]-?: T[P] } & { [P in Exclude<Keys, K>]?: never };\n}[Keys] &\n Omit<T, Keys>;\n\nexport type TranscriptParams = {\n lang?: string;\n text?: boolean;\n} & ExactlyOne<{ videoId: string; url: string }, 'videoId' | 'url'>;\n\nexport interface TranslateParams extends Omit<TranscriptParams, 'lang'> {\n lang: string;\n}\n\nexport interface ResourceParams {\n id: string;\n}\n\nexport interface ChannelVideosParams extends ResourceParams {\n limit?: number;\n}\n\nexport interface PlaylistVideosParams extends ResourceParams {\n limit?: number;\n}\n\nexport class YouTubeService extends BaseClient {\n /**\n * Fetches a transcript for a YouTube video.\n *\n * @param params - Parameters for fetching the transcript\n * @param params.videoId - The YouTube video ID (provide either this OR url)\n * @param params.url - The YouTube video URL (provide either this OR videoId)\n * @param params.lang - Optional language code for the transcript\n * @param params.text - Optional flag to return plain text instead of timestamped list\n * @returns A promise that resolves to the video transcript\n */\n async transcript(params: TranscriptParams): Promise<Transcript> {\n return this.fetch<Transcript>('/youtube/transcript', params);\n }\n\n /**\n * Translates a YouTube video transcript to a specified language.\n *\n * @param params - Parameters for translating the transcript\n * @param params.videoId - The YouTube video ID (provide either this OR url)\n * @param params.url - The YouTube video URL (provide either this OR videoId)\n * @param params.lang - The target language code for translation\n * @param params.text - Optional flag to return plain text instead of timestamped list\n * @returns A promise that resolves to the translated transcript\n */\n async translate(params: TranslateParams): Promise<TranslatedTranscript> {\n return this.fetch<TranslatedTranscript>(\n '/youtube/transcript/translate',\n params\n );\n }\n\n /**\n * Fetches a YouTube video based on the provided parameters.\n *\n * @param params - The parameters required to fetch the YouTube video.\n * @param params.id - The YouTube video ID.\n * @returns A promise that resolves to a `YoutubeVideo` object.\n */\n async video(params: ResourceParams): Promise<YoutubeVideo> {\n return this.fetch<YoutubeVideo>('/youtube/video', params);\n }\n\n /**\n * Fetches YouTube channel information and videos.\n *\n * @param params - The parameters required to fetch the YouTube channel information.\n * @param params.id - The YouTube channel ID.\n * @returns A promise that resolves to a `YoutubeChannel` object containing the channel information.\n *\n * @property videos - Fetches the videos of the YouTube channel.\n * @param params - The parameters required to fetch the YouTube channel videos.\n * @param params.id - The YouTube channel ID.\n * @param params.limit - The maximum number of videos to fetch.\n * Default: 30. Max: 5000.\n * @returns A promise that resolves to an array of video IDs.\n *\n * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000).\n */\n channel = Object.assign(\n async (params: ResourceParams): Promise<YoutubeChannel> => {\n return this.fetch<YoutubeChannel>('/youtube/channel', params);\n },\n {\n videos: async (params: ChannelVideosParams): Promise<string[]> => {\n // Validate the limit locally to avoid unnecessary API calls.\n this.validateLimit(params);\n return this.fetch<string[]>('/youtube/channel/videos', params);\n },\n }\n );\n\n /**\n * Fetches a YouTube playlist and its videos.\n *\n * @param params - The parameters required to fetch the playlist.\n * @param params.id - The YouTube playlist ID.\n * @returns A promise that resolves to a `YoutubePlaylist` object.\n *\n * @property videos - Fetches the videos of a YouTube playlist.\n * @param params - The parameters required to fetch the playlist videos.\n * @param params.id - The YouTube playlist ID.\n * @param params.limit - The maximum number of videos to fetch.\n * Default: 30. Max: 5000.\n * @returns A promise that resolves to an array of video IDs.\n *\n * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000).\n */\n playlist = Object.assign(\n async (params: ResourceParams): Promise<YoutubePlaylist> => {\n return this.fetch<YoutubePlaylist>('/youtube/playlist', params);\n },\n {\n videos: async (params: PlaylistVideosParams): Promise<string[]> => {\n // Validate the limit locally to avoid unnecessary API calls.\n this.validateLimit(params);\n return this.fetch<string[]>('/youtube/playlist/videos', params);\n },\n }\n );\n\n private validateLimit(params: { limit?: number }) {\n if (\n params.limit != undefined &&\n params.limit != null &&\n (params.limit < 1 || params.limit > 5000)\n ) {\n throw new SupadataError({\n error: 'invalid-request',\n message: 'Invalid limit.',\n details: 'The limit must be between 1 and 5000.',\n });\n }\n }\n}\n","import { BaseClient } from '../client.js';\nimport { Crawl, CrawlJob, CrawlRequest, Scrape, SiteMap } from '../types.js';\n\nexport class WebService extends BaseClient {\n /**\n * Extract content from any web page to Markdown format.\n *\n * @param url - URL of the webpage to scrape\n * @returns A promise that resolves to the scraped content\n */\n async scrape(url: string): Promise<Scrape> {\n return this.fetch<Scrape>('/web/scrape', { url });\n }\n\n /**\n * Extract all links found on a webpage.\n *\n * @param url - URL of the webpage to map\n * @returns A promise that resolves to a map of URLs found on the page\n */\n async map(url: string): Promise<SiteMap> {\n return this.fetch<SiteMap>('/web/map', { url });\n }\n\n /**\n * Create a crawl job to extract content from all pages on a website.\n *\n * @param request - Crawl request parameters\n * @param request.url - URL of the website to crawl\n * @param request.limit - Maximum number of pages to crawl (default: 100, max: 5000)\n * @returns A promise that resolves to the crawl job id\n */\n async crawl(request: CrawlRequest): Promise<Crawl> {\n return this.fetch<Crawl>('/web/crawl', request, 'POST');\n }\n\n /**\n * Get the status and results of a crawl job.\n * Automatically handles pagination to retrieve all pages from the crawl.\n *\n * @param jobId - The ID of the crawl job to retrieve\n * @returns A promise that resolves to the complete crawl job results\n */\n async getCrawlResults(jobId: string): Promise<CrawlJob> {\n let response: CrawlJob;\n let pages: Scrape[] = [];\n let nextUrl: string | undefined;\n\n do {\n response = await (nextUrl\n ? this.fetchUrl<CrawlJob>(nextUrl)\n : this.fetch<CrawlJob>(`/web/crawl/${jobId}`));\n\n if (response.pages) {\n pages = [...pages, ...response.pages];\n }\n nextUrl = response.next;\n } while (nextUrl);\n\n return response;\n }\n}\n","import { SupadataConfig } from './types.js';\nimport { YouTubeService } from './services/youtube.js';\nimport { WebService } from './services/web.js';\n\nexport * from './types.js';\nexport * from './client.js';\nexport * from './services/youtube.js';\nexport * from './services/web.js';\n\nexport class Supadata {\n readonly youtube: YouTubeService;\n readonly web: WebService;\n\n constructor(config: SupadataConfig) {\n this.youtube = new YouTubeService(config);\n this.web = new WebService(config);\n }\n}\n"]}
1
+ {"version":3,"sources":["../src/types.ts","../src/gateway-error-mapper.ts","../src/client.ts","../src/services/youtube.ts","../src/services/web.ts","../src/index.ts"],"names":["SupadataError","error","GATEWAY_STATUS_ERRORS","mapGatewayError","statusCode","errorText","BaseClient","config","endpoint","params","method","url","queryParams","key","value","body","options","response","contentType","errorData","YouTubeService","WebService","request","jobId","pages","nextUrl","Supadata"],"mappings":"aAoDO,IAAMA,CAAN,CAAA,cAA4B,KAAM,CACvC,MASA,OACA,CAAA,gBAAA,CAEA,WAAYC,CAAAA,CAAAA,CAKT,CACD,KAAA,CAAMA,CAAM,CAAA,OAAA,EAAW,8BAA8B,CACrD,CAAA,IAAA,CAAK,KAAQA,CAAAA,CAAAA,CAAM,KAAS,EAAA,gBAAA,CAC5B,IAAK,CAAA,OAAA,CAAUA,EAAM,OAAW,EAAA,8BAAA,CAChC,IAAK,CAAA,gBAAA,CAAmBA,CAAM,CAAA,gBAAA,EAAoB,EAClD,CAAA,IAAA,CAAK,KAAO,gBACd,CACF,ECtEA,IAAMC,CAAwB,CAAA,CAC5B,GAAK,CAAA,CACH,MAAO,iBACP,CAAA,OAAA,CAAS,4BACT,CAAA,OAAA,CAAS,iDACX,CAAA,CACA,GAAK,CAAA,CACH,MAAO,iBACP,CAAA,OAAA,CAAS,yBACT,CAAA,OAAA,CAAS,0DACX,CAAA,CACA,GAAK,CAAA,CACH,MAAO,gBACP,CAAA,OAAA,CAAS,gBACT,CAAA,OAAA,CAAS,4DACX,CACF,CAEaC,CAAAA,CAAAA,CAAkB,CAC7BC,CACAC,CAAAA,CAAAA,GAEID,CAAcF,IAAAA,CAAAA,CACT,IAAIF,CAAAA,CAAc,CACvB,GAAGE,EACDE,CACF,CAAA,CACA,OACEF,CAAAA,CAAAA,CAAsBE,CAAgD,CAAA,CACnE,OACL,CAAA,OAAA,CACEC,GACAH,CAAsBE,CAAAA,CAAgD,CACnE,CAAA,OACP,CAAC,CAAA,CAII,IAAIJ,CAAAA,CAAc,CACvB,KAAO,CAAA,gBAAA,CACP,OAAS,CAAA,8BAAA,CACT,OAASK,CAAAA,CACX,CAAC,CAAA,KC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,CAAC,CAClD,CAQA,MAAM,GAAA,CAAIA,CAA+B,CAAA,CACvC,OAAO,IAAA,CAAK,MAAe,UAAY,CAAA,CAAE,GAAAA,CAAAA,CAAI,CAAC,CAChD,CAUA,MAAM,MAAMW,CAAuC,CAAA,CACjD,OAAO,IAAA,CAAK,KAAa,CAAA,YAAA,CAAcA,CAAS,CAAA,MAAM,CACxD,CASA,MAAM,eAAgBC,CAAAA,CAAAA,CAAkC,CACtD,IAAIN,CACAO,CAAAA,CAAAA,CAAkB,EAClBC,CAAAA,CAAAA,CAEJ,GACER,CAAAA,CAAW,MAAOQ,CAAAA,CACd,IAAK,CAAA,QAAA,CAAmBA,CAAO,CAC/B,CAAA,IAAA,CAAK,KAAgB,CAAA,CAAA,WAAA,EAAcF,CAAK,CAAA,CAAE,CAE1CN,CAAAA,CAAAA,CAAAA,CAAS,QACXO,CAAQ,CAAA,CAAC,GAAGA,CAAAA,CAAO,GAAGP,CAAAA,CAAS,KAAK,CAAA,CAAA,CAEtCQ,EAAUR,CAAS,CAAA,IAAA,CAAA,MACZQ,CAET,EAAA,OAAOR,CACT,CACF,ECpDO,IAAMS,EAAN,KAAe,CACX,OACA,CAAA,GAAA,CAET,WAAYnB,CAAAA,CAAAA,CAAwB,CAClC,IAAA,CAAK,QAAU,IAAIa,CAAAA,CAAeb,CAAM,CAAA,CACxC,KAAK,GAAM,CAAA,IAAIc,CAAWd,CAAAA,CAAM,EAClC,CACF","file":"index.cjs","sourcesContent":["export interface TranscriptChunk {\n text: string;\n offset: number;\n duration: number;\n lang: string;\n}\n\nexport interface Transcript {\n content: TranscriptChunk[] | string;\n lang: string;\n availableLangs: string[];\n}\n\nexport interface TranslatedTranscript {\n content: TranscriptChunk[] | string;\n lang: string;\n}\n\nexport interface Scrape {\n url: string;\n content: string;\n name: string;\n description: string;\n ogUrl: string;\n countCharacters: number;\n urls: string[];\n}\n\nexport interface SiteMap {\n urls: string[];\n}\n\nexport interface CrawlRequest {\n url: string;\n limit?: number;\n}\n\nexport interface Crawl {\n jobId: string;\n}\n\nexport interface CrawlJob {\n status: 'scraping' | 'completed' | 'failed' | 'cancelled';\n pages?: Scrape[];\n next?: string;\n}\n\nexport interface SupadataConfig {\n apiKey: string;\n baseUrl?: string;\n}\n\nexport class SupadataError extends Error {\n error:\n | 'invalid-request'\n | 'missing-parameters'\n | 'internal-error'\n | 'transcript-unavailable'\n | 'video-not-found'\n | 'video-id-invalid'\n | 'youtube-api-error'\n | 'limit-exceeded';\n details: string;\n documentationUrl: string;\n\n constructor(error: {\n error: SupadataError['error'];\n message?: string;\n details?: string;\n documentationUrl?: string;\n }) {\n super(error.message || 'An unexpected error occurred');\n this.error = error.error || 'internal-error';\n this.details = error.details || 'An unexpected error occurred';\n this.documentationUrl = error.documentationUrl || '';\n this.name = 'SupadataError';\n }\n}\n\nexport interface YoutubeVideo {\n id: string;\n title: string;\n description: string;\n duration: number;\n channel: {\n id: string;\n name: string;\n };\n tags: string[];\n thumbnail: string;\n uploadDate: string;\n viewCount: number;\n likeCount: number;\n transcriptLanguages: string[];\n}\n\nexport interface YoutubeChannel {\n id: string;\n name: string;\n handle: string;\n description: string;\n subscriberCount: number;\n videoCount: number;\n thumbnail: string;\n banner: string;\n}\n\nexport interface YoutubePlaylist {\n id: string;\n title: string;\n videoCount: number;\n viewCount: number;\n lastUpdated: string;\n description: string;\n thumbnail: string;\n}\n","import { SupadataError } from './types';\n\n/*\nThe API gateway returns errors in text/plain content type. \nAs a temporary workaround we're mapping them to SupadataError.\n*/\n\nconst GATEWAY_STATUS_ERRORS = {\n 403: {\n error: 'invalid-request' as const,\n message: 'Invalid or missing API key',\n details: 'Please ensure you have provided a valid API key',\n },\n 404: {\n error: 'invalid-request' as const,\n message: 'Endpoint does not exist',\n details: 'The API endpoint you are trying to access does not exist',\n },\n 429: {\n error: 'limit-exceeded' as const,\n message: 'Limit exceeded',\n details: 'You have exceeded the allowed request rate or quota limits',\n },\n};\n\nexport const mapGatewayError = (\n statusCode: number,\n errorText: string\n): SupadataError => {\n if (statusCode in GATEWAY_STATUS_ERRORS) {\n return new SupadataError({\n ...GATEWAY_STATUS_ERRORS[\n statusCode as keyof typeof GATEWAY_STATUS_ERRORS\n ],\n message:\n GATEWAY_STATUS_ERRORS[statusCode as keyof typeof GATEWAY_STATUS_ERRORS]\n .message,\n details:\n errorText ||\n GATEWAY_STATUS_ERRORS[statusCode as keyof typeof GATEWAY_STATUS_ERRORS]\n .details,\n });\n }\n\n // Default error if status code is not recognized\n return new SupadataError({\n error: 'internal-error',\n message: 'An unexpected error occurred',\n details: errorText,\n });\n};\n","import { SupadataConfig, SupadataError } from './types.js';\nimport { mapGatewayError } from './gateway-error-mapper.js';\n\nexport class BaseClient {\n protected config: SupadataConfig;\n\n constructor(config: SupadataConfig) {\n this.config = config;\n }\n\n protected async fetch<T>(\n endpoint: string,\n params: Record<string, any> = {},\n method: 'GET' | 'POST' = 'GET'\n ): Promise<T> {\n const baseUrl = this.config.baseUrl || 'https://api.supadata.ai/v1';\n let url = `${baseUrl}${\n endpoint.startsWith('/') ? endpoint : `/${endpoint}`\n }`;\n\n if (method === 'GET' && Object.keys(params).length > 0) {\n const queryParams = new URLSearchParams();\n Object.entries(params).forEach(([key, value]) => {\n if (value !== undefined && value !== null) {\n queryParams.append(key, String(value));\n }\n });\n url += `?${queryParams.toString()}`;\n }\n\n return this.fetchUrl<T>(url, method, params);\n }\n\n protected async fetchUrl<T>(\n url: string,\n method: 'GET' | 'POST' = 'GET',\n body?: Record<string, any>\n ): Promise<T> {\n const options: RequestInit = {\n method,\n headers: {\n 'x-api-key': this.config.apiKey,\n 'Content-Type': 'application/json',\n },\n };\n\n if (method === 'POST' && body) {\n options.body = JSON.stringify(body);\n }\n\n const response = await fetch(url, options);\n\n const contentType = response.headers.get('content-type');\n\n if (!response.ok) {\n // First check for gateway-specific status codes\n if ([403, 404, 429].includes(response.status)) {\n const errorData = await response.json();\n throw mapGatewayError(response.status, errorData.message);\n }\n\n // Handle standard API errors\n if (contentType?.includes('application/json')) {\n const errorData = await response.json();\n throw new SupadataError(errorData);\n } else {\n // Fallback for unexpected non-JSON errors\n throw new SupadataError({\n error: 'internal-error',\n message: 'Unexpected error response format',\n details: await response.text(),\n });\n }\n }\n\n try {\n if (!contentType?.includes('application/json')) {\n throw new SupadataError({\n error: 'internal-error',\n message: 'Invalid response format',\n details: 'Expected JSON response but received different content type',\n });\n }\n\n return (await response.json()) as T;\n } catch (error) {\n throw new SupadataError({\n error: 'internal-error',\n message: 'Failed to parse response',\n details: error instanceof Error ? error.message : 'Unknown error',\n });\n }\n }\n}\n","import { BaseClient } from '../client.js';\nimport {\n SupadataError,\n Transcript,\n TranslatedTranscript,\n YoutubeChannel,\n YoutubePlaylist,\n YoutubeVideo,\n} from '../types.js';\n\n/**\n * Ensures exactly one property from the specified keys is provided.\n * @example\n * // Valid: { url: \"...\" } or { videoId: \"...\" }\n * // Invalid: {} or { url: \"...\", videoId: \"...\" }\n */\ntype ExactlyOne<T, Keys extends keyof T> = {\n [K in Keys]: { [P in K]-?: T[P] } & { [P in Exclude<Keys, K>]?: never };\n}[Keys] &\n Omit<T, Keys>;\n\nexport type TranscriptParams = {\n lang?: string;\n text?: boolean;\n} & ExactlyOne<{ videoId: string; url: string }, 'videoId' | 'url'>;\n\nexport interface TranslateParams extends Omit<TranscriptParams, 'lang'> {\n lang: string;\n}\n\nexport interface ResourceParams {\n id: string;\n}\n\nexport interface ChannelVideosParams extends ResourceParams {\n limit?: number;\n type?: 'video' | 'short' | 'all';\n}\n\nexport interface PlaylistVideosParams extends ResourceParams {\n limit?: number;\n}\n\nexport interface VideoIds {\n videoIds: string[];\n shortIds: string[];\n}\n\nexport class YouTubeService extends BaseClient {\n /**\n * Fetches a transcript for a YouTube video.\n *\n * @param params - Parameters for fetching the transcript\n * @param params.videoId - The YouTube video ID (provide either this OR url)\n * @param params.url - The YouTube video URL (provide either this OR videoId)\n * @param params.lang - Optional language code for the transcript\n * @param params.text - Optional flag to return plain text instead of timestamped list\n * @returns A promise that resolves to the video transcript\n */\n async transcript(params: TranscriptParams): Promise<Transcript> {\n return this.fetch<Transcript>('/youtube/transcript', params);\n }\n\n /**\n * Translates a YouTube video transcript to a specified language.\n *\n * @param params - Parameters for translating the transcript\n * @param params.videoId - The YouTube video ID (provide either this OR url)\n * @param params.url - The YouTube video URL (provide either this OR videoId)\n * @param params.lang - The target language code for translation\n * @param params.text - Optional flag to return plain text instead of timestamped list\n * @returns A promise that resolves to the translated transcript\n */\n async translate(params: TranslateParams): Promise<TranslatedTranscript> {\n return this.fetch<TranslatedTranscript>(\n '/youtube/transcript/translate',\n params\n );\n }\n\n /**\n * Fetches a YouTube video based on the provided parameters.\n *\n * @param params - The parameters required to fetch the YouTube video.\n * @param params.id - The YouTube video ID.\n * @returns A promise that resolves to a `YoutubeVideo` object.\n */\n async video(params: ResourceParams): Promise<YoutubeVideo> {\n return this.fetch<YoutubeVideo>('/youtube/video', params);\n }\n\n /**\n * Fetches YouTube channel information and videos.\n *\n * @param params - The parameters required to fetch the YouTube channel information.\n * @param params.id - The YouTube channel ID.\n * @returns A promise that resolves to a `YoutubeChannel` object containing the channel information.\n *\n * @property videos - Fetches the videos of the YouTube channel.\n * @param params - The parameters required to fetch the YouTube channel videos.\n * @param params.id - The YouTube channel ID.\n * @param params.limit - The maximum number of videos to fetch.\n * Default: 30. Max: 5000.\n * @param params.type - The type of videos to fetch.\n * Default: 'video'.\n * Allowed values: 'video', 'short', 'all'.\n * @returns A promise that resolves to an array of video IDs.\n *\n * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000).\n */\n channel = Object.assign(\n async (params: ResourceParams): Promise<YoutubeChannel> => {\n return this.fetch<YoutubeChannel>('/youtube/channel', params);\n },\n {\n videos: async (params: ChannelVideosParams): Promise<VideoIds> => {\n // Validate the limit locally to avoid unnecessary API calls.\n this.validateLimit(params);\n return this.fetch<VideoIds>('/youtube/channel/videos', params);\n },\n }\n );\n\n /**\n * Fetches a YouTube playlist and its videos.\n *\n * @param params - The parameters required to fetch the playlist.\n * @param params.id - The YouTube playlist ID.\n * @returns A promise that resolves to a `YoutubePlaylist` object.\n *\n * @property videos - Fetches the videos of a YouTube playlist.\n * @param params - The parameters required to fetch the playlist videos.\n * @param params.id - The YouTube playlist ID.\n * @param params.limit - The maximum number of videos to fetch.\n * Default: 30. Max: 5000.\n * @returns A promise that resolves to an array of video IDs.\n *\n * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000).\n */\n playlist = Object.assign(\n async (params: ResourceParams): Promise<YoutubePlaylist> => {\n return this.fetch<YoutubePlaylist>('/youtube/playlist', params);\n },\n {\n videos: async (params: PlaylistVideosParams): Promise<VideoIds> => {\n // Validate the limit locally to avoid unnecessary API calls.\n this.validateLimit(params);\n return this.fetch<VideoIds>('/youtube/playlist/videos', params);\n },\n }\n );\n\n private validateLimit(params: { limit?: number }) {\n if (\n params.limit != undefined &&\n params.limit != null &&\n (params.limit < 1 || params.limit > 5000)\n ) {\n throw new SupadataError({\n error: 'invalid-request',\n message: 'Invalid limit.',\n details: 'The limit must be between 1 and 5000.',\n });\n }\n }\n}\n","import { BaseClient } from '../client.js';\nimport { Crawl, CrawlJob, CrawlRequest, Scrape, SiteMap } from '../types.js';\n\nexport class WebService extends BaseClient {\n /**\n * Extract content from any web page to Markdown format.\n *\n * @param url - URL of the webpage to scrape\n * @returns A promise that resolves to the scraped content\n */\n async scrape(url: string): Promise<Scrape> {\n return this.fetch<Scrape>('/web/scrape', { url });\n }\n\n /**\n * Extract all links found on a webpage.\n *\n * @param url - URL of the webpage to map\n * @returns A promise that resolves to a map of URLs found on the page\n */\n async map(url: string): Promise<SiteMap> {\n return this.fetch<SiteMap>('/web/map', { url });\n }\n\n /**\n * Create a crawl job to extract content from all pages on a website.\n *\n * @param request - Crawl request parameters\n * @param request.url - URL of the website to crawl\n * @param request.limit - Maximum number of pages to crawl (default: 100, max: 5000)\n * @returns A promise that resolves to the crawl job id\n */\n async crawl(request: CrawlRequest): Promise<Crawl> {\n return this.fetch<Crawl>('/web/crawl', request, 'POST');\n }\n\n /**\n * Get the status and results of a crawl job.\n * Automatically handles pagination to retrieve all pages from the crawl.\n *\n * @param jobId - The ID of the crawl job to retrieve\n * @returns A promise that resolves to the complete crawl job results\n */\n async getCrawlResults(jobId: string): Promise<CrawlJob> {\n let response: CrawlJob;\n let pages: Scrape[] = [];\n let nextUrl: string | undefined;\n\n do {\n response = await (nextUrl\n ? this.fetchUrl<CrawlJob>(nextUrl)\n : this.fetch<CrawlJob>(`/web/crawl/${jobId}`));\n\n if (response.pages) {\n pages = [...pages, ...response.pages];\n }\n nextUrl = response.next;\n } while (nextUrl);\n\n return response;\n }\n}\n","import { SupadataConfig } from './types.js';\nimport { YouTubeService } from './services/youtube.js';\nimport { WebService } from './services/web.js';\n\nexport * from './types.js';\nexport * from './client.js';\nexport * from './services/youtube.js';\nexport * from './services/web.js';\n\nexport class Supadata {\n readonly youtube: YouTubeService;\n readonly web: WebService;\n\n constructor(config: SupadataConfig) {\n this.youtube = new YouTubeService(config);\n this.web = new WebService(config);\n }\n}\n"]}
package/dist/index.d.mts CHANGED
@@ -123,10 +123,15 @@ interface ResourceParams {
123
123
  }
124
124
  interface ChannelVideosParams extends ResourceParams {
125
125
  limit?: number;
126
+ type?: 'video' | 'short' | 'all';
126
127
  }
127
128
  interface PlaylistVideosParams extends ResourceParams {
128
129
  limit?: number;
129
130
  }
131
+ interface VideoIds {
132
+ videoIds: string[];
133
+ shortIds: string[];
134
+ }
130
135
  declare class YouTubeService extends BaseClient {
131
136
  /**
132
137
  * Fetches a transcript for a YouTube video.
@@ -170,12 +175,15 @@ declare class YouTubeService extends BaseClient {
170
175
  * @param params.id - The YouTube channel ID.
171
176
  * @param params.limit - The maximum number of videos to fetch.
172
177
  * Default: 30. Max: 5000.
178
+ * @param params.type - The type of videos to fetch.
179
+ * Default: 'video'.
180
+ * Allowed values: 'video', 'short', 'all'.
173
181
  * @returns A promise that resolves to an array of video IDs.
174
182
  *
175
183
  * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000).
176
184
  */
177
185
  channel: ((params: ResourceParams) => Promise<YoutubeChannel>) & {
178
- videos: (params: ChannelVideosParams) => Promise<string[]>;
186
+ videos: (params: ChannelVideosParams) => Promise<VideoIds>;
179
187
  };
180
188
  /**
181
189
  * Fetches a YouTube playlist and its videos.
@@ -194,7 +202,7 @@ declare class YouTubeService extends BaseClient {
194
202
  * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000).
195
203
  */
196
204
  playlist: ((params: ResourceParams) => Promise<YoutubePlaylist>) & {
197
- videos: (params: PlaylistVideosParams) => Promise<string[]>;
205
+ videos: (params: PlaylistVideosParams) => Promise<VideoIds>;
198
206
  };
199
207
  private validateLimit;
200
208
  }
@@ -239,4 +247,4 @@ declare class Supadata {
239
247
  constructor(config: SupadataConfig);
240
248
  }
241
249
 
242
- export { BaseClient, type ChannelVideosParams, type Crawl, type CrawlJob, type CrawlRequest, type PlaylistVideosParams, type ResourceParams, type Scrape, type SiteMap, Supadata, type SupadataConfig, SupadataError, type Transcript, type TranscriptChunk, type TranscriptParams, type TranslateParams, type TranslatedTranscript, WebService, YouTubeService, type YoutubeChannel, type YoutubePlaylist, type YoutubeVideo };
250
+ export { BaseClient, type ChannelVideosParams, type Crawl, type CrawlJob, type CrawlRequest, type PlaylistVideosParams, type ResourceParams, type Scrape, type SiteMap, Supadata, type SupadataConfig, SupadataError, type Transcript, type TranscriptChunk, type TranscriptParams, type TranslateParams, type TranslatedTranscript, type VideoIds, WebService, YouTubeService, type YoutubeChannel, type YoutubePlaylist, type YoutubeVideo };
package/dist/index.d.ts CHANGED
@@ -123,10 +123,15 @@ interface ResourceParams {
123
123
  }
124
124
  interface ChannelVideosParams extends ResourceParams {
125
125
  limit?: number;
126
+ type?: 'video' | 'short' | 'all';
126
127
  }
127
128
  interface PlaylistVideosParams extends ResourceParams {
128
129
  limit?: number;
129
130
  }
131
+ interface VideoIds {
132
+ videoIds: string[];
133
+ shortIds: string[];
134
+ }
130
135
  declare class YouTubeService extends BaseClient {
131
136
  /**
132
137
  * Fetches a transcript for a YouTube video.
@@ -170,12 +175,15 @@ declare class YouTubeService extends BaseClient {
170
175
  * @param params.id - The YouTube channel ID.
171
176
  * @param params.limit - The maximum number of videos to fetch.
172
177
  * Default: 30. Max: 5000.
178
+ * @param params.type - The type of videos to fetch.
179
+ * Default: 'video'.
180
+ * Allowed values: 'video', 'short', 'all'.
173
181
  * @returns A promise that resolves to an array of video IDs.
174
182
  *
175
183
  * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000).
176
184
  */
177
185
  channel: ((params: ResourceParams) => Promise<YoutubeChannel>) & {
178
- videos: (params: ChannelVideosParams) => Promise<string[]>;
186
+ videos: (params: ChannelVideosParams) => Promise<VideoIds>;
179
187
  };
180
188
  /**
181
189
  * Fetches a YouTube playlist and its videos.
@@ -194,7 +202,7 @@ declare class YouTubeService extends BaseClient {
194
202
  * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000).
195
203
  */
196
204
  playlist: ((params: ResourceParams) => Promise<YoutubePlaylist>) & {
197
- videos: (params: PlaylistVideosParams) => Promise<string[]>;
205
+ videos: (params: PlaylistVideosParams) => Promise<VideoIds>;
198
206
  };
199
207
  private validateLimit;
200
208
  }
@@ -239,4 +247,4 @@ declare class Supadata {
239
247
  constructor(config: SupadataConfig);
240
248
  }
241
249
 
242
- export { BaseClient, type ChannelVideosParams, type Crawl, type CrawlJob, type CrawlRequest, type PlaylistVideosParams, type ResourceParams, type Scrape, type SiteMap, Supadata, type SupadataConfig, SupadataError, type Transcript, type TranscriptChunk, type TranscriptParams, type TranslateParams, type TranslatedTranscript, WebService, YouTubeService, type YoutubeChannel, type YoutubePlaylist, type YoutubeVideo };
250
+ export { BaseClient, type ChannelVideosParams, type Crawl, type CrawlJob, type CrawlRequest, type PlaylistVideosParams, type ResourceParams, type Scrape, type SiteMap, Supadata, type SupadataConfig, SupadataError, type Transcript, type TranscriptChunk, type TranscriptParams, type TranslateParams, type TranslatedTranscript, type VideoIds, WebService, YouTubeService, type YoutubeChannel, type YoutubePlaylist, type YoutubeVideo };
package/dist/index.mjs CHANGED
@@ -1,3 +1,3 @@
1
- var t=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 p={403:{error:"invalid-request",message:"Invalid or missing API key",details:"Please ensure you have provided a valid API key"},404:{error:"invalid-request",message:"Endpoint does not exist",details:"The API endpoint you are trying to access does not exist"},429:{error:"limit-exceeded",message:"Limit exceeded",details:"You have exceeded the allowed request rate or quota limits"}},h=(i,e)=>i in p?new t({...p[i],message:p[i].message,details:e||p[i].details}):new t({error:"internal-error",message:"An unexpected error occurred",details:e});var c=class{config;constructor(e){this.config=e;}async fetch(e,r={},s="GET"){let n=`${this.config.baseUrl||"https://api.supadata.ai/v1"}${e.startsWith("/")?e:`/${e}`}`;if(s==="GET"&&Object.keys(r).length>0){let l=new URLSearchParams;Object.entries(r).forEach(([o,f])=>{f!=null&&l.append(o,String(f));}),n+=`?${l.toString()}`;}return this.fetchUrl(n,s,r)}async fetchUrl(e,r="GET",s){let a={method:r,headers:{"x-api-key":this.config.apiKey,"Content-Type":"application/json"}};r==="POST"&&s&&(a.body=JSON.stringify(s));let n=await fetch(e,a),l=n.headers.get("content-type");if(!n.ok){if([403,404,429].includes(n.status)){let o=await n.json();throw h(n.status,o.message)}if(l?.includes("application/json")){let o=await n.json();throw new t(o)}else throw new t({error:"internal-error",message:"Unexpected error response format",details:await n.text()})}try{if(!l?.includes("application/json"))throw new t({error:"internal-error",message:"Invalid response format",details:"Expected JSON response but received different content type"});return await n.json()}catch(o){throw new t({error:"internal-error",message:"Failed to parse response",details:o instanceof Error?o.message:"Unknown error"})}}};var m=class extends c{async transcript(e){return this.fetch("/youtube/transcript",e)}async translate(e){return this.fetch("/youtube/transcript/translate",e)}async video(e){return this.fetch("/youtube/video",e)}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))});validateLimit(e){if(e.limit!=null&&e.limit!=null&&(e.limit<1||e.limit>5e3))throw new t({error:"invalid-request",message:"Invalid limit.",details:"The limit must be between 1 and 5000."})}};var g=class extends c{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 r,s=[],a;do r=await(a?this.fetchUrl(a):this.fetch(`/web/crawl/${e}`)),r.pages&&(s=[...s,...r.pages]),a=r.next;while(a);return r}};var b=class{youtube;web;constructor(e){this.youtube=new m(e),this.web=new g(e);}};
1
+ var t=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 p={403:{error:"invalid-request",message:"Invalid or missing API key",details:"Please ensure you have provided a valid API key"},404:{error:"invalid-request",message:"Endpoint does not exist",details:"The API endpoint you are trying to access does not exist"},429:{error:"limit-exceeded",message:"Limit exceeded",details:"You have exceeded the allowed request rate or quota limits"}},h=(n,e)=>n in p?new t({...p[n],message:p[n].message,details:e||p[n].details}):new t({error:"internal-error",message:"An unexpected error occurred",details:e});var c=class{config;constructor(e){this.config=e;}async fetch(e,r={},s="GET"){let i=`${this.config.baseUrl||"https://api.supadata.ai/v1"}${e.startsWith("/")?e:`/${e}`}`;if(s==="GET"&&Object.keys(r).length>0){let l=new URLSearchParams;Object.entries(r).forEach(([o,f])=>{f!=null&&l.append(o,String(f));}),i+=`?${l.toString()}`;}return this.fetchUrl(i,s,r)}async fetchUrl(e,r="GET",s){let a={method:r,headers:{"x-api-key":this.config.apiKey,"Content-Type":"application/json"}};r==="POST"&&s&&(a.body=JSON.stringify(s));let i=await fetch(e,a),l=i.headers.get("content-type");if(!i.ok){if([403,404,429].includes(i.status)){let o=await i.json();throw h(i.status,o.message)}if(l?.includes("application/json")){let o=await i.json();throw new t(o)}else throw new t({error:"internal-error",message:"Unexpected error response format",details:await i.text()})}try{if(!l?.includes("application/json"))throw new t({error:"internal-error",message:"Invalid response format",details:"Expected JSON response but received different content type"});return await i.json()}catch(o){throw new t({error:"internal-error",message:"Failed to parse response",details:o instanceof Error?o.message:"Unknown error"})}}};var m=class extends c{async transcript(e){return this.fetch("/youtube/transcript",e)}async translate(e){return this.fetch("/youtube/transcript/translate",e)}async video(e){return this.fetch("/youtube/video",e)}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))});validateLimit(e){if(e.limit!=null&&e.limit!=null&&(e.limit<1||e.limit>5e3))throw new t({error:"invalid-request",message:"Invalid limit.",details:"The limit must be between 1 and 5000."})}};var g=class extends c{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 r,s=[],a;do r=await(a?this.fetchUrl(a):this.fetch(`/web/crawl/${e}`)),r.pages&&(s=[...s,...r.pages]),a=r.next;while(a);return r}};var b=class{youtube;web;constructor(e){this.youtube=new m(e),this.web=new g(e);}};
2
2
  export{c as BaseClient,b as Supadata,t as SupadataError,g as WebService,m as YouTubeService};//# sourceMappingURL=index.mjs.map
3
3
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/types.ts","../src/gateway-error-mapper.ts","../src/client.ts","../src/services/youtube.ts","../src/services/web.ts","../src/index.ts"],"names":["SupadataError","error","GATEWAY_STATUS_ERRORS","mapGatewayError","statusCode","errorText","BaseClient","config","endpoint","params","method","url","queryParams","key","value","body","options","response","contentType","errorData","YouTubeService","WebService","request","jobId","pages","nextUrl","Supadata"],"mappings":"AAoDO,IAAMA,CAAN,CAAA,cAA4B,KAAM,CACvC,MASA,OACA,CAAA,gBAAA,CAEA,WAAYC,CAAAA,CAAAA,CAKT,CACD,KAAA,CAAMA,CAAM,CAAA,OAAA,EAAW,8BAA8B,CACrD,CAAA,IAAA,CAAK,KAAQA,CAAAA,CAAAA,CAAM,KAAS,EAAA,gBAAA,CAC5B,IAAK,CAAA,OAAA,CAAUA,EAAM,OAAW,EAAA,8BAAA,CAChC,IAAK,CAAA,gBAAA,CAAmBA,CAAM,CAAA,gBAAA,EAAoB,EAClD,CAAA,IAAA,CAAK,KAAO,gBACd,CACF,ECtEA,IAAMC,CAAwB,CAAA,CAC5B,GAAK,CAAA,CACH,MAAO,iBACP,CAAA,OAAA,CAAS,4BACT,CAAA,OAAA,CAAS,iDACX,CAAA,CACA,GAAK,CAAA,CACH,MAAO,iBACP,CAAA,OAAA,CAAS,yBACT,CAAA,OAAA,CAAS,0DACX,CAAA,CACA,GAAK,CAAA,CACH,MAAO,gBACP,CAAA,OAAA,CAAS,gBACT,CAAA,OAAA,CAAS,4DACX,CACF,CAEaC,CAAAA,CAAAA,CAAkB,CAC7BC,CACAC,CAAAA,CAAAA,GAEID,CAAcF,IAAAA,CAAAA,CACT,IAAIF,CAAAA,CAAc,CACvB,GAAGE,EACDE,CACF,CAAA,CACA,OACEF,CAAAA,CAAAA,CAAsBE,CAAgD,CAAA,CACnE,OACL,CAAA,OAAA,CACEC,GACAH,CAAsBE,CAAAA,CAAgD,CACnE,CAAA,OACP,CAAC,CAAA,CAII,IAAIJ,CAAAA,CAAc,CACvB,KAAO,CAAA,gBAAA,CACP,OAAS,CAAA,8BAAA,CACT,OAASK,CAAAA,CACX,CAAC,CAAA,KC9CUC,CAAN,CAAA,KAAiB,CACZ,MAAA,CAEV,WAAYC,CAAA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vC,OAAO,IAAA,CAAK,MAAe,UAAY,CAAA,CAAE,GAAAA,CAAAA,CAAI,CAAC,CAChD,CAUA,MAAM,MAAMW,CAAuC,CAAA,CACjD,OAAO,IAAA,CAAK,KAAa,CAAA,YAAA,CAAcA,CAAS,CAAA,MAAM,CACxD,CASA,MAAM,eAAgBC,CAAAA,CAAAA,CAAkC,CACtD,IAAIN,CACAO,CAAAA,CAAAA,CAAkB,EAClBC,CAAAA,CAAAA,CAEJ,GACER,CAAAA,CAAW,MAAOQ,CAAAA,CACd,IAAK,CAAA,QAAA,CAAmBA,CAAO,CAC/B,CAAA,IAAA,CAAK,KAAgB,CAAA,CAAA,WAAA,EAAcF,CAAK,CAAA,CAAE,CAE1CN,CAAAA,CAAAA,CAAAA,CAAS,QACXO,CAAQ,CAAA,CAAC,GAAGA,CAAAA,CAAO,GAAGP,CAAAA,CAAS,KAAK,CAAA,CAAA,CAEtCQ,EAAUR,CAAS,CAAA,IAAA,CAAA,MACZQ,CAET,EAAA,OAAOR,CACT,CACF,ECpDO,IAAMS,EAAN,KAAe,CACX,OACA,CAAA,GAAA,CAET,WAAYnB,CAAAA,CAAAA,CAAwB,CAClC,IAAA,CAAK,QAAU,IAAIa,CAAAA,CAAeb,CAAM,CAAA,CACxC,KAAK,GAAM,CAAA,IAAIc,CAAWd,CAAAA,CAAM,EAClC,CACF","file":"index.mjs","sourcesContent":["export interface TranscriptChunk {\n text: string;\n offset: number;\n duration: number;\n lang: string;\n}\n\nexport interface Transcript {\n content: TranscriptChunk[] | string;\n lang: string;\n availableLangs: string[];\n}\n\nexport interface TranslatedTranscript {\n content: TranscriptChunk[] | string;\n lang: string;\n}\n\nexport interface Scrape {\n url: string;\n content: string;\n name: string;\n description: string;\n ogUrl: string;\n countCharacters: number;\n urls: string[];\n}\n\nexport interface SiteMap {\n urls: string[];\n}\n\nexport interface CrawlRequest {\n url: string;\n limit?: number;\n}\n\nexport interface Crawl {\n jobId: string;\n}\n\nexport interface CrawlJob {\n status: 'scraping' | 'completed' | 'failed' | 'cancelled';\n pages?: Scrape[];\n next?: string;\n}\n\nexport interface SupadataConfig {\n apiKey: string;\n baseUrl?: string;\n}\n\nexport class SupadataError extends Error {\n error:\n | 'invalid-request'\n | 'missing-parameters'\n | 'internal-error'\n | 'transcript-unavailable'\n | 'video-not-found'\n | 'video-id-invalid'\n | 'youtube-api-error'\n | 'limit-exceeded';\n details: string;\n documentationUrl: string;\n\n constructor(error: {\n error: SupadataError['error'];\n message?: string;\n details?: string;\n documentationUrl?: string;\n }) {\n super(error.message || 'An unexpected error occurred');\n this.error = error.error || 'internal-error';\n this.details = error.details || 'An unexpected error occurred';\n this.documentationUrl = error.documentationUrl || '';\n this.name = 'SupadataError';\n }\n}\n\nexport interface YoutubeVideo {\n id: string;\n title: string;\n description: string;\n duration: number;\n channel: {\n id: string;\n name: string;\n };\n tags: string[];\n thumbnail: string;\n uploadDate: string;\n viewCount: number;\n likeCount: number;\n transcriptLanguages: string[];\n}\n\nexport interface YoutubeChannel {\n id: string;\n name: string;\n handle: string;\n description: string;\n subscriberCount: number;\n videoCount: number;\n thumbnail: string;\n banner: string;\n}\n\nexport interface YoutubePlaylist {\n id: string;\n title: string;\n videoCount: number;\n viewCount: number;\n lastUpdated: string;\n description: string;\n thumbnail: string;\n}\n","import { SupadataError } from './types';\n\n/*\nThe API gateway returns errors in text/plain content type. \nAs a temporary workaround we're mapping them to SupadataError.\n*/\n\nconst GATEWAY_STATUS_ERRORS = {\n 403: {\n error: 'invalid-request' as const,\n message: 'Invalid or missing API key',\n details: 'Please ensure you have provided a valid API key',\n },\n 404: {\n error: 'invalid-request' as const,\n message: 'Endpoint does not exist',\n details: 'The API endpoint you are trying to access does not exist',\n },\n 429: {\n error: 'limit-exceeded' as const,\n message: 'Limit exceeded',\n details: 'You have exceeded the allowed request rate or quota limits',\n },\n};\n\nexport const mapGatewayError = (\n statusCode: number,\n errorText: string\n): SupadataError => {\n if (statusCode in GATEWAY_STATUS_ERRORS) {\n return new SupadataError({\n ...GATEWAY_STATUS_ERRORS[\n statusCode as keyof typeof GATEWAY_STATUS_ERRORS\n ],\n message:\n GATEWAY_STATUS_ERRORS[statusCode as keyof typeof GATEWAY_STATUS_ERRORS]\n .message,\n details:\n errorText ||\n GATEWAY_STATUS_ERRORS[statusCode as keyof typeof GATEWAY_STATUS_ERRORS]\n .details,\n });\n }\n\n // Default error if status code is not recognized\n return new SupadataError({\n error: 'internal-error',\n message: 'An unexpected error occurred',\n details: errorText,\n });\n};\n","import { SupadataConfig, SupadataError } from './types.js';\nimport { mapGatewayError } from './gateway-error-mapper.js';\n\nexport class BaseClient {\n protected config: SupadataConfig;\n\n constructor(config: SupadataConfig) {\n this.config = config;\n }\n\n protected async fetch<T>(\n endpoint: string,\n params: Record<string, any> = {},\n method: 'GET' | 'POST' = 'GET'\n ): Promise<T> {\n const baseUrl = this.config.baseUrl || 'https://api.supadata.ai/v1';\n let url = `${baseUrl}${\n endpoint.startsWith('/') ? endpoint : `/${endpoint}`\n }`;\n\n if (method === 'GET' && Object.keys(params).length > 0) {\n const queryParams = new URLSearchParams();\n Object.entries(params).forEach(([key, value]) => {\n if (value !== undefined && value !== null) {\n queryParams.append(key, String(value));\n }\n });\n url += `?${queryParams.toString()}`;\n }\n\n return this.fetchUrl<T>(url, method, params);\n }\n\n protected async fetchUrl<T>(\n url: string,\n method: 'GET' | 'POST' = 'GET',\n body?: Record<string, any>\n ): Promise<T> {\n const options: RequestInit = {\n method,\n headers: {\n 'x-api-key': this.config.apiKey,\n 'Content-Type': 'application/json',\n },\n };\n\n if (method === 'POST' && body) {\n options.body = JSON.stringify(body);\n }\n\n const response = await fetch(url, options);\n\n const contentType = response.headers.get('content-type');\n\n if (!response.ok) {\n // First check for gateway-specific status codes\n if ([403, 404, 429].includes(response.status)) {\n const errorData = await response.json();\n throw mapGatewayError(response.status, errorData.message);\n }\n\n // Handle standard API errors\n if (contentType?.includes('application/json')) {\n const errorData = await response.json();\n throw new SupadataError(errorData);\n } else {\n // Fallback for unexpected non-JSON errors\n throw new SupadataError({\n error: 'internal-error',\n message: 'Unexpected error response format',\n details: await response.text(),\n });\n }\n }\n\n try {\n if (!contentType?.includes('application/json')) {\n throw new SupadataError({\n error: 'internal-error',\n message: 'Invalid response format',\n details: 'Expected JSON response but received different content type',\n });\n }\n\n return (await response.json()) as T;\n } catch (error) {\n throw new SupadataError({\n error: 'internal-error',\n message: 'Failed to parse response',\n details: error instanceof Error ? error.message : 'Unknown error',\n });\n }\n }\n}\n","import { BaseClient } from '../client.js';\nimport {\n SupadataError,\n Transcript,\n TranslatedTranscript,\n YoutubeChannel,\n YoutubePlaylist,\n YoutubeVideo,\n} from '../types.js';\n\n/**\n * Ensures exactly one property from the specified keys is provided.\n * @example\n * // Valid: { url: \"...\" } or { videoId: \"...\" }\n * // Invalid: {} or { url: \"...\", videoId: \"...\" }\n */\ntype ExactlyOne<T, Keys extends keyof T> = {\n [K in Keys]: { [P in K]-?: T[P] } & { [P in Exclude<Keys, K>]?: never };\n}[Keys] &\n Omit<T, Keys>;\n\nexport type TranscriptParams = {\n lang?: string;\n text?: boolean;\n} & ExactlyOne<{ videoId: string; url: string }, 'videoId' | 'url'>;\n\nexport interface TranslateParams extends Omit<TranscriptParams, 'lang'> {\n lang: string;\n}\n\nexport interface ResourceParams {\n id: string;\n}\n\nexport interface ChannelVideosParams extends ResourceParams {\n limit?: number;\n}\n\nexport interface PlaylistVideosParams extends ResourceParams {\n limit?: number;\n}\n\nexport class YouTubeService extends BaseClient {\n /**\n * Fetches a transcript for a YouTube video.\n *\n * @param params - Parameters for fetching the transcript\n * @param params.videoId - The YouTube video ID (provide either this OR url)\n * @param params.url - The YouTube video URL (provide either this OR videoId)\n * @param params.lang - Optional language code for the transcript\n * @param params.text - Optional flag to return plain text instead of timestamped list\n * @returns A promise that resolves to the video transcript\n */\n async transcript(params: TranscriptParams): Promise<Transcript> {\n return this.fetch<Transcript>('/youtube/transcript', params);\n }\n\n /**\n * Translates a YouTube video transcript to a specified language.\n *\n * @param params - Parameters for translating the transcript\n * @param params.videoId - The YouTube video ID (provide either this OR url)\n * @param params.url - The YouTube video URL (provide either this OR videoId)\n * @param params.lang - The target language code for translation\n * @param params.text - Optional flag to return plain text instead of timestamped list\n * @returns A promise that resolves to the translated transcript\n */\n async translate(params: TranslateParams): Promise<TranslatedTranscript> {\n return this.fetch<TranslatedTranscript>(\n '/youtube/transcript/translate',\n params\n );\n }\n\n /**\n * Fetches a YouTube video based on the provided parameters.\n *\n * @param params - The parameters required to fetch the YouTube video.\n * @param params.id - The YouTube video ID.\n * @returns A promise that resolves to a `YoutubeVideo` object.\n */\n async video(params: ResourceParams): Promise<YoutubeVideo> {\n return this.fetch<YoutubeVideo>('/youtube/video', params);\n }\n\n /**\n * Fetches YouTube channel information and videos.\n *\n * @param params - The parameters required to fetch the YouTube channel information.\n * @param params.id - The YouTube channel ID.\n * @returns A promise that resolves to a `YoutubeChannel` object containing the channel information.\n *\n * @property videos - Fetches the videos of the YouTube channel.\n * @param params - The parameters required to fetch the YouTube channel videos.\n * @param params.id - The YouTube channel ID.\n * @param params.limit - The maximum number of videos to fetch.\n * Default: 30. Max: 5000.\n * @returns A promise that resolves to an array of video IDs.\n *\n * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000).\n */\n channel = Object.assign(\n async (params: ResourceParams): Promise<YoutubeChannel> => {\n return this.fetch<YoutubeChannel>('/youtube/channel', params);\n },\n {\n videos: async (params: ChannelVideosParams): Promise<string[]> => {\n // Validate the limit locally to avoid unnecessary API calls.\n this.validateLimit(params);\n return this.fetch<string[]>('/youtube/channel/videos', params);\n },\n }\n );\n\n /**\n * Fetches a YouTube playlist and its videos.\n *\n * @param params - The parameters required to fetch the playlist.\n * @param params.id - The YouTube playlist ID.\n * @returns A promise that resolves to a `YoutubePlaylist` object.\n *\n * @property videos - Fetches the videos of a YouTube playlist.\n * @param params - The parameters required to fetch the playlist videos.\n * @param params.id - The YouTube playlist ID.\n * @param params.limit - The maximum number of videos to fetch.\n * Default: 30. Max: 5000.\n * @returns A promise that resolves to an array of video IDs.\n *\n * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000).\n */\n playlist = Object.assign(\n async (params: ResourceParams): Promise<YoutubePlaylist> => {\n return this.fetch<YoutubePlaylist>('/youtube/playlist', params);\n },\n {\n videos: async (params: PlaylistVideosParams): Promise<string[]> => {\n // Validate the limit locally to avoid unnecessary API calls.\n this.validateLimit(params);\n return this.fetch<string[]>('/youtube/playlist/videos', params);\n },\n }\n );\n\n private validateLimit(params: { limit?: number }) {\n if (\n params.limit != undefined &&\n params.limit != null &&\n (params.limit < 1 || params.limit > 5000)\n ) {\n throw new SupadataError({\n error: 'invalid-request',\n message: 'Invalid limit.',\n details: 'The limit must be between 1 and 5000.',\n });\n }\n }\n}\n","import { BaseClient } from '../client.js';\nimport { Crawl, CrawlJob, CrawlRequest, Scrape, SiteMap } from '../types.js';\n\nexport class WebService extends BaseClient {\n /**\n * Extract content from any web page to Markdown format.\n *\n * @param url - URL of the webpage to scrape\n * @returns A promise that resolves to the scraped content\n */\n async scrape(url: string): Promise<Scrape> {\n return this.fetch<Scrape>('/web/scrape', { url });\n }\n\n /**\n * Extract all links found on a webpage.\n *\n * @param url - URL of the webpage to map\n * @returns A promise that resolves to a map of URLs found on the page\n */\n async map(url: string): Promise<SiteMap> {\n return this.fetch<SiteMap>('/web/map', { url });\n }\n\n /**\n * Create a crawl job to extract content from all pages on a website.\n *\n * @param request - Crawl request parameters\n * @param request.url - URL of the website to crawl\n * @param request.limit - Maximum number of pages to crawl (default: 100, max: 5000)\n * @returns A promise that resolves to the crawl job id\n */\n async crawl(request: CrawlRequest): Promise<Crawl> {\n return this.fetch<Crawl>('/web/crawl', request, 'POST');\n }\n\n /**\n * Get the status and results of a crawl job.\n * Automatically handles pagination to retrieve all pages from the crawl.\n *\n * @param jobId - The ID of the crawl job to retrieve\n * @returns A promise that resolves to the complete crawl job results\n */\n async getCrawlResults(jobId: string): Promise<CrawlJob> {\n let response: CrawlJob;\n let pages: Scrape[] = [];\n let nextUrl: string | undefined;\n\n do {\n response = await (nextUrl\n ? this.fetchUrl<CrawlJob>(nextUrl)\n : this.fetch<CrawlJob>(`/web/crawl/${jobId}`));\n\n if (response.pages) {\n pages = [...pages, ...response.pages];\n }\n nextUrl = response.next;\n } while (nextUrl);\n\n return response;\n }\n}\n","import { SupadataConfig } from './types.js';\nimport { YouTubeService } from './services/youtube.js';\nimport { WebService } from './services/web.js';\n\nexport * from './types.js';\nexport * from './client.js';\nexport * from './services/youtube.js';\nexport * from './services/web.js';\n\nexport class Supadata {\n readonly youtube: YouTubeService;\n readonly web: WebService;\n\n constructor(config: SupadataConfig) {\n this.youtube = new YouTubeService(config);\n this.web = new WebService(config);\n }\n}\n"]}
1
+ {"version":3,"sources":["../src/types.ts","../src/gateway-error-mapper.ts","../src/client.ts","../src/services/youtube.ts","../src/services/web.ts","../src/index.ts"],"names":["SupadataError","error","GATEWAY_STATUS_ERRORS","mapGatewayError","statusCode","errorText","BaseClient","config","endpoint","params","method","url","queryParams","key","value","body","options","response","contentType","errorData","YouTubeService","WebService","request","jobId","pages","nextUrl","Supadata"],"mappings":"AAoDO,IAAMA,CAAN,CAAA,cAA4B,KAAM,CACvC,MASA,OACA,CAAA,gBAAA,CAEA,WAAYC,CAAAA,CAAAA,CAKT,CACD,KAAA,CAAMA,CAAM,CAAA,OAAA,EAAW,8BAA8B,CACrD,CAAA,IAAA,CAAK,KAAQA,CAAAA,CAAAA,CAAM,KAAS,EAAA,gBAAA,CAC5B,IAAK,CAAA,OAAA,CAAUA,EAAM,OAAW,EAAA,8BAAA,CAChC,IAAK,CAAA,gBAAA,CAAmBA,CAAM,CAAA,gBAAA,EAAoB,EAClD,CAAA,IAAA,CAAK,KAAO,gBACd,CACF,ECtEA,IAAMC,CAAwB,CAAA,CAC5B,GAAK,CAAA,CACH,MAAO,iBACP,CAAA,OAAA,CAAS,4BACT,CAAA,OAAA,CAAS,iDACX,CAAA,CACA,GAAK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R,CACT,CACF,ECpDO,IAAMS,EAAN,KAAe,CACX,OACA,CAAA,GAAA,CAET,WAAYnB,CAAAA,CAAAA,CAAwB,CAClC,IAAA,CAAK,QAAU,IAAIa,CAAAA,CAAeb,CAAM,CAAA,CACxC,KAAK,GAAM,CAAA,IAAIc,CAAWd,CAAAA,CAAM,EAClC,CACF","file":"index.mjs","sourcesContent":["export interface TranscriptChunk {\n text: string;\n offset: number;\n duration: number;\n lang: string;\n}\n\nexport interface Transcript {\n content: TranscriptChunk[] | string;\n lang: string;\n availableLangs: string[];\n}\n\nexport interface TranslatedTranscript {\n content: TranscriptChunk[] | string;\n lang: string;\n}\n\nexport interface Scrape {\n url: string;\n content: string;\n name: string;\n description: string;\n ogUrl: string;\n countCharacters: number;\n urls: string[];\n}\n\nexport interface SiteMap {\n urls: string[];\n}\n\nexport interface CrawlRequest {\n url: string;\n limit?: number;\n}\n\nexport interface Crawl {\n jobId: string;\n}\n\nexport interface CrawlJob {\n status: 'scraping' | 'completed' | 'failed' | 'cancelled';\n pages?: Scrape[];\n next?: string;\n}\n\nexport interface SupadataConfig {\n apiKey: string;\n baseUrl?: string;\n}\n\nexport class SupadataError extends Error {\n error:\n | 'invalid-request'\n | 'missing-parameters'\n | 'internal-error'\n | 'transcript-unavailable'\n | 'video-not-found'\n | 'video-id-invalid'\n | 'youtube-api-error'\n | 'limit-exceeded';\n details: string;\n documentationUrl: string;\n\n constructor(error: {\n error: SupadataError['error'];\n message?: string;\n details?: string;\n documentationUrl?: string;\n }) {\n super(error.message || 'An unexpected error occurred');\n this.error = error.error || 'internal-error';\n this.details = error.details || 'An unexpected error occurred';\n this.documentationUrl = error.documentationUrl || '';\n this.name = 'SupadataError';\n }\n}\n\nexport interface YoutubeVideo {\n id: string;\n title: string;\n description: string;\n duration: number;\n channel: {\n id: string;\n name: string;\n };\n tags: string[];\n thumbnail: string;\n uploadDate: string;\n viewCount: number;\n likeCount: number;\n transcriptLanguages: string[];\n}\n\nexport interface YoutubeChannel {\n id: string;\n name: string;\n handle: string;\n description: string;\n subscriberCount: number;\n videoCount: number;\n thumbnail: string;\n banner: string;\n}\n\nexport interface YoutubePlaylist {\n id: string;\n title: string;\n videoCount: number;\n viewCount: number;\n lastUpdated: string;\n description: string;\n thumbnail: string;\n}\n","import { SupadataError } from './types';\n\n/*\nThe API gateway returns errors in text/plain content type. \nAs a temporary workaround we're mapping them to SupadataError.\n*/\n\nconst GATEWAY_STATUS_ERRORS = {\n 403: {\n error: 'invalid-request' as const,\n message: 'Invalid or missing API key',\n details: 'Please ensure you have provided a valid API key',\n },\n 404: {\n error: 'invalid-request' as const,\n message: 'Endpoint does not exist',\n details: 'The API endpoint you are trying to access does not exist',\n },\n 429: {\n error: 'limit-exceeded' as const,\n message: 'Limit exceeded',\n details: 'You have exceeded the allowed request rate or quota limits',\n },\n};\n\nexport const mapGatewayError = (\n statusCode: number,\n errorText: string\n): SupadataError => {\n if (statusCode in GATEWAY_STATUS_ERRORS) {\n return new SupadataError({\n ...GATEWAY_STATUS_ERRORS[\n statusCode as keyof typeof GATEWAY_STATUS_ERRORS\n ],\n message:\n GATEWAY_STATUS_ERRORS[statusCode as keyof typeof GATEWAY_STATUS_ERRORS]\n .message,\n details:\n errorText ||\n GATEWAY_STATUS_ERRORS[statusCode as keyof typeof GATEWAY_STATUS_ERRORS]\n .details,\n });\n }\n\n // Default error if status code is not recognized\n return new SupadataError({\n error: 'internal-error',\n message: 'An unexpected error occurred',\n details: errorText,\n });\n};\n","import { SupadataConfig, SupadataError } from './types.js';\nimport { mapGatewayError } from './gateway-error-mapper.js';\n\nexport class BaseClient {\n protected config: SupadataConfig;\n\n constructor(config: SupadataConfig) {\n this.config = config;\n }\n\n protected async fetch<T>(\n endpoint: string,\n params: Record<string, any> = {},\n method: 'GET' | 'POST' = 'GET'\n ): Promise<T> {\n const baseUrl = this.config.baseUrl || 'https://api.supadata.ai/v1';\n let url = `${baseUrl}${\n endpoint.startsWith('/') ? endpoint : `/${endpoint}`\n }`;\n\n if (method === 'GET' && Object.keys(params).length > 0) {\n const queryParams = new URLSearchParams();\n Object.entries(params).forEach(([key, value]) => {\n if (value !== undefined && value !== null) {\n queryParams.append(key, String(value));\n }\n });\n url += `?${queryParams.toString()}`;\n }\n\n return this.fetchUrl<T>(url, method, params);\n }\n\n protected async fetchUrl<T>(\n url: string,\n method: 'GET' | 'POST' = 'GET',\n body?: Record<string, any>\n ): Promise<T> {\n const options: RequestInit = {\n method,\n headers: {\n 'x-api-key': this.config.apiKey,\n 'Content-Type': 'application/json',\n },\n };\n\n if (method === 'POST' && body) {\n options.body = JSON.stringify(body);\n }\n\n const response = await fetch(url, options);\n\n const contentType = response.headers.get('content-type');\n\n if (!response.ok) {\n // First check for gateway-specific status codes\n if ([403, 404, 429].includes(response.status)) {\n const errorData = await response.json();\n throw mapGatewayError(response.status, errorData.message);\n }\n\n // Handle standard API errors\n if (contentType?.includes('application/json')) {\n const errorData = await response.json();\n throw new SupadataError(errorData);\n } else {\n // Fallback for unexpected non-JSON errors\n throw new SupadataError({\n error: 'internal-error',\n message: 'Unexpected error response format',\n details: await response.text(),\n });\n }\n }\n\n try {\n if (!contentType?.includes('application/json')) {\n throw new SupadataError({\n error: 'internal-error',\n message: 'Invalid response format',\n details: 'Expected JSON response but received different content type',\n });\n }\n\n return (await response.json()) as T;\n } catch (error) {\n throw new SupadataError({\n error: 'internal-error',\n message: 'Failed to parse response',\n details: error instanceof Error ? error.message : 'Unknown error',\n });\n }\n }\n}\n","import { BaseClient } from '../client.js';\nimport {\n SupadataError,\n Transcript,\n TranslatedTranscript,\n YoutubeChannel,\n YoutubePlaylist,\n YoutubeVideo,\n} from '../types.js';\n\n/**\n * Ensures exactly one property from the specified keys is provided.\n * @example\n * // Valid: { url: \"...\" } or { videoId: \"...\" }\n * // Invalid: {} or { url: \"...\", videoId: \"...\" }\n */\ntype ExactlyOne<T, Keys extends keyof T> = {\n [K in Keys]: { [P in K]-?: T[P] } & { [P in Exclude<Keys, K>]?: never };\n}[Keys] &\n Omit<T, Keys>;\n\nexport type TranscriptParams = {\n lang?: string;\n text?: boolean;\n} & ExactlyOne<{ videoId: string; url: string }, 'videoId' | 'url'>;\n\nexport interface TranslateParams extends Omit<TranscriptParams, 'lang'> {\n lang: string;\n}\n\nexport interface ResourceParams {\n id: string;\n}\n\nexport interface ChannelVideosParams extends ResourceParams {\n limit?: number;\n type?: 'video' | 'short' | 'all';\n}\n\nexport interface PlaylistVideosParams extends ResourceParams {\n limit?: number;\n}\n\nexport interface VideoIds {\n videoIds: string[];\n shortIds: string[];\n}\n\nexport class YouTubeService extends BaseClient {\n /**\n * Fetches a transcript for a YouTube video.\n *\n * @param params - Parameters for fetching the transcript\n * @param params.videoId - The YouTube video ID (provide either this OR url)\n * @param params.url - The YouTube video URL (provide either this OR videoId)\n * @param params.lang - Optional language code for the transcript\n * @param params.text - Optional flag to return plain text instead of timestamped list\n * @returns A promise that resolves to the video transcript\n */\n async transcript(params: TranscriptParams): Promise<Transcript> {\n return this.fetch<Transcript>('/youtube/transcript', params);\n }\n\n /**\n * Translates a YouTube video transcript to a specified language.\n *\n * @param params - Parameters for translating the transcript\n * @param params.videoId - The YouTube video ID (provide either this OR url)\n * @param params.url - The YouTube video URL (provide either this OR videoId)\n * @param params.lang - The target language code for translation\n * @param params.text - Optional flag to return plain text instead of timestamped list\n * @returns A promise that resolves to the translated transcript\n */\n async translate(params: TranslateParams): Promise<TranslatedTranscript> {\n return this.fetch<TranslatedTranscript>(\n '/youtube/transcript/translate',\n params\n );\n }\n\n /**\n * Fetches a YouTube video based on the provided parameters.\n *\n * @param params - The parameters required to fetch the YouTube video.\n * @param params.id - The YouTube video ID.\n * @returns A promise that resolves to a `YoutubeVideo` object.\n */\n async video(params: ResourceParams): Promise<YoutubeVideo> {\n return this.fetch<YoutubeVideo>('/youtube/video', params);\n }\n\n /**\n * Fetches YouTube channel information and videos.\n *\n * @param params - The parameters required to fetch the YouTube channel information.\n * @param params.id - The YouTube channel ID.\n * @returns A promise that resolves to a `YoutubeChannel` object containing the channel information.\n *\n * @property videos - Fetches the videos of the YouTube channel.\n * @param params - The parameters required to fetch the YouTube channel videos.\n * @param params.id - The YouTube channel ID.\n * @param params.limit - The maximum number of videos to fetch.\n * Default: 30. Max: 5000.\n * @param params.type - The type of videos to fetch.\n * Default: 'video'.\n * Allowed values: 'video', 'short', 'all'.\n * @returns A promise that resolves to an array of video IDs.\n *\n * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000).\n */\n channel = Object.assign(\n async (params: ResourceParams): Promise<YoutubeChannel> => {\n return this.fetch<YoutubeChannel>('/youtube/channel', params);\n },\n {\n videos: async (params: ChannelVideosParams): Promise<VideoIds> => {\n // Validate the limit locally to avoid unnecessary API calls.\n this.validateLimit(params);\n return this.fetch<VideoIds>('/youtube/channel/videos', params);\n },\n }\n );\n\n /**\n * Fetches a YouTube playlist and its videos.\n *\n * @param params - The parameters required to fetch the playlist.\n * @param params.id - The YouTube playlist ID.\n * @returns A promise that resolves to a `YoutubePlaylist` object.\n *\n * @property videos - Fetches the videos of a YouTube playlist.\n * @param params - The parameters required to fetch the playlist videos.\n * @param params.id - The YouTube playlist ID.\n * @param params.limit - The maximum number of videos to fetch.\n * Default: 30. Max: 5000.\n * @returns A promise that resolves to an array of video IDs.\n *\n * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000).\n */\n playlist = Object.assign(\n async (params: ResourceParams): Promise<YoutubePlaylist> => {\n return this.fetch<YoutubePlaylist>('/youtube/playlist', params);\n },\n {\n videos: async (params: PlaylistVideosParams): Promise<VideoIds> => {\n // Validate the limit locally to avoid unnecessary API calls.\n this.validateLimit(params);\n return this.fetch<VideoIds>('/youtube/playlist/videos', params);\n },\n }\n );\n\n private validateLimit(params: { limit?: number }) {\n if (\n params.limit != undefined &&\n params.limit != null &&\n (params.limit < 1 || params.limit > 5000)\n ) {\n throw new SupadataError({\n error: 'invalid-request',\n message: 'Invalid limit.',\n details: 'The limit must be between 1 and 5000.',\n });\n }\n }\n}\n","import { BaseClient } from '../client.js';\nimport { Crawl, CrawlJob, CrawlRequest, Scrape, SiteMap } from '../types.js';\n\nexport class WebService extends BaseClient {\n /**\n * Extract content from any web page to Markdown format.\n *\n * @param url - URL of the webpage to scrape\n * @returns A promise that resolves to the scraped content\n */\n async scrape(url: string): Promise<Scrape> {\n return this.fetch<Scrape>('/web/scrape', { url });\n }\n\n /**\n * Extract all links found on a webpage.\n *\n * @param url - URL of the webpage to map\n * @returns A promise that resolves to a map of URLs found on the page\n */\n async map(url: string): Promise<SiteMap> {\n return this.fetch<SiteMap>('/web/map', { url });\n }\n\n /**\n * Create a crawl job to extract content from all pages on a website.\n *\n * @param request - Crawl request parameters\n * @param request.url - URL of the website to crawl\n * @param request.limit - Maximum number of pages to crawl (default: 100, max: 5000)\n * @returns A promise that resolves to the crawl job id\n */\n async crawl(request: CrawlRequest): Promise<Crawl> {\n return this.fetch<Crawl>('/web/crawl', request, 'POST');\n }\n\n /**\n * Get the status and results of a crawl job.\n * Automatically handles pagination to retrieve all pages from the crawl.\n *\n * @param jobId - The ID of the crawl job to retrieve\n * @returns A promise that resolves to the complete crawl job results\n */\n async getCrawlResults(jobId: string): Promise<CrawlJob> {\n let response: CrawlJob;\n let pages: Scrape[] = [];\n let nextUrl: string | undefined;\n\n do {\n response = await (nextUrl\n ? this.fetchUrl<CrawlJob>(nextUrl)\n : this.fetch<CrawlJob>(`/web/crawl/${jobId}`));\n\n if (response.pages) {\n pages = [...pages, ...response.pages];\n }\n nextUrl = response.next;\n } while (nextUrl);\n\n return response;\n }\n}\n","import { SupadataConfig } from './types.js';\nimport { YouTubeService } from './services/youtube.js';\nimport { WebService } from './services/web.js';\n\nexport * from './types.js';\nexport * from './client.js';\nexport * from './services/youtube.js';\nexport * from './services/web.js';\n\nexport class Supadata {\n readonly youtube: YouTubeService;\n readonly web: WebService;\n\n constructor(config: SupadataConfig) {\n this.youtube = new YouTubeService(config);\n this.web = new WebService(config);\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supadata/js",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "TypeScript / JavaScript SDK for Supadata API",
5
5
  "homepage": "https://supadata.ai",
6
6
  "repository": "https://github.com/supadata-ai/js",