@libretexts/cxone-expert-node 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,37 @@
1
+ name: Release New Version
2
+
3
+ on: workflow_dispatch
4
+
5
+ jobs:
6
+ check_authorization:
7
+ name: Check authorization to release new version
8
+ runs-on: ubuntu-latest
9
+ outputs:
10
+ isAuthorized: ${{ steps.check-auth.outputs.is_authorized }}
11
+ steps:
12
+ - name: check-auth
13
+ id: check-auth
14
+ run: echo "is_authorized=${{ contains(secrets.DEPLOYMENT_AUTHORIZED_USERS, github.triggering_actor) }}" >> $GITHUB_OUTPUT
15
+ release:
16
+ name: Release new semantic version
17
+ needs: check_authorization
18
+ if: needs.check_authorization.outputs.isAuthorized == 'true'
19
+ runs-on: ubuntu-latest
20
+ outputs:
21
+ didRelease: ${{ steps.semver.outputs.new_release_published }}
22
+ newVersion: ${{ steps.semver.outputs.new_release_version }}
23
+ steps:
24
+ - name: Checkout
25
+ uses: actions/checkout@v4
26
+ with:
27
+ fetch-depth: 0
28
+ persist-credentials: false
29
+ - name: semantic-release
30
+ uses: cycjimmy/semantic-release-action@v3
31
+ id: semver
32
+ env:
33
+ GITHUB_TOKEN: ${{ secrets.LIBREBOT_ACCESS_TOKEN }}
34
+ GIT_AUTHOR_NAME: libretexts-bot
35
+ GIT_AUTHOR_EMAIL: dev@libretexts.org
36
+ GIT_COMMITTER_NAME: libretexts-bot
37
+ GIT_COMMITTER_EMAIL: dev@libretexts.org
@@ -0,0 +1,15 @@
1
+ {
2
+ "branches": ["main"],
3
+ "plugins": [
4
+ "@semantic-release/commit-analyzer",
5
+ "@semantic-release/release-notes-generator",
6
+ ["@semantic-release/npm", {
7
+ "npmPublish": false
8
+ }],
9
+ ["@semantic-release/git", {
10
+ "assets": ["package.json"],
11
+ "message": "chore(release): ${nextRelease.version} [skip ci]"
12
+ }],
13
+ "@semantic-release/github"
14
+ ]
15
+ }
package/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # CXone Expert Node SDK
2
+ This package is a custom SDK developed by [LibreTexts](https://libretexts.org) to interact with the [CXone Expert API](https://expert-help.nice.com/Integrations_and_Extending_Content/API). It should be noted that this package is NOT developed or maintained by CXone and, as such, does not guarantee 100% compatibility with the latest API updates.
3
+
4
+ ## Install
5
+
6
+ ```
7
+ npm install @libretexts/cxone-expert-node
8
+ ```
9
+
10
+ ## Usage
11
+ ```
12
+ import Expert from "@libretexts/cxone-expert-node";
13
+
14
+ const tld = "mysite.example.com"; // Your CXone site domain
15
+
16
+ const expert = new Expert(tld);
17
+
18
+ // Authenticate using server credentials
19
+ const authInstance = await expert.auth.ServerToken({
20
+ key: "mykey",
21
+ secret: "mysecret",
22
+ user: "myuser",
23
+ });
24
+
25
+ // Or, use auth.BrowserToken() to use a CXone provided JWT (not intended for production use)
26
+
27
+ // Get authentication header(s) (X-Deki-Token)
28
+ const authHeaders = authInstance.getHeader();
29
+
30
+ // Use the appropriate module(s) to interact with your site's API
31
+ const page = await expert.pages.getPage(123, {
32
+ auth: authHeaders,
33
+ tld,
34
+ });
35
+
36
+ console.log(page);
37
+ ```
38
+
39
+ ## License
40
+ This package is open-source and free to use under the [MIT License](http://opensource.org/licenses/MIT).
package/license.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 LibreTexts
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@libretexts/cxone-expert-node",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.ts",
6
+ "scripts": {
7
+ "dev": "tsx test.ts",
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "keywords": [],
11
+ "author": "LibreTexts, Inc.",
12
+ "license": "MIT",
13
+ "devDependencies": {
14
+ "@types/node": "^22.2.0",
15
+ "dotenv": "^16.4.5",
16
+ "tsx": "^4.17.0",
17
+ "typescript": "^5.5.4"
18
+ },
19
+ "dependencies": {
20
+ "axios": "^1.7.3"
21
+ }
22
+ }
package/src/index.ts ADDED
@@ -0,0 +1,35 @@
1
+ import pages from './modules/pages';
2
+ import auth from './modules/auth';
3
+ import requests from './modules/requests';
4
+ import { AuthObject, BaseArgs, BrowserTokenParams, ExpertGlobalOptions, ServerTokenParams } from './types';
5
+ import Pages from './modules/pages';
6
+
7
+
8
+ export default class Expert {
9
+ private globals: ExpertGlobalOptions = {};
10
+ private _pages?: Pages;
11
+ private _auth?: auth;
12
+
13
+ constructor(tld?: string) {
14
+ if (tld) {
15
+ this.globals.tld = tld;
16
+ }
17
+ }
18
+
19
+ public get pages(): Pages {
20
+ if (!this._pages) {
21
+ this._pages = new pages(this.globals);
22
+ }
23
+
24
+ return this._pages;
25
+ }
26
+
27
+ public get auth(): auth {
28
+ if (!this._auth) {
29
+ this._auth = new auth();
30
+ }
31
+
32
+ return this._auth;
33
+ }
34
+
35
+ }
@@ -0,0 +1,31 @@
1
+ import { AuthObject, BrowserTokenParams, ServerTokenParams } from "../types";
2
+ import { createHmac } from 'crypto';
3
+
4
+ export default class Auth {
5
+ private token: string | null = null;
6
+
7
+ public BrowserToken({ key }: BrowserTokenParams) {
8
+ this.token = key;
9
+ return this;
10
+ }
11
+
12
+ public ServerToken({ key, secret, user }: ServerTokenParams) {
13
+ const hmac = createHmac('sha256', secret);
14
+ const epoch = Math.floor(Date.now() / 1000);
15
+ hmac.update(`${key}_${epoch}_${user}`);
16
+ const hash = hmac.digest('hex');
17
+ this.token = `tkn_${key}_${epoch}_${user}_${hash}`;
18
+ return this;
19
+ }
20
+
21
+ public getToken() {
22
+ return this.token;
23
+ }
24
+
25
+ public getHeader(): AuthObject {
26
+ return {
27
+ 'X-Deki-Token': this.token
28
+ }
29
+ }
30
+ }
31
+
@@ -0,0 +1,73 @@
1
+ import { BaseArgs, ExpertGlobalOptions, GetPageContentsParams, GetPageContentsResponse, GetPageParams, GetPageResponse, GetPageSecurityParams, GetPageSecurityResponse, GetPageSubPagesParams, GetPageSubPagesResponse } from "../types";
2
+ import { getTld } from "../utils";
3
+ import Auth from "./auth";
4
+ import Requests from "./requests";
5
+
6
+ export default class Pages {
7
+ private globals: ExpertGlobalOptions;
8
+ private _auth?: Auth;
9
+
10
+ constructor(args: ExpertGlobalOptions, auth?: Auth) {
11
+ this.globals = args;
12
+ this._auth = auth;
13
+ }
14
+
15
+ private parsePageId(id: string | number) {
16
+ if (typeof id === 'number') {
17
+ return id.toString();
18
+ }
19
+ return `=${encodeURIComponent(id)}`;
20
+ }
21
+
22
+ public async getPage(id: string | number, funcArgs: BaseArgs, reqArgs?: GetPageParams) {
23
+ const pageId = this.parsePageId(id);
24
+ const tld = getTld(this.globals, funcArgs.tld);
25
+ const requests = new Requests(tld, funcArgs.auth);
26
+
27
+ const res = await requests.get<GetPageResponse>(`/pages/${pageId}`, {
28
+ params: {
29
+ ...reqArgs
30
+ }
31
+ });
32
+ return res.data
33
+ }
34
+
35
+ public async getPageContents(id: string | number, funcArgs: BaseArgs, reqArgs?: GetPageContentsParams) {
36
+ const pageId = this.parsePageId(id);
37
+ const tld = getTld(this.globals, funcArgs.tld);
38
+ const requests = new Requests(tld, funcArgs.auth);
39
+
40
+ const res = await requests.get<GetPageContentsResponse>(`/pages/${pageId}/contents`, {
41
+ params: {
42
+ ...reqArgs
43
+ }
44
+ });
45
+ return res.data;
46
+ }
47
+
48
+ public async getPageSubPages(id: string | number, funcArgs: BaseArgs, reqArgs?: GetPageSubPagesParams) {
49
+ const pageId = this.parsePageId(id);
50
+ const tld = getTld(this.globals, funcArgs.tld);
51
+ const requests = new Requests(tld, funcArgs.auth);
52
+
53
+ const res = await requests.get<GetPageSubPagesResponse>(`/pages/${pageId}/subpages`, {
54
+ params: {
55
+ ...reqArgs
56
+ }
57
+ });
58
+ return res.data;
59
+ }
60
+
61
+ public async getPageSecurity(id: string | number, funcArgs: BaseArgs, reqArgs?: GetPageSecurityParams) {
62
+ const pageId = this.parsePageId(id);
63
+ const tld = getTld(this.globals, funcArgs.tld);
64
+ const requests = new Requests(tld, funcArgs.auth);
65
+
66
+ const res = await requests.get<GetPageSecurityResponse>(`/pages/${pageId}/security`, {
67
+ params: {
68
+ ...reqArgs
69
+ }
70
+ });
71
+ return res.data;
72
+ }
73
+ }
@@ -0,0 +1,68 @@
1
+ import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
2
+ import { AuthObject } from "../types";
3
+
4
+ const API_BASE_URL = '/@api/deki';
5
+
6
+ export default class Requests {
7
+ private axiosInstance: AxiosInstance = axios.create();
8
+ private format: 'json' | 'xml' = 'json';
9
+
10
+ constructor(tld?: string, authObject?: AuthObject, format: 'json' | 'xml' = 'json') {
11
+ if (!tld) {
12
+ throw new Error('TLD is required');
13
+ }
14
+ if (!authObject) {
15
+ throw new Error('Auth object is required');
16
+ }
17
+ const parsedTLD = tld.endsWith('/') ? tld.slice(
18
+ 0,
19
+ tld.length - 1
20
+ ) : `${tld}`;
21
+
22
+ this.format = format;
23
+
24
+ this.axiosInstance = axios.create({
25
+ baseURL: `${parsedTLD}${API_BASE_URL}`,
26
+ headers: {
27
+ 'Content-Type': 'application/json',
28
+ 'X-Requested-With': 'XMLHttpRequest',
29
+ ...authObject,
30
+ },
31
+ });
32
+ }
33
+
34
+ private getFormatParam() {
35
+ return {
36
+ 'dream.out.format': this.format
37
+ }
38
+ }
39
+
40
+ public async get<T>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
41
+ return await this.axiosInstance.get<T>(url, {
42
+ ...config, params: {
43
+ ...config?.params,
44
+ ...this.getFormatParam()
45
+ }
46
+ });
47
+ }
48
+
49
+ public async post<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
50
+ return await this.axiosInstance.post<T>(url, data, { ...config });
51
+ }
52
+
53
+ public async put<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
54
+ return await this.axiosInstance.put<T>(url, data, { ...config });
55
+ }
56
+
57
+ public async patch<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
58
+ return await this.axiosInstance.patch<T>(url, data, { ...config });
59
+ }
60
+
61
+ public async del<T>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
62
+ return await this.axiosInstance.delete<T>(url, { ...config });
63
+ }
64
+
65
+ public async head<T>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
66
+ return await this.axiosInstance.head<T>(url, { ...config });
67
+ }
68
+ }
@@ -0,0 +1,14 @@
1
+
2
+ export type BrowserTokenParams = {
3
+ key: string;
4
+ }
5
+
6
+ export type ServerTokenParams = {
7
+ key: string;
8
+ secret: string;
9
+ user: string;
10
+ }
11
+
12
+ export type AuthObject = {
13
+ 'X-Deki-Token': string | null;
14
+ }
@@ -0,0 +1,15 @@
1
+ import { AuthObject } from './auth';
2
+
3
+ export * from './auth';
4
+ export * from './pages';
5
+ export * from './requests';
6
+ export * from './security';
7
+
8
+ export type BaseArgs = {
9
+ auth: AuthObject;
10
+ tld?: string
11
+ }
12
+
13
+ export type ExpertGlobalOptions = {
14
+ tld?: string;
15
+ }
@@ -0,0 +1,172 @@
1
+ import { BaseQueryParams, PaginationQueryParams, RequestModeQueryParam } from "./requests";
2
+ import { ExpertUser, PageSecurity } from "./security";
3
+
4
+ export type GetPageParams = {
5
+ format?: 'html' | 'xhtml';
6
+ revision?: string;
7
+ include?: 'contents' | 'prevnext';
8
+ includeDeleted?: boolean;
9
+ mode?: RequestModeQueryParam
10
+ exclude?: string;
11
+ } & BaseQueryParams
12
+
13
+ export type GetPageContentsParams = {
14
+ overview?: boolean;
15
+ include?: string;
16
+ reltopath?: string;
17
+ relto?: number;
18
+ pageid?: number;
19
+ includes?: 'overview' | 'tags' | 'overview,tags';
20
+ format?: 'html' | 'xhtml' | 'text' | 'dekicode';
21
+ highlight?: string;
22
+ revision?: string;
23
+ mode: RequestModeQueryParam;
24
+ section?: string;
25
+ } & BaseQueryParams
26
+
27
+ export type GetPageSubPagesParams = BaseQueryParams & PaginationQueryParams;
28
+ export type GetPageSecurityParams = BaseQueryParams & {
29
+ export?: boolean;
30
+ }
31
+
32
+ export type GetPageResponse = PageBase & PageExtended;
33
+ export type GetPageSecurityResponse = PageSecurity;
34
+
35
+ export type GetPageContentsResponse = {
36
+ '@revision': string;
37
+ '@type': string;
38
+ '@title': string;
39
+ body: string[];
40
+ head: string;
41
+ tail: string;
42
+ }
43
+
44
+ export type GetPageSubPagesResponse = {
45
+ '@count': string;
46
+ '@href': string;
47
+ 'page.subpage': PageBase | PageBase[];
48
+ }
49
+
50
+ export type PageBase = {
51
+ '@id': string;
52
+ '@guid': string;
53
+ '@draft.state': string;
54
+ '@href': string;
55
+ '@deleted': string;
56
+ '@revision': string;
57
+ article: string;
58
+ 'date.created': string;
59
+ 'date.modified': string;
60
+ language: string;
61
+ namespace: string;
62
+ path: PagePath;
63
+ security: PageSecurity;
64
+ title: string;
65
+ 'uri.ui': string;
66
+ }
67
+
68
+ export type PageExtended = {
69
+ '@unpublish': string;
70
+ aliases: {
71
+ [key: string]: string;
72
+ },
73
+ comments: {
74
+ '@count': string;
75
+ '@href': string;
76
+ },
77
+ contents: {
78
+ '@type': string;
79
+ '@href': string;
80
+ '@etag': string;
81
+ },
82
+ 'contents.alt': {
83
+ '@type': string;
84
+ '@href': string;
85
+ },
86
+ 'date.edited': string;
87
+ description: string;
88
+ files: {
89
+ '@count': string;
90
+ '@href': string;
91
+ file: PageFile | PageFile[]
92
+ }
93
+ inbound: {
94
+ '@count': string;
95
+ },
96
+ 'language.effective': string;
97
+ metrics: {
98
+ 'metric.charcount': string;
99
+ 'metric.views': string;
100
+ }
101
+ outbound: {
102
+ '@count': string;
103
+ }
104
+ 'page.parent': GetPageResponse;
105
+ 'page.redirectedfrom': string;
106
+ properties: {
107
+ '@count': string;
108
+ '@href': string;
109
+ property: Record<string, string>[];
110
+ },
111
+ rating: {
112
+ '@score': string;
113
+ '@count': string;
114
+ '@seated.score': string;
115
+ '@seated.count': string;
116
+ '@unseated.score': string;
117
+ '@unseated.count': string;
118
+ '@anonymous.score': string;
119
+ '@anonymous.count': string;
120
+ },
121
+ revisions: {
122
+ '@count': string;
123
+ '@href': string;
124
+ },
125
+ subpages: {
126
+ '@href': string;
127
+ },
128
+ summary: string;
129
+ tags: {
130
+ '@count': string;
131
+ '@href': string;
132
+ tag: PageTag | PageTag[]
133
+ },
134
+ timeuuid: string;
135
+ 'user.author': ExpertUser;
136
+ 'user.createdby': ExpertUser;
137
+ }
138
+
139
+
140
+ export type PageFile = {
141
+ '@id': string;
142
+ '@revision': string;
143
+ '@res-id': string;
144
+ '@href': string;
145
+ '@res-is-head': string;
146
+ '@res-is-deleted': string;
147
+ '@res-rev-is-deleted': string;
148
+ '@res-contents-id': string;
149
+ 'alt-text': string;
150
+ contents: object;
151
+ 'contents.preview': any[];
152
+ 'date.created': string;
153
+ 'date.last-modified': string;
154
+ description: string;
155
+ filename: string;
156
+ revisions: object;
157
+ 'user.createdBy': ExpertUser;
158
+ }
159
+
160
+ export type PageTag = {
161
+ '@value': string;
162
+ '@id': string;
163
+ '@href': string;
164
+ title: string;
165
+ type: string;
166
+ url: string;
167
+ }
168
+
169
+ export type PagePath = {
170
+ '@seo': string;
171
+ '#text': string;
172
+ }
@@ -0,0 +1,11 @@
1
+ export type BaseQueryParams = {
2
+ authenticate?: boolean;
3
+ redirects?: number;
4
+ }
5
+
6
+ export type PaginationQueryParams = {
7
+ offset?: number;
8
+ limit?: string
9
+ }
10
+
11
+ export type RequestModeQueryParam = 'edit' | 'view' | 'raw';
@@ -0,0 +1,48 @@
1
+ export type ExpertUser = {
2
+ '@anonymous': string;
3
+ '@virtual': string;
4
+ '@id': string;
5
+ '@wikiid': string;
6
+ '@href': string;
7
+ '@guid': string;
8
+ nick: string;
9
+ username: string | '#RESTRICTED'
10
+ }
11
+
12
+ export type PageSecurity = {
13
+ '@href': string;
14
+ grants: {
15
+ grant: SecurityGrant | SecurityGrant[]
16
+ },
17
+ 'permissions.effective': SecurityGrantPermissions | SecurityGrantPermissions[]
18
+ 'permissions.page': SecurityGrantPermissions & SecurityGrantPermissionsRestriction | SecurityGrantPermissions & SecurityGrantPermissionsRestriction[]
19
+ 'permissions.revoked': Record<string, { operations: SecurityGrantPermissionsOperations | SecurityGrantPermissionsOperations[] }>
20
+ }
21
+
22
+ export type SecurityGrant = {
23
+ 'date.modified': string;
24
+ permissions: SecurityGrantPermissions;
25
+ user: ExpertUser
26
+ 'user.modifiedby': ExpertUser
27
+ }
28
+
29
+ export type SecurityGrantPermissions = {
30
+ operations: SecurityGrantPermissionsOperations | SecurityGrantPermissionsOperations[]
31
+ role: SecurityRole | SecurityRole[]
32
+ }
33
+
34
+ export type SecurityGrantPermissionsOperations = {
35
+ '@mask': string;
36
+ '#text': string;
37
+ }
38
+
39
+ export type SecurityGrantPermissionsRestriction = {
40
+ '@id': string;
41
+ '#text': string;
42
+ }
43
+
44
+ export type SecurityRole = {
45
+ '@id': string;
46
+ '@href': string;
47
+ '#text': string;
48
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { ExpertGlobalOptions } from "./types";
2
+
3
+ export function getTld(globals: ExpertGlobalOptions, tld?: string) {
4
+ if (!tld && !globals.tld) {
5
+ throw new Error('TLD is required');
6
+ }
7
+ return tld ?? globals.tld;
8
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "compilerOptions": {
3
+ "lib": ["dom", "dom.iterable", "esnext"],
4
+ "allowJs": true,
5
+ "skipLibCheck": true,
6
+ "strict": true,
7
+ "noEmit": true,
8
+ "esModuleInterop": true,
9
+ "module": "esnext",
10
+ "moduleResolution": "bundler",
11
+ "resolveJsonModule": true,
12
+ "isolatedModules": true,
13
+ "jsx": "preserve",
14
+ "incremental": true,
15
+ "plugins": [
16
+ {
17
+ "name": "next"
18
+ }
19
+ ],
20
+ "paths": {
21
+ "@/*": ["./*"]
22
+ }
23
+ },
24
+ "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
25
+ "exclude": ["node_modules"]
26
+ }