@regojs/erp-sdk 1.1.4 → 1.1.5

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
@@ -44,7 +44,7 @@
44
44
  "test": "bun --hot run __test/index.ts"
45
45
  },
46
46
  "types": "./dist/index.d.ts",
47
- "version": "1.1.4",
47
+ "version": "1.1.5",
48
48
  "exports": {
49
49
  ".": {
50
50
  "import": "./dist/index.js",
@@ -1,4 +1,4 @@
1
- import type { IRegoProvider } from "../types";
1
+ import type { IRegoProvider, TMedataKeys } from "../types";
2
2
 
3
3
  import { Enums } from "../constants";
4
4
  import {
@@ -14,8 +14,10 @@ export class RegoProvider implements IRegoProvider {
14
14
  #staticDatas: Map<string, IRegoProvider["staticData"]> = new Map();
15
15
  #datas: Map<string, IRegoProvider["data"]> = new Map();
16
16
  #metadatas: Map<string, IRegoProvider["metadata"]> = new Map();
17
+ #metadataElements: Map<TMedataKeys, Element> = new Map();
17
18
 
18
19
  constructor(private readonly window: Window) {
20
+ this.#findMetadataElements();
19
21
  this.#parseRoutes();
20
22
  this.#parseMetadata();
21
23
  this.#parseStaticData();
@@ -129,6 +131,148 @@ export class RegoProvider implements IRegoProvider {
129
131
  }
130
132
  }
131
133
 
134
+ #findMetadataElements() {
135
+ const titleElement = this.window.document.head.querySelector("title"),
136
+ descriptionElement = this.window.document.head.querySelector(
137
+ 'meta[name="description"]'
138
+ ),
139
+ keywordsElement = this.window.document.head.querySelector(
140
+ 'meta[name="keywords"]'
141
+ ),
142
+ canonicalElement = this.window.document.head.querySelector(
143
+ 'link[rel="canonical"]'
144
+ ),
145
+ ogTypeElement = this.window.document.head.querySelector(
146
+ 'meta[property="og:type"]'
147
+ ),
148
+ ogTitleElement = this.window.document.head.querySelector(
149
+ 'meta[property="og:title"]'
150
+ ),
151
+ ogDescriptionElement = this.window.document.head.querySelector(
152
+ 'meta[property="og:description"]'
153
+ ),
154
+ ogImageElement = this.window.document.head.querySelector(
155
+ 'meta[property="og:image"]'
156
+ ),
157
+ ogUrlElement = this.window.document.head.querySelector(
158
+ 'meta[property="og:url"]'
159
+ );
160
+
161
+ if (titleElement) this.#metadataElements.set("title", titleElement);
162
+ if (descriptionElement)
163
+ this.#metadataElements.set("description", descriptionElement);
164
+ if (keywordsElement)
165
+ this.#metadataElements.set("keywords", keywordsElement);
166
+ if (canonicalElement)
167
+ this.#metadataElements.set("canonical", canonicalElement);
168
+ if (ogTypeElement) this.#metadataElements.set("ogType", ogTypeElement);
169
+ if (ogTitleElement)
170
+ this.#metadataElements.set("ogTitle", ogTitleElement);
171
+ if (ogDescriptionElement)
172
+ this.#metadataElements.set("ogDescription", ogDescriptionElement);
173
+ if (ogImageElement)
174
+ this.#metadataElements.set("ogImage", ogImageElement);
175
+ if (ogUrlElement) this.#metadataElements.set("ogUrl", ogUrlElement);
176
+ }
177
+
178
+ setMetadataToElement(
179
+ ...[key, value]: Parameters<IRegoProvider["setMetadataToElement"]>
180
+ ) {
181
+ switch (key) {
182
+ case "title": {
183
+ const titleElement = this.#metadataElements.get(key);
184
+
185
+ (titleElement &&
186
+ "innerText" in titleElement &&
187
+ (titleElement.innerText = value?.trim())) ||
188
+ "";
189
+ return;
190
+ }
191
+
192
+ case "description": {
193
+ this.#metadataElements
194
+ .get(key)
195
+ ?.setAttribute("content", value?.trim() || "");
196
+ return;
197
+ }
198
+
199
+ case "keywords": {
200
+ this.#metadataElements
201
+ .get(key)
202
+ ?.setAttribute("content", value?.trim() || "");
203
+ return;
204
+ }
205
+
206
+ case "canonical": {
207
+ this.#metadataElements
208
+ .get(key)
209
+ ?.setAttribute("href", value?.trim() || "");
210
+ return;
211
+ }
212
+
213
+ case "ogType": {
214
+ this.#metadataElements
215
+ .get(key)
216
+ ?.setAttribute("content", value?.trim() || "");
217
+ return;
218
+ }
219
+
220
+ case "ogDescription": {
221
+ this.#metadataElements
222
+ .get(key)
223
+ ?.setAttribute("content", value?.trim() || "");
224
+ return;
225
+ }
226
+
227
+ case "ogImage": {
228
+ this.#metadataElements
229
+ .get(key)
230
+ ?.setAttribute("content", value?.trim() || "");
231
+ return;
232
+ }
233
+
234
+ case "ogUrl": {
235
+ this.#metadataElements
236
+ .get(key)
237
+ ?.setAttribute("content", value?.trim() || "");
238
+ return;
239
+ }
240
+ }
241
+ }
242
+
243
+ inferMetadata(payload: Parameters<IRegoProvider["inferMetadata"]>[number]) {
244
+ for (const [key, value] of Object.entries(payload)) {
245
+ switch (key) {
246
+ case "title": {
247
+ this.setMetadataToElement("title", value);
248
+ this.setMetadataToElement("ogTitle", value);
249
+ break;
250
+ }
251
+ case "description": {
252
+ this.setMetadataToElement("description", value);
253
+ this.setMetadataToElement("ogDescription", value);
254
+ break;
255
+ }
256
+ case "image": {
257
+ this.setMetadataToElement("ogImage", value);
258
+ break;
259
+ }
260
+ case "canonical": {
261
+ this.setMetadataToElement("canonical", value);
262
+ break;
263
+ }
264
+ case "url": {
265
+ this.setMetadataToElement("ogUrl", value);
266
+ break;
267
+ }
268
+ case "type": {
269
+ this.setMetadataToElement("ogType", value);
270
+ break;
271
+ }
272
+ }
273
+ }
274
+ }
275
+
132
276
  async fetchRoute({
133
277
  cache,
134
278
  ...payload
@@ -146,6 +290,10 @@ export class RegoProvider implements IRegoProvider {
146
290
  staticData = this.#staticDatas.get(uri);
147
291
 
148
292
  if (data && metadata && staticData) {
293
+ if (metadata) {
294
+ this.inferMetadata(metadata);
295
+ }
296
+
149
297
  return Object.freeze({
150
298
  data,
151
299
  metadata,
@@ -180,6 +328,10 @@ export class RegoProvider implements IRegoProvider {
180
328
  this.#metadatas.set(uri, validation.data.metadata);
181
329
  this.#staticDatas.set(uri, validation.data.staticData);
182
330
 
331
+ if (validation.data.metadata) {
332
+ this.inferMetadata(validation.data.metadata);
333
+ }
334
+
183
335
  return Object.freeze(validation.data);
184
336
  } catch (error) {
185
337
  console.error("Error on fetch full route data:", error);
@@ -6,6 +6,17 @@ import type {
6
6
  TStaticData
7
7
  } from "../validators";
8
8
 
9
+ export type TMedataKeys =
10
+ | "title"
11
+ | "description"
12
+ | "keywords"
13
+ | "canonical"
14
+ | "ogType"
15
+ | "ogTitle"
16
+ | "ogDescription"
17
+ | "ogImage"
18
+ | "ogUrl";
19
+
9
20
  export interface IRegoProvider {
10
21
  routes: Array<TRoute> | undefined;
11
22
  staticData: TStaticData;
@@ -25,4 +36,9 @@ export interface IRegoProvider {
25
36
  cache?: boolean;
26
37
  }
27
38
  ): Promise<Readonly<TFullData> | undefined>;
39
+ setMetadataToElement(
40
+ key: TMedataKeys,
41
+ value: string | null | undefined
42
+ ): void;
43
+ inferMetadata(payload: Record<string, string | null | undefined>): void;
28
44
  }