Skip to content

Commit

Permalink
consistent language default across library, minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierce01 committed Oct 26, 2023
1 parent 95f1de2 commit 09264c2
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 32 deletions.
4 changes: 3 additions & 1 deletion src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions src/lib/Content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Content {
}
}

async getVersions(contentId: number, language: string): Promise<VersionDTO[]> {
async getVersions(contentId: number, language: string = this.client.language): Promise<VersionDTO[]> {
const response = await this.client.call('GET', `${ContentEndpoint}/${contentId}/${language}/version`, null)
let jsonResp = await response.json()
if (jsonResp.errorText) {
Expand All @@ -38,18 +38,18 @@ export class Content {
return jsonResp
}

async get(contentId: number, sectionId: number, language: string): Promise<ContentDTO> {
async get(contentId: number, sectionId: number, language: string = this.client.language): Promise<ContentDTO> {
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<ContentDTO> {
async getWithoutSection(contentId: number, version?: string, language: string = this.client.language): Promise<ContentDTO> {
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
}
Expand Down Expand Up @@ -77,7 +77,7 @@ export class Content {
return await response.json()
}

async prePopulateContentInfo(contentTypeId: number, sectionId:number): Promise<NewContentDTO> {
async prePopulateContentInfo(contentTypeId: number, sectionId: number): Promise<NewContentDTO> {
const response = await this.client.call('GET', `${ContentEndpoint}/type/${contentTypeId}/${sectionId}`, null)
return await response.json()
}
Expand All @@ -87,7 +87,7 @@ export class Content {
return await response.json()
}

async modify(contentId: number, sectionId: number, options: ContentDTO, language: string = 'en'): Promise<ContentDTO> {
async modify(contentId: number, sectionId: number, options: ContentDTO, language: string = this.client.language): Promise<ContentDTO> {
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}`, {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export class Download {
this.client = client
}

async getFileFromElement(element: string, contentId: number, language: string): Promise<FileDownload> {
async getFileFromElement(element: string, contentId: number, language: string = this.client.language): Promise<FileDownload> {
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<FileDownload> {
async getFileFromElementVersion(element: string, contentId: number, version: number, language: string): Promise<FileDownload> {
const response = await this.client.call('GET', `${DownloadEndpoint}/${contentId}/${language}/${version}/${element}`, null)
return await response.json()
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/FormBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class FormBuilder {
this.client = client
}

async list(language: string = 'en'): Promise<FormBuilderResponseDTO> {
async list(language: string = this.client.language): Promise<FormBuilderResponseDTO> {
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: {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Hierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class Hierarchy {
this.client = client
}

async get(id: number, language: string): Promise<SectionDTO> {
async get(id: number, language: string = this.client.language): Promise<SectionDTO> {
const response = await this.client.call('GET', `${HierarchyEndpoint}/${id}/${language}`, null)
return await response.json()
}
Expand Down Expand Up @@ -57,7 +57,7 @@ export class Hierarchy {
return response?.ok
}

async update(id: number, language: string, options: Partial<SectionDTO>) {
async update(id: number, options: Partial<SectionDTO>, 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
Expand Down
2 changes: 1 addition & 1 deletion src/lib/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class List {
this.client = client
}

async get(listId: number, language: string = 'en'): Promise<PredefinedListDTO> {
async get(listId: number, language: string = this.client.language): Promise<PredefinedListDTO> {
const response = await this.client.call('GET', `${ListEndpoint}/${listId}/${language}`, null)
return await response.json()
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/Media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MediaItemTableData> {
async list(categoryID: number, language: string = this.client.language): Promise<MediaItemTableData> {
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: {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/MediaCategory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export class MediaCategory {
return await response.json()
}

async get(categoryID: number, language:string | 'en'): Promise<Category> {
async get(categoryID: number, language: string = this.client.language): Promise<Category> {
const response = await this.client.call('GET', `${MediaCategoryEndpoint}/${categoryID}/${language}`, null)
return await response.json()
}

async update(categoryID: number, options: Partial<Category>, language:string | 'en'): Promise<Category> {
async update(categoryID: number, options: Partial<Category>, language: string = this.client.language): Promise<Category> {
const current = await this.get(categoryID, language), newObj = Object.assign({}, current, options)
const response = await this.client.call('PUT', `${MediaCategoryEndpoint}/${categoryID}/${language}`, {
body: newObj
Expand Down
16 changes: 7 additions & 9 deletions src/lib/ServerSideLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServerSideLinkDTO[]> => {
getFromSection: async (sectionId: number, language: string = this.client.language): Promise<ServerSideLinkDTO[]> => {
const { hierarchy, content } = this.client
const serverSideLinks: ServerSideLinkDTO[] = []
const contentIds = (await hierarchy.getContents(sectionId)).contents.map(content => content.id)
Expand All @@ -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<ServerSideLinkDTO> {
async get(linkId: number, sectionId: number, contentId: number, language: string = this.client.language): Promise<ServerSideLinkDTO> {
const response = await this.client.call('GET', `${ServerSideLinkEndpoint}/${linkId}/${language}/${sectionId}/${contentId}`, null)
try {
return await response.json()
Expand All @@ -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()
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/Version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<VersionDTO[]> {
const response = await this.clinet.call('GET', `${VersionEndpoint}/${contentId}/${language}`, null)
async get(contentId: number, language: string = this.client.language): Promise<VersionDTO[]> {
const response = await this.client.call('GET', `${VersionEndpoint}/${contentId}/${language}`, null)
return await response.json()
}
}

0 comments on commit 09264c2

Please sign in to comment.