osuny-owl 1.0.0 → 1.1.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.
Files changed (2) hide show
  1. package/index.js +134 -18
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,11 +1,14 @@
1
1
  import 'dotenv/config'
2
2
 
3
+ import fetch from 'node-fetch'
4
+
3
5
  export class OsunyOwl {
4
6
  constructor(website_id, api_url){
5
7
  this.website_id = website_id
6
8
  this.category_ids = []
7
9
  this.api_key_defined = process.env.OSUNY_API_KEY ? true : false
8
10
  this.api_url = api_url
11
+ this.last_media = undefined
9
12
  }
10
13
 
11
14
  set website_id(website_id){
@@ -46,14 +49,6 @@ export class OsunyOwl {
46
49
  }
47
50
  }
48
51
 
49
- /**
50
- * TODO
51
- *
52
- * - function : postToOsuny(Communication::Post object)
53
- * * Vérifier la connexion API
54
- * * Vérifier qu'il y ait un site
55
- *
56
- */
57
52
 
58
53
  /**
59
54
  * Async function to post a Communication::Post object to a specific website
@@ -86,21 +81,89 @@ export class OsunyOwl {
86
81
  }
87
82
  } else {
88
83
  throw new Error("No API Key Defined")
84
+ }
85
+ }
86
+
87
+ async importImage(img_bdy){
88
+
89
+ if(this.api_key_defined){
90
+ const url = this.api_url + "/communication/medias"
91
+
92
+ try {
93
+ const response = await fetch(url, {
94
+ method: "POST",
95
+ headers:{
96
+ "X-Osuny-Token": process.env.OSUNY_API_KEY
97
+ },
98
+ body: img_bdy
99
+ })
100
+
101
+ if (!response.ok){
102
+ throw new Error(`Response status: ${response.status}, ${response.statusText}. Error: ${response.text()}`)
103
+ } else {
104
+
105
+ let dataR = await response.json()
106
+ this.last_media = dataR.original_blob
107
+ return true
108
+ }
109
+ } catch (error) {
110
+ console.error(error.message)
111
+ }
112
+ } else {
113
+ throw new Error("No API Key Defined")
89
114
  }
90
-
91
115
  }
92
116
  }
93
117
 
94
118
  export class OsunyUtility{
119
+
120
+ /**
121
+ *
122
+ * @param {string} title **(Required)** Title to display
123
+ * @param {*} migration_identifier **(Required)** A unique migration identifier
124
+ * @param {*} position **(Required)** The position of the block. The first element positionned in a post has the value : 0
125
+ * @param {*} layout (Optional) Layout of the block. By default set to 1. 1 = classic, 2 = collapsed,
126
+ * @returns Osuny's Communication::Block (Title) object
127
+ */
128
+ static createTitle(title, migration_identifier, position, layout = 1){
129
+ let layout_title;
130
+
131
+ switch (layout) {
132
+ case 1:
133
+ layout_title = "classic"
134
+ break;
135
+
136
+ case 2:
137
+ layout_title = "collapsed"
138
+ break;
139
+
140
+ default:
141
+ layout_title = "classic"
142
+ break;
143
+ }
144
+
145
+ return {
146
+ "id": null,
147
+ "migration_identifier": migration_identifier,
148
+ "template_kind": "title",
149
+ "title": title,
150
+ "position": position,
151
+ "published": true,
152
+ "html_class": null,
153
+ "data": {
154
+ "layout": layout_title
155
+ }
156
+ }
157
+ }
95
158
 
96
159
  /**
97
160
  * Create a chapter Block to include in an Osuny's post
98
161
  *
99
- * @param {string} text - (Required) Main text of the chapter
100
- * @param {string} migration_identifier - (Required) a unique migration identifier
101
- * @param {number} position - (Required) The position of the block. The first element positionned in a post has the value : 0
102
- * @param {string} title - (Optional) Title of the block, will be displayed as h3 on the website
103
- * @param {number} layout - Layout of the block. By default set to 1. 1 = no_background, 2 = alt_background, 3 = accent_background
162
+ * @param {string} text **(Required)** Main text of the chapter
163
+ * @param {string} migration_identifier **(Required)** a unique migration identifier
164
+ * @param {number} position **(Required)** The position of the block. The first element positionned in a post has the value : 0
165
+ * @param {string} title (Optional) Title of the block, will be displayed as h3 on the website
166
+ * @param {number} layout Layout of the block. By default set to 1. 1 = no_background, 2 = alt_background, 3 = accent_background
104
167
  * @returns Osuny's Communication::Block (Chapter) object
105
168
  */
106
169
 
@@ -144,7 +207,7 @@ export class OsunyUtility{
144
207
  /**
145
208
  * Create a Datatable Block to include in an Osuny's Post
146
209
  *
147
- * @param {Array} table_data (Required) Array of objects with the following form
210
+ * @param {Array} table_data **(Required)** Array of objects with the following form
148
211
  * ```
149
212
  * { cells: [
150
213
  * "value_1",
@@ -153,9 +216,9 @@ export class OsunyUtility{
153
216
  * }
154
217
  * ```
155
218
  * - The number of strings in a "cells" array is representative of each cell in a row.
156
- * @param {Array} table_headers (Required) Array of strings with all the columns header of the datatable
157
- * @param {string} migration_identifier (Required) a unique migration identifier
158
- * @param {number} position (Required) The position of the block. The first element positionned in a post has the value : 0
219
+ * @param {Array} table_headers **(Required)** Array of strings with all the columns header of the datatable
220
+ * @param {string} migration_identifier **(Required)** a unique migration identifier
221
+ * @param {number} position **(Required)** The position of the block. The first element positionned in a post has the value : 0
159
222
  * @param {string} title (Optional) Title of the block, will be displayed as h3 on the website
160
223
  * @param {boolean} alphabetical (Optional) Sort the datable in alphabetical order if set to true. False by default
161
224
  * @param {string} caption (Optional) Set the datable caption (usualy after the table). Set to an empty string by default.
@@ -181,6 +244,59 @@ export class OsunyUtility{
181
244
  }
182
245
  }
183
246
 
247
+ /**
248
+ * Create a Video Block to include in an Osuny's Post
249
+ *
250
+ * @param {string} video_url **(Required)** The actual url of the video
251
+ * @param {string} migration_identifier (**Required)** A unique migration identifier
252
+ * @param {number} position **(Required)** Position of the block in the post
253
+ * @param {string} video_title Title of the video, will be displayed alongside the media
254
+ * @param {string} video_desc Description of the video, will be displayed as a paragraph alongside the media
255
+ * @param {string} video_transc (Recommended) Transcription of the video
256
+ * @param {string} title (Optional) Title of the block, will be displayed as h3 on the website
257
+ * @returns Osuny's Communication::Block (Video) object
258
+ */
259
+ static createVideo(video_url, migration_identifier, position, video_title = "", video_desc = "", video_transc = "", title = ""){
260
+ return {
261
+ "id": null,
262
+ "migration_identifier": migration_identifier,
263
+ "template_kind": "video",
264
+ "title": title,
265
+ "position": position,
266
+ "published": "true",
267
+ "html_class": null,
268
+ "data": {
269
+ "layout": "player",
270
+ "description": video_desc,
271
+ "url": video_url,
272
+ "video_title": video_title,
273
+ "transcription": video_transc
274
+ }
275
+ }
276
+ }
277
+
278
+ static createImage(img_id, img_filename, img_signedid, migration_identifier, position, alt="", credit="", text="", title = ""){
279
+ return {
280
+ "id": null,
281
+ "migration_identifier": migration_identifier,
282
+ "template_kind": "image",
283
+ "title": title,
284
+ "position": position,
285
+ "published": "true",
286
+ "html_class": null,
287
+ "data": {
288
+ "image": {
289
+ "id": img_id,
290
+ "filename": img_filename,
291
+ "signed_id": img_signedid
292
+ },
293
+ "alt": alt,
294
+ "credit": credit,
295
+ "text": text
296
+ }
297
+ }
298
+ }
299
+
184
300
  /**
185
301
  * Create a post Object to publish on a Osuny website
186
302
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "osuny-owl",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Osuny Owl is an object that can go talk to the Osuny API, to create post or other osuny components from a large amount of data",
5
5
  "keywords": [
6
6
  "osuny",
@@ -15,6 +15,6 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "dotenv": "^16.5.0",
18
- "node-fetch": "^2.7.0"
18
+ "node-fetch": "^3.3.2"
19
19
  }
20
20
  }