import axios from 'axios' export type Result = | { tag: 'success'; value: T } | { tag: 'err'; value: E } export type ApiResponse = Result // export type ProgressUpdate = { // tag: 'continue', // value: ProgressUpdateContinue // } | { tag: 'complete' } | { tag: 'error', value: string } export type ProgressUpdate = | { tag: 'mediaDownloaded' value: { downloaded: number } } | { tag: 'renderStep1' value: { percent: number } } | { tag: 'renderStep2' value: { out_time_ms: number done: boolean } } | { tag: 'done' } | { tag: 'error' value: string } export type ProgressUpdateContinue = { frame: number fps: number out_time_ms: number } export type ProgressStatus = 'continue' | 'complete' export class ApiClient { url: string constructor(url: string) { this.url = url } async health( pathParams: {}, queryParams: {}, body: {} ): Promise> { try { const url = new URL(this.url) url.pathname = '/' + `health` const resp = await axios.get>( url.toString() ) return resp.data } catch (err: any) { if (err.response.data && err.response.data.tag !== 'undefined') { return err.response.data as ApiResponse } throw new Error(err) } } async render( pathParams: {}, queryParams: AuthQueryParams, body: {} ): Promise> { try { const url = new URL(this.url) Object.keys(queryParams).forEach((key) => url.searchParams.set(key, queryParams[key as keyof typeof queryParams]) ) url.pathname = '/' + `render` const resp = await axios.get>(url.toString()) return resp.data } catch (err: any) { if (err.response.data && err.response.data.tag !== 'undefined') { return err.response.data as ApiResponse } throw new Error(err) } } async addMediaProperties( pathParams: {}, queryParams: AuthQueryParams, body: AddMediaRequest ): Promise> { try { const url = new URL(this.url) Object.keys(queryParams).forEach((key) => url.searchParams.set(key, queryParams[key as keyof typeof queryParams]) ) url.pathname = '/' + `add-media-properties` const resp = await axios.post>( url.toString(), body ) return resp.data } catch (err: any) { if (err.response.data && err.response.data.tag !== 'undefined') { return err.response.data as ApiResponse } throw new Error(err) } } async addMediaFile( pathParams: { id: string; media_kind: MediaKind }, queryParams: AuthQueryParams, body: {} ): Promise> { try { const url = new URL(this.url) Object.keys(queryParams).forEach((key) => url.searchParams.set(key, queryParams[key as keyof typeof queryParams]) ) url.pathname = '/' + `add-media-file/${pathParams.id}/${pathParams.media_kind}` const resp = await axios.get>(url.toString()) return resp.data } catch (err: any) { if (err.response.data && err.response.data.tag !== 'undefined') { return err.response.data as ApiResponse } throw new Error(err) } } async addMediaFileLocal( pathParams: { id: string; media_kind: MediaKind }, queryParams: AuthQueryParams, body: FormData ): Promise> { try { const url = new URL(this.url) Object.keys(queryParams).forEach((key) => url.searchParams.set(key, queryParams[key as keyof typeof queryParams]) ) url.pathname = '/' + `add-media-file/${pathParams.id}/${pathParams.media_kind}` const resp = await axios.post>( url.toString(), body ) return resp.data } catch (err: any) { if (err.response.data && err.response.data.tag !== 'undefined') { return err.response.data as ApiResponse } throw new Error(err) } } async outputFileLocal( pathParams: {}, queryParams: AuthQueryParams, body: {} ): Promise { try { const url = new URL(this.url) Object.keys(queryParams).forEach((key) => url.searchParams.set(key, queryParams[key as keyof typeof queryParams]) ) url.pathname = '/' + `output-local` const resp = await axios.get(url.toString(), { responseType: 'arraybuffer' }) return resp.data } catch (err: any) { if (err.response.data && err.response.data.tag !== 'undefined') { return err.response.data as ArrayBuffer } throw new Error(err) } } async init( pathParams: {}, queryParams: AuthQueryParams, body: InitRequest ): Promise> { try { const url = new URL(this.url) Object.keys(queryParams).forEach((key) => url.searchParams.set(key, queryParams[key as keyof typeof queryParams]) ) url.pathname = '/' + `init` const resp = await axios.post>( url.toString(), body ) return resp.data } catch (err: any) { if (err.response.data && err.response.data.tag !== 'undefined') { return err.response.data as ApiResponse } throw new Error(err) } } async outputFile( pathParams: {}, queryParams: AuthQueryParams, body: {} ): Promise> { try { const url = new URL(this.url) Object.keys(queryParams).forEach((key) => url.searchParams.set(key, queryParams[key as keyof typeof queryParams]) ) url.pathname = '/' + `output` const resp = await axios.get>(url.toString()) return resp.data } catch (err: any) { if (err.response.data && err.response.data.tag !== 'undefined') { return err.response.data as ApiResponse } throw new Error(err) } } async isReady( pathParams: {}, queryParams: AuthQueryParams, body: {} ): Promise> { try { const url = new URL(this.url) Object.keys(queryParams).forEach((key) => url.searchParams.set(key, queryParams[key as keyof typeof queryParams]) ) url.pathname = '/' + `is-ready` const resp = await axios.get>( url.toString() ) return resp.data } catch (err: any) { if (err.response.data && err.response.data.tag !== 'undefined') { return err.response.data as ApiResponse } throw new Error(err) } } async setTimelineConfig( pathParams: {}, queryParams: AuthQueryParams, body: TimelineSettings ): Promise> { try { const url = new URL(this.url) Object.keys(queryParams).forEach((key) => url.searchParams.set(key, queryParams[key as keyof typeof queryParams]) ) url.pathname = '/' + `set-timeline-config` const resp = await axios.post>( url.toString(), body ) return resp.data } catch (err: any) { if (err.response.data && err.response.data.tag !== 'undefined') { return err.response.data as ApiResponse } throw new Error(err) } } } export type AddMediaRequest = AddMediaPropertiesRequest export type MediaId = string export type MediaProperties = { tag: 'Text' value: TextMediaProperties } export type CSSColor = string export type TextAlign = 'left' | 'center' | 'right' export type VerticalAlign = 'top' | 'middle' | 'bottom' export interface AddMediaPropertiesRequest { mediaId: MediaId properties: MediaProperties } /** * This corresponds to Text class in Typescript codebase, but only includes the properties we care about */ export interface TextMediaProperties { style: TextStyle text: string } export interface TextStyle { backgroundColor: CSSColor borderRadius: number fontFamily: string fontSize: number fontWeight: number lineHeight: number textAlign: TextAlign textColor: CSSColor verticalAlign: VerticalAlign } export type UserId = string export interface AuthQueryParams { token: UserId userId: UserId } export type FontStyle = 'normal' | 'italic' export type UnicodeBlock = | 'latin' | 'latin-ext' | 'cyrillic' | 'cyrillic-ext' | 'greek' | 'greek-ext' | 'vietnamese' export type FontDefs = FontDef[] export type Frames = number export type MediaKind = 'audio' | 'video' | 'image' | 'text' | 'black' export type Track = Media[] export type MediaTracks = Track[] export type Uuid = string export interface InitRequest { fontDefs: FontDefs media: MediaTracks projectId: Uuid } export interface FontDef { family: string format: string properties: FontProperties unicodeBlock: UnicodeBlock } export interface FontProperties { fontStyle: FontStyle fontWeight: number src: string unicodeRange: string } export interface Media { mediaElement: MediaElement source?: string | null } export interface MediaElement { animations?: Animation[] /** * in seconds */ duration: number id: string mediaId: MediaId playbackRate?: number position: Position /** * in seconds */ start: number /** * in seconds */ startOnTimeline: number type: MediaKind } export interface Animation { height?: number | null opacity?: number | null rotationDegrees?: number | null scale?: number | null start: Frames width?: number | null x?: number | null y?: number | null } export interface Position { height: number rotationDegrees?: number | null scale: number width: number x: number y: number } export type ReadyState = | { tag: 'missingMediaProperties' value: MediaId[] } | { tag: 'missingMediaFiles' value: MediaId[] } | { tag: 'noMedia' } | { tag: 'missingXML' } | { tag: 'missingConfig' } | { tag: 'ready' } export interface TimelineSettings { fps: number height: number outputFile: string width: number }