@communecter/cocolight-api-client 1.0.66 → 1.0.68
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/dist/cocolight-api-client.browser.js +3 -3
- package/dist/cocolight-api-client.cjs +1 -1
- package/dist/cocolight-api-client.mjs.js +1 -1
- package/dist/cocolight-api-client.vite.mjs.js +1 -1
- package/dist/cocolight-api-client.vite.mjs.js.map +1 -1
- package/package.json +1 -1
- package/src/api/News.ts +59 -1
- package/types/api/News.d.ts +19 -0
package/package.json
CHANGED
package/src/api/News.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { ApiError, ApiResponseError } from "../error.js";
|
|
|
2
2
|
import { BaseEntity } from "./BaseEntity.js";
|
|
3
3
|
|
|
4
4
|
import type { Comment } from "./Comment.js";
|
|
5
|
-
import type { AddNewsData, UpdateNewsData, DeleteNewsData, AddImageNewsData, AddFileNewsData, GetCommentsData, AddVoteData, AddReportAbuseData } from "./EndpointApi.types.js";
|
|
5
|
+
import type { AddNewsData, UpdateNewsData, DeleteNewsData, AddImageNewsData, AddFileNewsData, GetCommentsData, AddVoteData, AddReportAbuseData, ShareNewsData } from "./EndpointApi.types.js";
|
|
6
6
|
import type { NewsItemNormalized } from "./serverDataType/News.js";
|
|
7
7
|
|
|
8
8
|
export class News extends BaseEntity<NewsItemNormalized> {
|
|
@@ -377,4 +377,62 @@ export class News extends BaseEntity<NewsItemNormalized> {
|
|
|
377
377
|
return await this.callIsConnected(() =>this.endpointApi.addReportAbuse(payload));
|
|
378
378
|
}
|
|
379
379
|
|
|
380
|
+
/**
|
|
381
|
+
* Partage cette news vers une entité (utilisateur, projet ou organisation)
|
|
382
|
+
*
|
|
383
|
+
* @param params - Paramètres de partage
|
|
384
|
+
* @param params.childId - ID de l'entité cible (par défaut: utilisateur connecté)
|
|
385
|
+
* @param params.childType - Type de l'entité cible (par défaut: "citoyens")
|
|
386
|
+
* @param params.comment - Commentaire optionnel accompagnant le partage
|
|
387
|
+
* @returns Promise contenant la réponse de l'API après le partage
|
|
388
|
+
* @throws {ApiError} Si la news n'a pas d'ID (404)
|
|
389
|
+
* @throws {ApiError} Si l'utilisateur n'est pas connecté (400)
|
|
390
|
+
* @throws {ApiError} Si le parent n'est pas défini (400)
|
|
391
|
+
* @throws {ApiError} Si on tente de partager sa propre news à soi-même (400)
|
|
392
|
+
* @throws {ApiError} Si le type cible n'est pas valide (400)
|
|
393
|
+
*/
|
|
394
|
+
async shareNews({ childId, childType, comment }: { childId?: string, childType?: string, comment?: string } = {}){
|
|
395
|
+
// Validations préalables
|
|
396
|
+
if (!this.id) {
|
|
397
|
+
throw new ApiError(`${this.constructor.name} non enregistrée.`, 404);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
if (!this.userId) {
|
|
401
|
+
throw new ApiError("L'utilisateur connecté n'est pas défini.", 400);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
if(!this.parent?.id){
|
|
405
|
+
throw new ApiError("Le parent doit être défini pour partager une news", 400);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// Déterminer la cible du partage (par défaut: l'utilisateur connecté)
|
|
409
|
+
const targetId = childId ?? this.userId;
|
|
410
|
+
const targetType = childType ?? "citoyens";
|
|
411
|
+
|
|
412
|
+
// Valider que le type est autorisé
|
|
413
|
+
const validChildTypes: Array<"citoyens" | "projects" | "organizations"> = ["citoyens", "projects", "organizations"];
|
|
414
|
+
if (!validChildTypes.includes(targetType as any)) {
|
|
415
|
+
throw new ApiError(
|
|
416
|
+
`Le type de cible "${targetType}" n'est pas valide. Types autorisés: ${validChildTypes.join(", ")}`,
|
|
417
|
+
400
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// Empêcher l'auto-partage: on ne peut pas partager sa propre news à soi-même
|
|
422
|
+
if (this.parent.id === this.userId && targetId === this.userId) {
|
|
423
|
+
throw new ApiError("Vous ne pouvez pas partager votre propre news à vous-même", 400);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const payload: ShareNewsData = {
|
|
427
|
+
parentId: this.id,
|
|
428
|
+
parentType: "news",
|
|
429
|
+
connectType: "share",
|
|
430
|
+
childType: targetType as "citoyens" | "projects" | "organizations",
|
|
431
|
+
childId: targetId,
|
|
432
|
+
comment
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
return await this.callIsConnected(() => this.endpointApi.shareNews(payload));
|
|
436
|
+
}
|
|
437
|
+
|
|
380
438
|
}
|
package/types/api/News.d.ts
CHANGED
|
@@ -95,4 +95,23 @@ export declare class News extends BaseEntity<NewsItemNormalized> {
|
|
|
95
95
|
reason: string;
|
|
96
96
|
comment?: string;
|
|
97
97
|
}): Promise<unknown>;
|
|
98
|
+
/**
|
|
99
|
+
* Partage cette news vers une entité (utilisateur, projet ou organisation)
|
|
100
|
+
*
|
|
101
|
+
* @param params - Paramètres de partage
|
|
102
|
+
* @param params.childId - ID de l'entité cible (par défaut: utilisateur connecté)
|
|
103
|
+
* @param params.childType - Type de l'entité cible (par défaut: "citoyens")
|
|
104
|
+
* @param params.comment - Commentaire optionnel accompagnant le partage
|
|
105
|
+
* @returns Promise contenant la réponse de l'API après le partage
|
|
106
|
+
* @throws {ApiError} Si la news n'a pas d'ID (404)
|
|
107
|
+
* @throws {ApiError} Si l'utilisateur n'est pas connecté (400)
|
|
108
|
+
* @throws {ApiError} Si le parent n'est pas défini (400)
|
|
109
|
+
* @throws {ApiError} Si on tente de partager sa propre news à soi-même (400)
|
|
110
|
+
* @throws {ApiError} Si le type cible n'est pas valide (400)
|
|
111
|
+
*/
|
|
112
|
+
shareNews({ childId, childType, comment }?: {
|
|
113
|
+
childId?: string;
|
|
114
|
+
childType?: string;
|
|
115
|
+
comment?: string;
|
|
116
|
+
}): Promise<unknown>;
|
|
98
117
|
}
|