@pakento/cms-sdk 4.0.3 → 4.0.5
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 +2 -86
- package/dist/index.d.mts +36 -51
- package/dist/index.d.ts +36 -51
- package/dist/index.js +113 -119
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +112 -116
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,99 +1,15 @@
|
|
|
1
1
|
# @pakento/cms-sdk
|
|
2
2
|
|
|
3
|
-
SDK para conectar tiendas en línea con Pakento CMS
|
|
3
|
+
SDK para conectar tiendas en línea con Pakento CMS de forma rápida y sencilla.
|
|
4
4
|
|
|
5
5
|
## Instalación
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
8
|
npm install @pakento/cms-sdk
|
|
9
9
|
# o
|
|
10
|
-
yarn add @pakento/cms-sdk
|
|
11
|
-
# o
|
|
12
10
|
bun add @pakento/cms-sdk
|
|
13
11
|
```
|
|
14
12
|
|
|
15
|
-
**Nota importante**: Este paquete requiere las siguientes peer dependencies:
|
|
16
|
-
|
|
17
|
-
- `react >= 16.8.0`
|
|
18
|
-
- `react-dom >= 16.8.0`
|
|
19
|
-
- `axios >= 1.6.0`
|
|
20
|
-
- `@upstash/redis >= 1.35.3` (opcional, solo si usas cache con Redis)
|
|
21
|
-
|
|
22
|
-
## Uso del Cart Context en Next.js
|
|
23
|
-
|
|
24
|
-
### 1. Configurar el Provider en tu app
|
|
25
|
-
|
|
26
|
-
En tu `app/layout.tsx` o `pages/_app.tsx`:
|
|
27
|
-
|
|
28
|
-
```tsx
|
|
29
|
-
import { CartProvider } from "@pakento/cms-sdk";
|
|
30
|
-
|
|
31
|
-
export default function RootLayout({
|
|
32
|
-
children,
|
|
33
|
-
}: {
|
|
34
|
-
children: React.ReactNode;
|
|
35
|
-
}) {
|
|
36
|
-
return (
|
|
37
|
-
<html lang="es">
|
|
38
|
-
<body>
|
|
39
|
-
<CartProvider>{children}</CartProvider>
|
|
40
|
-
</body>
|
|
41
|
-
</html>
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
### 2. Usar el hook useCart en tus componentes
|
|
47
|
-
|
|
48
|
-
```tsx
|
|
49
|
-
"use client";
|
|
50
|
-
|
|
51
|
-
import { useCart } from "@pakento/cms-sdk";
|
|
52
|
-
|
|
53
|
-
export default function CartComponent() {
|
|
54
|
-
const { state, dispatch } = useCart();
|
|
55
|
-
|
|
56
|
-
const addToCart = (item) => {
|
|
57
|
-
dispatch({ type: "ADD_TO_CART", payload: item });
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const removeFromCart = (itemId) => {
|
|
61
|
-
dispatch({ type: "REMOVE_FROM_CART", payload: itemId });
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
return (
|
|
65
|
-
<div>
|
|
66
|
-
<p>Items en carrito: {state.cart.itemCount}</p>
|
|
67
|
-
<p>Total: ${state.cart.total}</p>
|
|
68
|
-
{/* Tu UI del carrito aquí */}
|
|
69
|
-
</div>
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
### 3. API Usage
|
|
75
|
-
|
|
76
|
-
````tsx
|
|
77
|
-
import { pakentoCMSAPI } from '@pakento/cms-sdk';
|
|
78
|
-
|
|
79
|
-
// Configurar variables de entorno
|
|
80
|
-
// PAKENTO_API_URL=tu_url_de_api
|
|
81
|
-
// PAKENTO_API_KEY=tu_api_key
|
|
82
|
-
|
|
83
|
-
const items = await pakentoCMSAPI.getItems({
|
|
84
|
-
limit: 10,
|
|
85
|
-
page: 1
|
|
86
|
-
});
|
|
87
|
-
``` de forma rápida y sencilla.
|
|
88
|
-
|
|
89
|
-
## Instalación
|
|
90
|
-
|
|
91
|
-
```bash
|
|
92
|
-
npm install @pakento/cms-sdk
|
|
93
|
-
# o
|
|
94
|
-
bun add @pakento/cms-sdk
|
|
95
|
-
````
|
|
96
|
-
|
|
97
13
|
## Configuración
|
|
98
14
|
|
|
99
15
|
Agrega las siguientes variables de entorno a tu proyecto:
|
|
@@ -277,4 +193,4 @@ El SDK incluye manejo de errores para:
|
|
|
277
193
|
|
|
278
194
|
MIT
|
|
279
195
|
|
|
280
|
-
.build
|
|
196
|
+
.build
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import * as React from 'react';
|
|
3
|
-
|
|
4
1
|
interface ItemImage {
|
|
5
2
|
alt: string;
|
|
6
3
|
url: string;
|
|
@@ -223,15 +220,6 @@ interface CustomGraphQLResponse<T> {
|
|
|
223
220
|
errorMessage: string | null;
|
|
224
221
|
errors?: T[];
|
|
225
222
|
}
|
|
226
|
-
interface CartItem {
|
|
227
|
-
item: Item;
|
|
228
|
-
quantity: number;
|
|
229
|
-
}
|
|
230
|
-
interface Cart {
|
|
231
|
-
cartItems: CartItem[];
|
|
232
|
-
total: number;
|
|
233
|
-
itemCount: number;
|
|
234
|
-
}
|
|
235
223
|
|
|
236
224
|
declare class PakentoCMSAPI {
|
|
237
225
|
private client;
|
|
@@ -242,17 +230,23 @@ declare class PakentoCMSAPI {
|
|
|
242
230
|
constructor(config?: {
|
|
243
231
|
cacheTTL?: number;
|
|
244
232
|
});
|
|
233
|
+
/**
|
|
234
|
+
* Genera un hash único para los parámetros de búsqueda
|
|
235
|
+
*/
|
|
236
|
+
private generateParamsHash;
|
|
237
|
+
/**
|
|
238
|
+
* Genera una key única y calculable para el cache basada en:
|
|
239
|
+
* - API Token (para namespacing entre proyectos)
|
|
240
|
+
* - Nombre de la función
|
|
241
|
+
* - Hash de los parámetros de búsqueda
|
|
242
|
+
*/
|
|
243
|
+
private buildCacheKey;
|
|
245
244
|
/**
|
|
246
245
|
* Obtiene datos desde cache si existen; de lo contrario llama a fetcher() y guarda.
|
|
247
246
|
*/
|
|
248
247
|
private safeJsonParse;
|
|
249
248
|
private serializeForCache;
|
|
250
249
|
private getCachedOrFetch;
|
|
251
|
-
/**
|
|
252
|
-
* Construye una llave única para el cache a partir del nombre de la
|
|
253
|
-
* consulta y los parámetros.
|
|
254
|
-
*/
|
|
255
|
-
private buildCacheKey;
|
|
256
250
|
getItems(params?: GetItemsParams): Promise<ItemsApiResponse>;
|
|
257
251
|
private fetchItemsFromAPI;
|
|
258
252
|
getCategories(params?: GetCategoriesParams): Promise<CategoriesApiResponse>;
|
|
@@ -264,40 +258,31 @@ declare class PakentoCMSAPI {
|
|
|
264
258
|
createEcommerceOrder(params: CreateEcommerceOrderParams): Promise<CreateEcommerceOrderResponse>;
|
|
265
259
|
executeCustomQuery<T>(params: CustomGraphQLParams<T>): Promise<CustomGraphQLResponse<T>>;
|
|
266
260
|
sendContactUsEmail(params: SendContactUsEmailParams): Promise<SendContactUsEmailResponse>;
|
|
261
|
+
/**
|
|
262
|
+
* Verifica si existe cache para una función y parámetros específicos
|
|
263
|
+
*/
|
|
264
|
+
hasCache(functionName: string, params?: Record<string, unknown>): Promise<boolean>;
|
|
265
|
+
/**
|
|
266
|
+
* Obtiene la key de cache para una función y parámetros específicos
|
|
267
|
+
*/
|
|
268
|
+
getCacheKey(functionName: string, params?: Record<string, unknown>): string;
|
|
269
|
+
/**
|
|
270
|
+
* Limpia el cache para una función y parámetros específicos
|
|
271
|
+
*/
|
|
272
|
+
clearCache(functionName: string, params?: Record<string, unknown>): Promise<boolean>;
|
|
273
|
+
/**
|
|
274
|
+
* Limpia todo el cache relacionado con este API Key
|
|
275
|
+
*/
|
|
276
|
+
clearAllCache(): Promise<boolean>;
|
|
277
|
+
/**
|
|
278
|
+
* Obtiene información del cache (útil para debugging)
|
|
279
|
+
*/
|
|
280
|
+
getCacheInfo(functionName: string, params?: Record<string, unknown>): Promise<{
|
|
281
|
+
exists: boolean;
|
|
282
|
+
key: string;
|
|
283
|
+
ttl?: number;
|
|
284
|
+
}>;
|
|
267
285
|
}
|
|
268
286
|
declare const pakentoCMSAPI: PakentoCMSAPI;
|
|
269
287
|
|
|
270
|
-
|
|
271
|
-
cart: Cart;
|
|
272
|
-
isCartOpen: boolean;
|
|
273
|
-
}
|
|
274
|
-
type CartAction = {
|
|
275
|
-
type: "ADD_TO_CART";
|
|
276
|
-
payload: Item;
|
|
277
|
-
} | {
|
|
278
|
-
type: "REMOVE_FROM_CART";
|
|
279
|
-
payload: string;
|
|
280
|
-
} | {
|
|
281
|
-
type: "UPDATE_QUANTITY";
|
|
282
|
-
payload: {
|
|
283
|
-
itemId: string;
|
|
284
|
-
quantity: number;
|
|
285
|
-
};
|
|
286
|
-
} | {
|
|
287
|
-
type: "CLEAR_CART";
|
|
288
|
-
} | {
|
|
289
|
-
type: "TOGGLE_CART";
|
|
290
|
-
} | {
|
|
291
|
-
type: "OPEN_CART";
|
|
292
|
-
} | {
|
|
293
|
-
type: "CLOSE_CART";
|
|
294
|
-
};
|
|
295
|
-
declare function CartProvider({ children }: {
|
|
296
|
-
children: React.ReactNode;
|
|
297
|
-
}): react_jsx_runtime.JSX.Element;
|
|
298
|
-
declare function useCart(): {
|
|
299
|
-
state: CartState;
|
|
300
|
-
dispatch: React.Dispatch<CartAction>;
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
export { type Brand, type BrandsApiResponse, type BrandsRawResponse, CartProvider, type CategoriesApiResponse, type CategoriesRawResponse, type CategoriesWhere, type Category, type CreateEcommerceOrderParams, type CreateEcommerceOrderResponse, type CustomGraphQLParams, type CustomGraphQLResponse, type Entity, type EntityApiResponse, type EntityRawResponse, type GetBrandsParams, type GetCategoriesParams, type GetEntityParams, type GetItemsParams, type Item, type ItemImage, type ItemsApiResponse, type ItemsRawResponse, type ItemsWhere, type SendContactUsEmailParams, type SendContactUsEmailResponse, type WhereEquals, pakentoCMSAPI, useCart };
|
|
288
|
+
export { type Brand, type BrandsApiResponse, type BrandsRawResponse, type CategoriesApiResponse, type CategoriesRawResponse, type CategoriesWhere, type Category, type CreateEcommerceOrderParams, type CreateEcommerceOrderResponse, type CustomGraphQLParams, type CustomGraphQLResponse, type Entity, type EntityApiResponse, type EntityRawResponse, type GetBrandsParams, type GetCategoriesParams, type GetEntityParams, type GetItemsParams, type Item, type ItemImage, type ItemsApiResponse, type ItemsRawResponse, type ItemsWhere, type SendContactUsEmailParams, type SendContactUsEmailResponse, type WhereEquals, pakentoCMSAPI };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import * as React from 'react';
|
|
3
|
-
|
|
4
1
|
interface ItemImage {
|
|
5
2
|
alt: string;
|
|
6
3
|
url: string;
|
|
@@ -223,15 +220,6 @@ interface CustomGraphQLResponse<T> {
|
|
|
223
220
|
errorMessage: string | null;
|
|
224
221
|
errors?: T[];
|
|
225
222
|
}
|
|
226
|
-
interface CartItem {
|
|
227
|
-
item: Item;
|
|
228
|
-
quantity: number;
|
|
229
|
-
}
|
|
230
|
-
interface Cart {
|
|
231
|
-
cartItems: CartItem[];
|
|
232
|
-
total: number;
|
|
233
|
-
itemCount: number;
|
|
234
|
-
}
|
|
235
223
|
|
|
236
224
|
declare class PakentoCMSAPI {
|
|
237
225
|
private client;
|
|
@@ -242,17 +230,23 @@ declare class PakentoCMSAPI {
|
|
|
242
230
|
constructor(config?: {
|
|
243
231
|
cacheTTL?: number;
|
|
244
232
|
});
|
|
233
|
+
/**
|
|
234
|
+
* Genera un hash único para los parámetros de búsqueda
|
|
235
|
+
*/
|
|
236
|
+
private generateParamsHash;
|
|
237
|
+
/**
|
|
238
|
+
* Genera una key única y calculable para el cache basada en:
|
|
239
|
+
* - API Token (para namespacing entre proyectos)
|
|
240
|
+
* - Nombre de la función
|
|
241
|
+
* - Hash de los parámetros de búsqueda
|
|
242
|
+
*/
|
|
243
|
+
private buildCacheKey;
|
|
245
244
|
/**
|
|
246
245
|
* Obtiene datos desde cache si existen; de lo contrario llama a fetcher() y guarda.
|
|
247
246
|
*/
|
|
248
247
|
private safeJsonParse;
|
|
249
248
|
private serializeForCache;
|
|
250
249
|
private getCachedOrFetch;
|
|
251
|
-
/**
|
|
252
|
-
* Construye una llave única para el cache a partir del nombre de la
|
|
253
|
-
* consulta y los parámetros.
|
|
254
|
-
*/
|
|
255
|
-
private buildCacheKey;
|
|
256
250
|
getItems(params?: GetItemsParams): Promise<ItemsApiResponse>;
|
|
257
251
|
private fetchItemsFromAPI;
|
|
258
252
|
getCategories(params?: GetCategoriesParams): Promise<CategoriesApiResponse>;
|
|
@@ -264,40 +258,31 @@ declare class PakentoCMSAPI {
|
|
|
264
258
|
createEcommerceOrder(params: CreateEcommerceOrderParams): Promise<CreateEcommerceOrderResponse>;
|
|
265
259
|
executeCustomQuery<T>(params: CustomGraphQLParams<T>): Promise<CustomGraphQLResponse<T>>;
|
|
266
260
|
sendContactUsEmail(params: SendContactUsEmailParams): Promise<SendContactUsEmailResponse>;
|
|
261
|
+
/**
|
|
262
|
+
* Verifica si existe cache para una función y parámetros específicos
|
|
263
|
+
*/
|
|
264
|
+
hasCache(functionName: string, params?: Record<string, unknown>): Promise<boolean>;
|
|
265
|
+
/**
|
|
266
|
+
* Obtiene la key de cache para una función y parámetros específicos
|
|
267
|
+
*/
|
|
268
|
+
getCacheKey(functionName: string, params?: Record<string, unknown>): string;
|
|
269
|
+
/**
|
|
270
|
+
* Limpia el cache para una función y parámetros específicos
|
|
271
|
+
*/
|
|
272
|
+
clearCache(functionName: string, params?: Record<string, unknown>): Promise<boolean>;
|
|
273
|
+
/**
|
|
274
|
+
* Limpia todo el cache relacionado con este API Key
|
|
275
|
+
*/
|
|
276
|
+
clearAllCache(): Promise<boolean>;
|
|
277
|
+
/**
|
|
278
|
+
* Obtiene información del cache (útil para debugging)
|
|
279
|
+
*/
|
|
280
|
+
getCacheInfo(functionName: string, params?: Record<string, unknown>): Promise<{
|
|
281
|
+
exists: boolean;
|
|
282
|
+
key: string;
|
|
283
|
+
ttl?: number;
|
|
284
|
+
}>;
|
|
267
285
|
}
|
|
268
286
|
declare const pakentoCMSAPI: PakentoCMSAPI;
|
|
269
287
|
|
|
270
|
-
|
|
271
|
-
cart: Cart;
|
|
272
|
-
isCartOpen: boolean;
|
|
273
|
-
}
|
|
274
|
-
type CartAction = {
|
|
275
|
-
type: "ADD_TO_CART";
|
|
276
|
-
payload: Item;
|
|
277
|
-
} | {
|
|
278
|
-
type: "REMOVE_FROM_CART";
|
|
279
|
-
payload: string;
|
|
280
|
-
} | {
|
|
281
|
-
type: "UPDATE_QUANTITY";
|
|
282
|
-
payload: {
|
|
283
|
-
itemId: string;
|
|
284
|
-
quantity: number;
|
|
285
|
-
};
|
|
286
|
-
} | {
|
|
287
|
-
type: "CLEAR_CART";
|
|
288
|
-
} | {
|
|
289
|
-
type: "TOGGLE_CART";
|
|
290
|
-
} | {
|
|
291
|
-
type: "OPEN_CART";
|
|
292
|
-
} | {
|
|
293
|
-
type: "CLOSE_CART";
|
|
294
|
-
};
|
|
295
|
-
declare function CartProvider({ children }: {
|
|
296
|
-
children: React.ReactNode;
|
|
297
|
-
}): react_jsx_runtime.JSX.Element;
|
|
298
|
-
declare function useCart(): {
|
|
299
|
-
state: CartState;
|
|
300
|
-
dispatch: React.Dispatch<CartAction>;
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
export { type Brand, type BrandsApiResponse, type BrandsRawResponse, CartProvider, type CategoriesApiResponse, type CategoriesRawResponse, type CategoriesWhere, type Category, type CreateEcommerceOrderParams, type CreateEcommerceOrderResponse, type CustomGraphQLParams, type CustomGraphQLResponse, type Entity, type EntityApiResponse, type EntityRawResponse, type GetBrandsParams, type GetCategoriesParams, type GetEntityParams, type GetItemsParams, type Item, type ItemImage, type ItemsApiResponse, type ItemsRawResponse, type ItemsWhere, type SendContactUsEmailParams, type SendContactUsEmailResponse, type WhereEquals, pakentoCMSAPI, useCart };
|
|
288
|
+
export { type Brand, type BrandsApiResponse, type BrandsRawResponse, type CategoriesApiResponse, type CategoriesRawResponse, type CategoriesWhere, type Category, type CreateEcommerceOrderParams, type CreateEcommerceOrderResponse, type CustomGraphQLParams, type CustomGraphQLResponse, type Entity, type EntityApiResponse, type EntityRawResponse, type GetBrandsParams, type GetCategoriesParams, type GetEntityParams, type GetItemsParams, type Item, type ItemImage, type ItemsApiResponse, type ItemsRawResponse, type ItemsWhere, type SendContactUsEmailParams, type SendContactUsEmailResponse, type WhereEquals, pakentoCMSAPI };
|
package/dist/index.js
CHANGED
|
@@ -30,15 +30,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
-
|
|
34
|
-
pakentoCMSAPI: () => pakentoCMSAPI,
|
|
35
|
-
useCart: () => useCart
|
|
33
|
+
pakentoCMSAPI: () => pakentoCMSAPI
|
|
36
34
|
});
|
|
37
35
|
module.exports = __toCommonJS(index_exports);
|
|
38
36
|
|
|
39
37
|
// src/services/api.ts
|
|
40
38
|
var import_axios = __toESM(require("axios"));
|
|
41
39
|
var import_redis = require("@upstash/redis");
|
|
40
|
+
var import_crypto = require("crypto");
|
|
42
41
|
var PakentoCMSAPI = class {
|
|
43
42
|
constructor(config) {
|
|
44
43
|
this.defaultTTL = 86400;
|
|
@@ -76,6 +75,28 @@ var PakentoCMSAPI = class {
|
|
|
76
75
|
}
|
|
77
76
|
);
|
|
78
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Genera un hash único para los parámetros de búsqueda
|
|
80
|
+
*/
|
|
81
|
+
generateParamsHash(params) {
|
|
82
|
+
const sortedParams = Object.keys(params).sort().reduce((acc, key) => {
|
|
83
|
+
acc[key] = params[key];
|
|
84
|
+
return acc;
|
|
85
|
+
}, {});
|
|
86
|
+
const paramsString = JSON.stringify(sortedParams);
|
|
87
|
+
return (0, import_crypto.createHash)("sha256").update(paramsString).digest("hex").substring(0, 16);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Genera una key única y calculable para el cache basada en:
|
|
91
|
+
* - API Token (para namespacing entre proyectos)
|
|
92
|
+
* - Nombre de la función
|
|
93
|
+
* - Hash de los parámetros de búsqueda
|
|
94
|
+
*/
|
|
95
|
+
buildCacheKey(functionName, params = {}) {
|
|
96
|
+
const paramsHash = this.generateParamsHash(params);
|
|
97
|
+
const apiKeyHash = (0, import_crypto.createHash)("sha256").update(this.apiKey).digest("hex").substring(0, 8);
|
|
98
|
+
return `pakento:${apiKeyHash}:${functionName}:${paramsHash}`;
|
|
99
|
+
}
|
|
79
100
|
/**
|
|
80
101
|
* Obtiene datos desde cache si existen; de lo contrario llama a fetcher() y guarda.
|
|
81
102
|
*/
|
|
@@ -91,37 +112,38 @@ var PakentoCMSAPI = class {
|
|
|
91
112
|
}
|
|
92
113
|
async getCachedOrFetch(key, fetcher, ttl, skipCache = false) {
|
|
93
114
|
if (!this.redis || skipCache) {
|
|
115
|
+
console.log(`[PakentoSDK] ${!this.redis ? "Redis no configurado" : "Saltando cache"} - llamando a API`);
|
|
94
116
|
return fetcher();
|
|
95
117
|
}
|
|
96
118
|
try {
|
|
119
|
+
console.log(`[PakentoSDK] Buscando en cache: ${key}`);
|
|
97
120
|
const cached = await this.redis.get(key);
|
|
98
121
|
if (cached) {
|
|
122
|
+
console.log(`[PakentoSDK] \u2705 Cache hit para: ${key}`);
|
|
99
123
|
const parsed = this.safeJsonParse(cached);
|
|
100
124
|
if (parsed !== null) {
|
|
101
125
|
return parsed;
|
|
126
|
+
} else {
|
|
127
|
+
console.warn(`[PakentoSDK] Error parseando cache para: ${key}`);
|
|
102
128
|
}
|
|
129
|
+
} else {
|
|
130
|
+
console.log(`[PakentoSDK] \u274C Cache miss para: ${key}`);
|
|
103
131
|
}
|
|
104
132
|
} catch (err) {
|
|
105
133
|
console.warn("[PakentoSDK] Error leyendo cache Redis", err);
|
|
106
134
|
}
|
|
135
|
+
console.log(`[PakentoSDK] Llamando a API para: ${key}`);
|
|
107
136
|
const result = await fetcher();
|
|
108
137
|
try {
|
|
109
138
|
await this.redis.set(key, this.serializeForCache(result), {
|
|
110
139
|
ex: ttl ?? this.defaultTTL
|
|
111
140
|
});
|
|
141
|
+
console.log(`[PakentoSDK] \u2705 Guardado en cache: ${key} (TTL: ${ttl ?? this.defaultTTL}s)`);
|
|
112
142
|
} catch (err) {
|
|
113
143
|
console.warn("[PakentoSDK] Error escribiendo cache Redis", err);
|
|
114
144
|
}
|
|
115
145
|
return result;
|
|
116
146
|
}
|
|
117
|
-
/**
|
|
118
|
-
* Construye una llave única para el cache a partir del nombre de la
|
|
119
|
-
* consulta y los parámetros.
|
|
120
|
-
*/
|
|
121
|
-
buildCacheKey(queryName, variables) {
|
|
122
|
-
const vars = typeof variables === "string" ? variables : JSON.stringify(variables ?? {});
|
|
123
|
-
return `${this.apiKey}-${queryName}:${vars}`;
|
|
124
|
-
}
|
|
125
147
|
async getItems(params = {}) {
|
|
126
148
|
const { skipCache = false, cacheTTL, ...rest } = params;
|
|
127
149
|
const cacheKey = this.buildCacheKey("GetEcommerceItems", rest);
|
|
@@ -823,125 +845,97 @@ var PakentoCMSAPI = class {
|
|
|
823
845
|
};
|
|
824
846
|
}
|
|
825
847
|
}
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
var { createContext, useContext, useReducer } = React;
|
|
833
|
-
var initialState = {
|
|
834
|
-
cart: {
|
|
835
|
-
cartItems: [],
|
|
836
|
-
total: 0,
|
|
837
|
-
itemCount: 0
|
|
838
|
-
},
|
|
839
|
-
isCartOpen: false
|
|
840
|
-
};
|
|
841
|
-
function cartReducer(state, action) {
|
|
842
|
-
switch (action.type) {
|
|
843
|
-
case "ADD_TO_CART": {
|
|
844
|
-
const existingItem = state.cart.cartItems.find(
|
|
845
|
-
(cartItem) => cartItem.item.id === action.payload.id
|
|
846
|
-
);
|
|
847
|
-
let newItems;
|
|
848
|
-
if (existingItem) {
|
|
849
|
-
newItems = state.cart.cartItems.map(
|
|
850
|
-
(cartItem) => cartItem.item.id === action.payload.id ? { ...cartItem, quantity: cartItem.quantity + 1 } : cartItem
|
|
851
|
-
);
|
|
852
|
-
} else {
|
|
853
|
-
newItems = [
|
|
854
|
-
...state.cart.cartItems,
|
|
855
|
-
{ item: action.payload, quantity: 1 }
|
|
856
|
-
];
|
|
857
|
-
}
|
|
858
|
-
const total = newItems.reduce(
|
|
859
|
-
(sum, cartItem) => sum + cartItem.item.price * cartItem.quantity,
|
|
860
|
-
0
|
|
861
|
-
);
|
|
862
|
-
const itemCount = newItems.reduce(
|
|
863
|
-
(sum, cartItem) => sum + cartItem.quantity,
|
|
864
|
-
0
|
|
865
|
-
);
|
|
866
|
-
return {
|
|
867
|
-
...state,
|
|
868
|
-
cart: { cartItems: newItems, total, itemCount }
|
|
869
|
-
};
|
|
848
|
+
/**
|
|
849
|
+
* Verifica si existe cache para una función y parámetros específicos
|
|
850
|
+
*/
|
|
851
|
+
async hasCache(functionName, params = {}) {
|
|
852
|
+
if (!this.redis) {
|
|
853
|
+
return false;
|
|
870
854
|
}
|
|
871
|
-
|
|
872
|
-
const
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
);
|
|
879
|
-
const itemCount = newItems.reduce(
|
|
880
|
-
(sum, cartItem) => sum + cartItem.quantity,
|
|
881
|
-
0
|
|
882
|
-
);
|
|
883
|
-
return {
|
|
884
|
-
...state,
|
|
885
|
-
cart: { cartItems: newItems, total, itemCount }
|
|
886
|
-
};
|
|
855
|
+
try {
|
|
856
|
+
const key = this.buildCacheKey(functionName, params);
|
|
857
|
+
const cached = await this.redis.get(key);
|
|
858
|
+
return cached !== null;
|
|
859
|
+
} catch (err) {
|
|
860
|
+
console.warn("[PakentoSDK] Error verificando cache", err);
|
|
861
|
+
return false;
|
|
887
862
|
}
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
return
|
|
901
|
-
...state,
|
|
902
|
-
cart: { cartItems: newItems, total, itemCount }
|
|
903
|
-
};
|
|
863
|
+
}
|
|
864
|
+
/**
|
|
865
|
+
* Obtiene la key de cache para una función y parámetros específicos
|
|
866
|
+
*/
|
|
867
|
+
getCacheKey(functionName, params = {}) {
|
|
868
|
+
return this.buildCacheKey(functionName, params);
|
|
869
|
+
}
|
|
870
|
+
/**
|
|
871
|
+
* Limpia el cache para una función y parámetros específicos
|
|
872
|
+
*/
|
|
873
|
+
async clearCache(functionName, params = {}) {
|
|
874
|
+
if (!this.redis) {
|
|
875
|
+
return false;
|
|
904
876
|
}
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
877
|
+
try {
|
|
878
|
+
const key = this.buildCacheKey(functionName, params);
|
|
879
|
+
await this.redis.del(key);
|
|
880
|
+
console.log(`[PakentoSDK] \u2705 Cache limpiado para: ${key}`);
|
|
881
|
+
return true;
|
|
882
|
+
} catch (err) {
|
|
883
|
+
console.warn("[PakentoSDK] Error limpiando cache", err);
|
|
884
|
+
return false;
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
/**
|
|
888
|
+
* Limpia todo el cache relacionado con este API Key
|
|
889
|
+
*/
|
|
890
|
+
async clearAllCache() {
|
|
891
|
+
if (!this.redis) {
|
|
892
|
+
return false;
|
|
893
|
+
}
|
|
894
|
+
try {
|
|
895
|
+
const apiKeyHash = (0, import_crypto.createHash)("sha256").update(this.apiKey).digest("hex").substring(0, 8);
|
|
896
|
+
const pattern = `pakento:${apiKeyHash}:*`;
|
|
897
|
+
const keys = await this.redis.keys(pattern);
|
|
898
|
+
if (keys.length > 0) {
|
|
899
|
+
await this.redis.del(...keys);
|
|
900
|
+
console.log(`[PakentoSDK] \u2705 Cache limpiado para ${keys.length} keys`);
|
|
901
|
+
}
|
|
902
|
+
return true;
|
|
903
|
+
} catch (err) {
|
|
904
|
+
console.warn("[PakentoSDK] Error limpiando todo el cache", err);
|
|
905
|
+
return false;
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
/**
|
|
909
|
+
* Obtiene información del cache (útil para debugging)
|
|
910
|
+
*/
|
|
911
|
+
async getCacheInfo(functionName, params = {}) {
|
|
912
|
+
if (!this.redis) {
|
|
911
913
|
return {
|
|
912
|
-
|
|
913
|
-
|
|
914
|
+
exists: false,
|
|
915
|
+
key: this.buildCacheKey(functionName, params)
|
|
914
916
|
};
|
|
915
|
-
|
|
917
|
+
}
|
|
918
|
+
try {
|
|
919
|
+
const key = this.buildCacheKey(functionName, params);
|
|
920
|
+
const exists = await this.redis.exists(key);
|
|
921
|
+
const ttl = exists ? await this.redis.ttl(key) : void 0;
|
|
916
922
|
return {
|
|
917
|
-
|
|
918
|
-
|
|
923
|
+
exists: exists === 1,
|
|
924
|
+
key,
|
|
925
|
+
ttl: ttl === -1 ? void 0 : ttl
|
|
919
926
|
};
|
|
920
|
-
|
|
927
|
+
} catch (err) {
|
|
928
|
+
console.warn("[PakentoSDK] Error obteniendo info del cache", err);
|
|
921
929
|
return {
|
|
922
|
-
|
|
923
|
-
|
|
930
|
+
exists: false,
|
|
931
|
+
key: this.buildCacheKey(functionName, params)
|
|
924
932
|
};
|
|
925
|
-
|
|
926
|
-
return state;
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
var CartContext = createContext(null);
|
|
930
|
-
function CartProvider({ children }) {
|
|
931
|
-
const [state, dispatch] = useReducer(cartReducer, initialState);
|
|
932
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CartContext.Provider, { value: { state, dispatch }, children });
|
|
933
|
-
}
|
|
934
|
-
function useCart() {
|
|
935
|
-
const context = useContext(CartContext);
|
|
936
|
-
if (!context) {
|
|
937
|
-
throw new Error("useCart must be used within a CartProvider");
|
|
933
|
+
}
|
|
938
934
|
}
|
|
939
|
-
|
|
940
|
-
|
|
935
|
+
};
|
|
936
|
+
var pakentoCMSAPI = new PakentoCMSAPI();
|
|
941
937
|
// Annotate the CommonJS export names for ESM import in node:
|
|
942
938
|
0 && (module.exports = {
|
|
943
|
-
|
|
944
|
-
pakentoCMSAPI,
|
|
945
|
-
useCart
|
|
939
|
+
pakentoCMSAPI
|
|
946
940
|
});
|
|
947
941
|
//# sourceMappingURL=index.js.map
|