osuny-owl 1.1.0 → 2.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,21 @@
1
+ # This workflow will publish a package to npm when a release is created
2
+
3
+ name: Node.js Package
4
+
5
+ on:
6
+ release:
7
+ types: [created]
8
+
9
+ jobs:
10
+ publish-npm:
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/checkout@v4
14
+ - uses: actions/setup-node@v4
15
+ with:
16
+ node-version: 20
17
+ registry-url: https://registry.npmjs.org/
18
+ - run: npm ci
19
+ - run: npm publish
20
+ env:
21
+ NODE_AUTH_TOKEN: ${{secrets.npm_token}}
@@ -303,36 +303,65 @@ export class OsunyUtility{
303
303
  * @param {string} title (Required) Title of the article / post
304
304
  * @param {string} migration_identifier (Required) A unique identifier to catch its migration in the logs
305
305
  * @param {Array} blocks (Required) Array of the different Communication::Blocks composing the post
306
+ * @param {String} locale (Optional) Set the locale of the post created. Set to "fr" by default
306
307
  * @param {Array} category_ids (Optional) Array of strings representing the category or all the categories attached to this post. Empty Array by default
308
+ * @param {Date} crea_dt (Optional) Date of the post. Undefined by default
307
309
  * @param {boolean} full_width (Optional) Indicates if the article should take all the theme width. False by default
308
310
  * @param {string} summary (Optional) A short text that summerize the post. Empty string by default
309
311
  * @returns Osuny's Communication::Post object.
310
312
  */
311
- static createPost(title, migration_identifier, blocks, category_ids = [], full_width=false, summary=""){
313
+ static createPost(title, migration_identifier, blocks, locale = "fr", category_ids = [], crea_dt=undefined, full_width=false, summary=""){
312
314
  return {
313
315
  "id": undefined,
314
316
  "migration_identifier": migration_identifier,
315
317
  "full_width": full_width,
316
318
  "category_ids": category_ids,
317
319
  "localizations": {
318
- "fr": {
320
+ [locale]: {
319
321
  "id": undefined,
320
- "migration_identifier": migration_identifier + "_fr",
322
+ "migration_identifier": migration_identifier + `_${locale}`,
321
323
  "title": title,
322
324
  "featured_image": undefined,
323
325
  "pinned": false,
324
326
  "published": true,
325
- "published_at": undefined,
327
+ "published_at": crea_dt,
326
328
  "slug": this.slugify(title),
327
329
  "summary": summary,
328
330
  "blocks": blocks,
329
- "created_at": undefined
331
+ "created_at": crea_dt
330
332
  }
331
333
  }
332
334
 
333
335
  }
334
336
  }
335
337
 
338
+ static createProject(title, migration_identifier, year, blocks, locale="fr", category_ids = [], full_width=true, crea_dt=undefined, summary=""){
339
+ return {
340
+ "id": undefined,
341
+ "migration_identifier": migration_identifier,
342
+ "full_width": full_width,
343
+ "year": year,
344
+ "category_ids": category_ids,
345
+ "localizations": {
346
+ [locale]: {
347
+ "id": undefined,
348
+ "migration_identifier": migration_identifier + `_${locale}`,
349
+ "title": title,
350
+ "featured_image": undefined,
351
+ "pinned": false,
352
+ "published": true,
353
+ "published_at": crea_dt,
354
+ "slug": this.slugify(title),
355
+ "summary": summary,
356
+ "blocks": blocks,
357
+ "created_at": crea_dt
358
+ }
359
+ }
360
+
361
+ }
362
+ }
363
+
364
+
336
365
  /**
337
366
  *
338
367
  * @param {...Object} osunyBlocks Osuny's Communication::Block in JSON.
package/package.json CHANGED
@@ -1,20 +1,17 @@
1
1
  {
2
2
  "name": "osuny-owl",
3
- "version": "1.1.0",
3
+ "version": "2.0.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
- "keywords": [
6
- "osuny",
7
- "api"
8
- ],
5
+ "keywords": ["osuny", "api"],
9
6
  "license": "GPL-3.0",
10
7
  "author": "Sacha André",
11
8
  "type": "module",
12
- "main": "index.js",
9
+ "main": "src/index.js",
13
10
  "scripts": {
14
- "test": "echo \"Error: no test specified\" && exit 1"
11
+ "test": "echo \"No test for this package\" && exit 0"
15
12
  },
16
13
  "dependencies": {
17
14
  "dotenv": "^16.5.0",
18
15
  "node-fetch": "^3.3.2"
19
16
  }
20
- }
17
+ }
package/readme.md CHANGED
@@ -3,8 +3,6 @@
3
3
  Osuny Owl is a little package that adds a cute owl who can help you talk with an Osuny API.
4
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
5
 
6
- >[!NOTE]
7
- > This package is for the moment usable only for websites localized in french.
8
6
 
9
7
  ## Adopt an owl
10
8
 
@@ -20,7 +18,7 @@ Here, `website_id` is the unique identifier of one of your websites and `api_url
20
18
  ## The OsunyUtility
21
19
 
22
20
  > [!NOTE]
23
- > The Utility is for the moment very limited. Images are not supported for the moment
21
+ > The Utility is for the moment very limited. All the Blocks are not supported
24
22
 
25
23
  To use it in your app, you can import it like this
26
24
 
@@ -30,4 +28,4 @@ import { OsunyUtility } from 'osuny-owl';
30
28
  let chapterOne = OsunyUtility.createChapter(...)
31
29
  let datatableOne = OsunyUtility.createDatable(...)
32
30
  let postOne = OsunyUtility.createPost(...)
33
- ```
31
+ ```
@@ -0,0 +1,63 @@
1
+ import 'dotenv/config';
2
+ import { apiPost } from './api/client.js';
3
+
4
+ export class OsunyOwl {
5
+ constructor(website_id, api_url) {
6
+ this.website_id = website_id;
7
+ this.category_ids = [];
8
+ this.api_key_defined = process.env.OSUNY_API_KEY ? true : false;
9
+ this.api_url = api_url;
10
+ this.last_media = undefined;
11
+ }
12
+
13
+ set website_id(website_id) {
14
+ this._website_id = website_id;
15
+ }
16
+
17
+ set category_ids(category_ids) {
18
+ this._category_ids = category_ids;
19
+ }
20
+
21
+ checkApiKey() {
22
+ this.api_key_defined = process.env.OSUNY_API_KEY ? true : false;
23
+ }
24
+
25
+ get website_id() {
26
+ return this._website_id;
27
+ }
28
+
29
+ get category_ids() {
30
+ return this._category_ids;
31
+ }
32
+
33
+ addCategory_id(category_id) {
34
+ this.category_ids.push(category_id);
35
+ }
36
+
37
+ removeCategory_id(category_id) {
38
+ if (this.category_ids.includes(category_id)) {
39
+ this.category_ids = this.category_ids.filter(e => e !== category_id);
40
+ }
41
+ }
42
+
43
+ async postToOsuny(post) {
44
+ if (this.api_key_defined) {
45
+ const url = this.api_url + "/communication/websites/" + this.website_id + "/posts";
46
+ await apiPost(url, post, process.env.OSUNY_API_KEY, false);
47
+ return true;
48
+ } else {
49
+ throw new Error("No API Key Defined");
50
+ }
51
+ }
52
+
53
+ async importImage(img_bdy) {
54
+ if (this.api_key_defined) {
55
+ const url = this.api_url + "/communication/medias";
56
+ let dataR = await apiPost(url, img_bdy, process.env.OSUNY_API_KEY, true);
57
+ this.last_media = dataR.original_blob;
58
+ return true;
59
+ } else {
60
+ throw new Error("No API Key Defined");
61
+ }
62
+ }
63
+ }
@@ -0,0 +1,28 @@
1
+ import fetch from 'node-fetch';
2
+
3
+ export async function apiPost(url, data, apiKey, isFormData = false) {
4
+ const headers = {
5
+ 'X-Osuny-Token': apiKey
6
+ };
7
+
8
+ if (!isFormData) {
9
+ headers['Content-Type'] = 'application/json';
10
+ }
11
+
12
+ try {
13
+ const response = await fetch(url, {
14
+ method: 'POST',
15
+ headers,
16
+ body: isFormData ? data : JSON.stringify(data)
17
+ });
18
+
19
+ if (!response.ok) {
20
+ throw new Error(`Response status: ${response.status}`);
21
+ }
22
+
23
+ return await response.json();
24
+ } catch (error) {
25
+ console.error(error.message);
26
+ throw error;
27
+ }
28
+ }
@@ -0,0 +1,4 @@
1
+ export const endpoints = {
2
+ posts: (websiteId) => `/communication/websites/${websiteId}/posts`,
3
+ media: () => '/communication/medias',
4
+ };
@@ -0,0 +1,32 @@
1
+ export function createChapter(text, migration_identifier, position, title = "", layout = 1) {
2
+ let layout_text;
3
+
4
+ switch (layout) {
5
+ case 1:
6
+ layout_text = "no_background";
7
+ break;
8
+ case 2:
9
+ layout_text = "alt_background";
10
+ break;
11
+ case 3:
12
+ layout_text = "accent_background";
13
+ break;
14
+ default:
15
+ layout_text = "no_background";
16
+ break;
17
+ }
18
+
19
+ return {
20
+ "id": null,
21
+ "migration_identifier": migration_identifier,
22
+ "template_kind": "chapter",
23
+ "title": title,
24
+ "position": position,
25
+ "published": true,
26
+ "html_class": null,
27
+ "data": {
28
+ "layout": layout_text,
29
+ "text": text,
30
+ }
31
+ };
32
+ }
@@ -0,0 +1,17 @@
1
+ export function createDatatable(table_data, table_headers = [], migration_identifier, position, title = "", alphabetical = false, caption = "") {
2
+ return {
3
+ "id": null,
4
+ "migration_identifier": migration_identifier,
5
+ "template_kind": "datatable",
6
+ "title": title,
7
+ "position": position,
8
+ "published": true,
9
+ "html_class": null,
10
+ "data": {
11
+ "columns": table_headers,
12
+ "elements": table_data,
13
+ "alphabetical": alphabetical,
14
+ "caption": caption
15
+ }
16
+ };
17
+ }
@@ -0,0 +1,21 @@
1
+ export function createImage(img_id, img_filename, img_signedid, migration_identifier, position, alt = "", credit = "", text = "", title = "") {
2
+ return {
3
+ "id": null,
4
+ "migration_identifier": migration_identifier,
5
+ "template_kind": "image",
6
+ "title": title,
7
+ "position": position,
8
+ "published": "true",
9
+ "html_class": null,
10
+ "data": {
11
+ "image": {
12
+ "id": img_id,
13
+ "filename": img_filename,
14
+ "signed_id": img_signedid
15
+ },
16
+ "alt": alt,
17
+ "credit": credit,
18
+ "text": text
19
+ }
20
+ };
21
+ }
@@ -0,0 +1,5 @@
1
+ export { createTitle } from './title.js';
2
+ export { createChapter } from './chapter.js';
3
+ export { createDatatable } from './datatable.js';
4
+ export { createVideo } from './video.js';
5
+ export { createImage } from './image.js';
@@ -0,0 +1,28 @@
1
+ export function createTitle(title, migration_identifier, position, layout = 1) {
2
+ let layout_title;
3
+
4
+ switch (layout) {
5
+ case 1:
6
+ layout_title = "classic";
7
+ break;
8
+ case 2:
9
+ layout_title = "collapsed";
10
+ break;
11
+ default:
12
+ layout_title = "classic";
13
+ break;
14
+ }
15
+
16
+ return {
17
+ "id": null,
18
+ "migration_identifier": migration_identifier,
19
+ "template_kind": "title",
20
+ "title": title,
21
+ "position": position,
22
+ "published": true,
23
+ "html_class": null,
24
+ "data": {
25
+ "layout": layout_title
26
+ }
27
+ };
28
+ }
@@ -0,0 +1,18 @@
1
+ export function createVideo(video_url, migration_identifier, position, video_title = "", video_desc = "", video_transc = "", title = "") {
2
+ return {
3
+ "id": null,
4
+ "migration_identifier": migration_identifier,
5
+ "template_kind": "video",
6
+ "title": title,
7
+ "position": position,
8
+ "published": "true",
9
+ "html_class": null,
10
+ "data": {
11
+ "layout": "player",
12
+ "description": video_desc,
13
+ "url": video_url,
14
+ "video_title": video_title,
15
+ "transcription": video_transc
16
+ }
17
+ };
18
+ }
@@ -0,0 +1,58 @@
1
+ import { slugify } from '../utils/slug.js';
2
+
3
+ export function createPost(title, migration_identifier, blocks, locale = "fr", category_ids = [], crea_dt = undefined, full_width = false, summary = "") {
4
+ return {
5
+ "id": undefined,
6
+ "migration_identifier": migration_identifier,
7
+ "full_width": full_width,
8
+ "category_ids": category_ids,
9
+ "localizations": {
10
+ [locale]: {
11
+ "id": undefined,
12
+ "migration_identifier": migration_identifier + `_${locale}`,
13
+ "title": title,
14
+ "featured_image": undefined,
15
+ "pinned": false,
16
+ "published": true,
17
+ "published_at": crea_dt,
18
+ "slug": slugify(title),
19
+ "summary": summary,
20
+ "blocks": blocks,
21
+ "created_at": crea_dt
22
+ }
23
+ }
24
+ };
25
+ }
26
+
27
+ export function createProject(title, migration_identifier, year, blocks, locale = "fr", category_ids = [], full_width = true, crea_dt = undefined, summary = "") {
28
+ return {
29
+ "id": undefined,
30
+ "migration_identifier": migration_identifier,
31
+ "full_width": full_width,
32
+ "year": year,
33
+ "category_ids": category_ids,
34
+ "localizations": {
35
+ [locale]: {
36
+ "id": undefined,
37
+ "migration_identifier": migration_identifier + `_${locale}`,
38
+ "title": title,
39
+ "featured_image": undefined,
40
+ "pinned": false,
41
+ "published": true,
42
+ "published_at": crea_dt,
43
+ "slug": slugify(title),
44
+ "summary": summary,
45
+ "blocks": blocks,
46
+ "created_at": crea_dt
47
+ }
48
+ }
49
+ };
50
+ }
51
+
52
+ export function composePost(...osunyBlocks) {
53
+ let blocksArray = [];
54
+ osunyBlocks.forEach((el) => {
55
+ blocksArray.push(el);
56
+ });
57
+ return blocksArray;
58
+ }
@@ -0,0 +1,15 @@
1
+ import * as blocks from './blocks/index.js';
2
+ import { createPost, createProject, composePost } from './content.js';
3
+ import { slugify } from '../utils/slug.js';
4
+
5
+ export class OsunyUtility {
6
+ static createTitle = blocks.createTitle;
7
+ static createChapter = blocks.createChapter;
8
+ static createDatatable = blocks.createDatatable;
9
+ static createVideo = blocks.createVideo;
10
+ static createImage = blocks.createImage;
11
+ static createPost = createPost;
12
+ static createProject = createProject;
13
+ static composePost = composePost;
14
+ static slugify = slugify;
15
+ }
package/src/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { OsunyOwl } from './OsunyOwl.js';
2
+ export { OsunyUtility } from './builders/index.js';
@@ -0,0 +1,13 @@
1
+ import { endpoints } from '../api/endpoints.js';
2
+
3
+ export class PostsResource {
4
+ constructor(client, websiteId) {
5
+ this.client = client;
6
+ this.websiteId = websiteId;
7
+ }
8
+
9
+ async create(post) {
10
+ const endpoint = endpoints.posts(this.websiteId);
11
+ return this.client.post(endpoint, post);
12
+ }
13
+ }
@@ -0,0 +1,9 @@
1
+ export function slugify(str) {
2
+ // Code taken from: https://dev.to/bybydev/how-to-slugify-a-string-in-javascript-4o9n
3
+ str = str.replace(/^\s+|\s+$/g, ''); // trim leading/trailing white space
4
+ str = str.toLowerCase(); // convert string to lowercase
5
+ str = str.replace(/[^a-z0-9 -]/g, '') // remove any non-alphanumeric characters
6
+ .replace(/\s+/g, '-') // replace spaces with hyphens
7
+ .replace(/-+/g, '-'); // remove consecutive hyphens
8
+ return str;
9
+ }
package/osuny_utility.js DELETED
File without changes