1 | # @kweli/cs-rest
|
2 |
|
3 | Simple authentication and REST calls for OpenText Content Server.
|
4 |
|
5 | ## Features
|
6 |
|
7 | - Provides a simplified interface for managing authentication with the OpenText Content Server REST API
|
8 | - Refreshes the `OTCSTicket` token automatically (minimising token expiration errors)
|
9 | - Simplifies POST, PUT, & PATCH requests (since Content Server doesn't support the `application/json` content type)
|
10 | - Based on the [axios](https://github.com/axios/axios) HTTP client
|
11 | - Works with Node.js and the browser
|
12 |
|
13 | ## Installing
|
14 |
|
15 | Using npm:
|
16 |
|
17 | ```bash
|
18 | $ npm install @kweli/cs-rest
|
19 | ```
|
20 |
|
21 | Using yarn:
|
22 |
|
23 | ```bash
|
24 | $ yarn add @kweli/cs-rest
|
25 | ```
|
26 |
|
27 | Using unpkg CDN:
|
28 |
|
29 | ```html
|
30 | <script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.min.js"></script>
|
31 | <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
|
32 | <script src="https://unpkg.com/@kweli/cs-rest"></script>
|
33 | ```
|
34 |
|
35 | ## Example
|
36 |
|
37 | Authenticate with a username and password and get the details of a node:
|
38 |
|
39 | ```js
|
40 | const { Session } = require('@kweli/cs-rest')
|
41 |
|
42 | // session wraps an axios instance
|
43 | const session = new Session({
|
44 | baseURL: 'https://.../cs.exe',
|
45 | username: 'Admin',
|
46 | password: '******'
|
47 | })
|
48 |
|
49 | // a Session instance can issue authenticated requests to Content Server
|
50 | const response = await session.get('/api/v1/nodes/2000')
|
51 | ```
|
52 |
|
53 | Authenticate with an `OTCSTicket`:
|
54 |
|
55 | ```js
|
56 | const session = new Session({
|
57 | baseURL: 'https://.../cs.exe',
|
58 | otcsticket: '<token>'
|
59 | })
|
60 | ```
|
61 |
|
62 | ## cs-rest API
|
63 |
|
64 | Requests can be made with the `get`, `post`, `put`, `patch`, `delete`, and `options` methods on the `Session` instance. These have the same interface as the respective methods in [axios](https://github.com/axios/axios).
|
65 |
|
66 | Content Server returns a fresh `OTCSTicket` with each successful API call. The `Session` instance automatically retains it for the subsequent request.
|
67 |
|
68 | #### POST, PUT, & PATCH
|
69 |
|
70 | The OpenText Content Server REST API doesn't accept requests that use the `application/json` content type. This means POST, PUT, & PATCH requests need to use a content type of `multipart/form-data`, which makes writing a request a little more verbose. For example, to create a new folder:
|
71 |
|
72 | ```js
|
73 | const formData = new FormData()
|
74 | formData.append('type', 0)
|
75 | formData.append('parent_id', 2000)
|
76 | formDAta.append('name', 'My New Folder')
|
77 |
|
78 | const response = await session.post('api/v2/nodes', formData)
|
79 | ```
|
80 |
|
81 | The `Session` class provides a `postForm` (also `putForm` and `patchForm`) method to simplify this:
|
82 |
|
83 | ```js
|
84 | const response = await session.postForm('api/v2/nodes', {
|
85 | type: 0,
|
86 | parent_id: 2000,
|
87 | name: 'My New Folder'
|
88 | })
|
89 | ```
|
90 |
|
91 | #### axios instance
|
92 |
|
93 | The underlying `axios` instance is available if these methods don't suffice:
|
94 |
|
95 | ```js
|
96 | const axios = session.axios
|
97 | ```
|
98 |
|
99 | #### Thin Wrapper
|
100 |
|
101 | The `Session` class provides a few convenience methods for performing commonly used REST requests. By no means is this complete, and it's possible the API will change in the future.
|
102 |
|
103 | For example, there is a method for creating a new folder:
|
104 |
|
105 | ```js
|
106 | const response = await session.nodes.addFolder(2000, 'My New Folder')
|
107 | ```
|
108 |
|
109 | A method also exists for uploading a document, where `file` is either:
|
110 |
|
111 | - a browser [File](https://developer.mozilla.org/en-US/docs/Web/API/File) object (e.g,. from drag and drop); or
|
112 | - a local file path, when using Node.js (e.g., `c:/temp/file.pdf`.
|
113 |
|
114 | ```js
|
115 | const response = await session.nodes.addDocument(2000, file)
|
116 | ```
|
117 |
|
118 | See the `src/` directory for more examples.
|
119 |
|
120 | ## Credits
|
121 |
|
122 | - [OpenText Content Server REST API](https://developer.opentext.com/webaccess/#url=%2Fawd%2Fresources%2Fapis%2Fcs-rest-api-for-cs-16-s&tab=501)
|
123 | - [Kwe.li GmbH](https://kwe.li/)
|
124 |
|
125 | ## License
|
126 |
|
127 | [MIT](LICENSE) |
\ | No newline at end of file |