astro-gas-api 0.1.0 → 0.1.1

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 CHANGED
@@ -71,6 +71,28 @@ defineTable → pestaña en Sheets defineApi → endpoints HTTP
71
71
  | `response` | schema zod | no | Validación/schema de la respuesta |
72
72
  | `handler` | función | sí | Lógica del endpoint: `(payload, ctx) => respuesta` |
73
73
 
74
+ ## Setup: `apiSetup`
75
+
76
+ El paquete aporta su propio runtime `runtime/api_Setup.gs` que define la función global `apiSetup(ctx)`, declarada en `setup.gasFunction` de la factory. Se ejecuta cuando corresponda el flujo de setup del proyecto (`astro-gas setup` / `astroGasSetup()` en Apps Script).
77
+
78
+ `apiSetup` lee la configuración del manifest inyectado por el core (`GAS_MANIFEST.extensions.api.manifest`) y actúa según `auth.mode`:
79
+
80
+ - `auth.mode === 'token'`: si falta el token en Script Properties, lo genera y lo guarda (respetando `ctx.dryRun`); si ya existe, lo reusa sin rotar. Nunca imprime el token en logs.
81
+ - `auth.mode === 'none'` o desconocido: no muta nada.
82
+
83
+ El setup nunca debe fallar: si no puede determinar `auth.mode`, sale sin hacer cambios.
84
+
85
+ ### Ejemplo de uso en config
86
+
87
+ ```js
88
+ extensions: [
89
+ api({
90
+ auth: { mode: 'token', property: 'API_TOKEN' },
91
+ endpoints: [],
92
+ }),
93
+ ],
94
+ ```
95
+
74
96
  ## Autenticación
75
97
 
76
98
  `astro-gas-api` no implementa OAuth ni sesiones de Google. El contrato de auth es el que declares:
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "astro-gas-api",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "API declarativa en el mismo proyecto Apps Script que las vistas",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "private": false,
8
8
  "main": "index.mjs",
9
+ "files": ["index.mjs", "src/", "runtime/", "README.md"],
9
10
  "engines": {
10
11
  "node": ">=20"
11
12
  },
12
13
  "peerDependencies": {
13
- "astro-to-appsscript": "0.5.0"
14
+ "astro-to-appsscript": "^0.5.0"
14
15
  },
15
16
  "dependencies": {
16
17
  "zod": "^3.24.0"
@@ -0,0 +1,81 @@
1
+ // runtime/api_Setup.gs
2
+ // Runtime de astro-gas-api: setup del token de API en Script Properties.
3
+ // ES2019 plano, sin import/export.
4
+ // Función pública (sin sufijo _): apiSetup.
5
+ // Toda la configuración llega desde GAS_MANIFEST.extensions.api.manifest
6
+ // (inyectada por el build). Si no hay config disponible, no falla.
7
+
8
+ var API_CONFIG = {
9
+ auth: { mode: 'none' },
10
+ };
11
+
12
+ // --- Función pública ---
13
+
14
+ function apiSetup(ctx) {
15
+ // ctx: { properties, log, dryRun }
16
+ var reporte = {
17
+ created: [],
18
+ reused: [],
19
+ };
20
+
21
+ // Obtener auth.mode de forma segura desde el manifest; si no se puede determinar, no hacer nada.
22
+ var manifest =
23
+ typeof GAS_MANIFEST !== 'undefined' && GAS_MANIFEST && GAS_MANIFEST.extensions
24
+ ? GAS_MANIFEST.extensions.api
25
+ : undefined;
26
+ var config = manifest && manifest.manifest ? manifest.manifest : API_CONFIG;
27
+ var auth = config && config.auth ? config.auth : { mode: 'none' };
28
+ var mode = auth && auth.mode ? auth.mode : 'none';
29
+
30
+ // Sin modo token: no mutar nada.
31
+ if (mode !== 'token') {
32
+ if (ctx && ctx.log) {
33
+ ctx.log('apiSetup: auth.mode no es "token", no se crea ni reusa token.');
34
+ }
35
+ return reporte;
36
+ }
37
+
38
+ var property = auth.property || 'API_TOKEN';
39
+ var propiedades =
40
+ ctx && ctx.properties ? ctx.properties : PropertiesService.getScriptProperties();
41
+ var tokenExistente = propiedades.getProperty ? propiedades.getProperty(property) : undefined;
42
+
43
+ if (ctx && ctx.dryRun === true) {
44
+ if (!tokenExistente) {
45
+ reporte.created.push(property);
46
+ } else {
47
+ reporte.reused.push(property);
48
+ }
49
+ return reporte;
50
+ }
51
+
52
+ if (tokenExistente) {
53
+ reporte.reused.push(property);
54
+ return reporte;
55
+ }
56
+
57
+ // Generar y guardar token (nunca imprimirlo en logs).
58
+ var tokenNuevo = _generarToken();
59
+ if (propiedades.setProperty) {
60
+ propiedades.setProperty(property, tokenNuevo);
61
+ }
62
+ reporte.created.push(property);
63
+
64
+ return reporte;
65
+ }
66
+
67
+ // --- Funciones privadas ---
68
+
69
+ function _generarToken(longitud) {
70
+ longitud = longitud || 43;
71
+ var caracteres =
72
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.';
73
+ var token = '';
74
+ for (var i = 0; i < longitud; i++) {
75
+ token += caracteres.charAt(Math.floor(Math.random() * caracteres.length));
76
+ }
77
+ return token;
78
+ }
79
+
80
+ // apiSetup() es la única función pública de este archivo (sin _);
81
+ // las que empiezan con _ son privadas (convención del ecosistema).
package/src/api.mjs CHANGED
@@ -42,7 +42,7 @@ export function api(config = {}) {
42
42
  name: 'api',
43
43
  contractVersion: 1,
44
44
  config: config.config || {},
45
- runtime: [],
45
+ runtime: [{ file: 'api_Setup.gs', source: new URL('../runtime/api_Setup.gs', import.meta.url) }],
46
46
  routes,
47
47
  manifest: {},
48
48
  publicFunctions,