@forgenex/cli 0.2.1 → 0.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/GUIA-IA.md +87 -2
- package/README.md +3 -1
- package/REFERENCIA.md +13 -2
- package/dist/forgenex.js +24 -16
- package/ejemplos/archivos-ficha/extension.json +18 -0
- package/ejemplos/archivos-ficha/interfaz/index.html +100 -0
- package/extension.schema.json +17 -0
- package/licencias.txt +1 -1
- package/package.json +1 -1
- package/puente-extension.d.ts +37 -0
- package/puente-extension.mjs +15 -2
- package/solucion.schema.json +1 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"formato": "forgenex.extension",
|
|
3
|
+
"formato_version": 1,
|
|
4
|
+
"id": "com.ejemplo.archivos-ficha",
|
|
5
|
+
"nombre": "Archivo a la ficha",
|
|
6
|
+
"version": "1.0.0",
|
|
7
|
+
"autor": { "nombre": "Ejemplo para desarrolladores" },
|
|
8
|
+
"descripcion": "Ejemplo de archivos (1.17.0): eliges un archivo del almacenamiento con el explorador del CRM, la app lo enseña y lo vincula a la ficha. Sin tokens de OneDrive ni de Google.",
|
|
9
|
+
"icono": "paperclip",
|
|
10
|
+
"permisos": {
|
|
11
|
+
"modulos": { "contactos": ["ver", "editar"] },
|
|
12
|
+
"archivos": ["leer", "escribir"]
|
|
13
|
+
},
|
|
14
|
+
"montajes": [
|
|
15
|
+
{ "id": "pestana", "punto": "ficha_pestana", "modulo": "contactos", "entrada": "interfaz/index.html", "titulo": "Archivo a la ficha", "icono": "paperclip" },
|
|
16
|
+
{ "id": "pagina", "punto": "pagina", "entrada": "interfaz/index.html", "titulo": "Archivo a la ficha", "icono": "paperclip" }
|
|
17
|
+
]
|
|
18
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="es">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<style>
|
|
6
|
+
/* Las variables (--card, --primary…) llegan con el tema del CRM. */
|
|
7
|
+
html, body { margin: 0; background: var(--card, #fff); color: var(--card-foreground, #111); font: 14px/1.5 system-ui, sans-serif; }
|
|
8
|
+
main { padding: 12px 16px; display: grid; gap: 10px; max-width: 640px; }
|
|
9
|
+
h2 { margin: 0; font-size: 16px; color: var(--primary, #6d28d9); }
|
|
10
|
+
button { font: inherit; padding: 6px 12px; border-radius: 8px; border: 1px solid var(--border, #ccc); background: var(--background, #fff); color: inherit; cursor: pointer; }
|
|
11
|
+
button.principal { background: var(--primary, #6d28d9); color: var(--primary-foreground, #fff); border-color: transparent; }
|
|
12
|
+
button:disabled { opacity: .5; cursor: default; }
|
|
13
|
+
.fila { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
|
|
14
|
+
.suave { color: var(--muted-foreground, #666); margin: 0; }
|
|
15
|
+
.aviso { color: var(--destructive, #b91c1c); margin: 0; }
|
|
16
|
+
.ok { color: var(--primary, #6d28d9); margin: 0; }
|
|
17
|
+
.archivo { border: 1px solid var(--border, #ddd); border-radius: 10px; padding: 10px; display: grid; gap: 6px; }
|
|
18
|
+
img { max-width: 100%; max-height: 240px; border-radius: 6px; }
|
|
19
|
+
pre { margin: 0; max-height: 180px; overflow: auto; white-space: pre-wrap; background: var(--muted, #f4f4f5); padding: 8px; border-radius: 6px; }
|
|
20
|
+
</style>
|
|
21
|
+
<script type="module">
|
|
22
|
+
import { crearClienteCRM } from "@forgenex/crm-sdk";
|
|
23
|
+
const crm = crearClienteCRM();
|
|
24
|
+
const $ = (id) => document.getElementById(id);
|
|
25
|
+
let elegido = null;
|
|
26
|
+
let ficha = null;
|
|
27
|
+
|
|
28
|
+
// En palabras de cliente: el porqué y el paso siguiente.
|
|
29
|
+
const textoError = (e) => ({
|
|
30
|
+
sin_acceso: "Esta app no tiene permiso para los archivos. Lo concede quien administra el espacio en Ajustes › Apps.",
|
|
31
|
+
invalido: "Pulsa el botón para elegir un archivo.",
|
|
32
|
+
tiempo_agotado: "Se ha tardado demasiado. Vuelve a intentarlo.",
|
|
33
|
+
})[e?.message] ?? e?.detalle ?? "No se ha podido hacer. Vuelve a intentarlo.";
|
|
34
|
+
const tamano = (b) => b < 1024 ? `${b} B` : b < 1048576 ? `${(b / 1024).toFixed(0)} KB` : `${(b / 1048576).toFixed(1)} MB`;
|
|
35
|
+
const avisar = (texto, clase = "aviso") => { const p = $("estado"); p.className = clase; p.textContent = texto; };
|
|
36
|
+
|
|
37
|
+
async function elegir() {
|
|
38
|
+
avisar("", "suave");
|
|
39
|
+
try {
|
|
40
|
+
// Abre el explorador del CRM: la app solo recibe lo que la persona elige.
|
|
41
|
+
const a = await crm.archivos.elegir({ titulo: ficha ? "Para adjuntarlo a esta ficha" : "Para verlo aquí" });
|
|
42
|
+
if (!a) return avisar("No has elegido nada.", "suave");
|
|
43
|
+
elegido = a;
|
|
44
|
+
$("nombre").textContent = a.nombre;
|
|
45
|
+
$("detalle").textContent = `${a.tipo || "archivo"} · ${tamano(a.bytes)}`;
|
|
46
|
+
$("vista").replaceChildren();
|
|
47
|
+
$("archivo").hidden = false;
|
|
48
|
+
$("vincular").disabled = !ficha;
|
|
49
|
+
// El contenido lo sirve el CRM (hasta 5 MB).
|
|
50
|
+
const { data } = await crm.archivos.leer(a.id);
|
|
51
|
+
if (data.tipo.startsWith("image/")) {
|
|
52
|
+
const img = document.createElement("img");
|
|
53
|
+
img.alt = data.nombre;
|
|
54
|
+
img.src = `data:${data.tipo};base64,${data.contenido_base64}`;
|
|
55
|
+
$("vista").append(img);
|
|
56
|
+
} else if (data.tipo.startsWith("text/") || data.tipo === "application/json") {
|
|
57
|
+
const pre = document.createElement("pre");
|
|
58
|
+
pre.textContent = new TextDecoder().decode(Uint8Array.from(atob(data.contenido_base64), (c) => c.charCodeAt(0))).slice(0, 4000);
|
|
59
|
+
$("vista").append(pre);
|
|
60
|
+
} else {
|
|
61
|
+
const p = document.createElement("p");
|
|
62
|
+
p.className = "suave";
|
|
63
|
+
p.textContent = `Leído: ${tamano(data.bytes)}.`;
|
|
64
|
+
$("vista").append(p);
|
|
65
|
+
}
|
|
66
|
+
} catch (e) { avisar(textoError(e)); }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function vincular() {
|
|
70
|
+
if (!elegido || !ficha) return;
|
|
71
|
+
$("vincular").disabled = true;
|
|
72
|
+
try {
|
|
73
|
+
await crm.archivos.vincular(elegido.id, { modulo: ficha.modulo, id: ficha.id });
|
|
74
|
+
avisar(`«${elegido.nombre}» está vinculado a la ficha. Lo ves en su pestaña Archivos.`, "ok");
|
|
75
|
+
} catch (e) { avisar(textoError(e)); $("vincular").disabled = false; }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
$("elegir").addEventListener("click", elegir);
|
|
79
|
+
$("vincular").addEventListener("click", vincular);
|
|
80
|
+
crm.contexto().then((c) => {
|
|
81
|
+
ficha = c && c.id ? { modulo: c.modulo, id: c.id } : null;
|
|
82
|
+
$("donde").textContent = ficha ? "Elige un archivo del almacenamiento y vincúlalo a esta ficha." : "Elige un archivo del almacenamiento para verlo aquí.";
|
|
83
|
+
}).catch(() => {});
|
|
84
|
+
</script>
|
|
85
|
+
</head>
|
|
86
|
+
<body>
|
|
87
|
+
<main>
|
|
88
|
+
<h2>📎 Archivo a la ficha</h2>
|
|
89
|
+
<p id="donde" class="suave">Cargando…</p>
|
|
90
|
+
<div class="fila"><button id="elegir" class="principal" type="button" data-ejemplo-elegir>Elegir un archivo…</button></div>
|
|
91
|
+
<section id="archivo" class="archivo" hidden data-ejemplo-archivo>
|
|
92
|
+
<strong id="nombre"></strong>
|
|
93
|
+
<span id="detalle" class="suave"></span>
|
|
94
|
+
<div id="vista"></div>
|
|
95
|
+
<div class="fila"><button id="vincular" type="button" disabled data-ejemplo-vincular>Vincular a esta ficha</button></div>
|
|
96
|
+
</section>
|
|
97
|
+
<p id="estado" class="suave" role="status" data-ejemplo-estado></p>
|
|
98
|
+
</main>
|
|
99
|
+
</body>
|
|
100
|
+
</html>
|
package/extension.schema.json
CHANGED
|
@@ -93,6 +93,11 @@
|
|
|
93
93
|
"type": "string",
|
|
94
94
|
"maxLength": 40
|
|
95
95
|
},
|
|
96
|
+
"politica_datos": {
|
|
97
|
+
"type": "string",
|
|
98
|
+
"format": "uri",
|
|
99
|
+
"description": "https: tu política de datos. Obligatoria para entrar en la tienda si la app usa red (permisos.red_hacia)."
|
|
100
|
+
},
|
|
96
101
|
"depende_de": {
|
|
97
102
|
"type": "array",
|
|
98
103
|
"maxItems": 20,
|
|
@@ -643,6 +648,18 @@
|
|
|
643
648
|
"type": "string"
|
|
644
649
|
},
|
|
645
650
|
"description": "Los eventos del CRM de los que se entera (registro.creado, registro.actualizado…). El administrador acepta cada uno."
|
|
651
|
+
},
|
|
652
|
+
"archivos": {
|
|
653
|
+
"type": "array",
|
|
654
|
+
"maxItems": 2,
|
|
655
|
+
"uniqueItems": true,
|
|
656
|
+
"items": {
|
|
657
|
+
"enum": [
|
|
658
|
+
"leer",
|
|
659
|
+
"escribir"
|
|
660
|
+
]
|
|
661
|
+
},
|
|
662
|
+
"description": "1.17.0: los archivos del almacenamiento del espacio que la persona ELIGE con archivos.elegir() (el explorador del CRM). «leer»: verlos (archivos.leer); «escribir» (pide también «leer»): vincularlos a una ficha (archivos.vincular). Nunca ve el árbol ni recibe tokens de OneDrive o Google."
|
|
646
663
|
}
|
|
647
664
|
}
|
|
648
665
|
}
|
package/licencias.txt
CHANGED
package/package.json
CHANGED
package/puente-extension.d.ts
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
export interface OpcionesPeticion {
|
|
2
2
|
metodo?: "GET" | "POST";
|
|
3
3
|
idempotencia?: string;
|
|
4
|
+
/** Milisegundos antes de «tiempo_agotado» (de serie, los del cliente). */
|
|
5
|
+
espera?: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 1.17.0: un archivo que la persona eligió para la app con `archivos.elegir()`.
|
|
10
|
+
* `id` es opaco y vale solo para esta app y esta persona (30 días).
|
|
11
|
+
*/
|
|
12
|
+
export interface ArchivoElegido {
|
|
13
|
+
id: string;
|
|
14
|
+
nombre: string;
|
|
15
|
+
tipo: string;
|
|
16
|
+
bytes: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** `archivos.leer(id)`: el contenido lo sirve el CRM (hasta 5 MB). */
|
|
20
|
+
export interface ContenidoArchivo {
|
|
21
|
+
archivo: string;
|
|
22
|
+
nombre: string;
|
|
23
|
+
tipo: string;
|
|
24
|
+
bytes: number;
|
|
25
|
+
contenido_base64: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Los archivos del almacenamiento del espacio (OneDrive, SharePoint o Drive),
|
|
30
|
+
* con el permiso `"archivos": ["leer"]` (y `"escribir"` para vincular). La app
|
|
31
|
+
* NUNCA ve el árbol ni recibe tokens del proveedor: solo lo que la persona elige.
|
|
32
|
+
*/
|
|
33
|
+
export interface ArchivosCRM {
|
|
34
|
+
/** Abre el explorador del CRM en un modal. Llámalo en un clic de la persona; null si cancela. */
|
|
35
|
+
elegir(opciones?: { titulo?: string }): Promise<ArchivoElegido | null>;
|
|
36
|
+
leer(archivo: string): Promise<{ data: ContenidoArchivo }>;
|
|
37
|
+
/** Vincula (sin moverlo) a una ficha que la app puede editar; con `campo`, a su campo «Archivo». */
|
|
38
|
+
vincular(archivo: string, registro: { modulo: string; id: number; campo?: string }): Promise<{ data: { hechos: number } }>;
|
|
4
39
|
}
|
|
5
40
|
|
|
6
41
|
/**
|
|
@@ -21,6 +56,8 @@ export interface ClienteCRM {
|
|
|
21
56
|
pedir<T = unknown>(op: string, parametros?: Record<string, unknown>, opciones?: OpcionesPeticion): Promise<T>;
|
|
22
57
|
/** Solo en la página pública: llama a una de las `funciones` del montaje «publica». */
|
|
23
58
|
publica<T = unknown>(funcion: string, entrada?: unknown): Promise<T>;
|
|
59
|
+
/** 1.17.0: lo que la persona elige del almacenamiento (permiso «archivos»). */
|
|
60
|
+
archivos: ArchivosCRM;
|
|
24
61
|
cerrar(): void;
|
|
25
62
|
}
|
|
26
63
|
|
package/puente-extension.mjs
CHANGED
|
@@ -3,19 +3,31 @@ export function crearClienteCRM(ventana = window, espera = 15000) {
|
|
|
3
3
|
let siguiente = 0;
|
|
4
4
|
let cerrado = false;
|
|
5
5
|
const pendientes = new Map();
|
|
6
|
-
|
|
6
|
+
const cliente = {
|
|
7
7
|
contexto() { return this.pedir("contexto.actual"); },
|
|
8
8
|
// 1.14.0: en la página pública (montaje «publica»), una de sus funciones «publica».
|
|
9
9
|
publica(funcion, entrada = null) {
|
|
10
10
|
return this.pedir("publica.llamar", { funcion, entrada }, { metodo: "POST", idempotencia: `pub-${Date.now().toString(36)}-${siguiente}` });
|
|
11
11
|
},
|
|
12
|
+
// 1.17.0: los archivos que la persona ELIGE (permiso «archivos» de la app).
|
|
13
|
+
// elegir() abre el explorador del CRM (llámalo en un clic de la persona) y
|
|
14
|
+
// espera a que elija: hasta 10 minutos; null si cancela.
|
|
15
|
+
archivos: {
|
|
16
|
+
elegir: (opciones = {}) => cliente.pedir("archivos.elegir", opciones.titulo ? { titulo: String(opciones.titulo) } : {},
|
|
17
|
+
{ metodo: "POST", idempotencia: `elegir-${Date.now().toString(36)}-${siguiente}`, espera: 600000 }),
|
|
18
|
+
leer: (archivo) => cliente.pedir("archivos.leer", { archivo: String(archivo) }),
|
|
19
|
+
vincular: (archivo, registro) => cliente.pedir("archivos.vincular_elegido",
|
|
20
|
+
{ archivo: String(archivo), modulo: registro?.modulo, id: registro?.id, ...(registro?.campo ? { campo: registro.campo } : {}) },
|
|
21
|
+
{ metodo: "POST", idempotencia: `vincular-${archivo}-${registro?.modulo}-${registro?.id}-${registro?.campo ?? ""}` }),
|
|
22
|
+
},
|
|
12
23
|
pedir(op, parametros = {}, opciones = {}) {
|
|
13
24
|
if (cerrado) return Promise.reject(new Error("cerrado"));
|
|
14
25
|
const metodo = opciones.metodo ?? "GET";
|
|
26
|
+
const tope = opciones.espera ?? espera;
|
|
15
27
|
const id = siguiente++;
|
|
16
28
|
return new Promise((resolve, reject) => {
|
|
17
29
|
const canal = new MessageChannel();
|
|
18
|
-
const timer = setTimeout(() => { pendientes.delete(id); canal.port1.close(); reject(new Error("tiempo_agotado")); },
|
|
30
|
+
const timer = setTimeout(() => { pendientes.delete(id); canal.port1.close(); reject(new Error("tiempo_agotado")); }, tope);
|
|
19
31
|
pendientes.set(id, { reject, timer, puerto: canal.port1 });
|
|
20
32
|
canal.port1.onmessage = ({ data: d }) => {
|
|
21
33
|
if (!d || d.fx !== "crm-sdk" || d.v !== 1 || d.tipo !== "respuesta" || d.id !== id || typeof d.ok !== "boolean") return;
|
|
@@ -52,4 +64,5 @@ export function crearClienteCRM(ventana = window, espera = 15000) {
|
|
|
52
64
|
pendientes.clear();
|
|
53
65
|
},
|
|
54
66
|
};
|
|
67
|
+
return cliente;
|
|
55
68
|
}
|