@supadata/js 1.1.0 → 1.1.2
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 +6 -4
- package/dist/index.cjs +2 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +6 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -57,7 +57,7 @@ const channel: YoutubeChannel = await supadata.youtube.channel({
|
|
|
57
57
|
// Get a list of video IDs from a YouTube channel
|
|
58
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'
|
|
60
|
+
type: 'all', // 'video', 'short', 'live', 'all'
|
|
61
61
|
limit: 10,
|
|
62
62
|
});
|
|
63
63
|
|
|
@@ -77,20 +77,22 @@ const transcriptBatch = await supadata.youtube.transcript.batch({
|
|
|
77
77
|
videoIds: ['dQw4w9WgXcQ', 'xvFZjo5PgG0'],
|
|
78
78
|
// playlistId: 'PLlaN88a7y2_plecYoJxvRFTLHVbIVAOoc' // alternatively
|
|
79
79
|
// channelId: 'UC_9-kyTW8ZkZNDHQJ6FgpwQ' // alternatively
|
|
80
|
-
lang: 'en'
|
|
80
|
+
lang: 'en',
|
|
81
81
|
});
|
|
82
82
|
console.log(`Started transcript batch job: ${transcriptBatch.jobId}`);
|
|
83
83
|
|
|
84
84
|
// Start a YouTube video metadata batch job
|
|
85
85
|
const videoBatch = await supadata.youtube.video.batch({
|
|
86
|
-
videoIds: ['dQw4w9WgXcQ', 'xvFZjo5PgG0', 'L_jWHffIx5E']
|
|
86
|
+
videoIds: ['dQw4w9WgXcQ', 'xvFZjo5PgG0', 'L_jWHffIx5E'],
|
|
87
87
|
// playlistId: 'PLlaN88a7y2_plecYoJxvRFTLHVbIVAOoc' // alternatively
|
|
88
88
|
// channelId: 'UC_9-kyTW8ZkZNDHQJ6FgpwQ' // alternatively
|
|
89
89
|
});
|
|
90
90
|
console.log(`Started video batch job: ${videoBatch.jobId}`);
|
|
91
91
|
|
|
92
92
|
// Get results for a batch job (poll until status is 'completed' or 'failed')
|
|
93
|
-
const batchResults = await supadata.youtube.batch.getBatchResults(
|
|
93
|
+
const batchResults = await supadata.youtube.batch.getBatchResults(
|
|
94
|
+
transcriptBatch.jobId
|
|
95
|
+
); // or videoBatch.jobId
|
|
94
96
|
if (batchResults.status === 'completed') {
|
|
95
97
|
console.log('Batch job completed:', batchResults.results);
|
|
96
98
|
console.log('Stats:', batchResults.stats);
|
package/dist/index.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
'use strict';var
|
|
2
|
-
exports.BaseClient=
|
|
1
|
+
'use strict';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 o=class{config;constructor(e){this.config=e;}async fetch(e,t={},i="GET"){let s=`${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(([a,b])=>{b!=null&&u.append(a,String(b));}),s+=`?${u.toString()}`;}return this.fetchUrl(s,i,t)}async fetchUrl(e,t="GET",i){let n={method:t,headers:{"x-api-key":this.config.apiKey,"Content-Type":"application/json"}};t==="POST"&&i&&(n.body=JSON.stringify(i));let s=await fetch(e,n),u=s.headers.get("content-type");if(!s.ok)if(u?.includes("application/json")){let a=await s.json();throw new r(a)}else throw new r({error:"internal-error",message:"Unexpected error response format",details:await s.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 s.json()}catch(a){throw new r({error:"internal-error",message:"Failed to parse response",details:a instanceof Error?a.message:"Unknown error"})}}};var d=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 p=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=[],n;do t=await(n?this.fetchUrl(n):this.fetch(`/web/crawl/${e}`)),t.pages&&(i=[...i,...t.pages]),n=t.next;while(n);return t}};var h=class{youtube;web;constructor(e){this.youtube=new d(e),this.web=new p(e);}};
|
|
2
|
+
exports.BaseClient=o;exports.Supadata=h;exports.SupadataError=r;exports.WebService=p;exports.YouTubeService=d;//# sourceMappingURL=index.cjs.map
|
|
3
3
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -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","jobId","WebService","request","pages","nextUrl","Supadata"],"mappings":"aAoDO,IAAMA,EAAN,cAA4B,KAAM,CACvC,KAAA,CASA,QACA,gBAEA,CAAA,WAAA,CAAYC,CAKT,CAAA,CACD,MAAMA,CAAM,CAAA,OAAA,EAAW,8BAA8B,CAAA,CACrD,KAAK,KAAQA,CAAAA,CAAAA,CAAM,KAAS,EAAA,gBAAA,CAC5B,KAAK,OAAUA,CAAAA,CAAAA,CAAM,OAAW,EAAA,8BAAA,CAChC,KAAK,gBAAmBA,CAAAA,CAAAA,CAAM,gBAAoB,EAAA,EAAA,CAClD,KAAK,IAAO,CAAA,gBACd,CACF,ECtEA,IAAMC,EAAwB,CAC5B,GAAA,CAAK,CACH,KAAA,CAAO,kBACP,OAAS,CAAA,4BAAA,CACT,OAAS,CAAA,iDACX,EACA,GAAK,CAAA,CACH,KAAO,CAAA,iBAAA,CACP,QAAS,yBACT,CAAA,OAAA,CAAS,0DACX,CAAA,CACA,IAAK,CACH,KAAA,CAAO,gBACP,CAAA,OAAA,CAAS,iBACT,OAAS,CAAA,4DACX,CACF,CAAA,CAEaC,EAAkB,CAC7BC,CAAAA,CACAC,CAEID,GAAAA,CAAAA,IAAcF,EACT,IAAIF,CAAAA,CAAc,CACvB,GAAGE,CAAAA,CACDE,CACF,CACA,CAAA,OAAA,CACEF,CAAsBE,CAAAA,CAAgD,EACnE,OACL,CAAA,OAAA,CACEC,CACAH,EAAAA,CAAAA,CAAsBE,CAAgD,CACnE,CAAA,OACP,CAAC,CAAA,CAII,IAAIJ,CAAc,CAAA,CACvB,KAAO,CAAA,gBAAA,CACP,QAAS,8BACT,CAAA,OAAA,CAASK,CACX,CAAC,EC9CUC,IAAAA,CAAAA,CAAN,K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CAAUR,EAAS,IACZQ,CAAAA,MAAAA,CAAAA,EAET,OAAOR,CACT,CACF,ECpDaS,IAAAA,CAAAA,CAAN,KAAe,CACX,QACA,GAET,CAAA,WAAA,CAAYnB,CAAwB,CAAA,CAClC,KAAK,OAAU,CAAA,IAAIa,CAAeb,CAAAA,CAAM,EACxC,IAAK,CAAA,GAAA,CAAM,IAAIe,CAAWf,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\nexport interface YoutubeBatchSource {\n videoIds?: string[];\n playlistId?: string;\n channelId?: string;\n limit?: number;\n}\n\nexport interface YoutubeTranscriptBatchRequest extends YoutubeBatchSource {\n lang?: string;\n text?: boolean;\n}\n\nexport interface YoutubeVideoBatchRequest extends YoutubeBatchSource {}\n\nexport interface YoutubeBatchJob {\n jobId: string;\n}\n\nexport type YoutubeBatchJobStatus =\n | 'queued'\n | 'active'\n | 'completed'\n | 'failed';\n\nexport interface YoutubeBatchResultItem {\n videoId: string;\n transcript?: Transcript;\n video?: YoutubeVideo;\n errorCode?: string;\n}\n\nexport interface YoutubeBatchStats {\n total: number;\n succeeded: number;\n failed: number;\n}\n\nexport interface YoutubeBatchResults {\n status: YoutubeBatchJobStatus;\n results?: YoutubeBatchResultItem[];\n stats?: YoutubeBatchStats;\n completedAt?: 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 YoutubeBatchJob,\n YoutubeBatchResults,\n YoutubeChannel,\n YoutubePlaylist,\n YoutubeTranscriptBatchRequest,\n YoutubeVideo,\n YoutubeVideoBatchRequest,\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 * Handles YouTube Transcript operations.\n */\n transcript = Object.assign(\n /**\n * Fetches a transcript for a YouTube video.\n * @param params - Parameters for fetching the transcript\n * @param params.videoId - The YouTube video ID (mutually exclusive with url)\n * @param params.url - The YouTube video URL (mutually exclusive with videoId)\n * @param params.lang - The language code for the transcript (optional)\n * @param params.text - Whether to return only the text content (optional)\n * @returns A promise that resolves to a Transcript object\n */\n async (params: TranscriptParams): Promise<Transcript> => {\n return this.fetch<Transcript>('/youtube/transcript', params);\n },\n {\n /**\n * Batch fetches transcripts for multiple YouTube videos.\n * @param params - Parameters for the transcript batch job\n * @param params.videoIds - Array of YouTube video IDs to fetch transcripts for\n * @param params.lang - The language code for the transcripts (optional)\n * @param params.limit - Maximum number of videos to process (optional, default: 10, max: 5000)\n * @param params.text - Whether to return only the text content (optional)\n * @returns A promise that resolves to a YoutubeBatchJob object with the job ID\n */\n batch: async (\n params: YoutubeTranscriptBatchRequest\n ): Promise<YoutubeBatchJob> => {\n this.validateBatchLimit(params);\n return this.fetch<YoutubeBatchJob>(\n '/youtube/transcript/batch',\n params,\n 'POST'\n );\n },\n }\n );\n\n /**\n * Handles YouTube video operations.\n */\n video = Object.assign(\n /**\n * Fetches a YouTube video based on the provided parameters.\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 (params: ResourceParams): Promise<YoutubeVideo> => {\n return this.fetch<YoutubeVideo>('/youtube/video', params);\n },\n {\n /**\n * Batch fetches metadata for multiple YouTube videos.\n * @param params - Parameters for the video metadata batch job\n * @param params.videoIds - Array of YouTube video IDs to fetch metadata for\n * @param params.limit - Maximum number of videos to process (optional, default: 10, max: 5000)\n * @returns A promise that resolves to a YoutubeBatchJob object with the job ID\n */\n batch: async (\n params: YoutubeVideoBatchRequest\n ): Promise<YoutubeBatchJob> => {\n this.validateBatchLimit(params);\n return this.fetch<YoutubeBatchJob>(\n '/youtube/video/batch',\n params,\n 'POST'\n );\n },\n }\n );\n\n /**\n * Handles YouTube channel operations.\n */\n channel = Object.assign(\n /**\n * Fetches YouTube channel information.\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 async (params: ResourceParams): Promise<YoutubeChannel> => {\n return this.fetch<YoutubeChannel>('/youtube/channel', params);\n },\n {\n /**\n * Fetches the videos of a 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 (default: 30, max: 5000)\n * @param params.type - The type of videos to fetch ('video', 'short', or 'all', default: 'video')\n * @returns A promise that resolves to an object containing arrays of video IDs and short IDs\n * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000)\n */\n videos: async (params: ChannelVideosParams): Promise<VideoIds> => {\n this.validateLimit(params);\n return this.fetch<VideoIds>('/youtube/channel/videos', params);\n },\n }\n );\n\n /**\n * Handles YouTube playlist operations.\n */\n playlist = Object.assign(\n /**\n * Fetches a YouTube playlist.\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 async (params: ResourceParams): Promise<YoutubePlaylist> => {\n return this.fetch<YoutubePlaylist>('/youtube/playlist', params);\n },\n {\n /**\n * 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 (default: 30, max: 5000)\n * @returns A promise that resolves to an object containing arrays of video IDs and short IDs\n * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000)\n */\n videos: async (params: PlaylistVideosParams): Promise<VideoIds> => {\n this.validateLimit(params);\n return this.fetch<VideoIds>('/youtube/playlist/videos', params);\n },\n }\n );\n\n /**\n * Handles YouTube batch operations.\n */\n batch = {\n /**\n * Retrieves the status and results of a batch job.\n * @param jobId - The ID of the batch job\n * @returns A promise that resolves to the YoutubeBatchResults containing job status and results\n * @throws {SupadataError} If jobId is not provided\n */\n getBatchResults: async (jobId: string): Promise<YoutubeBatchResults> => {\n if (!jobId) {\n throw new SupadataError({\n error: 'missing-parameters',\n message: 'Missing jobId',\n details: 'The jobId parameter is required to get batch results.',\n });\n }\n return this.fetch<YoutubeBatchResults>(`/youtube/batch/${jobId}`);\n },\n };\n\n /**\n * Translates a YouTube video transcript to a specified language.\n * @param params - Parameters for translating the transcript\n * @param params.videoId - The YouTube video ID (mutually exclusive with url)\n * @param params.url - The YouTube video URL (mutually exclusive with videoId)\n * @param params.lang - The target language code for translation\n * @param params.text - Whether to return only the text content (optional)\n * @returns A promise that resolves to a TranslatedTranscript object\n */\n translate = async (\n params: TranslateParams\n ): Promise<TranslatedTranscript> => {\n return this.fetch<TranslatedTranscript>(\n '/youtube/transcript/translate',\n params\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 // Add a specific validator for batch limits as per documentation (Max: 5000, Default: 10)\n private validateBatchLimit(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 for batch operation.',\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/client.ts","../src/services/youtube.ts","../src/services/web.ts","../src/index.ts"],"names":["SupadataError","error","BaseClient","config","endpoint","params","method","url","queryParams","key","value","body","options","response","contentType","errorData","YouTubeService","jobId","WebService","request","pages","nextUrl","Supadata"],"mappings":"aAoDO,IAAMA,CAAN,CAAA,cAA4B,KAAM,CACvC,KAQA,CAAA,OAAA,CACA,gBAEA,CAAA,WAAA,CAAYC,CAKT,CAAA,CACD,KAAMA,CAAAA,CAAAA,CAAM,SAAW,8BAA8B,CAAA,CACrD,IAAK,CAAA,KAAA,CAAQA,CAAM,CAAA,KAAA,EAAS,gBAC5B,CAAA,IAAA,CAAK,QAAUA,CAAM,CAAA,OAAA,EAAW,8BAChC,CAAA,IAAA,CAAK,gBAAmBA,CAAAA,CAAAA,CAAM,gBAAoB,EAAA,EAAA,CAClD,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AQ,CAAC,GAAGA,CAAO,CAAA,GAAGP,CAAS,CAAA,KAAK,CAEtCQ,CAAAA,CAAAA,CAAAA,CAAUR,EAAS,IACZQ,CAAAA,MAAAA,CAAAA,EAET,OAAOR,CACT,CACF,ECpDaS,IAAAA,CAAAA,CAAN,KAAe,CACX,OAAA,CACA,GAET,CAAA,WAAA,CAAYnB,CAAwB,CAAA,CAClC,IAAK,CAAA,OAAA,CAAU,IAAIa,CAAeb,CAAAA,CAAM,CACxC,CAAA,IAAA,CAAK,GAAM,CAAA,IAAIe,CAAWf,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 | 'internal-error'\n | 'transcript-unavailable'\n | 'not-found'\n | 'unauthorized'\n | 'upgrade-required'\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\nexport interface YoutubeBatchSource {\n videoIds?: string[];\n playlistId?: string;\n channelId?: string;\n limit?: number;\n}\n\nexport interface YoutubeTranscriptBatchRequest extends YoutubeBatchSource {\n lang?: string;\n text?: boolean;\n}\n\nexport interface YoutubeVideoBatchRequest extends YoutubeBatchSource {}\n\nexport interface YoutubeBatchJob {\n jobId: string;\n}\n\nexport type YoutubeBatchJobStatus =\n | 'queued'\n | 'active'\n | 'completed'\n | 'failed';\n\nexport interface YoutubeBatchResultItem {\n videoId: string;\n transcript?: Transcript;\n video?: YoutubeVideo;\n errorCode?: string;\n}\n\nexport interface YoutubeBatchStats {\n total: number;\n succeeded: number;\n failed: number;\n}\n\nexport interface YoutubeBatchResults {\n status: YoutubeBatchJobStatus;\n results?: YoutubeBatchResultItem[];\n stats?: YoutubeBatchStats;\n completedAt?: string;\n}\n","import { SupadataConfig, SupadataError } from './types.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 // 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 YoutubeBatchJob,\n YoutubeBatchResults,\n YoutubeChannel,\n YoutubePlaylist,\n YoutubeTranscriptBatchRequest,\n YoutubeVideo,\n YoutubeVideoBatchRequest,\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' | 'live' | 'all';\n}\n\nexport interface PlaylistVideosParams extends ResourceParams {\n limit?: number;\n}\n\nexport interface VideoIds {\n videoIds: string[];\n shortIds: string[];\n liveIds: string[];\n}\n\nexport class YouTubeService extends BaseClient {\n /**\n * Handles YouTube Transcript operations.\n */\n transcript = Object.assign(\n /**\n * Fetches a transcript for a YouTube video.\n * @param params - Parameters for fetching the transcript\n * @param params.videoId - The YouTube video ID (mutually exclusive with url)\n * @param params.url - The YouTube video URL (mutually exclusive with videoId)\n * @param params.lang - The language code for the transcript (optional)\n * @param params.text - Whether to return only the text content (optional)\n * @returns A promise that resolves to a Transcript object\n */\n async (params: TranscriptParams): Promise<Transcript> => {\n return this.fetch<Transcript>('/youtube/transcript', params);\n },\n {\n /**\n * Batch fetches transcripts for multiple YouTube videos.\n * @param params - Parameters for the transcript batch job\n * @param params.videoIds - Array of YouTube video IDs to fetch transcripts for\n * @param params.lang - The language code for the transcripts (optional)\n * @param params.limit - Maximum number of videos to process (optional, default: 10, max: 5000)\n * @param params.text - Whether to return only the text content (optional)\n * @returns A promise that resolves to a YoutubeBatchJob object with the job ID\n */\n batch: async (\n params: YoutubeTranscriptBatchRequest\n ): Promise<YoutubeBatchJob> => {\n this.validateBatchLimit(params);\n return this.fetch<YoutubeBatchJob>(\n '/youtube/transcript/batch',\n params,\n 'POST'\n );\n },\n }\n );\n\n /**\n * Handles YouTube video operations.\n */\n video = Object.assign(\n /**\n * Fetches a YouTube video based on the provided parameters.\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 (params: ResourceParams): Promise<YoutubeVideo> => {\n return this.fetch<YoutubeVideo>('/youtube/video', params);\n },\n {\n /**\n * Batch fetches metadata for multiple YouTube videos.\n * @param params - Parameters for the video metadata batch job\n * @param params.videoIds - Array of YouTube video IDs to fetch metadata for\n * @param params.limit - Maximum number of videos to process (optional, default: 10, max: 5000)\n * @returns A promise that resolves to a YoutubeBatchJob object with the job ID\n */\n batch: async (\n params: YoutubeVideoBatchRequest\n ): Promise<YoutubeBatchJob> => {\n this.validateBatchLimit(params);\n return this.fetch<YoutubeBatchJob>(\n '/youtube/video/batch',\n params,\n 'POST'\n );\n },\n }\n );\n\n /**\n * Handles YouTube channel operations.\n */\n channel = Object.assign(\n /**\n * Fetches YouTube channel information.\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 async (params: ResourceParams): Promise<YoutubeChannel> => {\n return this.fetch<YoutubeChannel>('/youtube/channel', params);\n },\n {\n /**\n * Fetches the videos of a 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 (default: 30, max: 5000)\n * @param params.type - The type of videos to fetch ('video', 'short', 'live', or 'all', default: 'video')\n * @returns A promise that resolves to an object containing arrays of video IDs, short IDs, and live IDs\n * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000)\n */\n videos: async (params: ChannelVideosParams): Promise<VideoIds> => {\n this.validateLimit(params);\n return this.fetch<VideoIds>('/youtube/channel/videos', params);\n },\n }\n );\n\n /**\n * Handles YouTube playlist operations.\n */\n playlist = Object.assign(\n /**\n * Fetches a YouTube playlist.\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 async (params: ResourceParams): Promise<YoutubePlaylist> => {\n return this.fetch<YoutubePlaylist>('/youtube/playlist', params);\n },\n {\n /**\n * 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 (default: 30, max: 5000)\n * @returns A promise that resolves to an object containing arrays of video IDs, short IDs, and live IDs\n * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000)\n */\n videos: async (params: PlaylistVideosParams): Promise<VideoIds> => {\n this.validateLimit(params);\n return this.fetch<VideoIds>('/youtube/playlist/videos', params);\n },\n }\n );\n\n /**\n * Handles YouTube batch operations.\n */\n batch = {\n /**\n * Retrieves the status and results of a batch job.\n * @param jobId - The ID of the batch job\n * @returns A promise that resolves to the YoutubeBatchResults containing job status and results\n * @throws {SupadataError} If jobId is not provided\n */\n getBatchResults: async (jobId: string): Promise<YoutubeBatchResults> => {\n if (!jobId) {\n throw new SupadataError({\n error: 'invalid-request',\n message: 'Missing jobId',\n details: 'The jobId parameter is required to get batch results.',\n });\n }\n return this.fetch<YoutubeBatchResults>(`/youtube/batch/${jobId}`);\n },\n };\n\n /**\n * Translates a YouTube video transcript to a specified language.\n * @param params - Parameters for translating the transcript\n * @param params.videoId - The YouTube video ID (mutually exclusive with url)\n * @param params.url - The YouTube video URL (mutually exclusive with videoId)\n * @param params.lang - The target language code for translation\n * @param params.text - Whether to return only the text content (optional)\n * @returns A promise that resolves to a TranslatedTranscript object\n */\n translate = async (\n params: TranslateParams\n ): Promise<TranslatedTranscript> => {\n return this.fetch<TranslatedTranscript>(\n '/youtube/transcript/translate',\n params\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 // Add a specific validator for batch limits as per documentation (Max: 5000, Default: 10)\n private validateBatchLimit(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 for batch operation.',\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
|
@@ -42,7 +42,7 @@ interface SupadataConfig {
|
|
|
42
42
|
baseUrl?: string;
|
|
43
43
|
}
|
|
44
44
|
declare class SupadataError extends Error {
|
|
45
|
-
error: 'invalid-request' | '
|
|
45
|
+
error: 'invalid-request' | 'internal-error' | 'transcript-unavailable' | 'not-found' | 'unauthorized' | 'upgrade-required' | 'limit-exceeded';
|
|
46
46
|
details: string;
|
|
47
47
|
documentationUrl: string;
|
|
48
48
|
constructor(error: {
|
|
@@ -156,7 +156,7 @@ interface ResourceParams {
|
|
|
156
156
|
}
|
|
157
157
|
interface ChannelVideosParams extends ResourceParams {
|
|
158
158
|
limit?: number;
|
|
159
|
-
type?: 'video' | 'short' | 'all';
|
|
159
|
+
type?: 'video' | 'short' | 'live' | 'all';
|
|
160
160
|
}
|
|
161
161
|
interface PlaylistVideosParams extends ResourceParams {
|
|
162
162
|
limit?: number;
|
|
@@ -164,6 +164,7 @@ interface PlaylistVideosParams extends ResourceParams {
|
|
|
164
164
|
interface VideoIds {
|
|
165
165
|
videoIds: string[];
|
|
166
166
|
shortIds: string[];
|
|
167
|
+
liveIds: string[];
|
|
167
168
|
}
|
|
168
169
|
declare class YouTubeService extends BaseClient {
|
|
169
170
|
/**
|
|
@@ -203,8 +204,8 @@ declare class YouTubeService extends BaseClient {
|
|
|
203
204
|
* @param params - The parameters required to fetch the YouTube channel videos
|
|
204
205
|
* @param params.id - The YouTube channel ID
|
|
205
206
|
* @param params.limit - The maximum number of videos to fetch (default: 30, max: 5000)
|
|
206
|
-
* @param params.type - The type of videos to fetch ('video', 'short', or 'all', default: 'video')
|
|
207
|
-
* @returns A promise that resolves to an object containing arrays of video IDs and
|
|
207
|
+
* @param params.type - The type of videos to fetch ('video', 'short', 'live', or 'all', default: 'video')
|
|
208
|
+
* @returns A promise that resolves to an object containing arrays of video IDs, short IDs, and live IDs
|
|
208
209
|
* @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000)
|
|
209
210
|
*/
|
|
210
211
|
videos: (params: ChannelVideosParams) => Promise<VideoIds>;
|
|
@@ -218,7 +219,7 @@ declare class YouTubeService extends BaseClient {
|
|
|
218
219
|
* @param params - The parameters required to fetch the playlist videos
|
|
219
220
|
* @param params.id - The YouTube playlist ID
|
|
220
221
|
* @param params.limit - The maximum number of videos to fetch (default: 30, max: 5000)
|
|
221
|
-
* @returns A promise that resolves to an object containing arrays of video IDs and
|
|
222
|
+
* @returns A promise that resolves to an object containing arrays of video IDs, short IDs, and live IDs
|
|
222
223
|
* @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000)
|
|
223
224
|
*/
|
|
224
225
|
videos: (params: PlaylistVideosParams) => Promise<VideoIds>;
|
package/dist/index.d.ts
CHANGED
|
@@ -42,7 +42,7 @@ interface SupadataConfig {
|
|
|
42
42
|
baseUrl?: string;
|
|
43
43
|
}
|
|
44
44
|
declare class SupadataError extends Error {
|
|
45
|
-
error: 'invalid-request' | '
|
|
45
|
+
error: 'invalid-request' | 'internal-error' | 'transcript-unavailable' | 'not-found' | 'unauthorized' | 'upgrade-required' | 'limit-exceeded';
|
|
46
46
|
details: string;
|
|
47
47
|
documentationUrl: string;
|
|
48
48
|
constructor(error: {
|
|
@@ -156,7 +156,7 @@ interface ResourceParams {
|
|
|
156
156
|
}
|
|
157
157
|
interface ChannelVideosParams extends ResourceParams {
|
|
158
158
|
limit?: number;
|
|
159
|
-
type?: 'video' | 'short' | 'all';
|
|
159
|
+
type?: 'video' | 'short' | 'live' | 'all';
|
|
160
160
|
}
|
|
161
161
|
interface PlaylistVideosParams extends ResourceParams {
|
|
162
162
|
limit?: number;
|
|
@@ -164,6 +164,7 @@ interface PlaylistVideosParams extends ResourceParams {
|
|
|
164
164
|
interface VideoIds {
|
|
165
165
|
videoIds: string[];
|
|
166
166
|
shortIds: string[];
|
|
167
|
+
liveIds: string[];
|
|
167
168
|
}
|
|
168
169
|
declare class YouTubeService extends BaseClient {
|
|
169
170
|
/**
|
|
@@ -203,8 +204,8 @@ declare class YouTubeService extends BaseClient {
|
|
|
203
204
|
* @param params - The parameters required to fetch the YouTube channel videos
|
|
204
205
|
* @param params.id - The YouTube channel ID
|
|
205
206
|
* @param params.limit - The maximum number of videos to fetch (default: 30, max: 5000)
|
|
206
|
-
* @param params.type - The type of videos to fetch ('video', 'short', or 'all', default: 'video')
|
|
207
|
-
* @returns A promise that resolves to an object containing arrays of video IDs and
|
|
207
|
+
* @param params.type - The type of videos to fetch ('video', 'short', 'live', or 'all', default: 'video')
|
|
208
|
+
* @returns A promise that resolves to an object containing arrays of video IDs, short IDs, and live IDs
|
|
208
209
|
* @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000)
|
|
209
210
|
*/
|
|
210
211
|
videos: (params: ChannelVideosParams) => Promise<VideoIds>;
|
|
@@ -218,7 +219,7 @@ declare class YouTubeService extends BaseClient {
|
|
|
218
219
|
* @param params - The parameters required to fetch the playlist videos
|
|
219
220
|
* @param params.id - The YouTube playlist ID
|
|
220
221
|
* @param params.limit - The maximum number of videos to fetch (default: 30, max: 5000)
|
|
221
|
-
* @returns A promise that resolves to an object containing arrays of video IDs and
|
|
222
|
+
* @returns A promise that resolves to an object containing arrays of video IDs, short IDs, and live IDs
|
|
222
223
|
* @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000)
|
|
223
224
|
*/
|
|
224
225
|
videos: (params: PlaylistVideosParams) => Promise<VideoIds>;
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var
|
|
2
|
-
export{
|
|
1
|
+
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 o=class{config;constructor(e){this.config=e;}async fetch(e,t={},i="GET"){let s=`${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(([a,h])=>{h!=null&&u.append(a,String(h));}),s+=`?${u.toString()}`;}return this.fetchUrl(s,i,t)}async fetchUrl(e,t="GET",i){let n={method:t,headers:{"x-api-key":this.config.apiKey,"Content-Type":"application/json"}};t==="POST"&&i&&(n.body=JSON.stringify(i));let s=await fetch(e,n),u=s.headers.get("content-type");if(!s.ok)if(u?.includes("application/json")){let a=await s.json();throw new r(a)}else throw new r({error:"internal-error",message:"Unexpected error response format",details:await s.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 s.json()}catch(a){throw new r({error:"internal-error",message:"Failed to parse response",details:a instanceof Error?a.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 b=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=[],n;do t=await(n?this.fetchUrl(n):this.fetch(`/web/crawl/${e}`)),t.pages&&(i=[...i,...t.pages]),n=t.next;while(n);return t}};var g=class{youtube;web;constructor(e){this.youtube=new p(e),this.web=new b(e);}};
|
|
2
|
+
export{o as BaseClient,g as Supadata,r as SupadataError,b as WebService,p as YouTubeService};//# sourceMappingURL=index.mjs.map
|
|
3
3
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -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","jobId","WebService","request","pages","nextUrl","Supadata"],"mappings":"AAoDO,IAAMA,EAAN,cAA4B,KAAM,CACvC,KAAA,CASA,QACA,gBAEA,CAAA,WAAA,CAAYC,CAKT,CAAA,CACD,MAAMA,CAAM,CAAA,OAAA,EAAW,8BAA8B,CAAA,CACrD,KAAK,KAAQA,CAAAA,CAAAA,CAAM,KAAS,EAAA,gBAAA,CAC5B,KAAK,OAAUA,CAAAA,CAAAA,CAAM,OAAW,EAAA,8BAAA,CAChC,KAAK,gBAAmBA,CAAAA,CAAAA,CAAM,gBAAoB,EAAA,EAAA,CAClD,KAAK,IAAO,CAAA,gBACd,CACF,ECtEA,IAAMC,EAAwB,CAC5B,GAAA,CAAK,CACH,KAAA,CAAO,kBACP,OAAS,CAAA,4BAAA,CACT,OAAS,CAAA,iDACX,EACA,GAAK,CAAA,CACH,KAAO,CAAA,iBAAA,CACP,QAAS,yBACT,CAAA,OAAA,CAAS,0DACX,CAAA,CACA,IAAK,CACH,KAAA,CAAO,gBACP,CAAA,OAAA,CAAS,iBACT,OAAS,CAAA,4DACX,CACF,CAAA,CAEaC,EAAkB,CAC7BC,CAAAA,CACAC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A,CAAgBF,CAAkC,CAAA,CACtD,IAAIJ,CACAO,CAAAA,CAAAA,CAAkB,EAAC,CACnBC,EAEJ,GACER,CAAAA,CAAW,MAAOQ,CAAAA,CACd,KAAK,QAAmBA,CAAAA,CAAO,EAC/B,IAAK,CAAA,KAAA,CAAgB,cAAcJ,CAAK,CAAA,CAAE,CAE1CJ,CAAAA,CAAAA,CAAAA,CAAS,QACXO,CAAQ,CAAA,CAAC,GAAGA,CAAAA,CAAO,GAAGP,CAAS,CAAA,KAAK,CAEtCQ,CAAAA,CAAAA,CAAAA,CAAUR,EAAS,IACZQ,CAAAA,MAAAA,CAAAA,EAET,OAAOR,CACT,CACF,ECpDaS,IAAAA,CAAAA,CAAN,KAAe,CACX,QACA,GAET,CAAA,WAAA,CAAYnB,CAAwB,CAAA,CAClC,KAAK,OAAU,CAAA,IAAIa,CAAeb,CAAAA,CAAM,EACxC,IAAK,CAAA,GAAA,CAAM,IAAIe,CAAWf,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\nexport interface YoutubeBatchSource {\n videoIds?: string[];\n playlistId?: string;\n channelId?: string;\n limit?: number;\n}\n\nexport interface YoutubeTranscriptBatchRequest extends YoutubeBatchSource {\n lang?: string;\n text?: boolean;\n}\n\nexport interface YoutubeVideoBatchRequest extends YoutubeBatchSource {}\n\nexport interface YoutubeBatchJob {\n jobId: string;\n}\n\nexport type YoutubeBatchJobStatus =\n | 'queued'\n | 'active'\n | 'completed'\n | 'failed';\n\nexport interface YoutubeBatchResultItem {\n videoId: string;\n transcript?: Transcript;\n video?: YoutubeVideo;\n errorCode?: string;\n}\n\nexport interface YoutubeBatchStats {\n total: number;\n succeeded: number;\n failed: number;\n}\n\nexport interface YoutubeBatchResults {\n status: YoutubeBatchJobStatus;\n results?: YoutubeBatchResultItem[];\n stats?: YoutubeBatchStats;\n completedAt?: 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 YoutubeBatchJob,\n YoutubeBatchResults,\n YoutubeChannel,\n YoutubePlaylist,\n YoutubeTranscriptBatchRequest,\n YoutubeVideo,\n YoutubeVideoBatchRequest,\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 * Handles YouTube Transcript operations.\n */\n transcript = Object.assign(\n /**\n * Fetches a transcript for a YouTube video.\n * @param params - Parameters for fetching the transcript\n * @param params.videoId - The YouTube video ID (mutually exclusive with url)\n * @param params.url - The YouTube video URL (mutually exclusive with videoId)\n * @param params.lang - The language code for the transcript (optional)\n * @param params.text - Whether to return only the text content (optional)\n * @returns A promise that resolves to a Transcript object\n */\n async (params: TranscriptParams): Promise<Transcript> => {\n return this.fetch<Transcript>('/youtube/transcript', params);\n },\n {\n /**\n * Batch fetches transcripts for multiple YouTube videos.\n * @param params - Parameters for the transcript batch job\n * @param params.videoIds - Array of YouTube video IDs to fetch transcripts for\n * @param params.lang - The language code for the transcripts (optional)\n * @param params.limit - Maximum number of videos to process (optional, default: 10, max: 5000)\n * @param params.text - Whether to return only the text content (optional)\n * @returns A promise that resolves to a YoutubeBatchJob object with the job ID\n */\n batch: async (\n params: YoutubeTranscriptBatchRequest\n ): Promise<YoutubeBatchJob> => {\n this.validateBatchLimit(params);\n return this.fetch<YoutubeBatchJob>(\n '/youtube/transcript/batch',\n params,\n 'POST'\n );\n },\n }\n );\n\n /**\n * Handles YouTube video operations.\n */\n video = Object.assign(\n /**\n * Fetches a YouTube video based on the provided parameters.\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 (params: ResourceParams): Promise<YoutubeVideo> => {\n return this.fetch<YoutubeVideo>('/youtube/video', params);\n },\n {\n /**\n * Batch fetches metadata for multiple YouTube videos.\n * @param params - Parameters for the video metadata batch job\n * @param params.videoIds - Array of YouTube video IDs to fetch metadata for\n * @param params.limit - Maximum number of videos to process (optional, default: 10, max: 5000)\n * @returns A promise that resolves to a YoutubeBatchJob object with the job ID\n */\n batch: async (\n params: YoutubeVideoBatchRequest\n ): Promise<YoutubeBatchJob> => {\n this.validateBatchLimit(params);\n return this.fetch<YoutubeBatchJob>(\n '/youtube/video/batch',\n params,\n 'POST'\n );\n },\n }\n );\n\n /**\n * Handles YouTube channel operations.\n */\n channel = Object.assign(\n /**\n * Fetches YouTube channel information.\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 async (params: ResourceParams): Promise<YoutubeChannel> => {\n return this.fetch<YoutubeChannel>('/youtube/channel', params);\n },\n {\n /**\n * Fetches the videos of a 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 (default: 30, max: 5000)\n * @param params.type - The type of videos to fetch ('video', 'short', or 'all', default: 'video')\n * @returns A promise that resolves to an object containing arrays of video IDs and short IDs\n * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000)\n */\n videos: async (params: ChannelVideosParams): Promise<VideoIds> => {\n this.validateLimit(params);\n return this.fetch<VideoIds>('/youtube/channel/videos', params);\n },\n }\n );\n\n /**\n * Handles YouTube playlist operations.\n */\n playlist = Object.assign(\n /**\n * Fetches a YouTube playlist.\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 async (params: ResourceParams): Promise<YoutubePlaylist> => {\n return this.fetch<YoutubePlaylist>('/youtube/playlist', params);\n },\n {\n /**\n * 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 (default: 30, max: 5000)\n * @returns A promise that resolves to an object containing arrays of video IDs and short IDs\n * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000)\n */\n videos: async (params: PlaylistVideosParams): Promise<VideoIds> => {\n this.validateLimit(params);\n return this.fetch<VideoIds>('/youtube/playlist/videos', params);\n },\n }\n );\n\n /**\n * Handles YouTube batch operations.\n */\n batch = {\n /**\n * Retrieves the status and results of a batch job.\n * @param jobId - The ID of the batch job\n * @returns A promise that resolves to the YoutubeBatchResults containing job status and results\n * @throws {SupadataError} If jobId is not provided\n */\n getBatchResults: async (jobId: string): Promise<YoutubeBatchResults> => {\n if (!jobId) {\n throw new SupadataError({\n error: 'missing-parameters',\n message: 'Missing jobId',\n details: 'The jobId parameter is required to get batch results.',\n });\n }\n return this.fetch<YoutubeBatchResults>(`/youtube/batch/${jobId}`);\n },\n };\n\n /**\n * Translates a YouTube video transcript to a specified language.\n * @param params - Parameters for translating the transcript\n * @param params.videoId - The YouTube video ID (mutually exclusive with url)\n * @param params.url - The YouTube video URL (mutually exclusive with videoId)\n * @param params.lang - The target language code for translation\n * @param params.text - Whether to return only the text content (optional)\n * @returns A promise that resolves to a TranslatedTranscript object\n */\n translate = async (\n params: TranslateParams\n ): Promise<TranslatedTranscript> => {\n return this.fetch<TranslatedTranscript>(\n '/youtube/transcript/translate',\n params\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 // Add a specific validator for batch limits as per documentation (Max: 5000, Default: 10)\n private validateBatchLimit(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 for batch operation.',\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/client.ts","../src/services/youtube.ts","../src/services/web.ts","../src/index.ts"],"names":["SupadataError","error","BaseClient","config","endpoint","params","method","url","queryParams","key","value","body","options","response","contentType","errorData","YouTubeService","jobId","WebService","request","pages","nextUrl","Supadata"],"mappings":"AAo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AAA,CAAK,KAAa,CAAA,YAAA,CAAcA,CAAS,CAAA,MAAM,CACxD,CASA,MAAM,eAAA,CAAgBF,CAAkC,CAAA,CACtD,IAAIJ,CAAAA,CACAO,CAAkB,CAAA,GAClBC,CAEJ,CAAA,GACER,CAAW,CAAA,MAAOQ,CACd,CAAA,IAAA,CAAK,QAAmBA,CAAAA,CAAO,EAC/B,IAAK,CAAA,KAAA,CAAgB,CAAcJ,WAAAA,EAAAA,CAAK,CAAE,CAAA,CAAA,CAAA,CAE1CJ,CAAS,CAAA,KAAA,GACXO,EAAQ,CAAC,GAAGA,CAAO,CAAA,GAAGP,CAAS,CAAA,KAAK,CAEtCQ,CAAAA,CAAAA,CAAAA,CAAUR,EAAS,IACZQ,CAAAA,MAAAA,CAAAA,EAET,OAAOR,CACT,CACF,ECpDaS,IAAAA,CAAAA,CAAN,KAAe,CACX,OAAA,CACA,GAET,CAAA,WAAA,CAAYnB,CAAwB,CAAA,CAClC,IAAK,CAAA,OAAA,CAAU,IAAIa,CAAeb,CAAAA,CAAM,CACxC,CAAA,IAAA,CAAK,GAAM,CAAA,IAAIe,CAAWf,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 | 'internal-error'\n | 'transcript-unavailable'\n | 'not-found'\n | 'unauthorized'\n | 'upgrade-required'\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\nexport interface YoutubeBatchSource {\n videoIds?: string[];\n playlistId?: string;\n channelId?: string;\n limit?: number;\n}\n\nexport interface YoutubeTranscriptBatchRequest extends YoutubeBatchSource {\n lang?: string;\n text?: boolean;\n}\n\nexport interface YoutubeVideoBatchRequest extends YoutubeBatchSource {}\n\nexport interface YoutubeBatchJob {\n jobId: string;\n}\n\nexport type YoutubeBatchJobStatus =\n | 'queued'\n | 'active'\n | 'completed'\n | 'failed';\n\nexport interface YoutubeBatchResultItem {\n videoId: string;\n transcript?: Transcript;\n video?: YoutubeVideo;\n errorCode?: string;\n}\n\nexport interface YoutubeBatchStats {\n total: number;\n succeeded: number;\n failed: number;\n}\n\nexport interface YoutubeBatchResults {\n status: YoutubeBatchJobStatus;\n results?: YoutubeBatchResultItem[];\n stats?: YoutubeBatchStats;\n completedAt?: string;\n}\n","import { SupadataConfig, SupadataError } from './types.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 // 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 YoutubeBatchJob,\n YoutubeBatchResults,\n YoutubeChannel,\n YoutubePlaylist,\n YoutubeTranscriptBatchRequest,\n YoutubeVideo,\n YoutubeVideoBatchRequest,\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' | 'live' | 'all';\n}\n\nexport interface PlaylistVideosParams extends ResourceParams {\n limit?: number;\n}\n\nexport interface VideoIds {\n videoIds: string[];\n shortIds: string[];\n liveIds: string[];\n}\n\nexport class YouTubeService extends BaseClient {\n /**\n * Handles YouTube Transcript operations.\n */\n transcript = Object.assign(\n /**\n * Fetches a transcript for a YouTube video.\n * @param params - Parameters for fetching the transcript\n * @param params.videoId - The YouTube video ID (mutually exclusive with url)\n * @param params.url - The YouTube video URL (mutually exclusive with videoId)\n * @param params.lang - The language code for the transcript (optional)\n * @param params.text - Whether to return only the text content (optional)\n * @returns A promise that resolves to a Transcript object\n */\n async (params: TranscriptParams): Promise<Transcript> => {\n return this.fetch<Transcript>('/youtube/transcript', params);\n },\n {\n /**\n * Batch fetches transcripts for multiple YouTube videos.\n * @param params - Parameters for the transcript batch job\n * @param params.videoIds - Array of YouTube video IDs to fetch transcripts for\n * @param params.lang - The language code for the transcripts (optional)\n * @param params.limit - Maximum number of videos to process (optional, default: 10, max: 5000)\n * @param params.text - Whether to return only the text content (optional)\n * @returns A promise that resolves to a YoutubeBatchJob object with the job ID\n */\n batch: async (\n params: YoutubeTranscriptBatchRequest\n ): Promise<YoutubeBatchJob> => {\n this.validateBatchLimit(params);\n return this.fetch<YoutubeBatchJob>(\n '/youtube/transcript/batch',\n params,\n 'POST'\n );\n },\n }\n );\n\n /**\n * Handles YouTube video operations.\n */\n video = Object.assign(\n /**\n * Fetches a YouTube video based on the provided parameters.\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 (params: ResourceParams): Promise<YoutubeVideo> => {\n return this.fetch<YoutubeVideo>('/youtube/video', params);\n },\n {\n /**\n * Batch fetches metadata for multiple YouTube videos.\n * @param params - Parameters for the video metadata batch job\n * @param params.videoIds - Array of YouTube video IDs to fetch metadata for\n * @param params.limit - Maximum number of videos to process (optional, default: 10, max: 5000)\n * @returns A promise that resolves to a YoutubeBatchJob object with the job ID\n */\n batch: async (\n params: YoutubeVideoBatchRequest\n ): Promise<YoutubeBatchJob> => {\n this.validateBatchLimit(params);\n return this.fetch<YoutubeBatchJob>(\n '/youtube/video/batch',\n params,\n 'POST'\n );\n },\n }\n );\n\n /**\n * Handles YouTube channel operations.\n */\n channel = Object.assign(\n /**\n * Fetches YouTube channel information.\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 async (params: ResourceParams): Promise<YoutubeChannel> => {\n return this.fetch<YoutubeChannel>('/youtube/channel', params);\n },\n {\n /**\n * Fetches the videos of a 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 (default: 30, max: 5000)\n * @param params.type - The type of videos to fetch ('video', 'short', 'live', or 'all', default: 'video')\n * @returns A promise that resolves to an object containing arrays of video IDs, short IDs, and live IDs\n * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000)\n */\n videos: async (params: ChannelVideosParams): Promise<VideoIds> => {\n this.validateLimit(params);\n return this.fetch<VideoIds>('/youtube/channel/videos', params);\n },\n }\n );\n\n /**\n * Handles YouTube playlist operations.\n */\n playlist = Object.assign(\n /**\n * Fetches a YouTube playlist.\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 async (params: ResourceParams): Promise<YoutubePlaylist> => {\n return this.fetch<YoutubePlaylist>('/youtube/playlist', params);\n },\n {\n /**\n * 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 (default: 30, max: 5000)\n * @returns A promise that resolves to an object containing arrays of video IDs, short IDs, and live IDs\n * @throws {SupadataError} If the limit is invalid (less than 1 or greater than 5000)\n */\n videos: async (params: PlaylistVideosParams): Promise<VideoIds> => {\n this.validateLimit(params);\n return this.fetch<VideoIds>('/youtube/playlist/videos', params);\n },\n }\n );\n\n /**\n * Handles YouTube batch operations.\n */\n batch = {\n /**\n * Retrieves the status and results of a batch job.\n * @param jobId - The ID of the batch job\n * @returns A promise that resolves to the YoutubeBatchResults containing job status and results\n * @throws {SupadataError} If jobId is not provided\n */\n getBatchResults: async (jobId: string): Promise<YoutubeBatchResults> => {\n if (!jobId) {\n throw new SupadataError({\n error: 'invalid-request',\n message: 'Missing jobId',\n details: 'The jobId parameter is required to get batch results.',\n });\n }\n return this.fetch<YoutubeBatchResults>(`/youtube/batch/${jobId}`);\n },\n };\n\n /**\n * Translates a YouTube video transcript to a specified language.\n * @param params - Parameters for translating the transcript\n * @param params.videoId - The YouTube video ID (mutually exclusive with url)\n * @param params.url - The YouTube video URL (mutually exclusive with videoId)\n * @param params.lang - The target language code for translation\n * @param params.text - Whether to return only the text content (optional)\n * @returns A promise that resolves to a TranslatedTranscript object\n */\n translate = async (\n params: TranslateParams\n ): Promise<TranslatedTranscript> => {\n return this.fetch<TranslatedTranscript>(\n '/youtube/transcript/translate',\n params\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 // Add a specific validator for batch limits as per documentation (Max: 5000, Default: 10)\n private validateBatchLimit(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 for batch operation.',\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"]}
|