UNPKG

@tsavo/creatify-api-ts

Version:

TypeScript client for the Creatify AI API

4 lines 13.1 kB
import V from'axios';// Creatify API TypeScript Client - MIT License var I=Object.defineProperty;var v=(p,e)=>{for(var t in e)I(p,t,{get:e[t],enumerable:true});};var l=class{client;apiId;apiKey;constructor(e){this.apiId=e.apiId,this.apiKey=e.apiKey,this.client=V.create({baseURL:e.baseUrl||"https://api.creatify.ai",timeout:e.timeout||3e4,headers:{"Content-Type":"application/json","X-API-ID":this.apiId,"X-API-KEY":this.apiKey}}),this.client.interceptors.response.use(t=>t,t=>{if(t.response){let{status:s,data:i}=t.response;return Promise.reject({status:s,message:i.message||"An error occurred with the API request",data:i})}else return t.request?Promise.reject({message:"No response received from the API server",request:t.request}):Promise.reject({message:t.message||"An error occurred while setting up the request"})});}async get(e,t,s){return (await this.client.get(e,{params:t,...s})).data}async post(e,t,s){return (await this.client.post(e,t,s)).data}async put(e,t,s){return (await this.client.put(e,t,s)).data}async delete(e,t,s){return (await this.client.delete(e,{params:t,...s})).data}};var h=class{createClient(e){return new l(e)}},a=new h;var y=class{client;constructor(e,t=a){this.client=t.createClient(e);}async getAvatars(e){try{let t=await this.client.get("/api/personas/",e);return Array.isArray(t)?t.map(s=>({...s,avatar_id:s.id,name:s.name&&s.name.trim()?s.name:`Avatar ${s.id}`})):[]}catch(t){return console.error("Error fetching avatars:",t),[]}}async getAvatarsPaginated(e=1,t=20,s){return this.client.get("/api/personas-paginated/",{page:e,limit:t,...s})}async getVoices(){try{let e=await this.client.get("/api/voices/");return Array.isArray(e)?e.map(t=>({id:t.id||t.voice_id||"",voice_id:t.voice_id||t.id||"",name:t.name||"Unnamed Voice",language:t.language||"",gender:t.gender||"",accents:t.accents||[],preview_url:t.preview_url||t.accents&&t.accents.length>0&&t.accents[0].preview_url||""})):[]}catch(e){return console.error("Error fetching voices:",e),[]}}async createLipsync(e){try{return await this.client.post("/api/lipsyncs/",e)}catch(t){throw console.error("Error creating lipsync:",t),t}}async getLipsync(e){try{return await this.client.get(`/api/lipsyncs/${e}/`)}catch(t){return console.error(`Error fetching lipsync result for ID ${e}:`,t),{id:e,status:"error",error_message:t instanceof Error?t.message:"An error occurred with the API request"}}}async getLipsyncs(){try{let e=await this.client.get("/api/lipsyncs/");return Array.isArray(e)?e:[]}catch(e){return console.error("Error fetching lipsync tasks:",e),[]}}async createMultiAvatarLipsync(e){try{return await this.client.post("/api/lipsyncs/multi_avatar/",e)}catch(t){throw console.error("Error creating multi-avatar lipsync:",t),t}}async createAndWaitForLipsync(e,t=2e3,s=30){let i=await this.createLipsync(e),o=0,r=await this.getLipsync(i.id);for(;o<s&&r.status!=="done"&&r.status!=="error";)await new Promise(n=>setTimeout(n,t)),r=await this.getLipsync(i.id),o++;if(o>=s&&r.status!=="done"&&r.status!=="error")throw new Error(`Lipsync task ${i.id} did not complete within the timeout period`);return r}};var d=class{client;constructor(e,t=a){this.client=t.createClient(e);}async createLink(e){return this.client.post("/api/links/",e)}async createLinkWithParams(e){return this.client.post("/api/links/link_with_params/",e)}async updateLink(e,t){return this.client.put(`/api/links/${e}/`,t)}async getLink(e){return this.client.get(`/api/links/${e}/`)}async getLinks(e,t){return this.client.get("/api/links/",{page:e,limit:t})}async getLinksPaginated(e=1,t=20){return this.client.get("/api/links/paginated/",{page:e,limit:t})}async createVideoFromLink(e){return this.client.post("/api/link_to_videos/",e)}async getVideo(e){return this.client.get(`/api/link_to_videos/${e}/`)}async getVideos(){return this.client.get("/api/link_to_videos/")}};var g=class{client;constructor(e,t=a){this.client=t.createClient(e);}async createTextToSpeech(e){return this.client.post("/api/text_to_speech/",e)}async getTextToSpeech(e){return this.client.get(`/api/text_to_speech/${e}/`)}async getTextToSpeechList(e,t){return this.client.get("/api/text_to_speech/",{page:e,limit:t})}async getTextToSpeechPaginated(e=1,t=20){return this.client.get("/api/text_to_speech/paginated/",{page:e,limit:t})}async createAndWaitForTextToSpeech(e,t=5e3,s=60){let i=await this.createTextToSpeech(e),o=0,r=await this.getTextToSpeech(i.id);for(;o<s&&r.status!=="done"&&r.status!=="error";)await new Promise(n=>setTimeout(n,t)),r=await this.getTextToSpeech(i.id),o++;if(o>=s&&r.status!=="done"&&r.status!=="error")throw new Error(`Text-to-speech task ${i.id} did not complete within the timeout period`);return r}};var A=class{client;constructor(e,t=a){this.client=t.createClient(e);}async createAiEditing(e){return this.client.post("/api/ai_editing/",e)}async getAiEditing(e){return this.client.get(`/api/ai_editing/${e}/`)}async getAiEditingList(){return this.client.get("/api/ai_editing/")}async createAndWaitForAiEditing(e,t=5e3,s=120){let i=await this.createAiEditing(e),o=0,r=await this.getAiEditing(i.id);for(;o<s&&r.status!=="done"&&r.status!=="error";)await new Promise(n=>setTimeout(n,t)),r=await this.getAiEditing(i.id),o++;if(o>=s&&r.status!=="done"&&r.status!=="error")throw new Error(`AI editing task ${i.id} did not complete within the timeout period`);return r}};var f=class{client;constructor(e,t=a){this.client=t.createClient(e);}async createCustomTemplate(e){return this.client.post("/api/custom_templates/",e)}async getCustomTemplate(e){return this.client.get(`/api/custom_templates/${e}/`)}async getCustomTemplateList(){return this.client.get("/api/custom_templates/")}async createAndWaitForCustomTemplate(e,t=5e3,s=120){let i=await this.createCustomTemplate(e),o=0,r=await this.getCustomTemplate(i.id);for(;o<s&&r.status!=="done"&&r.status!=="error";)await new Promise(n=>setTimeout(n,t)),r=await this.getCustomTemplate(i.id),o++;if(o>=s&&r.status!=="done"&&r.status!=="error")throw new Error(`Custom template task ${i.id} did not complete within the timeout period`);return r}};var w=class{client;constructor(e,t=a){this.client=t.createClient(e);}async createDyoa(e){return this.client.post("/api/dyoa/",e)}async getDyoa(e){return this.client.get(`/api/dyoa/${e}/`)}async getDyoaList(){return this.client.get("/api/dyoa/")}async submitDyoaForReview(e,t){return this.client.post(`/api/dyoa/${e}/submit_for_review/`,t)}async deleteDyoa(e){return this.client.delete(`/api/dyoa/${e}/`)}async createAndWaitForDyoaPhotos(e,t=1e4,s=30){let i=await this.createDyoa(e),o=0,r=await this.getDyoa(i.id);for(;o<s&&(r.status==="initializing"||r.photos.length===0);)await new Promise(n=>setTimeout(n,t)),r=await this.getDyoa(i.id),o++;if(o>=s&&r.photos.length===0)throw new Error(`DYOA ${i.id} photos were not generated within the timeout period`);return r}async createSubmitAndWaitForDyoa(e,t=0,s=1e4,i=30,o=6e4,r=60){let n=await this.createAndWaitForDyoaPhotos(e,s,i);if(n.photos.length===0)throw new Error("No photos were generated for the DYOA");let L=Math.min(t,n.photos.length-1),_=n.photos[L],m=await this.submitDyoaForReview(n.id,{chosen_photo_id:_.id}),u=0,c=await this.getDyoa(m.id);for(;u<r&&c.status==="pending";)await new Promise(D=>setTimeout(D,o)),c=await this.getDyoa(m.id),u++;if(u>=r&&c.status==="pending")throw new Error(`DYOA ${m.id} review did not complete within the timeout period`);return c}};var C=class{client;constructor(e,t=a){this.client=t.createClient(e);}async createAiShorts(e){return this.client.post("/api/ai_shorts/",e)}async getAiShorts(e){try{return await this.client.get(`/api/ai_shorts/${e}/`)}catch(t){return console.error(`Error fetching AI Shorts result for ID ${e}:`,t),{id:e,status:"error",error_message:t instanceof Error?t.message:"An error occurred with the API request",success:false,created_at:new Date().toISOString(),updated_at:new Date().toISOString()}}}async getAiShortsList(){try{let e=await this.client.get("/api/ai_shorts/");return Array.isArray(e)?e:[]}catch(e){return console.error("Error fetching AI Shorts tasks:",e),[]}}async generateAiShortsPreview(e){return this.client.post(`/api/ai_shorts/${e}/preview/`,{})}async renderAiShorts(e){return this.client.post(`/api/ai_shorts/${e}/render/`,{})}async createAndWaitForAiShorts(e,t=2e3,s=30){try{let i=await this.createAiShorts(e),o=0,r=await this.getAiShorts(i.id);for(;o<s&&r.status!=="done"&&r.status!=="error";)await new Promise(n=>setTimeout(n,t)),r=await this.getAiShorts(i.id),o++;if(o>=s&&r.status!=="done"&&r.status!=="error"){let n=new Error(`AI Shorts task ${i.id} did not complete within the timeout period`);return {id:i.id,status:"error",error_message:n.message,success:!1,created_at:r.created_at||new Date().toISOString(),updated_at:new Date().toISOString()}}return r}catch(i){return console.error("Error in createAndWaitForAiShorts:",i),{id:"error",status:"error",error_message:i instanceof Error?i.message:"Unknown error",success:false,created_at:new Date().toISOString(),updated_at:new Date().toISOString()}}}};var R=class{client;constructor(e,t=a){this.client=t.createClient(e);}async createAiScript(e){return this.client.post("/api/ai_scripts/",e)}async getAiScript(e){try{return await this.client.get(`/api/ai_scripts/${e}/`)}catch(t){return console.error(`Error fetching AI Script result for ID ${e}:`,t),{id:e,status:"error",error_message:t instanceof Error?t.message:"An error occurred with the API request",success:false,created_at:new Date().toISOString(),updated_at:new Date().toISOString()}}}async getAiScriptsList(){try{let e=await this.client.get("/api/ai_scripts/");return Array.isArray(e)?e:[]}catch(e){return console.error("Error fetching AI Script tasks:",e),[]}}async createAndWaitForAiScript(e,t=2e3,s=30){try{let i=await this.createAiScript(e),o=0,r=await this.getAiScript(i.id);for(;o<s&&r.status!=="done"&&r.status!=="error";)await new Promise(n=>setTimeout(n,t)),r=await this.getAiScript(i.id),o++;if(o>=s&&r.status!=="done"&&r.status!=="error"){let n=new Error(`AI Script task ${i.id} did not complete within the timeout period`);return {id:i.id,status:"error",error_message:n.message,success:!1,created_at:r.created_at||new Date().toISOString(),updated_at:new Date().toISOString()}}return r}catch(i){return console.error("Error in createAndWaitForAiScript:",i),{id:"error",status:"error",error_message:i instanceof Error?i.message:"Unknown error",success:false,created_at:new Date().toISOString(),updated_at:new Date().toISOString()}}}};var S=class{client;constructor(e,t=a){this.client=t.createClient(e);}async getMusicCategories(){try{let e=await this.client.get("/api/music_categories/");return Array.isArray(e)?e:[]}catch(e){return console.error("Error fetching music categories:",e),[]}}async getMusics(e){try{let t=e?{category:e}:void 0,s=await this.client.get("/api/musics/",t);return Array.isArray(s)?s:[]}catch(t){return console.error("Error fetching music tracks:",t),[]}}};var T=class{client;constructor(e,t=a){this.client=t.createClient(e);}async getRemainingCredits(){try{return await this.client.get("/api/remaining_credits/")}catch(e){return console.error("Error fetching remaining credits:",e),{remaining_credits:0}}}};var P=class{client;constructor(e,t=a){this.client=t.createClient(e);}async createLipsyncV2(e){try{return await this.client.post("/api/lipsyncs_v2/",e)}catch(t){throw console.error("Error creating lipsync v2:",t),t}}async getLipsyncV2(e){try{return await this.client.get(`/api/lipsyncs_v2/${e}/`)}catch(t){return console.error(`Error fetching lipsync v2 result for ID ${e}:`,t),{id:e,status:"error",error_message:t instanceof Error?t.message:"An error occurred with the API request",success:false,created_at:new Date().toISOString(),updated_at:new Date().toISOString()}}}async getLipsyncsV2(){try{let e=await this.client.get("/api/lipsyncs_v2/");return Array.isArray(e)?e:[]}catch(e){return console.error("Error fetching lipsync v2 tasks:",e),[]}}async generateLipsyncV2Preview(e){return this.client.post(`/api/lipsyncs_v2/${e}/preview/`,{})}async renderLipsyncV2(e){return this.client.post(`/api/lipsyncs_v2/${e}/render/`,{})}async createAndWaitForLipsyncV2(e,t=2e3,s=30){try{let i=await this.createLipsyncV2(e),o=0,r=await this.getLipsyncV2(i.id);for(;o<s&&r.status!=="done"&&r.status!=="error";)await new Promise(n=>setTimeout(n,t)),r=await this.getLipsyncV2(i.id),o++;if(o>=s&&r.status!=="done"&&r.status!=="error"){let n=new Error(`Lipsync v2 task ${i.id} did not complete within the timeout period`);return {id:i.id,status:"error",error_message:n instanceof Error?n.message:"Unknown error",success:!1,created_at:r.created_at||new Date().toISOString(),updated_at:new Date().toISOString()}}return r}catch(i){return console.error("Error in createAndWaitForLipsyncV2:",i),{id:"error",status:"error",error_message:i instanceof Error?i.message:"Unknown error",success:false,created_at:new Date().toISOString(),updated_at:new Date().toISOString()}}}}; export{v as a,l as b,h as c,a as d,y as e,d as f,g,A as h,f as i,w as j,C as k,R as l,S as m,T as n,P as o};//# sourceMappingURL=chunk-6Z5X5CX6.mjs.map //# sourceMappingURL=chunk-6Z5X5CX6.mjs.map