@lab-anssi/lib 1.1.0 → 1.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.
@@ -6,7 +6,9 @@ export type ArticleMarkdownCrisp = {
6
6
  export declare class AdaptateurCmsCrisp {
7
7
  readonly urlBase: string;
8
8
  readonly enteteCrisp: {
9
- [cle: string]: string;
9
+ headers: {
10
+ [cle: string]: string;
11
+ };
10
12
  };
11
13
  constructor(idSite: string, cleApi: string);
12
14
  recupereArticle: (idArticle: string) => Promise<ArticleMarkdownCrisp>;
@@ -1,22 +1,25 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.AdaptateurCmsCrisp = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
4
8
  class AdaptateurCmsCrisp {
5
9
  urlBase;
6
10
  enteteCrisp;
7
11
  constructor(idSite, cleApi) {
8
12
  this.urlBase = `https://api.crisp.chat/v1/website/${idSite}/`;
9
13
  this.enteteCrisp = {
10
- Authorization: `Basic ${btoa(cleApi)}`,
11
- 'X-Crisp-Tier': 'plugin',
14
+ headers: {
15
+ Authorization: `Basic ${btoa(cleApi)}`,
16
+ 'X-Crisp-Tier': 'plugin',
17
+ }
12
18
  };
13
19
  }
14
20
  recupereArticle = async (idArticle) => {
15
- const reponse = await fetch(`${this.urlBase}helpdesk/locale/fr/article/${idArticle}`, {
16
- method: 'GET',
17
- headers: this.enteteCrisp,
18
- });
19
- const donnees = await reponse.json();
21
+ const reponse = await axios_1.default.get(`${this.urlBase}helpdesk/locale/fr/article/${idArticle}`, this.enteteCrisp);
22
+ const donnees = reponse.data;
20
23
  return {
21
24
  contenuMarkdown: donnees.data.content,
22
25
  titre: donnees.data.title,
@@ -3,6 +3,7 @@ declare class CrispMarkdown {
3
3
  private contenuHTML;
4
4
  private aDejaParse;
5
5
  private tdm;
6
+ private marked;
6
7
  constructor(contenuMarkdown: string);
7
8
  parseLeMarkdown(): void;
8
9
  versHTML(): string | null;
@@ -30,6 +30,7 @@ class CrispMarkdown {
30
30
  contenuHTML = null;
31
31
  aDejaParse = false;
32
32
  tdm = [];
33
+ marked;
33
34
  constructor(contenuMarkdown) {
34
35
  this.contenuMarkdown = contenuMarkdown;
35
36
  const boiteAide = extensionBoite(/^\|([^|\n]+)/, 'boiteAide', 'aide');
@@ -59,6 +60,37 @@ class CrispMarkdown {
59
60
  return `<div class='conteneur-video'><video src='${token.text}' controls></video><p class='legende'>${token.legende}</p></div>`;
60
61
  },
61
62
  };
63
+ // Source d'inspiration pour la gestion des sections :
64
+ // https://github.com/markedjs/marked/discussions/2889
65
+ let niveauDeSection = 0;
66
+ const sectionRegexp = new RegExp(`^(# )[^]*?(?:\\n(?=\\1)|$)`);
67
+ const section = {
68
+ name: 'sectionBlock',
69
+ level: 'block',
70
+ start(source) {
71
+ return source.match(/^#/m)?.index;
72
+ },
73
+ tokenizer(source) {
74
+ if (niveauDeSection > 0)
75
+ return;
76
+ const match = source.match(sectionRegexp);
77
+ if (!match) {
78
+ return;
79
+ }
80
+ niveauDeSection++;
81
+ const tokens = this.lexer.blockTokens(match[0]);
82
+ niveauDeSection--;
83
+ return {
84
+ type: 'sectionBlock',
85
+ raw: match[0],
86
+ level: 1,
87
+ tokens,
88
+ };
89
+ },
90
+ renderer(token) {
91
+ return `<section>${this.parser.parse(token.tokens)}</section>`;
92
+ },
93
+ };
62
94
  const moteurDeRendu = (that) => ({
63
95
  heading(...[texte, profondeur]) {
64
96
  const slugDuTitre = texte.toLowerCase().replace(/\W+/g, '-');
@@ -76,13 +108,13 @@ class CrispMarkdown {
76
108
  return `<a href='${lien}' target='_blank' rel='nofollow'>${texte}</a>`;
77
109
  },
78
110
  });
79
- marked_1.marked.use({
111
+ this.marked = new marked_1.Marked({
80
112
  renderer: moteurDeRendu(this),
81
- extensions: [boiteAide, boiteInfo, boiteAlerte, video],
113
+ extensions: [boiteAide, boiteInfo, boiteAlerte, video, section],
82
114
  });
83
115
  }
84
116
  parseLeMarkdown() {
85
- this.contenuHTML = marked_1.marked.parse(this.contenuMarkdown);
117
+ this.contenuHTML = this.marked.parse(this.contenuMarkdown);
86
118
  this.aDejaParse = true;
87
119
  }
88
120
  versHTML() {
package/dist/index.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export { CmsCrisp, type PageHtmlCrisp } from './cms/cmsCrisp';
2
+ export { AdaptateurProfilAnssi } from './profilAnssi/adaptateurProfilAnssi';
package/dist/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CmsCrisp = void 0;
3
+ exports.AdaptateurProfilAnssi = exports.CmsCrisp = void 0;
4
4
  var cmsCrisp_1 = require("./cms/cmsCrisp");
5
5
  Object.defineProperty(exports, "CmsCrisp", { enumerable: true, get: function () { return cmsCrisp_1.CmsCrisp; } });
6
+ var adaptateurProfilAnssi_1 = require("./profilAnssi/adaptateurProfilAnssi");
7
+ Object.defineProperty(exports, "AdaptateurProfilAnssi", { enumerable: true, get: function () { return adaptateurProfilAnssi_1.AdaptateurProfilAnssi; } });
@@ -0,0 +1,29 @@
1
+ type Profil = {
2
+ nom: string;
3
+ prenom: string;
4
+ email: string;
5
+ entite: {
6
+ nom: string;
7
+ departement: string;
8
+ siret: string;
9
+ };
10
+ telephone?: string;
11
+ domainesSpecialite: string[];
12
+ };
13
+ type ProfilsAInscrire = {
14
+ dateInscription: Date;
15
+ donneesProfil: Profil;
16
+ }[];
17
+ export declare class AdaptateurProfilAnssi {
18
+ private urlBase;
19
+ readonly entete: {
20
+ headers: {
21
+ [cle: string]: string;
22
+ };
23
+ };
24
+ constructor(urlBase: string, cleApi: string);
25
+ metsAJour({ nom, prenom, email, entite, telephone, domainesSpecialite }: Profil): Promise<void>;
26
+ recupere(email: string): Promise<any>;
27
+ inscris(profils: ProfilsAInscrire): Promise<void>;
28
+ }
29
+ export {};
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.AdaptateurProfilAnssi = void 0;
7
+ const html_entities_1 = require("html-entities");
8
+ const serviceMiseEnForme_1 = require("../services/serviceMiseEnForme");
9
+ const axios_1 = __importDefault(require("axios"));
10
+ class AdaptateurProfilAnssi {
11
+ urlBase;
12
+ entete;
13
+ constructor(urlBase, cleApi) {
14
+ this.urlBase = urlBase;
15
+ this.entete = {
16
+ headers: {
17
+ Authorization: `Bearer ${cleApi}`
18
+ }
19
+ };
20
+ }
21
+ async metsAJour({ nom, prenom, email, entite, telephone, domainesSpecialite }) {
22
+ const urlProfil = `${this.urlBase}/profil/${email}`;
23
+ await axios_1.default.put(urlProfil, {
24
+ nom: (0, html_entities_1.decode)(nom),
25
+ prenom: (0, html_entities_1.decode)(prenom),
26
+ entite,
27
+ telephone,
28
+ domainesSpecialite,
29
+ }, this.entete);
30
+ }
31
+ ;
32
+ async recupere(email) {
33
+ const urlProfil = `${this.urlBase}/profil/${email}`;
34
+ try {
35
+ const reponse = await axios_1.default.get(urlProfil, this.entete);
36
+ return reponse.data;
37
+ }
38
+ catch (e) {
39
+ if (axios_1.default.isAxiosError(e) && e?.response?.status === 404)
40
+ return undefined;
41
+ throw e;
42
+ }
43
+ }
44
+ ;
45
+ async inscris(profils) {
46
+ const urlInscriptions = `${this.urlBase}/inscriptions`;
47
+ const profilsParTrancheDe500 = (0, serviceMiseEnForme_1.decoupeTableau)(profils, 500);
48
+ const promesses = profilsParTrancheDe500.map(profils => axios_1.default.post(urlInscriptions, profils, this.entete));
49
+ await Promise.all(promesses);
50
+ }
51
+ }
52
+ exports.AdaptateurProfilAnssi = AdaptateurProfilAnssi;
@@ -0,0 +1,2 @@
1
+ declare const decoupeTableau: <T>(tableau: T[], taille: number) => T[][];
2
+ export { decoupeTableau };
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decoupeTableau = void 0;
4
+ const decoupeTableau = (tableau, taille) => {
5
+ if (taille <= 0)
6
+ throw new RangeError();
7
+ const resultat = [];
8
+ for (let i = 0; i < tableau.length; i += taille) {
9
+ resultat.push(tableau.slice(i, i + taille));
10
+ }
11
+ return resultat;
12
+ };
13
+ exports.decoupeTableau = decoupeTableau;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lab-anssi/lib",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/betagouv/lab-anssi-lib.git"
@@ -18,7 +18,7 @@
18
18
  "dist"
19
19
  ],
20
20
  "types": "./dist/index.d.ts",
21
- "type": "module",
21
+ "type": "commonjs",
22
22
  "exports": {
23
23
  ".": {
24
24
  "types": "./dist/index.d.ts",
@@ -27,11 +27,13 @@
27
27
  },
28
28
  "devDependencies": {
29
29
  "@types/node": "^22.14.1",
30
+ "prettier": "^3.5.3",
30
31
  "tsx": "^4.19.3",
31
- "typescript": "^5.8.3",
32
- "prettier": "^3.5.3"
32
+ "typescript": "^5.8.3"
33
33
  },
34
34
  "dependencies": {
35
+ "axios": "^1.9.0",
36
+ "html-entities": "^2.6.0",
35
37
  "marked": "^13.0.3"
36
38
  }
37
39
  }