Skip to content

Commit

Permalink
Add Type endpoint, change interfaces to types, add modify media
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierce01 committed Mar 25, 2024
1 parent ce65404 commit ffbce81
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 89 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "t4.ts",
"version": "0.0.22",
"version": "0.0.23",
"description": "TypeScript API Library for TerminalFour",
"type": "module",
"exports": {
Expand Down
7 changes: 5 additions & 2 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { MediaCategory } from "./MediaCategory.js"
import { MediaType } from "./MediaType.js"
import { List } from "./List.js"
import { Profile } from "./Profile.js"
import { Upload } from "./Upload.js"
import { ServerSideLink } from "./ServerSideLink.js"
import { Type } from "./Type.js"
import { Upload } from "./Upload.js"
import { Version } from "./Version.js"

export class Client {
Expand All @@ -30,6 +31,7 @@ export class Client {
list: List
profile: Profile
serverSideLink: ServerSideLink
type: Type
upload: Upload
version: Version
constructor(url: string, token: string, language: string = 'en', _fetch = fetch) {
Expand All @@ -48,14 +50,15 @@ export class Client {
this.list = new List(this)
this.profile = new Profile(this)
this.serverSideLink = new ServerSideLink(this)
this.type = new Type(this)
this.upload = new Upload(this)
this.version = new Version(this)

this.isAuthorized = this.isAuthorized.bind(this)
this.fetch = _fetch
}

async call(method: string, endpoint: string, options: any) {
async call(method: string, endpoint: string, options: any): Promise<Response> {
if (!this.token) throw Error('Token not specified')
try {
let headers: Elements = {
Expand Down
42 changes: 34 additions & 8 deletions src/lib/Media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,48 @@ export class Media {
}

async add(data: MediaUpload) {
const formData = new FormData()
const expandedData: MediaData = MediaUploadData(data)
const filePath = path.resolve(data.file)
if (!await stat(filePath)) throw Error(`File at ${filePath} does not exist.`)
if (!expandedData.fileName || expandedData.fileName == 'undefined') expandedData.fileName = path.basename(filePath)
const blob = new Blob([await readFile(filePath)])
for (let key in expandedData) { key == 'file' ? formData.append('file', blob, expandedData.fileName) : formData.append(key, expandedData[key]) }
const formData = await this.prepareMediaData(data.file, expandedData)
const response = await this.client.call('POST', `${MediaEndpoint}`, { body: formData })
return await response.json()
}

async get(contentId: number, language: string = this.client.language): Promise<MediaItemDTO> {
const response = await this.client.call('GET', `${MediaEndpoint}/${contentId}/${language}`, null)
async get(mediaId: number, language: string = this.client.language): Promise<MediaItemDTO> {
const response = await this.client.call('GET', `${MediaEndpoint}/${mediaId}/${language}`, null)
return await response.json()
}

async modify(mediaId: number, data: Partial<MediaUpload>, categoryID?: number): Promise<number> {
const mediaObj = await this.get(mediaId)
if (mediaId != mediaObj.id) throw Error(`Media ID mismatch: ${mediaId} != ${mediaObj.id}`)
const expandedData: MediaData = MediaUploadData(data, mediaObj)
expandedData.version = String(parseFloat(mediaObj.version) + 1)
const formData = await this.prepareMediaData(data.file!, expandedData)
formData.append('mediaID', String(mediaId))
const response = await this.client.call('POST', `${MediaEndpoint}/category/${categoryID ?? mediaObj.categories[0]}/${expandedData.binaryLanguage}/${mediaId}`, { body: formData })
return response.status
}

async prepareMediaData(file: string | Blob, expandedData: MediaData): Promise<FormData> {
const formData = new FormData()
let blob: Blob
if (file instanceof Blob) {
blob = file
} else {
const filePath = path.resolve(file)
if (!await stat(filePath)) throw Error(`File at ${filePath} does not exist.`)
if (!expandedData.fileName || expandedData.fileName == 'undefined') expandedData.fileName = path.basename(filePath)
try {
blob = new Blob([await readFile(filePath)], { type: 'application/octet-stream' })
} catch(e) {
throw Error(`Error reading file at ${filePath}: ${e}`)
}
}
Object.entries(expandedData).forEach(([key, value]) => formData.append(key, value));
formData.append('file', blob, expandedData.fileName)
return formData
}

async getMediaUsage(mediaID: number, language: string = this.client.language): Promise<MediaUsageDTO[]> {
const response = await this.client.call('GET', `${MediaEndpoint}/${mediaID}/${language}/usage`, null)
return await response.json()
Expand Down
14 changes: 14 additions & 0 deletions src/lib/Type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Client } from "./Client.js"

export const TypeEndpoint = 'type'
export class Type {
private clinet: Client
constructor(client:Client) {
this.clinet = client
}

async list() {
const response = await this.clinet.call('GET', TypeEndpoint, null)
return await response.json()
}
}
Loading

0 comments on commit ffbce81

Please sign in to comment.