Skip to content

Commit

Permalink
Allow fetch to be passed into client, mediaItemDTO
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierce01 committed Jan 29, 2024
1 parent c4603ae commit f076593
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 18 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "t4.ts",
"version": "0.0.21",
"version": "0.0.22",
"description": "TypeScript API Library for TerminalFour",
"type": "module",
"exports": {
Expand All @@ -19,7 +19,7 @@
"module": "./mjs/index.js",
"scripts": {
"build": "npx tsc --module es2022 --outDir esm/ && npx tsc --module commonjs --outDir cjs/",
"docs": "npx typedoc src/index.ts --plugin typedoc-plugin-missing-exports --excludeExternals true"
"docs": "npx typedoc src/index.ts --plugin typedoc-plugin-missing-exports --excludeExternals true"
},
"repository": {
"type": "git",
Expand Down
8 changes: 5 additions & 3 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { Version } from "./Version.js"
export class Client {
url: String
private token: String
language: string
fetch: any

content: Content
contentType: ContentType
Expand All @@ -25,13 +27,12 @@ export class Client {
media: Media
mediaCategory: MediaCategory
mediaType: MediaType
language: string
list: List
profile: Profile
serverSideLink: ServerSideLink
upload: Upload
version: Version
constructor(url: string, token: string, language: string = 'en') {
constructor(url: string, token: string, language: string = 'en', _fetch = fetch) {
this.url = url
this.token = token
this.language = language
Expand All @@ -51,6 +52,7 @@ export class Client {
this.version = new Version(this)

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

async call(method: string, endpoint: string, options: any) {
Expand All @@ -66,7 +68,7 @@ export class Client {
headers['content-type'] = 'application/json'
}
headers = options?.headers ? Object.assign(options.headers, headers) : headers
const request = await fetch(`${this.url}/${endpoint}`, {
const request = await this.fetch(`${this.url}/${endpoint}`, {
...options,
headers,
method,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class Content {
return await response.json()
}

async delete(contentId: number, sectionId: number, language: string = this.client.language) {
async delete(contentId: number, sectionId: number, language: string = this.client.language): Promise<boolean> {
const response = await this.client.call('DELETE', `${ContentEndpoint}/${sectionId}/${contentId}/${language}`, null)
return response?.ok
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Hierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ export class Hierarchy {
return await response.json()
}

async delete(id: number, isMandatory?: boolean) {
async delete(id: number, isMandatory?: boolean): Promise<boolean> {
const response = await this.client.call('DELETE', `${HierarchyEndpoint}/${id}${isMandatory ? '?mandatory=true' : ''}`, null)
return response?.ok
}

async update(id: number, options: Partial<SectionDTO>, language: string = this.client.language) {
async update(id: number, options: Partial<SectionDTO>, language: string = this.client.language): Promise<boolean> {
let section = await this.get(id, language)
const response = await this.client.call('PUT', `${HierarchyEndpoint}/${id}/${language}`, { body: Object.assign({}, section, options) })
return response?.ok
Expand Down
2 changes: 1 addition & 1 deletion src/lib/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class List {
return await response.json()
}

async modify(list: PredefinedListDTO, language: string = this.client.language): Promise<any> {
async modify(list: PredefinedListDTO, language: string = this.client.language): Promise<PredefinedListDTO> {
// Assuming the list object was retrieved from the API, it will have a primaryGroup object name, fullAccess, and group.
// The API expects the primaryGroup object to only have an id property and will fail if the other properties are present.
if (Object.keys(list.primaryGroup).length > 1) list.primaryGroup = { id: list.primaryGroup.id }
Expand Down
14 changes: 7 additions & 7 deletions src/lib/Media.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Client } from './Client.js'
import { MediaCategoryObject, MediaData, MediaItemTableData, MediaRow, MediaUpload, MediaUploadData } from './utility/Global.js'
import { MediaCategoryObject, MediaData, MediaItemDTO, MediaItemTableData, MediaRow, MediaUsageDTO, MediaUpload, MediaUploadData } from './utility/Global.js'
import { batcher } from './utility/helpers.js'
import * as path from 'path'
import { readFile, stat } from 'fs/promises'
Expand All @@ -11,7 +11,7 @@ export class Media {
constructor(client:Client) {
this.client = client
this.util = {
getMediaIDs: async (parentID: number, arrLimit: number = 50, reqTimeout: number = 10000) => {
getMediaIDs: async (parentID: number, arrLimit: number = 50, reqTimeout: number = 10000): Promise<number[]> => {
const structure = await this.client.mediaCategory.list(parentID, 'en')
let categoryIds: number[] = []
const populateIds = (category: MediaCategoryObject) => {
Expand Down Expand Up @@ -39,18 +39,18 @@ export class Media {
return await response.json()
}

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

async getMediaUsage(mediaID: number, language: string = this.client.language) {
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()
}

async bulkGetMediaUsage (mediaIDs: number[], language: string = this.client.language) {
const response = await this.client.call('POST', `${MediaEndpoint}/getUsage/${language}`, {body: mediaIDs})
async bulkGetMediaUsage(mediaIDs: number[], language: string = this.client.language) {
const response = await this.client.call('POST', `${MediaEndpoint}/getUsage/${language}`, { body: mediaIDs })
return await response.json()
}

Expand All @@ -64,7 +64,7 @@ export class Media {
return await response.json()
}

async downloadSingle(id: number, type: 'media' | 'file' | 'thumbnail', version?: string) {
async downloadSingle(id: number, type: 'media' | 'file' | 'thumbnail', version?: string): Promise<ArrayBuffer | null> {
if (!version) version = (await this.client.content.getVersions(id, 'smxx'))[0].version
const inital = await this.client.call('GET', `${MediaEndpoint}/${id}/smxx/${version}/${type}`, { redirect: 'manual' })
if (inital.status != 302) return null
Expand Down
50 changes: 50 additions & 0 deletions src/lib/utility/Global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,41 @@ export interface MediaItemTableData {
mediaRows: MediaRow[];
}

export interface MediaItemDTO {
id: number
contentTypeID: number
archiveSection: number
language: string
name: string
status: number
lastModifiedBy: number
version: string
editable: boolean
expired: boolean
canPublishNow: boolean
canSaveAndApprove: boolean
contentTypeAccess: number
elements: Elements
contentType: ContentTypeDTO
types: Type[]
syntax: number
type: number
binaryLanguage: string
categories: number[]
description: string
typeName: string
mediaURL: string
thumbnailURL: string
mediaPath: string
mediaSize: number
fileName: string
variant: number
variantName: string
variantDimensions: string
accessLevel: number
mediaCanHaveVariants: boolean
}

export interface MediaRow {
id: string;
status: string;
Expand Down Expand Up @@ -556,6 +591,21 @@ export interface FormUsageDTO {
name: string
}

export interface MediaUsageDTO {
contentName: string
assetType: string
variant: string
location: string
occurrences: number
language: string
contentID: number
sectionID: number
hasAccess: boolean
contentLayoutContentTypeID: number
mediaID: number
variantUsage: boolean
}

export interface contenUploadDTO {
id?: number
channels?: number[]
Expand Down

0 comments on commit f076593

Please sign in to comment.