@regojs/erp-sdk 1.1.4 → 1.1.6

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.6",
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,147 @@ 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
+ return;
189
+ }
190
+
191
+ case "description": {
192
+ this.#metadataElements
193
+ .get(key)
194
+ ?.setAttribute("content", value?.trim() || "");
195
+ return;
196
+ }
197
+
198
+ case "keywords": {
199
+ this.#metadataElements
200
+ .get(key)
201
+ ?.setAttribute("content", value?.trim() || "");
202
+ return;
203
+ }
204
+
205
+ case "canonical": {
206
+ this.#metadataElements
207
+ .get(key)
208
+ ?.setAttribute("href", value?.trim() || "");
209
+ return;
210
+ }
211
+
212
+ case "ogType": {
213
+ this.#metadataElements
214
+ .get(key)
215
+ ?.setAttribute("content", value?.trim() || "");
216
+ return;
217
+ }
218
+
219
+ case "ogDescription": {
220
+ this.#metadataElements
221
+ .get(key)
222
+ ?.setAttribute("content", value?.trim() || "");
223
+ return;
224
+ }
225
+
226
+ case "ogImage": {
227
+ this.#metadataElements
228
+ .get(key)
229
+ ?.setAttribute("content", value?.trim() || "");
230
+ return;
231
+ }
232
+
233
+ case "ogUrl": {
234
+ this.#metadataElements
235
+ .get(key)
236
+ ?.setAttribute("content", value?.trim() || "");
237
+ return;
238
+ }
239
+ }
240
+ }
241
+
242
+ inferMetadata(payload: Parameters<IRegoProvider["inferMetadata"]>[number]) {
243
+ for (const [key, value] of Object.entries(payload)) {
244
+ switch (key) {
245
+ case "title": {
246
+ this.setMetadataToElement("title", value);
247
+ this.setMetadataToElement("ogTitle", value);
248
+ break;
249
+ }
250
+ case "description": {
251
+ this.setMetadataToElement("description", value);
252
+ this.setMetadataToElement("ogDescription", value);
253
+ break;
254
+ }
255
+ case "image": {
256
+ this.setMetadataToElement("ogImage", value);
257
+ break;
258
+ }
259
+ case "canonical": {
260
+ this.setMetadataToElement("canonical", value);
261
+ break;
262
+ }
263
+ case "url": {
264
+ this.setMetadataToElement("ogUrl", value);
265
+ break;
266
+ }
267
+ case "type": {
268
+ this.setMetadataToElement("ogType", value);
269
+ break;
270
+ }
271
+ }
272
+ }
273
+ }
274
+
132
275
  async fetchRoute({
133
276
  cache,
134
277
  ...payload
@@ -146,6 +289,10 @@ export class RegoProvider implements IRegoProvider {
146
289
  staticData = this.#staticDatas.get(uri);
147
290
 
148
291
  if (data && metadata && staticData) {
292
+ if (metadata) {
293
+ this.inferMetadata(metadata);
294
+ }
295
+
149
296
  return Object.freeze({
150
297
  data,
151
298
  metadata,
@@ -180,6 +327,10 @@ export class RegoProvider implements IRegoProvider {
180
327
  this.#metadatas.set(uri, validation.data.metadata);
181
328
  this.#staticDatas.set(uri, validation.data.staticData);
182
329
 
330
+ if (validation.data.metadata) {
331
+ this.inferMetadata(validation.data.metadata);
332
+ }
333
+
183
334
  return Object.freeze(validation.data);
184
335
  } catch (error) {
185
336
  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
  }