osuny-owl 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.
- package/index.js +247 -0
- package/osuny_utility.js +0 -0
- package/package.json +20 -0
- package/readme.md +33 -0
package/index.js
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import 'dotenv/config'
|
|
2
|
+
|
|
3
|
+
export class OsunyOwl {
|
|
4
|
+
constructor(website_id, api_url){
|
|
5
|
+
this.website_id = website_id
|
|
6
|
+
this.category_ids = []
|
|
7
|
+
this.api_key_defined = process.env.OSUNY_API_KEY ? true : false
|
|
8
|
+
this.api_url = api_url
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
set website_id(website_id){
|
|
12
|
+
this._website_id = website_id
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
set category_ids(category_ids){
|
|
16
|
+
this._category_ids = category_ids
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
checkApiKey(){
|
|
20
|
+
this.api_key_defined = process.env.OSUNY_API_KEY ? true : false
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get website_id(){
|
|
24
|
+
return this._website_id
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get category_ids(){
|
|
28
|
+
return this._category_ids
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Add a category id to the category_ids array
|
|
33
|
+
* @param {string} category_id id of a Post::Category object in osuny
|
|
34
|
+
*/
|
|
35
|
+
addCategory_id(category_id){
|
|
36
|
+
this.category_ids.push(category_id);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Remove given category id of the category_ids array
|
|
41
|
+
* @param {string} category_id id of a Post::Category object in osuny
|
|
42
|
+
*/
|
|
43
|
+
removeCategory_id(category_id){
|
|
44
|
+
if (this.category_ids.include(category_id)){
|
|
45
|
+
this.category_ids = this.category_ids.filter(e => e !== category_id)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
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
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Async function to post a Communication::Post object to a specific website
|
|
60
|
+
*
|
|
61
|
+
* @param {Object} post A Communication::Post object for Osuny's websites
|
|
62
|
+
* @returns Returns true if the operation is succesfull
|
|
63
|
+
*/
|
|
64
|
+
async postToOsuny(post){
|
|
65
|
+
if(this.api_key_defined){
|
|
66
|
+
const url = this.api_url + "/communication/websites/" + this.website_id + "/posts"
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
const response = await fetch(url, {
|
|
70
|
+
method: "POST",
|
|
71
|
+
headers:{
|
|
72
|
+
"Content-Type": "application/json",
|
|
73
|
+
"X-Osuny-Token": process.env.OSUNY_API_KEY
|
|
74
|
+
},
|
|
75
|
+
body: JSON.stringify(post)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
if (!response.ok){
|
|
79
|
+
throw new Error(`Response status: ${response.status}`)
|
|
80
|
+
} else {
|
|
81
|
+
return true
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
} catch (error) {
|
|
85
|
+
console.error(error.message)
|
|
86
|
+
}
|
|
87
|
+
} else {
|
|
88
|
+
throw new Error("No API Key Defined")
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export class OsunyUtility{
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Create a chapter Block to include in an Osuny's post
|
|
98
|
+
*
|
|
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
|
|
104
|
+
* @returns Osuny's Communication::Block (Chapter) object
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
static createChapter(text, migration_identifier, position, title = "", layout = 1){
|
|
108
|
+
// To define the layout kind of the block
|
|
109
|
+
let layout_text;
|
|
110
|
+
|
|
111
|
+
switch (layout) {
|
|
112
|
+
case 1:
|
|
113
|
+
layout_text = "no_background"
|
|
114
|
+
break;
|
|
115
|
+
|
|
116
|
+
case 2:
|
|
117
|
+
layout_text = "alt_background"
|
|
118
|
+
break;
|
|
119
|
+
|
|
120
|
+
case 3:
|
|
121
|
+
layout_text = "accent_background"
|
|
122
|
+
break;
|
|
123
|
+
|
|
124
|
+
default:
|
|
125
|
+
layout_text = "no_background"
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
"id": null,
|
|
131
|
+
"migration_identifier": migration_identifier,
|
|
132
|
+
"template_kind": "chapter",
|
|
133
|
+
"title": title,
|
|
134
|
+
"position": position,
|
|
135
|
+
"published": true,
|
|
136
|
+
"html_class": null,
|
|
137
|
+
"data": {
|
|
138
|
+
"layout": layout_text,
|
|
139
|
+
"text": text,
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Create a Datatable Block to include in an Osuny's Post
|
|
146
|
+
*
|
|
147
|
+
* @param {Array} table_data (Required) Array of objects with the following form
|
|
148
|
+
* ```
|
|
149
|
+
* { cells: [
|
|
150
|
+
* "value_1",
|
|
151
|
+
* "value_2"...
|
|
152
|
+
* ]
|
|
153
|
+
* }
|
|
154
|
+
* ```
|
|
155
|
+
* - 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
|
|
159
|
+
* @param {string} title (Optional) Title of the block, will be displayed as h3 on the website
|
|
160
|
+
* @param {boolean} alphabetical (Optional) Sort the datable in alphabetical order if set to true. False by default
|
|
161
|
+
* @param {string} caption (Optional) Set the datable caption (usualy after the table). Set to an empty string by default.
|
|
162
|
+
* @returns Osuny's Communication::Block (Datable) object
|
|
163
|
+
*/
|
|
164
|
+
|
|
165
|
+
static createDatatable(table_data, table_headers = [], migration_identifier, position, title = "", alphabetical = false, caption = ""){
|
|
166
|
+
|
|
167
|
+
return {
|
|
168
|
+
"id": null,
|
|
169
|
+
"migration_identifier": migration_identifier,
|
|
170
|
+
"template_kind": "datatable",
|
|
171
|
+
"title": title,
|
|
172
|
+
"position": position,
|
|
173
|
+
"published": true,
|
|
174
|
+
"html_class": null,
|
|
175
|
+
"data": {
|
|
176
|
+
"columns": table_headers,
|
|
177
|
+
"elements": table_data,
|
|
178
|
+
"alphabetical": alphabetical,
|
|
179
|
+
"caption": caption
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Create a post Object to publish on a Osuny website
|
|
186
|
+
*
|
|
187
|
+
* @param {string} title (Required) Title of the article / post
|
|
188
|
+
* @param {string} migration_identifier (Required) A unique identifier to catch its migration in the logs
|
|
189
|
+
* @param {Array} blocks (Required) Array of the different Communication::Blocks composing the post
|
|
190
|
+
* @param {Array} category_ids (Optional) Array of strings representing the category or all the categories attached to this post. Empty Array by default
|
|
191
|
+
* @param {boolean} full_width (Optional) Indicates if the article should take all the theme width. False by default
|
|
192
|
+
* @param {string} summary (Optional) A short text that summerize the post. Empty string by default
|
|
193
|
+
* @returns Osuny's Communication::Post object.
|
|
194
|
+
*/
|
|
195
|
+
static createPost(title, migration_identifier, blocks, category_ids = [], full_width=false, summary=""){
|
|
196
|
+
return {
|
|
197
|
+
"id": undefined,
|
|
198
|
+
"migration_identifier": migration_identifier,
|
|
199
|
+
"full_width": full_width,
|
|
200
|
+
"category_ids": category_ids,
|
|
201
|
+
"localizations": {
|
|
202
|
+
"fr": {
|
|
203
|
+
"id": undefined,
|
|
204
|
+
"migration_identifier": migration_identifier + "_fr",
|
|
205
|
+
"title": title,
|
|
206
|
+
"featured_image": undefined,
|
|
207
|
+
"pinned": false,
|
|
208
|
+
"published": true,
|
|
209
|
+
"published_at": undefined,
|
|
210
|
+
"slug": this.slugify(title),
|
|
211
|
+
"summary": summary,
|
|
212
|
+
"blocks": blocks,
|
|
213
|
+
"created_at": undefined
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
*
|
|
222
|
+
* @param {...Object} osunyBlocks Osuny's Communication::Block in JSON.
|
|
223
|
+
* @returns Array of Osuny's Communication::Block, usable as an argument for the createPost method.
|
|
224
|
+
*/
|
|
225
|
+
static composePost(...osunyBlocks){
|
|
226
|
+
let blocksArray = []
|
|
227
|
+
osunyBlocks.forEach((el) => {
|
|
228
|
+
blocksArray.push(el)
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
return blocksArray
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
static slugify(str){
|
|
235
|
+
// Code taken from the following article : https://dev.to/bybydev/how-to-slugify-a-string-in-javascript-4o9n
|
|
236
|
+
|
|
237
|
+
str = str.replace(/^\s+|\s+$/g, ''); // trim leading/trailing white space
|
|
238
|
+
str = str.toLowerCase(); // convert string to lowercase
|
|
239
|
+
str = str.replace(/[^a-z0-9 -]/g, '') // remove any non-alphanumeric characters
|
|
240
|
+
.replace(/\s+/g, '-') // replace spaces with hyphens
|
|
241
|
+
.replace(/-+/g, '-'); // remove consecutive hyphens
|
|
242
|
+
return str;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
package/osuny_utility.js
ADDED
|
File without changes
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "osuny-owl",
|
|
3
|
+
"version": "1.0.0",
|
|
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
|
+
"keywords": [
|
|
6
|
+
"osuny",
|
|
7
|
+
"api"
|
|
8
|
+
],
|
|
9
|
+
"license": "GPL-3.0",
|
|
10
|
+
"author": "Sacha André",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "index.js",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"dotenv": "^16.5.0",
|
|
18
|
+
"node-fetch": "^2.7.0"
|
|
19
|
+
}
|
|
20
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Osuny Owl
|
|
2
|
+
|
|
3
|
+
Osuny Owl is a little package that adds a cute owl who can help you talk with an Osuny API.
|
|
4
|
+
Still under construction, it's main use is for the creation of large sums of data, or for external apps that want to create posts in a static website. (With Peuplier for exemple)
|
|
5
|
+
|
|
6
|
+
>[!NOTE]
|
|
7
|
+
> This package is for the moment usable only for websites localized in french.
|
|
8
|
+
|
|
9
|
+
## Adopt an owl
|
|
10
|
+
|
|
11
|
+
After installing the package, simply use
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import { OsunyOwl } from 'osuny-owl';
|
|
15
|
+
|
|
16
|
+
let myOwl = new OsunyOwl(website_id, api_url)
|
|
17
|
+
```
|
|
18
|
+
Here, `website_id` is the unique identifier of one of your websites and `api_url` the url of your Osuny Instance API
|
|
19
|
+
|
|
20
|
+
## The OsunyUtility
|
|
21
|
+
|
|
22
|
+
> [!NOTE]
|
|
23
|
+
> The Utility is for the moment very limited. Images are not supported for the moment
|
|
24
|
+
|
|
25
|
+
To use it in your app, you can import it like this
|
|
26
|
+
|
|
27
|
+
```javascript
|
|
28
|
+
import { OsunyUtility } from 'osuny-owl';
|
|
29
|
+
|
|
30
|
+
let chapterOne = OsunyUtility.createChapter(...)
|
|
31
|
+
let datatableOne = OsunyUtility.createDatable(...)
|
|
32
|
+
let postOne = OsunyUtility.createPost(...)
|
|
33
|
+
```
|