@saasmakers/ui 1.4.38 → 1.4.40

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.
@@ -0,0 +1,55 @@
1
+ import { appendHeader, getHeader, getQuery, setHeader } from "h3";
2
+ import { parseQuery } from "ufo";
3
+
4
+ export function safeGetQuery(event) {
5
+ try {
6
+ return getQuery(event);
7
+ }
8
+ catch {
9
+ const path = event.path || event.node?.req?.url || "/";
10
+ const searchIndex = path.indexOf("?");
11
+ const search = searchIndex === -1 ? "" : path.slice(searchIndex);
12
+ return parseQuery(search);
13
+ }
14
+ }
15
+
16
+ function useNodeResponse(event) {
17
+ return !event.res?.headers && event.node?.res;
18
+ }
19
+
20
+ export function safeGetHeader(event, name) {
21
+ if (typeof event.req?.headers?.get === "function") {
22
+ return getHeader(event, name);
23
+ }
24
+ const nodeHeaders = event.node?.req?.headers;
25
+ if (nodeHeaders) {
26
+ const key = name.toLowerCase();
27
+ const value = nodeHeaders[key] ?? nodeHeaders[name];
28
+ return Array.isArray(value) ? value[0] : value;
29
+ }
30
+ return void 0;
31
+ }
32
+
33
+ export function safeSetHeader(event, name, value) {
34
+ if (useNodeResponse(event)) {
35
+ event.node.res.setHeader(name, value);
36
+ return;
37
+ }
38
+ setHeader(event, name, value);
39
+ }
40
+
41
+ export function safeAppendHeader(event, name, value) {
42
+ if (useNodeResponse(event)) {
43
+ const res = event.node.res;
44
+ const current = res.getHeader(name);
45
+ if (current === void 0) {
46
+ res.setHeader(name, value);
47
+ } else if (Array.isArray(current)) {
48
+ res.setHeader(name, [...current, value]);
49
+ } else {
50
+ res.setHeader(name, [current, value]);
51
+ }
52
+ return;
53
+ }
54
+ appendHeader(event, name, value);
55
+ }
@@ -0,0 +1,35 @@
1
+ import { defineEventHandler, getQuery, setHeader } from "h3";
2
+ import { parseQuery } from "ufo";
3
+ import { getPathRobotConfig } from "../composables/getPathRobotConfig.js";
4
+ import { useRuntimeConfigNuxtRobots } from "../composables/useRuntimeConfigNuxtRobots.js";
5
+
6
+ function safeGetQuery(event) {
7
+ try {
8
+ return getQuery(event);
9
+ }
10
+ catch {
11
+ const path = event.path || event.node?.req?.url || "/";
12
+ const searchIndex = path.indexOf("?");
13
+ const search = searchIndex === -1 ? "" : path.slice(searchIndex);
14
+ return parseQuery(search);
15
+ }
16
+ }
17
+
18
+ export default defineEventHandler(async (e) => {
19
+ if (e.path === "/robots.txt" || e.path.startsWith("/__") || e.path.startsWith("/api") || e.path.startsWith("/_nuxt"))
20
+ return;
21
+ const nuxtRobotsConfig = useRuntimeConfigNuxtRobots(e);
22
+ if (nuxtRobotsConfig) {
23
+ const { header } = nuxtRobotsConfig;
24
+ const robotConfig = getPathRobotConfig(e, { skipSiteIndexable: Boolean(safeGetQuery(e)?.mockProductionEnv) });
25
+ if (header) {
26
+ setHeader(e, "X-Robots-Tag", robotConfig.rule);
27
+ }
28
+ e.context.robots = robotConfig;
29
+ if (import.meta.dev) {
30
+ const productionRobotConfig = getPathRobotConfig(e, { skipSiteIndexable: true });
31
+ setHeader(e, "X-Robots-Production", productionRobotConfig.rule);
32
+ e.context.robotsProduction = productionRobotConfig;
33
+ }
34
+ }
35
+ });