osuny-owl 2.0.0 → 2.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "osuny-owl",
3
- "version": "2.0.0",
3
+ "version": "2.2.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": ["osuny", "api"],
6
6
  "license": "GPL-3.0",
package/src/OsunyOwl.js CHANGED
@@ -4,7 +4,8 @@ import { apiPost } from './api/client.js';
4
4
  export class OsunyOwl {
5
5
  constructor(website_id, api_url) {
6
6
  this.website_id = website_id;
7
- this.category_ids = [];
7
+ this.post_category_ids = [];
8
+ this.portfolio_category_ids = [];
8
9
  this.api_key_defined = process.env.OSUNY_API_KEY ? true : false;
9
10
  this.api_url = api_url;
10
11
  this.last_media = undefined;
@@ -14,8 +15,12 @@ export class OsunyOwl {
14
15
  this._website_id = website_id;
15
16
  }
16
17
 
17
- set category_ids(category_ids) {
18
- this._category_ids = category_ids;
18
+ set post_category_ids(category_ids) {
19
+ this._post_category_ids = category_ids;
20
+ }
21
+
22
+ set portfolio_category_ids(category_ids){
23
+ this._portfolio_category_ids = category_ids;
19
24
  }
20
25
 
21
26
  checkApiKey() {
@@ -26,21 +31,36 @@ export class OsunyOwl {
26
31
  return this._website_id;
27
32
  }
28
33
 
29
- get category_ids() {
30
- return this._category_ids;
34
+ get post_category_ids() {
35
+ return this._post_category_ids;
31
36
  }
32
37
 
33
- addCategory_id(category_id) {
34
- this.category_ids.push(category_id);
38
+ get portfolio_category_ids() {
39
+ return this._portfolio_category_ids;
40
+ }
41
+
42
+
43
+ addPostCategory_id(category_id) {
44
+ this.post_category_ids.push(category_id);
35
45
  }
36
46
 
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);
47
+ removePostCategory_id(category_id) {
48
+ if (this.post_category_ids.includes(category_id)) {
49
+ this.post_category_ids = this.post_category_ids.filter(e => e !== category_id);
40
50
  }
41
51
  }
42
52
 
43
- async postToOsuny(post) {
53
+ addPortfolioCategory_id(category_id) {
54
+ this.portfolio_category_ids.push(category_id);
55
+ }
56
+
57
+ removePortfolioCategory_id(category_id) {
58
+ if (this.portfolio_category_ids.includes(category_id)) {
59
+ this.portfolio_category_ids = this.portfolio_category_ids.filter(e => e !== category_id);
60
+ }
61
+ }
62
+
63
+ async postPostToOsuny(post) {
44
64
  if (this.api_key_defined) {
45
65
  const url = this.api_url + "/communication/websites/" + this.website_id + "/posts";
46
66
  await apiPost(url, post, process.env.OSUNY_API_KEY, false);
@@ -50,6 +70,16 @@ export class OsunyOwl {
50
70
  }
51
71
  }
52
72
 
73
+ async postProjectToOsuny(post) {
74
+ if (this.api_key_defined) {
75
+ const url = this.api_url + "/communication/websites/" + this.website_id + "/portfolio/projects";
76
+ await apiPost(url, post, process.env.OSUNY_API_KEY, false);
77
+ return true;
78
+ } else {
79
+ throw new Error("No API Key Defined")
80
+ }
81
+ }
82
+
53
83
  async importImage(img_bdy) {
54
84
  if (this.api_key_defined) {
55
85
  const url = this.api_url + "/communication/medias";
package/src/api/client.js CHANGED
@@ -17,9 +17,22 @@ export async function apiPost(url, data, apiKey, isFormData = false) {
17
17
  });
18
18
 
19
19
  if (!response.ok) {
20
- throw new Error(`Response status: ${response.status}`);
20
+ const errorBody = await response.text();
21
+ let errorDetails;
22
+ try {
23
+ errorDetails = JSON.parse(errorBody);
24
+ } catch {
25
+ errorDetails = errorBody;
26
+ }
27
+
28
+ console.error('\x1b[31m=== Erreur API ===\x1b[0m');
29
+ console.error('Status:', response.status, response.statusText);
30
+ console.error('Détails:', JSON.stringify(errorDetails, null, 2));
31
+
32
+ throw new Error(`Response status: ${response.status} - ${response.statusText}`);
21
33
  }
22
34
 
35
+
23
36
  return await response.json();
24
37
  } catch (error) {
25
38
  console.error(error.message);
@@ -1,4 +1,5 @@
1
1
  export const endpoints = {
2
2
  posts: (websiteId) => `/communication/websites/${websiteId}/posts`,
3
+ projects: (websiteId) => `/communication/websites/${websiteId}/portfolio/projects`,
3
4
  media: () => '/communication/medias',
4
5
  };
@@ -2,4 +2,5 @@ export { createTitle } from './title.js';
2
2
  export { createChapter } from './chapter.js';
3
3
  export { createDatatable } from './datatable.js';
4
4
  export { createVideo } from './video.js';
5
- export { createImage } from './image.js';
5
+ export { createImage } from './image.js';
6
+ export { createTimeline } from './timeline.js';
@@ -0,0 +1,30 @@
1
+ export function createTimeline(timeline_elements, migration_identifier, position, title = "", layout=1){
2
+ let layout_text;
3
+
4
+ switch (layout) {
5
+ case 1:
6
+ layout_text = "vertical";
7
+ break;
8
+ case 2:
9
+ layout_text = "horizontal";
10
+ break;
11
+ default:
12
+ layout_text = "vertical";
13
+ break;
14
+ }
15
+
16
+ return {
17
+ "id":null,
18
+ "migration_identifier": migration_identifier,
19
+ "template_kind": "timeline",
20
+ "title": title,
21
+ "position": position,
22
+ "published": true,
23
+ "html_class": null,
24
+ "data": {
25
+ "layout": layout_text,
26
+ "elements": timeline_elements
27
+ }
28
+
29
+ }
30
+ }
@@ -5,7 +5,7 @@ export function createVideo(video_url, migration_identifier, position, video_tit
5
5
  "template_kind": "video",
6
6
  "title": title,
7
7
  "position": position,
8
- "published": "true",
8
+ "published": true,
9
9
  "html_class": null,
10
10
  "data": {
11
11
  "layout": "player",
@@ -33,10 +33,10 @@ export function createProject(title, migration_identifier, year, blocks, locale
33
33
  "category_ids": category_ids,
34
34
  "localizations": {
35
35
  [locale]: {
36
- "id": undefined,
36
+ "id": null,
37
37
  "migration_identifier": migration_identifier + `_${locale}`,
38
38
  "title": title,
39
- "featured_image": undefined,
39
+ "featured_image": null,
40
40
  "pinned": false,
41
41
  "published": true,
42
42
  "published_at": crea_dt,
@@ -49,6 +49,66 @@ export function createProject(title, migration_identifier, year, blocks, locale
49
49
  };
50
50
  }
51
51
 
52
+ /**
53
+ * Ajoute une localisation à un post existant
54
+ *
55
+ * @param {Object} post - Le post existant (créé avec createPost)
56
+ * @param {string} locale - Code de la locale (ex: "en", "es", "de")
57
+ * @param {string} title - Titre dans la nouvelle locale
58
+ * @param {Array} blocks - Blocs dans la nouvelle locale
59
+ * @param {string} summary - Résumé dans la nouvelle locale (optionnel)
60
+ * @param {Date} crea_dt - Date de création (optionnel)
61
+ * @returns {Object} Le post modifié avec la nouvelle locale
62
+ */
63
+ export function addLocaleToPost(post, locale, title, blocks, summary = "", crea_dt = undefined) {
64
+ // Ajouter la nouvelle locale dans l'objet localizations
65
+ post.localizations[locale] = {
66
+ "id": undefined,
67
+ "migration_identifier": post.migration_identifier + `_${locale}`,
68
+ "title": title,
69
+ "featured_image": undefined,
70
+ "pinned": false,
71
+ "published": true,
72
+ "published_at": crea_dt,
73
+ "slug": slugify(title),
74
+ "summary": summary,
75
+ "blocks": blocks,
76
+ "created_at": crea_dt
77
+ };
78
+
79
+ return post;
80
+ }
81
+
82
+ /**
83
+ * Ajoute une localisation à un projet existant
84
+ *
85
+ * @param {Object} project - Le projet existant (créé avec createProject)
86
+ * @param {string} locale - Code de la locale (ex: "en", "es", "de")
87
+ * @param {string} title - Titre dans la nouvelle locale
88
+ * @param {Array} blocks - Blocs dans la nouvelle locale
89
+ * @param {string} summary - Résumé dans la nouvelle locale (optionnel)
90
+ * @param {Date} crea_dt - Date de création (optionnel)
91
+ * @returns {Object} Le projet modifié avec la nouvelle locale
92
+ */
93
+ export function addLocaleToProject(project, locale, title, blocks, summary = "", crea_dt = undefined) {
94
+ // Ajouter la nouvelle locale dans l'objet localizations
95
+ project.localizations[locale] = {
96
+ "id": undefined,
97
+ "migration_identifier": project.migration_identifier + `_${locale}`,
98
+ "title": title,
99
+ "featured_image": undefined,
100
+ "pinned": false,
101
+ "published": true,
102
+ "published_at": crea_dt,
103
+ "slug": slugify(title),
104
+ "summary": summary,
105
+ "blocks": blocks,
106
+ "created_at": crea_dt
107
+ };
108
+
109
+ return project;
110
+ }
111
+
52
112
  export function composePost(...osunyBlocks) {
53
113
  let blocksArray = [];
54
114
  osunyBlocks.forEach((el) => {
@@ -1,5 +1,11 @@
1
1
  import * as blocks from './blocks/index.js';
2
- import { createPost, createProject, composePost } from './content.js';
2
+ import {
3
+ createPost,
4
+ createProject,
5
+ composePost,
6
+ addLocaleToPost,
7
+ addLocaleToProject
8
+ } from './content.js';
3
9
  import { slugify } from '../utils/slug.js';
4
10
 
5
11
  export class OsunyUtility {
@@ -8,8 +14,11 @@ export class OsunyUtility {
8
14
  static createDatatable = blocks.createDatatable;
9
15
  static createVideo = blocks.createVideo;
10
16
  static createImage = blocks.createImage;
17
+ static createTimeline = blocks.createTimeline;
11
18
  static createPost = createPost;
12
19
  static createProject = createProject;
13
20
  static composePost = composePost;
21
+ static addLocaleToPost = addLocaleToPost;
22
+ static addLocaleToProject = addLocaleToProject;
14
23
  static slugify = slugify;
15
24
  }