@webzolver/client 0.0.0-stage → 1.4.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.
- package/README.md +14 -2
- package/dist/chunk-WDRMR735.js +73 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +87 -0
- package/dist/seo.d.ts +104 -0
- package/dist/seo.js +10 -0
- package/package.json +36 -3
package/README.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @webzolver/client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Lee el contenido **publicado** de un sitio en el portal de WebZolver CMS (API pública, sin llaves).
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { createClient } from '@webzolver/client';
|
|
7
|
+
|
|
8
|
+
const cms = createClient({ site: 'mi-cliente', apiUrl: 'https://portal.webzolver.cl', schema: cmsSchema });
|
|
9
|
+
const portada = await cms.getSection('portada');
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
- `getSection`, `getList`, `getBySlug`, `getStaticPaths`, `getSeo`, `getRedirects` (una sola petición por build).
|
|
13
|
+
- `renderSeoTags`, `generateSitemapXml`, `generateCloudflareRedirects`.
|
|
14
|
+
|
|
15
|
+
En webs Astro, usa `@webzolver/astro`, que se apoya en este paquete.
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// src/seo.ts
|
|
2
|
+
function renderSeoTags(seo, defaults) {
|
|
3
|
+
const title = seo.title ? `${seo.title} | ${defaults.siteName}` : defaults.siteName;
|
|
4
|
+
const description = seo.description || "";
|
|
5
|
+
const canonicalUrl = `${defaults.baseUrl.replace(/\/$/, "")}${defaults.currentPath}`;
|
|
6
|
+
const image = seo.image?.url || "";
|
|
7
|
+
const type = defaults.type || "website";
|
|
8
|
+
const tags = [
|
|
9
|
+
`<title>${escapeHtml(title)}</title>`,
|
|
10
|
+
description ? `<meta name="description" content="${escapeAttr(description)}">` : "",
|
|
11
|
+
seo.noindex ? '<meta name="robots" content="noindex">' : "",
|
|
12
|
+
`<link rel="canonical" href="${escapeAttr(canonicalUrl)}">`,
|
|
13
|
+
"",
|
|
14
|
+
`<!-- OpenGraph / WhatsApp Preview -->`,
|
|
15
|
+
`<meta property="og:title" content="${escapeAttr(title)}">`,
|
|
16
|
+
description ? `<meta property="og:description" content="${escapeAttr(description)}">` : "",
|
|
17
|
+
`<meta property="og:url" content="${escapeAttr(canonicalUrl)}">`,
|
|
18
|
+
`<meta property="og:type" content="${escapeAttr(type)}">`,
|
|
19
|
+
`<meta property="og:site_name" content="${escapeAttr(defaults.siteName)}">`,
|
|
20
|
+
image ? `<meta property="og:image" content="${escapeAttr(image)}">` : "",
|
|
21
|
+
"",
|
|
22
|
+
`<!-- Twitter Card -->`,
|
|
23
|
+
`<meta name="twitter:card" content="${image ? "summary_large_image" : "summary"}">`,
|
|
24
|
+
`<meta name="twitter:title" content="${escapeAttr(title)}">`,
|
|
25
|
+
description ? `<meta name="twitter:description" content="${escapeAttr(description)}">` : "",
|
|
26
|
+
image ? `<meta name="twitter:image" content="${escapeAttr(image)}">` : ""
|
|
27
|
+
];
|
|
28
|
+
return tags.filter(Boolean).join("\n ");
|
|
29
|
+
}
|
|
30
|
+
function generateSitemapXml(baseUrl, staticPaths, collectionEntries) {
|
|
31
|
+
const cleanBase = baseUrl.replace(/\/$/, "");
|
|
32
|
+
const urls = [];
|
|
33
|
+
for (const path of staticPaths) {
|
|
34
|
+
const cleanPath = path.startsWith("/") ? path : `/${path}`;
|
|
35
|
+
urls.push(` <url>
|
|
36
|
+
<loc>${cleanBase}${cleanPath}</loc>
|
|
37
|
+
<changefreq>weekly</changefreq>
|
|
38
|
+
</url>`);
|
|
39
|
+
}
|
|
40
|
+
for (const entry of collectionEntries) {
|
|
41
|
+
const prefix = entry.routePrefix.startsWith("/") ? entry.routePrefix : `/${entry.routePrefix}`;
|
|
42
|
+
const cleanPrefix = prefix.endsWith("/") ? prefix : `${prefix}/`;
|
|
43
|
+
const loc = `${cleanBase}${cleanPrefix}${entry.slug}`;
|
|
44
|
+
const lastmod = entry.updatedAt ? `<lastmod>${entry.updatedAt.split("T")[0]}</lastmod>` : "";
|
|
45
|
+
urls.push(` <url>
|
|
46
|
+
<loc>${loc}</loc>
|
|
47
|
+
${lastmod}
|
|
48
|
+
<changefreq>daily</changefreq>
|
|
49
|
+
</url>`);
|
|
50
|
+
}
|
|
51
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
52
|
+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
53
|
+
${urls.join("\n")}
|
|
54
|
+
</urlset>`;
|
|
55
|
+
}
|
|
56
|
+
function generateCloudflareRedirects(options) {
|
|
57
|
+
const portal = (options.portalUrl || "https://portal.webzolver.cl").replace(/\/$/, "");
|
|
58
|
+
const lines = [`/admin ${portal}/s/${options.site} 302`];
|
|
59
|
+
for (const r of options.redirects) if (r.from !== r.to) lines.push(`${r.from} ${r.to} 301`);
|
|
60
|
+
return lines.join("\n") + "\n";
|
|
61
|
+
}
|
|
62
|
+
function escapeHtml(str) {
|
|
63
|
+
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
64
|
+
}
|
|
65
|
+
function escapeAttr(str) {
|
|
66
|
+
return escapeHtml(str);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export {
|
|
70
|
+
renderSeoTags,
|
|
71
|
+
generateSitemapXml,
|
|
72
|
+
generateCloudflareRedirects
|
|
73
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { C as ContentResponse, L as ListItem, a as ListSectionKeys, S as SectionFields, b as SeoMetadata, c as SingleSection, d as SingleSectionKeys, W as WebZolverClient, e as WebZolverClientConfig, f as WebZolverError, g as createClient, generateCloudflareRedirects, generateSitemapXml, renderSeoTags } from './seo.js';
|
|
2
|
+
import '@webzolver/schema';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {
|
|
2
|
+
generateCloudflareRedirects,
|
|
3
|
+
generateSitemapXml,
|
|
4
|
+
renderSeoTags
|
|
5
|
+
} from "./chunk-WDRMR735.js";
|
|
6
|
+
|
|
7
|
+
// src/client.ts
|
|
8
|
+
var WebZolverError = class extends Error {
|
|
9
|
+
constructor(message, status) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.status = status;
|
|
12
|
+
this.name = "WebZolverError";
|
|
13
|
+
}
|
|
14
|
+
status;
|
|
15
|
+
};
|
|
16
|
+
var WebZolverClient = class {
|
|
17
|
+
constructor(config) {
|
|
18
|
+
this.config = config;
|
|
19
|
+
if (!config.site) throw new Error("WebZolverClient: falta `site`");
|
|
20
|
+
this.base = `${config.apiUrl.replace(/\/$/, "")}/api/v1/sites/${encodeURIComponent(config.site)}`;
|
|
21
|
+
this.fetchImpl = config.fetchImpl ?? ((...a) => globalThis.fetch(...a));
|
|
22
|
+
}
|
|
23
|
+
config;
|
|
24
|
+
content = null;
|
|
25
|
+
base;
|
|
26
|
+
fetchImpl;
|
|
27
|
+
async request(path) {
|
|
28
|
+
const res = await this.fetchImpl(`${this.base}${path}`, { headers: { accept: "application/json" } });
|
|
29
|
+
if (!res.ok) {
|
|
30
|
+
let message = res.statusText;
|
|
31
|
+
try {
|
|
32
|
+
message = (await res.json()).error?.message ?? message;
|
|
33
|
+
} catch {
|
|
34
|
+
}
|
|
35
|
+
throw new WebZolverError(`WebZolver (${res.status}): ${message}`, res.status);
|
|
36
|
+
}
|
|
37
|
+
return await res.json();
|
|
38
|
+
}
|
|
39
|
+
/** Todo el contenido publicado en una sola petición (se reutiliza durante el build). Sin llave: es público. */
|
|
40
|
+
getContent() {
|
|
41
|
+
if (!this.content) {
|
|
42
|
+
this.content = this.request("/content");
|
|
43
|
+
this.content.catch(() => {
|
|
44
|
+
this.content = null;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return this.content;
|
|
48
|
+
}
|
|
49
|
+
async getSection(key) {
|
|
50
|
+
const s = (await this.getContent()).sections[key];
|
|
51
|
+
return s && !Array.isArray(s) ? s.data : null;
|
|
52
|
+
}
|
|
53
|
+
async getSectionWithSeo(key) {
|
|
54
|
+
const s = (await this.getContent()).sections[key];
|
|
55
|
+
return s && !Array.isArray(s) ? s : null;
|
|
56
|
+
}
|
|
57
|
+
async getList(key) {
|
|
58
|
+
const s = (await this.getContent()).sections[key];
|
|
59
|
+
return Array.isArray(s) ? s : [];
|
|
60
|
+
}
|
|
61
|
+
async getBySlug(key, slug) {
|
|
62
|
+
return (await this.getList(key)).find((i) => i.slug === slug) ?? null;
|
|
63
|
+
}
|
|
64
|
+
/** Para `getStaticPaths()` en `src/pages/productos/[slug].astro`. */
|
|
65
|
+
async getStaticPaths(key) {
|
|
66
|
+
return (await this.getList(key)).map((entry) => ({ params: { slug: entry.slug }, props: { entry } }));
|
|
67
|
+
}
|
|
68
|
+
async getSeo(key, slug) {
|
|
69
|
+
const s = (await this.getContent()).sections[key];
|
|
70
|
+
if (Array.isArray(s)) return s.find((i) => i.slug === slug)?.seo ?? {};
|
|
71
|
+
return s?.seo ?? {};
|
|
72
|
+
}
|
|
73
|
+
async getRedirects() {
|
|
74
|
+
return (await this.request("/redirects")).redirects;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
function createClient(config) {
|
|
78
|
+
return new WebZolverClient(config);
|
|
79
|
+
}
|
|
80
|
+
export {
|
|
81
|
+
WebZolverClient,
|
|
82
|
+
WebZolverError,
|
|
83
|
+
createClient,
|
|
84
|
+
generateCloudflareRedirects,
|
|
85
|
+
generateSitemapXml,
|
|
86
|
+
renderSeoTags
|
|
87
|
+
};
|
package/dist/seo.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { SiteSchemaDefinition, InferSectionFields } from '@webzolver/schema';
|
|
2
|
+
|
|
3
|
+
type SingleSectionKeys<T extends SiteSchemaDefinition> = {
|
|
4
|
+
[K in keyof T['sections']]: T['sections'][K]['mode'] extends 'single' ? K : never;
|
|
5
|
+
}[keyof T['sections']] & string;
|
|
6
|
+
type ListSectionKeys<T extends SiteSchemaDefinition> = {
|
|
7
|
+
[K in keyof T['sections']]: T['sections'][K]['mode'] extends 'list' ? K : never;
|
|
8
|
+
}[keyof T['sections']] & string;
|
|
9
|
+
type SectionFields<T extends SiteSchemaDefinition, K extends keyof T['sections']> = InferSectionFields<T['sections'][K]['fields']>;
|
|
10
|
+
interface SeoMetadata {
|
|
11
|
+
title?: string;
|
|
12
|
+
description?: string;
|
|
13
|
+
image?: {
|
|
14
|
+
url: string;
|
|
15
|
+
alt?: string;
|
|
16
|
+
};
|
|
17
|
+
noindex?: boolean;
|
|
18
|
+
}
|
|
19
|
+
interface ListItem<D> {
|
|
20
|
+
id: string;
|
|
21
|
+
slug: string;
|
|
22
|
+
sortOrder: number;
|
|
23
|
+
data: D;
|
|
24
|
+
seo: SeoMetadata;
|
|
25
|
+
publishedAt: string | null;
|
|
26
|
+
}
|
|
27
|
+
interface SingleSection<D> {
|
|
28
|
+
data: D;
|
|
29
|
+
seo: SeoMetadata;
|
|
30
|
+
updatedAt: string | null;
|
|
31
|
+
}
|
|
32
|
+
interface ContentResponse {
|
|
33
|
+
site: {
|
|
34
|
+
slug: string;
|
|
35
|
+
name: string;
|
|
36
|
+
};
|
|
37
|
+
contentVersion?: number;
|
|
38
|
+
publishedAt?: string | null;
|
|
39
|
+
sections: Record<string, SingleSection<Record<string, unknown>> | ListItem<Record<string, unknown>>[] | null>;
|
|
40
|
+
}
|
|
41
|
+
interface WebZolverClientConfig<T extends SiteSchemaDefinition = SiteSchemaDefinition> {
|
|
42
|
+
/** Slug del sitio en el portal (ej. `cye-clima`). */
|
|
43
|
+
site: string;
|
|
44
|
+
/** URL del portal (ej. `https://portal.webzolver.cl`). */
|
|
45
|
+
apiUrl: string;
|
|
46
|
+
schema?: T;
|
|
47
|
+
fetchImpl?: typeof fetch;
|
|
48
|
+
}
|
|
49
|
+
declare class WebZolverError extends Error {
|
|
50
|
+
readonly status: number;
|
|
51
|
+
constructor(message: string, status: number);
|
|
52
|
+
}
|
|
53
|
+
declare class WebZolverClient<T extends SiteSchemaDefinition = SiteSchemaDefinition> {
|
|
54
|
+
private readonly config;
|
|
55
|
+
private content;
|
|
56
|
+
private readonly base;
|
|
57
|
+
private readonly fetchImpl;
|
|
58
|
+
constructor(config: WebZolverClientConfig<T>);
|
|
59
|
+
private request;
|
|
60
|
+
/** Todo el contenido publicado en una sola petición (se reutiliza durante el build). Sin llave: es público. */
|
|
61
|
+
getContent(): Promise<ContentResponse>;
|
|
62
|
+
getSection<K extends SingleSectionKeys<T>>(key: K): Promise<SectionFields<T, K> | null>;
|
|
63
|
+
getSectionWithSeo<K extends SingleSectionKeys<T>>(key: K): Promise<SingleSection<SectionFields<T, K>> | null>;
|
|
64
|
+
getList<K extends ListSectionKeys<T>>(key: K): Promise<Array<ListItem<SectionFields<T, K>>>>;
|
|
65
|
+
getBySlug<K extends ListSectionKeys<T>>(key: K, slug: string): Promise<ListItem<SectionFields<T, K>> | null>;
|
|
66
|
+
/** Para `getStaticPaths()` en `src/pages/productos/[slug].astro`. */
|
|
67
|
+
getStaticPaths<K extends ListSectionKeys<T>>(key: K): Promise<{
|
|
68
|
+
params: {
|
|
69
|
+
slug: string;
|
|
70
|
+
};
|
|
71
|
+
props: {
|
|
72
|
+
entry: ListItem<InferSectionFields<T["sections"][K]["fields"]>>;
|
|
73
|
+
};
|
|
74
|
+
}[]>;
|
|
75
|
+
getSeo(key: keyof T['sections'] & string, slug?: string): Promise<SeoMetadata>;
|
|
76
|
+
getRedirects(): Promise<Array<{
|
|
77
|
+
from: string;
|
|
78
|
+
to: string;
|
|
79
|
+
}>>;
|
|
80
|
+
}
|
|
81
|
+
declare function createClient<T extends SiteSchemaDefinition>(config: WebZolverClientConfig<T>): WebZolverClient<T>;
|
|
82
|
+
|
|
83
|
+
declare function renderSeoTags(seo: SeoMetadata, defaults: {
|
|
84
|
+
siteName: string;
|
|
85
|
+
baseUrl: string;
|
|
86
|
+
currentPath: string;
|
|
87
|
+
type?: 'website' | 'article';
|
|
88
|
+
}): string;
|
|
89
|
+
declare function generateSitemapXml(baseUrl: string, staticPaths: string[], collectionEntries: Array<{
|
|
90
|
+
routePrefix: string;
|
|
91
|
+
slug: string;
|
|
92
|
+
updatedAt?: string;
|
|
93
|
+
}>): string;
|
|
94
|
+
/** Contenido para `public/_redirects` (Cloudflare Pages / Netlify). */
|
|
95
|
+
declare function generateCloudflareRedirects(options: {
|
|
96
|
+
site: string;
|
|
97
|
+
portalUrl?: string;
|
|
98
|
+
redirects: Array<{
|
|
99
|
+
from: string;
|
|
100
|
+
to: string;
|
|
101
|
+
}>;
|
|
102
|
+
}): string;
|
|
103
|
+
|
|
104
|
+
export { type ContentResponse as C, type ListItem as L, type SectionFields as S, WebZolverClient as W, type ListSectionKeys as a, type SeoMetadata as b, type SingleSection as c, type SingleSectionKeys as d, type WebZolverClientConfig as e, WebZolverError as f, createClient as g, generateCloudflareRedirects, generateSitemapXml, renderSeoTags };
|
package/dist/seo.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webzolver/client",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.4.0",
|
|
4
|
+
"description": "Lee el contenido publicado en WebZolver CMS desde una web (SEO, sitemap y redirecciones).",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./seo": {
|
|
15
|
+
"types": "./dist/seo.d.ts",
|
|
16
|
+
"import": "./dist/seo.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@webzolver/schema": "^1.4.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"typescript": "^5.7.2"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"README.md"
|
|
28
|
+
],
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=20"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"typecheck": "tsc --noEmit",
|
|
37
|
+
"build": "tsup src/index.ts src/seo.ts --format esm --dts --clean --out-dir dist --external @webzolver/schema"
|
|
38
|
+
}
|
|
6
39
|
}
|