osuny-owl 2.1.0 → 2.3.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.1.0",
3
+ "version": "2.3.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
@@ -1,10 +1,11 @@
1
1
  import 'dotenv/config';
2
- import { apiPost } from './api/client.js';
2
+ import { apiPost, apiPatch } from './api/client.js';
3
3
 
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;
36
+ }
37
+
38
+ get portfolio_category_ids() {
39
+ return this._portfolio_category_ids;
31
40
  }
32
41
 
33
- addCategory_id(category_id) {
34
- this.category_ids.push(category_id);
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,40 @@ 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
+
83
+ // PATCH d'un projet portfolio existant.
84
+ // `id` peut être l'id numérique OU le migration_identifier du projet.
85
+ // `project` doit contenir le migration_identifier existant (vérifié côté API).
86
+ async patchProjectToOsuny(id, project) {
87
+ if (this.api_key_defined) {
88
+ const url = this.api_url + "/communication/websites/" + this.website_id + "/portfolio/projects/" + id;
89
+ return await apiPatch(url, project, process.env.OSUNY_API_KEY, false);
90
+ } else {
91
+ throw new Error("No API Key Defined");
92
+ }
93
+ }
94
+
95
+ // PATCH d'une actualité (post) existante.
96
+ // `id` peut être l'id numérique OU le migration_identifier du post.
97
+ // `post` doit contenir le migration_identifier existant (vérifié côté API).
98
+ async patchPostToOsuny(id, post) {
99
+ if (this.api_key_defined) {
100
+ const url = this.api_url + "/communication/websites/" + this.website_id + "/posts/" + id;
101
+ return await apiPatch(url, post, process.env.OSUNY_API_KEY, false);
102
+ } else {
103
+ throw new Error("No API Key Defined");
104
+ }
105
+ }
106
+
53
107
  async importImage(img_bdy) {
54
108
  if (this.api_key_defined) {
55
109
  const url = this.api_url + "/communication/medias";
package/src/api/client.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import fetch from 'node-fetch';
2
2
 
3
- export async function apiPost(url, data, apiKey, isFormData = false) {
3
+ async function apiRequest(method, url, data, apiKey, isFormData = false) {
4
4
  const headers = {
5
5
  'X-Osuny-Token': apiKey
6
6
  };
@@ -11,13 +11,25 @@ export async function apiPost(url, data, apiKey, isFormData = false) {
11
11
 
12
12
  try {
13
13
  const response = await fetch(url, {
14
- method: 'POST',
14
+ method,
15
15
  headers,
16
16
  body: isFormData ? data : JSON.stringify(data)
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
 
23
35
  return await response.json();
@@ -25,4 +37,12 @@ export async function apiPost(url, data, apiKey, isFormData = false) {
25
37
  console.error(error.message);
26
38
  throw error;
27
39
  }
40
+ }
41
+
42
+ export async function apiPost(url, data, apiKey, isFormData = false) {
43
+ return apiRequest('POST', url, data, apiKey, isFormData);
44
+ }
45
+
46
+ export async function apiPatch(url, data, apiKey, isFormData = false) {
47
+ return apiRequest('PATCH', url, data, apiKey, isFormData);
28
48
  }
@@ -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,
@@ -14,6 +14,7 @@ export class OsunyUtility {
14
14
  static createDatatable = blocks.createDatatable;
15
15
  static createVideo = blocks.createVideo;
16
16
  static createImage = blocks.createImage;
17
+ static createTimeline = blocks.createTimeline;
17
18
  static createPost = createPost;
18
19
  static createProject = createProject;
19
20
  static composePost = composePost;