diff --git a/package-lock.json b/package-lock.json index 1bfaa7f..4c20db1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "t4.ts", - "version": "0.0.4", + "version": "0.0.21", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "t4.ts", - "version": "0.0.4", + "version": "0.0.21", "license": "MIT", "dependencies": { "typedoc-plugin-missing-exports": "^2.1.0" diff --git a/package.json b/package.json index 7d19e11..8c656a6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "t4.ts", - "version": "0.0.21", + "version": "0.0.22", "description": "TypeScript API Library for TerminalFour", "type": "module", "exports": { @@ -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", diff --git a/src/lib/Client.ts b/src/lib/Client.ts index 4194086..3108afe 100644 --- a/src/lib/Client.ts +++ b/src/lib/Client.ts @@ -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 @@ -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 @@ -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) { @@ -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, diff --git a/src/lib/Content.ts b/src/lib/Content.ts index 66f50e9..994982d 100644 --- a/src/lib/Content.ts +++ b/src/lib/Content.ts @@ -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 { const response = await this.client.call('DELETE', `${ContentEndpoint}/${sectionId}/${contentId}/${language}`, null) return response?.ok } diff --git a/src/lib/Hierarchy.ts b/src/lib/Hierarchy.ts index eebaa01..c1661e3 100644 --- a/src/lib/Hierarchy.ts +++ b/src/lib/Hierarchy.ts @@ -52,12 +52,12 @@ export class Hierarchy { return await response.json() } - async delete(id: number, isMandatory?: boolean) { + async delete(id: number, isMandatory?: boolean): Promise { const response = await this.client.call('DELETE', `${HierarchyEndpoint}/${id}${isMandatory ? '?mandatory=true' : ''}`, null) return response?.ok } - async update(id: number, options: Partial, language: string = this.client.language) { + async update(id: number, options: Partial, language: string = this.client.language): Promise { 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 diff --git a/src/lib/List.ts b/src/lib/List.ts index d2cd268..2e0cb34 100644 --- a/src/lib/List.ts +++ b/src/lib/List.ts @@ -13,7 +13,7 @@ export class List { return await response.json() } - async modify(list: PredefinedListDTO, language: string = this.client.language): Promise { + async modify(list: PredefinedListDTO, language: string = this.client.language): Promise { // 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 } diff --git a/src/lib/Media.ts b/src/lib/Media.ts index 606ecff..c1591fe 100644 --- a/src/lib/Media.ts +++ b/src/lib/Media.ts @@ -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' @@ -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 => { const structure = await this.client.mediaCategory.list(parentID, 'en') let categoryIds: number[] = [] const populateIds = (category: MediaCategoryObject) => { @@ -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 { 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 { 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() } @@ -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 { 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 diff --git a/src/lib/utility/Global.ts b/src/lib/utility/Global.ts index 309e92a..1860666 100644 --- a/src/lib/utility/Global.ts +++ b/src/lib/utility/Global.ts @@ -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; @@ -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[]