From 09264c25fbceb3210e362dbb9c49583a8069a71a Mon Sep 17 00:00:00 2001 From: Pierce Date: Thu, 26 Oct 2023 11:26:22 -0700 Subject: [PATCH] consistent language default across library, minor refactoring --- src/lib/Client.ts | 4 +++- src/lib/Content.ts | 12 ++++++------ src/lib/Download.ts | 4 ++-- src/lib/FormBuilder.ts | 2 +- src/lib/Hierarchy.ts | 4 ++-- src/lib/List.ts | 2 +- src/lib/Media.ts | 8 ++++---- src/lib/MediaCategory.ts | 4 ++-- src/lib/ServerSideLink.ts | 16 +++++++--------- src/lib/Version.ts | 8 ++++---- 10 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/lib/Client.ts b/src/lib/Client.ts index 9a4d10b..0c88c33 100644 --- a/src/lib/Client.ts +++ b/src/lib/Client.ts @@ -25,14 +25,16 @@ 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) { + constructor(url: string, token: string, language: string = 'en') { this.url = url this.token = token + this.language = language this.content = new Content(this) this.contentType = new ContentType(this) diff --git a/src/lib/Content.ts b/src/lib/Content.ts index 6d43c41..2eebfe5 100644 --- a/src/lib/Content.ts +++ b/src/lib/Content.ts @@ -29,7 +29,7 @@ export class Content { } } - async getVersions(contentId: number, language: string): Promise { + async getVersions(contentId: number, language: string = this.client.language): Promise { const response = await this.client.call('GET', `${ContentEndpoint}/${contentId}/${language}/version`, null) let jsonResp = await response.json() if (jsonResp.errorText) { @@ -38,18 +38,18 @@ export class Content { return jsonResp } - async get(contentId: number, sectionId: number, language: string): Promise { + async get(contentId: number, sectionId: number, language: string = this.client.language): Promise { const response = await this.client.call('GET', `${ContentEndpoint}/${sectionId}/${contentId}/${language}`, null) return await response.json() } - async getWithoutSection(contentId: number, language: string, version?: string): Promise { + async getWithoutSection(contentId: number, version?: string, language: string = this.client.language): Promise { if (!version) version = (await this.getVersions(contentId, language))[0].version const response = await this.client.call('GET', `${ContentEndpoint}/${contentId}/${language}/version/${version}`, null) return await response.json() } - async delete(contentId: number, sectionId: number, language: string) { + async delete(contentId: number, sectionId: number, language: string = this.client.language) { const response = await this.client.call('DELETE', `${ContentEndpoint}/${sectionId}/${contentId}/${language}`, null) return response?.ok } @@ -77,7 +77,7 @@ export class Content { return await response.json() } - async prePopulateContentInfo(contentTypeId: number, sectionId:number): Promise { + async prePopulateContentInfo(contentTypeId: number, sectionId: number): Promise { const response = await this.client.call('GET', `${ContentEndpoint}/type/${contentTypeId}/${sectionId}`, null) return await response.json() } @@ -87,7 +87,7 @@ export class Content { return await response.json() } - async modify(contentId: number, sectionId: number, options: ContentDTO, language: string = 'en'): Promise { + async modify(contentId: number, sectionId: number, options: ContentDTO, language: string = this.client.language): Promise { let existingContent = await this.get(contentId, sectionId, language) if (!existingContent) throw Error(`Content ${contentId} in section ${sectionId} does not exist`) const response = await this.client.call('POST', `${ContentEndpoint}/${sectionId}/${contentId}/${language}`, { diff --git a/src/lib/Download.ts b/src/lib/Download.ts index 3796030..1e913c0 100644 --- a/src/lib/Download.ts +++ b/src/lib/Download.ts @@ -8,12 +8,12 @@ export class Download { this.client = client } - async getFileFromElement(element: string, contentId: number, language: string): Promise { + async getFileFromElement(element: string, contentId: number, language: string = this.client.language): Promise { const response = await this.client.call('GET', `${DownloadEndpoint}/${contentId}/${language}/${element}`, null) return await response.json() } - async getFileFromElementVersion(element: string, contentId: number, language: string, version: number): Promise { + async getFileFromElementVersion(element: string, contentId: number, version: number, language: string): Promise { const response = await this.client.call('GET', `${DownloadEndpoint}/${contentId}/${language}/${version}/${element}`, null) return await response.json() } diff --git a/src/lib/FormBuilder.ts b/src/lib/FormBuilder.ts index b3012eb..eace659 100644 --- a/src/lib/FormBuilder.ts +++ b/src/lib/FormBuilder.ts @@ -8,7 +8,7 @@ export class FormBuilder { this.client = client } - async list(language: string = 'en'): Promise { + async list(language: string = this.client.language): Promise { const response = await this.client.call('POST', `${FormBuilderEndpoint}/${language}`, { body: 'draw=5&columns%5B0%5D%5Bdata%5D=0&columns%5B0%5D%5Bname%5D=name&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=lastModified&columns%5B1%5D%5Bname%5D=lastModified&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=2&columns%5B2%5D%5Bname%5D=usage&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=false&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=3&columns%5B3%5D%5Bname%5D=actions&columns%5B3%5D%5Bsearchable%5D=true&columns%5B3%5D%5Borderable%5D=false&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=asc&start=0&length=-1&search%5Bvalue%5D=&search%5Bregex%5D=false', headers: { diff --git a/src/lib/Hierarchy.ts b/src/lib/Hierarchy.ts index 1fc99fe..0955a1b 100644 --- a/src/lib/Hierarchy.ts +++ b/src/lib/Hierarchy.ts @@ -8,7 +8,7 @@ export class Hierarchy { this.client = client } - async get(id: number, language: string): Promise { + async get(id: number, language: string = this.client.language): Promise { const response = await this.client.call('GET', `${HierarchyEndpoint}/${id}/${language}`, null) return await response.json() } @@ -57,7 +57,7 @@ export class Hierarchy { return response?.ok } - async update(id: number, language: string, options: Partial) { + async update(id: number, options: Partial, language: string = this.client.language) { 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 298b685..6a75b6f 100644 --- a/src/lib/List.ts +++ b/src/lib/List.ts @@ -8,7 +8,7 @@ export class List { this.client = client } - async get(listId: number, language: string = 'en'): Promise { + async get(listId: number, language: string = this.client.language): Promise { const response = await this.client.call('GET', `${ListEndpoint}/${listId}/${language}`, null) return await response.json() } diff --git a/src/lib/Media.ts b/src/lib/Media.ts index bc9c15c..368acba 100644 --- a/src/lib/Media.ts +++ b/src/lib/Media.ts @@ -39,22 +39,22 @@ export class Media { return await response.json() } - async get(contentId: number, language: string) { + async get(contentId: number, language: string = this.client.language) { const response = await this.client.call('GET', `${MediaEndpoint}/${contentId}/${language}`, null) return await response.json() } - async getMediaUsage(mediaID: number, language: string) { + async getMediaUsage(mediaID: number, language: string = this.client.language) { const response = await this.client.call('GET', `${MediaEndpoint}/${mediaID}/${language}/usage`, null) return await response.json() } - async bulkGetMediaUsage (mediaIDs: number[], language: string) { + 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() } - async list(categoryID: number, language: string): Promise { + async list(categoryID: number, language: string = this.client.language): Promise { const response = await this.client.call('POST', `${MediaEndpoint}/category/${categoryID}/${language}/list?showPending=true&showUntranslated=true`, { body: 'draw=1&columns%5B0%5D%5Bdata%5D=0&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=false&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=1&columns%5B1%5D%5Bname%5D=&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=2&columns%5B2%5D%5Bname%5D=&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=true&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=3&columns%5B3%5D%5Bname%5D=&columns%5B3%5D%5Bsearchable%5D=true&columns%5B3%5D%5Borderable%5D=true&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B4%5D%5Bdata%5D=4&columns%5B4%5D%5Bname%5D=&columns%5B4%5D%5Bsearchable%5D=true&columns%5B4%5D%5Borderable%5D=true&columns%5B4%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B4%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B5%5D%5Bdata%5D=5&columns%5B5%5D%5Bname%5D=&columns%5B5%5D%5Bsearchable%5D=true&columns%5B5%5D%5Borderable%5D=true&columns%5B5%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B5%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B6%5D%5Bdata%5D=6&columns%5B6%5D%5Bname%5D=&columns%5B6%5D%5Bsearchable%5D=true&columns%5B6%5D%5Borderable%5D=false&columns%5B6%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B6%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B7%5D%5Bdata%5D=7&columns%5B7%5D%5Bname%5D=&columns%5B7%5D%5Bsearchable%5D=true&columns%5B7%5D%5Borderable%5D=false&columns%5B7%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B7%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=5&order%5B0%5D%5Bdir%5D=desc&start=0&length=10&search%5Bvalue%5D=&search%5Bregex%5D=false', headers: { diff --git a/src/lib/MediaCategory.ts b/src/lib/MediaCategory.ts index 3a56c5f..70ac672 100644 --- a/src/lib/MediaCategory.ts +++ b/src/lib/MediaCategory.ts @@ -24,12 +24,12 @@ export class MediaCategory { return await response.json() } - async get(categoryID: number, language:string | 'en'): Promise { + async get(categoryID: number, language: string = this.client.language): Promise { const response = await this.client.call('GET', `${MediaCategoryEndpoint}/${categoryID}/${language}`, null) return await response.json() } - async update(categoryID: number, options: Partial, language:string | 'en'): Promise { + async update(categoryID: number, options: Partial, language: string = this.client.language): Promise { const current = await this.get(categoryID, language), newObj = Object.assign({}, current, options) const response = await this.client.call('PUT', `${MediaCategoryEndpoint}/${categoryID}/${language}`, { body: newObj diff --git a/src/lib/ServerSideLink.ts b/src/lib/ServerSideLink.ts index 418125c..539bf6d 100644 --- a/src/lib/ServerSideLink.ts +++ b/src/lib/ServerSideLink.ts @@ -12,7 +12,7 @@ export class ServerSideLink { this.linkId = 14 this.sslRegex = /sslink_id="(\d+)"/ this.util = { - getFromSection: async (sectionId: number, language: string = 'en'): Promise => { + getFromSection: async (sectionId: number, language: string = this.client.language): Promise => { const { hierarchy, content } = this.client const serverSideLinks: ServerSideLinkDTO[] = [] const contentIds = (await hierarchy.getContents(sectionId)).contents.map(content => content.id) @@ -37,23 +37,21 @@ export class ServerSideLink { if (!destinationSection) throw Error(`${options.toSection} doesn't exist!`) if (!options.path) options.path = destinationSection.path if (!options.toContent) options.toContent = 0 - const response: ServerSideLinkDTO = await (await this.client.call('PUT', ServerSideLinkEndpoint, { body: { ...options } })).json() + const response: ServerSideLinkDTO = await (await this.client.call('PUT', ServerSideLinkEndpoint, { body: options })).json() return response } async delete(options: ServerSideLinkData) { - const response = await this.client.call('DELETE', ServerSideLinkEndpoint, { - body: options - }) + const response = await this.client.call('DELETE', ServerSideLinkEndpoint, { body: options }) return await response.text() } async modify(linkId: number, options: ServerSideLinkData) { - const response = await this.client.call('PUT', `${ServerSideLinkEndpoint}/${linkId}`, { body: { ...options } }) + const response = await this.client.call('PUT', `${ServerSideLinkEndpoint}/${linkId}`, { body: options }) return await response.json() } - async get(linkId: number, sectionId: number, contentId: number, language: string = 'en'): Promise { + async get(linkId: number, sectionId: number, contentId: number, language: string = this.client.language): Promise { const response = await this.client.call('GET', `${ServerSideLinkEndpoint}/${linkId}/${language}/${sectionId}/${contentId}`, null) try { return await response.json() @@ -62,12 +60,12 @@ export class ServerSideLink { } } - async getSelectedContentsLinks(ids: number[], language: string = 'en') { + async getSelectedContentsLinks(ids: number[], language: string = this.client.language) { const response = await this.client.call('POST', `${ServerSideLinkEndpoint}/${language}`, { body: ids }) return await response.json() } - async getLinks(sectionId: number, contentId: number, language: string = 'en') { + async getLinks(sectionId: number, contentId: number, language: string = this.client.language) { const response = await this.client.call('GET', `${ServerSideLinkEndpoint}/${language}/${sectionId}/${contentId}`, null) return await response.json() } diff --git a/src/lib/Version.ts b/src/lib/Version.ts index fcd1c1c..4d90bbc 100644 --- a/src/lib/Version.ts +++ b/src/lib/Version.ts @@ -3,13 +3,13 @@ import { VersionDTO } from "./utility/Global.js" export const VersionEndpoint = 'version' export class Version { - private clinet: Client + private client: Client constructor(client:Client) { - this.clinet = client + this.client = client } - async get(contentId: number, language: string = 'en'): Promise { - const response = await this.clinet.call('GET', `${VersionEndpoint}/${contentId}/${language}`, null) + async get(contentId: number, language: string = this.client.language): Promise { + const response = await this.client.call('GET', `${VersionEndpoint}/${contentId}/${language}`, null) return await response.json() } } \ No newline at end of file