Skip to content

Commit

Permalink
Initial Upload (Client, Profile, Hierarchy)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierce01 committed Jul 2, 2023
1 parent cef2856 commit 859cfe0
Show file tree
Hide file tree
Showing 10 changed files with 434 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/cjs
/esm
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# T4.ts
A TypeScript wrapper for TerminalFour's REST API



99 changes: 99 additions & 0 deletions package-lock.json

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

23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "t4ts",
"version": "0.0.1",
"description": "A simple terminalfour web api wrapper.",
"type": "module",
"exports": {
"require": "./cjs/index.js",
"import": "./esm/index.js",
"default": "./esm/index.js"
},
"typesVersions": {
"*": {
"index.d.ts": ["./esm/index.d.ts"]
}
},
"main": "./cjs/index.js",
"dependencies": {
"node-fetch": "^3.3.1"
},
"scripts": {
"build": "npx tsc --module es2022 --outDir esm/ && npx tsc --module commonjs --outDir cjs/"
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Client } from './lib/Client.js'
52 changes: 52 additions & 0 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Elements } from "./utility/Global.js"
import { Hierarchy } from "./Hierarchy.js"
import { Profile } from "./Profile.js"

export class Client {
url: String
private token: String
hierarchy: Hierarchy
profile: Profile
constructor(url: String, token: String) {
this.url = url
this.token = token

this.hierarchy = new Hierarchy(this)
this.profile = new Profile(this)
}

async call(method: string, endpoint: string, options: any) {
if (!this.token) throw Error('Token not specified')
try {
let headers: Elements = {
'authorization': `Bearer ${this.token}`,
'accept': 'application/json, text/javascript, */*; q=0.01',
'accept-language': 'en-US,en;q=0.9'
}
if (options?.body && typeof options.body == 'object') {
options.body = JSON.stringify(options.body)
headers['content-type'] = 'application/json'
}
const request = await fetch(`${this.url}/${endpoint}`, {
...options,
headers,
method,
})
return request
} catch (error) {
throw Error(`Request failed due to:\n${error}`)
}
}

setToken(token: string) {
this.token = token
}

setURL(url: string) {
this.url = url
}

verify() {
// Check if client can make requests
}
}
78 changes: 78 additions & 0 deletions src/lib/Hierarchy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Client } from './Client.js';
import { Channels, ContentTypeScopes, MetaDatas, MetaData,
LinkInfo, AccessControl, sortLock, InheritedPageLayouts } from './utility/Global.js';

export const HierarchyEndpoint = 'hierarchy'
export class Hierarchy {
private client: Client;
constructor(client: Client) {
this.client = client
}

async get(id: number, language: string) {
const response = await this.client.call('GET', `${HierarchyEndpoint}/${id}/${language}`, null)
return response?.ok ? await response.json() as SectionDTO : null
}

async delete(section:number | SectionDTO) {
const id = typeof section == 'number' ? section : section.id

}

}

export interface SectionDTO {
id: string;
parent: string;
name: string;
description: string;
outputUrl: string;
outputFilename: string;
accessKey: string;
keyPhrase: string;
status: string;
workflow: string;
parentWorkflowName: string;
show: string;
iseForm: string;
archive: string;
lastModified: Date;
printSequence: string;
contentSortMethod: string;
sectionSortMethod: string;
path: string;
mirrorOf: string;
sourceOfMirror: string;
link: string;
channels: Channels;
userIDs: string;
inheritedUserIDs: string;
groupIDs: string;
inheritedGroupIDs: string;
viewUserIDs: string;
viewGroupIDs: string;
contentTypeScopes: ContentTypeScopes;
metaDatas: MetaDatas;
linkInfo: LinkInfo;
excludedMirrorSections: string;
workflowName: string;
parentWorkflowID: string;
accessControl: AccessControl;
metaData: MetaData;
pathMembers: string;
sortLock: sortLock;
editable: string;
inheritedLinkSection: string;
accessControlEnabled: string;
accessControlType: string;
metaDataType: string;
accessControlInherited: string;
allowedGroups: string;
mirrorOfPath: string;
inheritedPageLayouts: InheritedPageLayouts;
outputUriEnabled: string;
publishEnabled: string;
outputFilenameEnabled: string;
spellCheckEnabled: string;
pathAsOutputUriEnabled: string;
}
38 changes: 38 additions & 0 deletions src/lib/Profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Client } from "./Client.js"

export const ProfileEndpoint = 'profile'
export class Profile {
private client: Client
constructor(client: Client) {
this.client = client
}

async get() {
const response = await this.client.call('GET', ProfileEndpoint, null)
return response?.ok ? await response.json() : null
}

async update(body: Partial<UserProfileView>) {
const currentProfile = await this.get()
if (!currentProfile) throw Error('Failed to get client profile')
const response = await this.client.call('POST', ProfileEndpoint, {
body: Object.assign(currentProfile, body),
})
return response?.ok ? await response.json() : false
}
}

export interface UserProfileView {
firstName: string;
lastName: string;
username: string;
emailAddress: string;
defaultLanguage: string;
oldPassword?: string;
newPassword?: string;
newPasswordConfirm?: string;
uiLocale: string;
htmlEditorId: string;
defaultPreviewChannelId: string;
userLevel: string;
}
Loading

0 comments on commit 859cfe0

Please sign in to comment.