@ecomiq/storefront 0.1.2 → 1.0.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 +211 -59
- package/dist/chunk-PCTSWSTB.js +346 -0
- package/dist/chunk-PCTSWSTB.js.map +1 -0
- package/dist/chunk-PDK2VYWD.js +1598 -0
- package/dist/chunk-PDK2VYWD.js.map +1 -0
- package/dist/chunk-Q762YXCQ.js +175 -0
- package/dist/chunk-Q762YXCQ.js.map +1 -0
- package/dist/chunks/client.d.ts +8563 -0
- package/dist/chunks/client.js +2046 -0
- package/dist/chunks/client.js.map +1 -0
- package/dist/chunks/config.js +3 -0
- package/dist/chunks/config.js.map +1 -0
- package/dist/chunks/server-nextjs.d.ts +33 -0
- package/dist/chunks/server-nextjs.js +30 -0
- package/dist/chunks/server-nextjs.js.map +1 -0
- package/dist/chunks/server-tanstack.d.ts +22 -0
- package/dist/chunks/server-tanstack.js +30 -0
- package/dist/chunks/server-tanstack.js.map +1 -0
- package/dist/ecomiq-shell-ui-SWIFLQVM.js +8 -0
- package/dist/ecomiq-shell-ui-SWIFLQVM.js.map +1 -0
- package/dist/format-currency-oJD9jGYW.d.ts +6751 -0
- package/dist/index.d.ts +20 -30
- package/dist/index.js +11 -103
- package/dist/index.js.map +1 -1
- package/dist/index.server.d.ts +8 -0
- package/dist/index.server.js +7 -0
- package/dist/index.server.js.map +1 -0
- package/dist/nextjs/api.d.ts +10 -14
- package/dist/nextjs/api.js +8 -148
- package/dist/nextjs/api.js.map +1 -1
- package/dist/nextjs.d.ts +13 -3213
- package/dist/nextjs.js +11 -1793
- package/dist/nextjs.js.map +1 -1
- package/dist/nextjs.server.d.ts +1 -0
- package/dist/nextjs.server.js +1 -0
- package/dist/nextjs.server.js.map +1 -0
- package/dist/store-proxy-handler-CNzKlTFA.d.ts +10 -0
- package/dist/tanstack/api.d.ts +41 -0
- package/dist/tanstack/api.js +28 -0
- package/dist/tanstack/api.js.map +1 -0
- package/dist/tanstack.d.ts +13 -0
- package/dist/tanstack.js +11 -0
- package/dist/tanstack.js.map +1 -0
- package/dist/tanstack.server.d.ts +1 -0
- package/dist/tanstack.server.js +1 -0
- package/dist/tanstack.server.js.map +1 -0
- package/package.json +33 -13
- package/dist/collection-anMkdWa7.d.ts +0 -4333
- package/dist/core.d.ts +0 -3396
- package/dist/core.js +0 -1239
- package/dist/core.js.map +0 -1
- package/dist/find-storefront-cms-page-DNwLo1iV.d.ts +0 -178
- package/dist/react.d.ts +0 -964
- package/dist/react.js +0 -2670
- package/dist/react.js.map +0 -1
- /package/dist/{..d.ts → chunks/config.d.ts} +0 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,30 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
declare function clearEcomiqConfigCache(): void;
|
|
22
|
-
/**
|
|
23
|
-
* Identificador de tienda para settings (API / KV).
|
|
24
|
-
* 1. `slugStore` en `ecomiq.config.*` si existe.
|
|
25
|
-
* 2. Si no, deriva del header `Host` (`resolveStorefrontSettingsHost`).
|
|
26
|
-
*/
|
|
27
|
-
declare function resolvePublicStoreKey(hostHeader: string | null | undefined, cwd?: string): string;
|
|
28
|
-
declare function hasEcomiqSlugStoreConfig(cwd?: string): boolean;
|
|
29
|
-
|
|
30
|
-
export { type EcomiqConfig, EcomiqConfigSchema, clearEcomiqConfigCache, hasEcomiqSlugStoreConfig, loadEcomiqConfig, resolvePublicStoreKey };
|
|
1
|
+
export type { EcomiqConfig } from "./chunks/config";
|
|
2
|
+
export {
|
|
3
|
+
EcomiqConfigSchema,
|
|
4
|
+
loadEcomiqConfig,
|
|
5
|
+
clearEcomiqConfigCache,
|
|
6
|
+
resolvePublicStoreKey,
|
|
7
|
+
hasEcomiqSlugStoreConfig,
|
|
8
|
+
} from "./chunks/config";
|
|
9
|
+
export {
|
|
10
|
+
EcomiqProvider,
|
|
11
|
+
useStoreProducts,
|
|
12
|
+
useStoreCollections,
|
|
13
|
+
useStorePages,
|
|
14
|
+
useStoreCart,
|
|
15
|
+
useStore,
|
|
16
|
+
useCart,
|
|
17
|
+
useStorefrontSession,
|
|
18
|
+
useStorefrontAuthClient,
|
|
19
|
+
} from "./chunks/client";
|
|
20
|
+
export type { EcomiqProviderProps } from "./chunks/client";
|
package/dist/index.js
CHANGED
|
@@ -1,103 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
let h = hostHeader.trim().toLowerCase();
|
|
13
|
-
const colon = h.indexOf(":");
|
|
14
|
-
if (colon !== -1) h = h.slice(0, colon);
|
|
15
|
-
if (h.startsWith("www.")) h = h.slice(4);
|
|
16
|
-
return h;
|
|
17
|
-
}
|
|
18
|
-
function resolveStorefrontSettingsHost(hostHeader) {
|
|
19
|
-
const host = normalizeStoreHost(hostHeader);
|
|
20
|
-
if (!host) {
|
|
21
|
-
throw Object.assign(new Error("Host inv\xE1lido para tienda"), { status: 400 });
|
|
22
|
-
}
|
|
23
|
-
if (host === ECOMIQ_APP_APEX) {
|
|
24
|
-
throw Object.assign(new Error("Host apex ecomiq.app sin slug de tienda"), { status: 400 });
|
|
25
|
-
}
|
|
26
|
-
if (ECOMIQ_APP_SUFFIX.some((suffix) => host.endsWith(suffix))) {
|
|
27
|
-
const slug = host.slice(0, -(ECOMIQ_APP_SUFFIX.find((suffix) => host.endsWith(suffix))?.length ?? 0));
|
|
28
|
-
if (!slug) {
|
|
29
|
-
throw Object.assign(new Error("Host sin slug para ecomiq.app"), { status: 400 });
|
|
30
|
-
}
|
|
31
|
-
return slug;
|
|
32
|
-
}
|
|
33
|
-
return host;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// src/core/config/ecomiq-config.ts
|
|
37
|
-
var CONFIG_FILENAMES = [
|
|
38
|
-
"ecomiq.config.js",
|
|
39
|
-
"ecomiq.config.cjs",
|
|
40
|
-
"ecomiq.config.mjs"
|
|
41
|
-
];
|
|
42
|
-
var EcomiqConfigSchema = z.object({
|
|
43
|
-
/**
|
|
44
|
-
* Slug de tienda en Ecomiq (p. ej. `mi-tienda` para `mi-tienda.ecomiq.app`).
|
|
45
|
-
* Si está definido, tiene prioridad sobre el `Host` de la request (útil en `localhost`).
|
|
46
|
-
*/
|
|
47
|
-
slugStore: z.string().min(1).optional()
|
|
48
|
-
});
|
|
49
|
-
var cachedConfig;
|
|
50
|
-
function parseExportedConfig(raw) {
|
|
51
|
-
const candidate = raw && typeof raw === "object" && "default" in raw ? raw.default : raw;
|
|
52
|
-
const parsed = EcomiqConfigSchema.safeParse(candidate);
|
|
53
|
-
if (!parsed.success) {
|
|
54
|
-
console.warn(
|
|
55
|
-
"[ecomiq] ecomiq.config.* inv\xE1lido:",
|
|
56
|
-
parsed.error.flatten().fieldErrors
|
|
57
|
-
);
|
|
58
|
-
return null;
|
|
59
|
-
}
|
|
60
|
-
const slugStore = parsed.data.slugStore?.trim();
|
|
61
|
-
return slugStore ? { slugStore } : {};
|
|
62
|
-
}
|
|
63
|
-
function loadEcomiqConfig(cwd = process.cwd()) {
|
|
64
|
-
if (cachedConfig !== void 0) return cachedConfig;
|
|
65
|
-
for (const filename of CONFIG_FILENAMES) {
|
|
66
|
-
const configPath = join(cwd, filename);
|
|
67
|
-
if (!existsSync(configPath)) continue;
|
|
68
|
-
try {
|
|
69
|
-
const requireFromApp = createRequire(join(cwd, "package.json"));
|
|
70
|
-
const loaded = requireFromApp(configPath);
|
|
71
|
-
const parsed = parseExportedConfig(loaded);
|
|
72
|
-
cachedConfig = parsed;
|
|
73
|
-
return cachedConfig;
|
|
74
|
-
} catch (error) {
|
|
75
|
-
console.warn(`[ecomiq] No se pudo cargar ${configPath}:`, error);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
cachedConfig = null;
|
|
79
|
-
return null;
|
|
80
|
-
}
|
|
81
|
-
function clearEcomiqConfigCache() {
|
|
82
|
-
cachedConfig = void 0;
|
|
83
|
-
}
|
|
84
|
-
function resolvePublicStoreKey(hostHeader, cwd) {
|
|
85
|
-
const slugFromConfig = loadEcomiqConfig(cwd)?.slugStore?.trim();
|
|
86
|
-
if (slugFromConfig) return slugFromConfig;
|
|
87
|
-
if (!hostHeader?.trim()) {
|
|
88
|
-
throw Object.assign(
|
|
89
|
-
new Error(
|
|
90
|
-
"Host is required, o define slugStore en ecomiq.config.js en la ra\xEDz del proyecto"
|
|
91
|
-
),
|
|
92
|
-
{ status: 400 }
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
return resolveStorefrontSettingsHost(hostHeader);
|
|
96
|
-
}
|
|
97
|
-
function hasEcomiqSlugStoreConfig(cwd) {
|
|
98
|
-
return Boolean(loadEcomiqConfig(cwd)?.slugStore?.trim());
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export { EcomiqConfigSchema, clearEcomiqConfigCache, hasEcomiqSlugStoreConfig, loadEcomiqConfig, resolvePublicStoreKey };
|
|
102
|
-
//# sourceMappingURL=index.js.map
|
|
103
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
export {
|
|
2
|
+
EcomiqProvider,
|
|
3
|
+
useStoreProducts,
|
|
4
|
+
useStoreCollections,
|
|
5
|
+
useStorePages,
|
|
6
|
+
useStoreCart,
|
|
7
|
+
useStore,
|
|
8
|
+
useCart,
|
|
9
|
+
useStorefrontSession,
|
|
10
|
+
useStorefrontAuthClient,
|
|
11
|
+
} from "./chunks/client.js";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"s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import { StorefrontSettingsSchema } from \"@ecomiq/storefront/core\";\nimport type { StorefrontSettings } from \"@ecomiq/storefront/core\";\n\n/** Debe coincidir con el `binding` en wrangler.jsonc (`kv_namespaces`). */\nexport const ECOMIQ_CLIENTS_STORE_KV_BINDING = \"ecomiq_clients_store\" as const;\n\nconst ECOMIQ_APP_APEX = \"ecomiq.app\";\nconst ECOMIQ_APP_SUFFIX = [`.${ECOMIQ_APP_APEX}`, `.localhost`];\n\ntype KvJsonGet = {\n get(key: string, options: { type: \"json\" }): Promise<unknown>;\n};\n\n/** Host sin puerto, minúsculas, sin `www.`. */\nexport function normalizeStoreHost(hostHeader: string): string {\n let h = hostHeader.trim().toLowerCase();\n const colon = h.indexOf(\":\");\n if (colon !== -1) h = h.slice(0, colon);\n if (h.startsWith(\"www.\")) h = h.slice(4);\n return h;\n}\n\n/**\n * Identificador de tienda a partir del `Host`: slug en `*.ecomiq.app`, FQDN en dominio propio.\n * Misma semántica que el sufijo de `buildStorefrontKvSnapshotKey` (sin el prefijo KV).\n */\nexport function resolveStorefrontSettingsHost(hostHeader: string): string {\n const host = normalizeStoreHost(hostHeader);\n if (!host) {\n throw Object.assign(new Error(\"Host inválido para tienda\"), { status: 400 });\n }\n if (host === ECOMIQ_APP_APEX) {\n throw Object.assign(new Error(\"Host apex ecomiq.app sin slug de tienda\"), { status: 400 });\n }\n if (ECOMIQ_APP_SUFFIX.some(suffix => host.endsWith(suffix))) {\n const slug = host.slice(0, -(ECOMIQ_APP_SUFFIX.find(suffix => host.endsWith(suffix))?.length ?? 0));\n if (!slug) {\n throw Object.assign(new Error(\"Host sin slug para ecomiq.app\"), { status: 400 });\n }\n return slug;\n }\n return host;\n}\n\n/**\n * Clave KV del snapshot de tienda.\n * - `*.ecomiq.app` → `storefront:snapshots:store:<slug>` (todo lo que va antes de `.ecomiq.app`, p. ej. `plata950`).\n * - Dominio propio (p. ej. `plata950.com`) → `storefront:snapshots:store:<dominio completo>`.\n */\nexport function buildStorefrontKvSnapshotKeyFromStoreKey(storeKey: string): string {\n const key = storeKey.trim();\n if (!key) {\n throw Object.assign(new Error(\"Store key vacío para KV\"), { status: 400 });\n }\n return `storefront:snapshots:store:${key}`;\n}\n\nexport function buildStorefrontKvSnapshotKey(hostHeader: string): string {\n return buildStorefrontKvSnapshotKeyFromStoreKey(\n resolveStorefrontSettingsHost(hostHeader),\n );\n}\n\nfunction getKvNamespace(env: unknown): KvJsonGet | undefined {\n if (!env || typeof env !== \"object\") return undefined;\n const ns = (env as Record<string, unknown>)[ECOMIQ_CLIENTS_STORE_KV_BINDING];\n if (!ns || typeof ns !== \"object\" || typeof (ns as KvJsonGet).get !== \"function\") {\n return undefined;\n }\n return ns as KvJsonGet;\n}\n\n/**\n * Lee settings desde KV con clave ya resuelta (slug o dominio).\n */\nexport async function getStorefrontSettingsFromKvByStoreKey(\n storeKey: string,\n): Promise<StorefrontSettings> {\n const { env } = await getCloudflareContext({ async: true });\n const kv = getKvNamespace(env);\n if (!kv) {\n const err = new Error(\n `Binding KV \"${ECOMIQ_CLIENTS_STORE_KV_BINDING}\" no encontrado. Configura kv_namespaces en wrangler.`\n );\n throw Object.assign(err, { status: 500 });\n }\n\n const cacheKey = buildStorefrontKvSnapshotKeyFromStoreKey(storeKey);\n const raw = await kv.get(cacheKey, { type: \"json\" });\n if (raw == null) {\n const err = new Error(\"Store settings not found in KV\");\n throw Object.assign(err, { status: 404 });\n }\n\n return StorefrontSettingsSchema.parse(raw);\n}\n\n/**\n * Lee settings desde KV (`ecomiq_clients_store`) usando la clave según el host.\n */\nexport async function getStorefrontSettingsFromKv(hostHeader: string): Promise<StorefrontSettings> {\n return getStorefrontSettingsFromKvByStoreKey(\n resolveStorefrontSettingsHost(hostHeader),\n );\n}\n","import { existsSync } from \"node:fs\";\nimport { createRequire } from \"node:module\";\nimport { join } from \"node:path\";\nimport { z } from \"zod\";\nimport { resolveStorefrontSettingsHost } from \"../client/storefront-settings-kv.service\";\n\nconst CONFIG_FILENAMES = [\n \"ecomiq.config.js\",\n \"ecomiq.config.cjs\",\n \"ecomiq.config.mjs\",\n] as const;\n\nexport const EcomiqConfigSchema = z.object({\n /**\n * Slug de tienda en Ecomiq (p. ej. `mi-tienda` para `mi-tienda.ecomiq.app`).\n * Si está definido, tiene prioridad sobre el `Host` de la request (útil en `localhost`).\n */\n slugStore: z.string().min(1).optional(),\n});\n\nexport type EcomiqConfig = z.infer<typeof EcomiqConfigSchema>;\n\nlet cachedConfig: EcomiqConfig | null | undefined;\n\nfunction parseExportedConfig(raw: unknown): EcomiqConfig | null {\n const candidate =\n raw && typeof raw === \"object\" && \"default\" in (raw as object)\n ? (raw as { default: unknown }).default\n : raw;\n const parsed = EcomiqConfigSchema.safeParse(candidate);\n if (!parsed.success) {\n console.warn(\n \"[ecomiq] ecomiq.config.* inválido:\",\n parsed.error.flatten().fieldErrors,\n );\n return null;\n }\n const slugStore = parsed.data.slugStore?.trim();\n return slugStore ? { slugStore } : {};\n}\n\n/**\n * Carga `ecomiq.config.js` (o `.cjs` / `.mjs`) desde la raíz del proyecto (`process.cwd()`).\n * Resultado en caché por proceso.\n */\nexport function loadEcomiqConfig(cwd = process.cwd()): EcomiqConfig | null {\n if (cachedConfig !== undefined) return cachedConfig;\n\n for (const filename of CONFIG_FILENAMES) {\n const configPath = join(cwd, filename);\n if (!existsSync(configPath)) continue;\n\n try {\n const requireFromApp = createRequire(join(cwd, \"package.json\"));\n const loaded = requireFromApp(configPath) as unknown;\n const parsed = parseExportedConfig(loaded);\n cachedConfig = parsed;\n return cachedConfig;\n } catch (error) {\n console.warn(`[ecomiq] No se pudo cargar ${configPath}:`, error);\n }\n }\n\n cachedConfig = null;\n return null;\n}\n\n/** Limpia la caché (tests o recarga en dev). */\nexport function clearEcomiqConfigCache(): void {\n cachedConfig = undefined;\n}\n\n/**\n * Identificador de tienda para settings (API / KV).\n * 1. `slugStore` en `ecomiq.config.*` si existe.\n * 2. Si no, deriva del header `Host` (`resolveStorefrontSettingsHost`).\n */\nexport function resolvePublicStoreKey(\n hostHeader: string | null | undefined,\n cwd?: string,\n): string {\n const slugFromConfig = loadEcomiqConfig(cwd)?.slugStore?.trim();\n if (slugFromConfig) return slugFromConfig;\n\n if (!hostHeader?.trim()) {\n throw Object.assign(\n new Error(\n \"Host is required, o define slugStore en ecomiq.config.js en la raíz del proyecto\",\n ),\n { status: 400 },\n );\n }\n\n return resolveStorefrontSettingsHost(hostHeader);\n}\n\nexport function hasEcomiqSlugStoreConfig(cwd?: string): boolean {\n return Boolean(loadEcomiqConfig(cwd)?.slugStore?.trim());\n}\n"]}
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
package/dist/nextjs/api.d.ts
CHANGED
|
@@ -1,24 +1,20 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
import { S as StoreProxyHandlerConfig } from '../store-proxy-handler-CNzKlTFA.js';
|
|
2
3
|
|
|
3
|
-
type StoreApiHandlerConfig =
|
|
4
|
-
/**
|
|
5
|
-
* Base URL del API de storefront. Default: "https://storefront.ecomiq.pe"
|
|
6
|
-
*/
|
|
7
|
-
apiBaseUrl?: string;
|
|
8
|
-
};
|
|
4
|
+
type StoreApiHandlerConfig = StoreProxyHandlerConfig;
|
|
9
5
|
/**
|
|
10
6
|
* Crea los handlers de ruta para montar la API de store como proxy en Next.js App Router.
|
|
11
|
-
* La base /v1/storefront se maneja internamente: el usuario llama /api/store/products
|
|
12
|
-
* y se hace proxy a https://storefront.ecomiq.pe/v1/storefront/products.
|
|
13
7
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* // app/api/store/[...path]/route.ts
|
|
10
|
+
* import { toNextJsStoreApiHandler } from "@ecomiq/storefront/nextjs/api";
|
|
16
11
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
12
|
+
* export const { GET, POST, PUT, PATCH, DELETE } = toNextJsStoreApiHandler({
|
|
13
|
+
* apiBaseUrl: process.env.STOREFRONT_API_URL,
|
|
14
|
+
* });
|
|
15
|
+
* ```
|
|
20
16
|
*/
|
|
21
|
-
declare function toNextJsStoreApiHandler(config
|
|
17
|
+
declare function toNextJsStoreApiHandler(config?: StoreApiHandlerConfig): {
|
|
22
18
|
GET: (request: NextRequest, context: {
|
|
23
19
|
params: Promise<{
|
|
24
20
|
path?: string[];
|
package/dist/nextjs/api.js
CHANGED
|
@@ -1,159 +1,19 @@
|
|
|
1
|
+
import { handleStoreProxyRequest } from '../chunk-Q762YXCQ.js';
|
|
1
2
|
import { NextResponse } from 'next/server';
|
|
2
|
-
import { STOREFRONT_ECOMIQ_OAUTH_COOKIE_NAME, STOREFRONT_API_PATH_PREFIX } from '@ecomiq/storefront/core';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
var DEFAULT_API_BASE_URL = "https://storefront.ecomiq.pe";
|
|
6
|
-
async function ensureStoreContext(request) {
|
|
7
|
-
const xStoreId = request.headers.get("x-store-id")?.trim();
|
|
8
|
-
const xStoreDomain = request.headers.get("x-store-domain")?.trim();
|
|
9
|
-
if (xStoreDomain) {
|
|
10
|
-
return {
|
|
11
|
-
ok: true,
|
|
12
|
-
storeId: xStoreId || "",
|
|
13
|
-
storeDomain: xStoreDomain || null
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
if (!xStoreId) {
|
|
17
|
-
return {
|
|
18
|
-
ok: false,
|
|
19
|
-
response: NextResponse.json({ error: "Store context required", message: "X-Store-Id is required", status: 400 })
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
return {
|
|
23
|
-
ok: true,
|
|
24
|
-
storeId: xStoreId || "",
|
|
25
|
-
storeDomain: xStoreDomain || null
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
var SKIP_FORWARD_HEADERS = /* @__PURE__ */ new Set([
|
|
29
|
-
"host",
|
|
30
|
-
"connection",
|
|
31
|
-
"content-length",
|
|
32
|
-
"transfer-encoding",
|
|
33
|
-
"x-store-id",
|
|
34
|
-
"x-store-domain",
|
|
35
|
-
/** Se reconstruye abajo desde el header entrante al proxy. */
|
|
36
|
-
"authorization",
|
|
37
|
-
/** Nunca reenviar sesión/cookies del storefront al API Ecomiq (solo headers operativos). */
|
|
38
|
-
"cookie",
|
|
39
|
-
"cookie2",
|
|
40
|
-
"set-cookie",
|
|
41
|
-
/**
|
|
42
|
-
* Headers de proxy/origen de Cloudflare: se reconstruyen abajo (X-Forwarded-*)
|
|
43
|
-
* para no contaminar la cadena con valores que el upstream no debe leer directamente
|
|
44
|
-
* y para garantizar que la IP del cliente final llegue como X-Forwarded-For.
|
|
45
|
-
*/
|
|
46
|
-
"x-forwarded-for",
|
|
47
|
-
"x-forwarded-proto",
|
|
48
|
-
"x-forwarded-host",
|
|
49
|
-
"x-real-ip",
|
|
50
|
-
"forwarded",
|
|
51
|
-
"cf-connecting-ip",
|
|
52
|
-
"cf-connecting-ipv6",
|
|
53
|
-
"true-client-ip",
|
|
54
|
-
"cf-ipcountry",
|
|
55
|
-
"cf-ray",
|
|
56
|
-
"cf-visitor"
|
|
57
|
-
]);
|
|
58
|
-
function getClientIp(request) {
|
|
59
|
-
const candidates = [
|
|
60
|
-
request.headers.get("cf-connecting-ip"),
|
|
61
|
-
request.headers.get("true-client-ip"),
|
|
62
|
-
request.headers.get("x-real-ip")
|
|
63
|
-
];
|
|
64
|
-
for (const value of candidates) {
|
|
65
|
-
const trimmed = value?.trim();
|
|
66
|
-
if (trimmed) return trimmed;
|
|
67
|
-
}
|
|
68
|
-
const xff = request.headers.get("x-forwarded-for");
|
|
69
|
-
if (xff?.trim()) {
|
|
70
|
-
const first = xff.split(",")[0]?.trim();
|
|
71
|
-
if (first) return first;
|
|
72
|
-
}
|
|
73
|
-
return null;
|
|
74
|
-
}
|
|
75
|
-
function buildUpstreamRequest(request, pathSegments, config, storeId, storeDomain) {
|
|
76
|
-
const base = (config.apiBaseUrl ?? DEFAULT_API_BASE_URL).replace(/\/$/, "");
|
|
77
|
-
const userPath = pathSegments.length ? pathSegments.join("/") : "";
|
|
78
|
-
const path = userPath ? `${STOREFRONT_API_PATH_PREFIX}/${userPath}` : STOREFRONT_API_PATH_PREFIX;
|
|
79
|
-
const search = request.nextUrl.searchParams.toString();
|
|
80
|
-
const url = search ? `${base}/${path}?${search}` : `${base}/${path}`;
|
|
81
|
-
const headers = new Headers();
|
|
82
|
-
request.headers.forEach((value, key) => {
|
|
83
|
-
const lower = key.toLowerCase();
|
|
84
|
-
if (!SKIP_FORWARD_HEADERS.has(lower)) {
|
|
85
|
-
headers.set(key, value);
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
headers.set("X-Store-Id", storeId);
|
|
89
|
-
if (storeDomain) {
|
|
90
|
-
headers.set("X-Store-domain", storeDomain);
|
|
91
|
-
}
|
|
92
|
-
const clientIp = getClientIp(request);
|
|
93
|
-
if (clientIp) {
|
|
94
|
-
headers.set("X-Forwarded-For", clientIp);
|
|
95
|
-
}
|
|
96
|
-
const forwardedProto = request.headers.get("cf-visitor")?.match(/"scheme":"(https?)"/)?.[1] ?? request.nextUrl.protocol.replace(":", "") ?? "https";
|
|
97
|
-
headers.set("X-Forwarded-Proto", forwardedProto);
|
|
98
|
-
const forwardedHost = request.headers.get("host") ?? request.nextUrl.host;
|
|
99
|
-
if (forwardedHost) {
|
|
100
|
-
headers.set("X-Forwarded-Host", forwardedHost);
|
|
101
|
-
}
|
|
102
|
-
const incomingAuth = request.headers.get("authorization")?.trim();
|
|
103
|
-
const storefrontOAuth = request.cookies.get(STOREFRONT_ECOMIQ_OAUTH_COOKIE_NAME)?.value?.trim();
|
|
104
|
-
if (incomingAuth && /^Bearer\s+\S+/i.test(incomingAuth)) {
|
|
105
|
-
headers.set("Authorization", incomingAuth);
|
|
106
|
-
} else if (storefrontOAuth) {
|
|
107
|
-
headers.set("Authorization", `Bearer ${storefrontOAuth}`);
|
|
108
|
-
}
|
|
109
|
-
headers.set("Accept-Encoding", "identity");
|
|
110
|
-
const init = {
|
|
111
|
-
method: request.method,
|
|
112
|
-
headers
|
|
113
|
-
};
|
|
114
|
-
const bodyMethods = ["POST", "PUT", "PATCH"];
|
|
115
|
-
if (bodyMethods.includes(request.method) && request.body) {
|
|
116
|
-
init.body = request.body;
|
|
117
|
-
init.duplex = "half";
|
|
118
|
-
}
|
|
119
|
-
return { url, init };
|
|
120
|
-
}
|
|
121
|
-
function isResponseBodyForbidden(status) {
|
|
122
|
-
return status === 204 || status === 205 || status === 304;
|
|
123
|
-
}
|
|
124
|
-
function toNextJsStoreApiHandler(config) {
|
|
4
|
+
function toNextJsStoreApiHandler(config = {}) {
|
|
125
5
|
const handler = async (request, context) => {
|
|
126
6
|
const params = await context.params;
|
|
127
7
|
const pathSegments = params.path ?? [];
|
|
128
|
-
const
|
|
129
|
-
if (!storeContext.ok) {
|
|
130
|
-
return storeContext.response;
|
|
131
|
-
}
|
|
132
|
-
const { storeId, storeDomain } = storeContext;
|
|
133
|
-
const { url, init } = buildUpstreamRequest(
|
|
8
|
+
const response = await handleStoreProxyRequest(
|
|
134
9
|
request,
|
|
135
10
|
pathSegments,
|
|
136
|
-
config
|
|
137
|
-
storeId,
|
|
138
|
-
storeDomain
|
|
11
|
+
config
|
|
139
12
|
);
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
responseHeaders.delete("content-length");
|
|
145
|
-
responseHeaders.delete("content-type");
|
|
146
|
-
return new NextResponse(null, {
|
|
147
|
-
status,
|
|
148
|
-
statusText: upstream.statusText,
|
|
149
|
-
headers: responseHeaders
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
const body = await upstream.text();
|
|
153
|
-
return new NextResponse(body, {
|
|
154
|
-
status,
|
|
155
|
-
statusText: upstream.statusText,
|
|
156
|
-
headers: responseHeaders
|
|
13
|
+
return new NextResponse(response.body, {
|
|
14
|
+
status: response.status,
|
|
15
|
+
statusText: response.statusText,
|
|
16
|
+
headers: response.headers
|
|
157
17
|
});
|
|
158
18
|
};
|
|
159
19
|
return {
|
package/dist/nextjs/api.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/react/nextjs/api/store-handler.ts"],"names":[],"mappings":";;;;AAOA,IAAM,oBAAA,GAAuB,8BAAA;AAiB7B,eAAe,mBAAmB,OAAA,EAAmD;AACnF,EAAA,MAAM,WAAW,OAAA,CAAQ,OAAA,CAAQ,GAAA,CAAI,YAAY,GAAG,IAAA,EAAK;AACzD,EAAA,MAAM,eAAe,OAAA,CAAQ,OAAA,CAAQ,GAAA,CAAI,gBAAgB,GAAG,IAAA,EAAK;AACjE,EAAA,IAAI,YAAA,EAAc;AAChB,IAAA,OAAO;AAAA,MACL,EAAA,EAAI,IAAA;AAAA,MACJ,SAAS,QAAA,IAAY,EAAA;AAAA,MACrB,aAAa,YAAA,IAAgB;AAAA,KAC/B;AAAA,EACF;AACA,EAAA,IAAI,CAAC,QAAA,EAAU;AACb,IAAA,OAAO;AAAA,MACL,EAAA,EAAI,KAAA;AAAA,MACJ,QAAA,EAAU,YAAA,CAAa,IAAA,CAAK,EAAE,KAAA,EAAO,0BAA0B,OAAA,EAAS,wBAAA,EAA0B,MAAA,EAAQ,GAAA,EAAK;AAAA,KACjH;AAAA,EACF;AAEA,EAAA,OAAO;AAAA,IACL,EAAA,EAAI,IAAA;AAAA,IACJ,SAAS,QAAA,IAAY,EAAA;AAAA,IACrB,aAAa,YAAA,IAAgB;AAAA,GAC/B;AACF;AAQA,IAAM,oBAAA,uBAA2B,GAAA,CAAI;AAAA,EACnC,MAAA;AAAA,EACA,YAAA;AAAA,EACA,gBAAA;AAAA,EACA,mBAAA;AAAA,EACA,YAAA;AAAA,EACA,gBAAA;AAAA;AAAA,EAEA,eAAA;AAAA;AAAA,EAEA,QAAA;AAAA,EACA,SAAA;AAAA,EACA,YAAA;AAAA;AAAA;AAAA;AAAA;AA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AAA;AAAA,EACH,CAAA;AAEA,EAAA,OAAO;AAAA,IACL,GAAA,EAAK,OAAA;AAAA,IACL,IAAA,EAAM,OAAA;AAAA,IACN,GAAA,EAAK,OAAA;AAAA,IACL,KAAA,EAAO,OAAA;AAAA,IACP,MAAA,EAAQ;AAAA,GACV;AACF","file":"api.js","sourcesContent":["import type { NextRequest } from \"next/server\";\nimport { NextResponse } from \"next/server\";\nimport {\n STOREFRONT_API_PATH_PREFIX,\n STOREFRONT_ECOMIQ_OAUTH_COOKIE_NAME,\n} from \"@ecomiq/storefront/core\";\n\nconst DEFAULT_API_BASE_URL = \"https://storefront.ecomiq.pe\";\n\nexport type StoreApiHandlerConfig = {\n /**\n * Base URL del API de storefront. Default: \"https://storefront.ecomiq.pe\"\n */\n apiBaseUrl?: string;\n};\n\ntype StoreContextResult =\n | {\n ok: true;\n storeId: string;\n storeDomain: string | null;\n }\n | { ok: false; response: NextResponse };\n\nasync function ensureStoreContext(request: NextRequest): Promise<StoreContextResult> {\n const xStoreId = request.headers.get(\"x-store-id\")?.trim();\n const xStoreDomain = request.headers.get(\"x-store-domain\")?.trim();\n if (xStoreDomain) {\n return {\n ok: true,\n storeId: xStoreId || \"\",\n storeDomain: xStoreDomain || null,\n };\n }\n if (!xStoreId) {\n return {\n ok: false,\n response: NextResponse.json({ error: \"Store context required\", message: \"X-Store-Id is required\", status: 400 }),\n };\n }\n\n return {\n ok: true,\n storeId: xStoreId || \"\",\n storeDomain: xStoreDomain || null,\n };\n}\n\n/**\n * Headers que no se reenvían al upstream Ecomiq.\n * La API remota no usa cookies del navegador para el contexto de tienda: toda petición al proxy debe traer\n * `X-Store-Id`. `X-Store-Domain` es opcional para reenvío al upstream. El token sale de `Authorization` /\n * cookie OAuth leída en el proxy.\n */\nconst SKIP_FORWARD_HEADERS = new Set([\n \"host\",\n \"connection\",\n \"content-length\",\n \"transfer-encoding\",\n \"x-store-id\",\n \"x-store-domain\",\n /** Se reconstruye abajo desde el header entrante al proxy. */\n \"authorization\",\n /** Nunca reenviar sesión/cookies del storefront al API Ecomiq (solo headers operativos). */\n \"cookie\",\n \"cookie2\",\n \"set-cookie\",\n /**\n * Headers de proxy/origen de Cloudflare: se reconstruyen abajo (X-Forwarded-*)\n * para no contaminar la cadena con valores que el upstream no debe leer directamente\n * y para garantizar que la IP del cliente final llegue como X-Forwarded-For.\n */\n \"x-forwarded-for\",\n \"x-forwarded-proto\",\n \"x-forwarded-host\",\n \"x-real-ip\",\n \"forwarded\",\n \"cf-connecting-ip\",\n \"cf-connecting-ipv6\",\n \"true-client-ip\",\n \"cf-ipcountry\",\n \"cf-ray\",\n \"cf-visitor\",\n]);\n\n/**\n * Obtiene la IP real del cliente (no la IP del worker/edge de Cloudflare).\n * En Cloudflare Workers, `cf-connecting-ip` siempre es la IP del cliente final.\n */\nfunction getClientIp(request: NextRequest): string | null {\n const candidates = [\n request.headers.get(\"cf-connecting-ip\"),\n request.headers.get(\"true-client-ip\"),\n request.headers.get(\"x-real-ip\"),\n ];\n for (const value of candidates) {\n const trimmed = value?.trim();\n if (trimmed) return trimmed;\n }\n const xff = request.headers.get(\"x-forwarded-for\");\n if (xff?.trim()) {\n const first = xff.split(\",\")[0]?.trim();\n if (first) return first;\n }\n return null;\n}\n\nfunction buildUpstreamRequest(\n request: NextRequest,\n pathSegments: string[],\n config: StoreApiHandlerConfig,\n storeId: string,\n storeDomain: string | null\n): { url: string; init: RequestInit } {\n const base = (config.apiBaseUrl ?? DEFAULT_API_BASE_URL).replace(/\\/$/, \"\");\n const userPath = pathSegments.length ? pathSegments.join(\"/\") : \"\";\n const path = userPath\n ? `${STOREFRONT_API_PATH_PREFIX}/${userPath}`\n : STOREFRONT_API_PATH_PREFIX;\n const search = request.nextUrl.searchParams.toString();\n const url = search ? `${base}/${path}?${search}` : `${base}/${path}`;\n\n const headers = new Headers();\n request.headers.forEach((value, key) => {\n const lower = key.toLowerCase();\n if (!SKIP_FORWARD_HEADERS.has(lower)) {\n headers.set(key, value);\n }\n });\n headers.set(\"X-Store-Id\", storeId);\n if (storeDomain) {\n headers.set(\"X-Store-domain\", storeDomain);\n }\n\n // Reenvío de IP/origen del cliente final hacia apiecomiq.\n // Sin esto, todas las requests llegan con la IP del worker de Cloudflare\n // y caen en el rate limit del backend (.NET ForwardedHeaders middleware\n // lee X-Forwarded-For / -Proto / -Host).\n const clientIp = getClientIp(request);\n if (clientIp) {\n headers.set(\"X-Forwarded-For\", clientIp);\n }\n const forwardedProto =\n request.headers.get(\"cf-visitor\")?.match(/\"scheme\":\"(https?)\"/)?.[1] ??\n request.nextUrl.protocol.replace(\":\", \"\") ??\n \"https\";\n headers.set(\"X-Forwarded-Proto\", forwardedProto);\n const forwardedHost =\n request.headers.get(\"host\") ?? request.nextUrl.host;\n if (forwardedHost) {\n headers.set(\"X-Forwarded-Host\", forwardedHost);\n }\n\n const incomingAuth = request.headers.get(\"authorization\")?.trim();\n const storefrontOAuth =\n request.cookies.get(STOREFRONT_ECOMIQ_OAUTH_COOKIE_NAME)?.value?.trim();\n\n if (incomingAuth && /^Bearer\\s+\\S+/i.test(incomingAuth)) {\n headers.set(\"Authorization\", incomingAuth);\n } else if (storefrontOAuth) {\n headers.set(\"Authorization\", `Bearer ${storefrontOAuth}`);\n }\n headers.set(\"Accept-Encoding\", \"identity\");\n\n const init: RequestInit & { duplex?: \"half\" } = {\n method: request.method,\n headers,\n };\n\n const bodyMethods = [\"POST\", \"PUT\", \"PATCH\"];\n if (bodyMethods.includes(request.method) && request.body) {\n init.body = request.body;\n init.duplex = \"half\";\n }\n\n return { url, init };\n}\n\n/** Códigos HTTP que no pueden llevar cuerpo (Fetch / NextResponse). */\nfunction isResponseBodyForbidden(status: number): boolean {\n return status === 204 || status === 205 || status === 304;\n}\n\n/**\n * Crea los handlers de ruta para montar la API de store como proxy en Next.js App Router.\n * La base /v1/storefront se maneja internamente: el usuario llama /api/store/products\n * y se hace proxy a https://storefront.ecomiq.pe/v1/storefront/products.\n *\n * // app/api/store/[...path]/route.ts\n * import { toNextJsStoreApiHandler } from \"@ecomiq/storefront/nextjs/api\";\n *\n * export const { GET, POST, PUT, PATCH, DELETE } = toNextJsStoreApiHandler({\n * apiBaseUrl: \"https://storefront.ecomiq.pe\", // opcional, es el default\n * });\n */\nexport function toNextJsStoreApiHandler(config: StoreApiHandlerConfig) {\n const handler = async (\n request: NextRequest,\n context: { params: Promise<{ path?: string[] }> }\n ): Promise<NextResponse> => {\n const params = await context.params;\n const pathSegments = params.path ?? [];\n\n const storeContext = await ensureStoreContext(request);\n if (!storeContext.ok) {\n return storeContext.response;\n }\n\n const { storeId, storeDomain } = storeContext;\n const { url, init } = buildUpstreamRequest(\n request,\n pathSegments,\n config,\n storeId,\n storeDomain\n );\n\n const upstream = await fetch(url, init);\n const responseHeaders = new Headers(upstream.headers);\n const status = upstream.status;\n\n if (isResponseBodyForbidden(status)) {\n responseHeaders.delete(\"content-length\");\n responseHeaders.delete(\"content-type\");\n return new NextResponse(null, {\n status,\n statusText: upstream.statusText,\n headers: responseHeaders,\n });\n }\n\n const body = await upstream.text();\n return new NextResponse(body, {\n status,\n statusText: upstream.statusText,\n headers: responseHeaders,\n });\n };\n\n return {\n GET: handler,\n POST: handler,\n PUT: handler,\n PATCH: handler,\n DELETE: handler,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/react/frameworks/nextjs/api/store-handler.ts"],"names":[],"mappings":";;;AAqBO,SAAS,uBAAA,CAAwB,MAAA,GAAgC,EAAC,EAAG;AAC1E,EAAA,MAAM,OAAA,GAAU,OACd,OAAA,EACA,OAAA,KAC0B;AAC1B,IAAA,MAAM,MAAA,GAAS,MAAM,OAAA,CAAQ,MAAA;AAC7B,IAAA,MAAM,YAAA,GAAe,MAAA,CAAO,IAAA,IAAQ,EAAC;AACrC,IAAA,MAAM,WAAW,MAAM,uBAAA;AAAA,MACrB,OAAA;AAAA,MACA,YAAA;AAAA,MACA;AAAA,KACF;AACA,IAAA,OAAO,IAAI,YAAA,CAAa,QAAA,CAAS,IAAA,EAAM;AAAA,MACrC,QAAQ,QAAA,CAAS,MAAA;AAAA,MACjB,YAAY,QAAA,CAAS,UAAA;AAAA,MACrB,SAAS,QAAA,CAAS;AAAA,KACnB,CAAA;AAAA,EACH,CAAA;AAEA,EAAA,OAAO;AAAA,IACL,GAAA,EAAK,OAAA;AAAA,IACL,IAAA,EAAM,OAAA;AAAA,IACN,GAAA,EAAK,OAAA;AAAA,IACL,KAAA,EAAO,OAAA;AAAA,IACP,MAAA,EAAQ;AAAA,GACV;AACF","file":"api.js","sourcesContent":["import type { NextRequest } from \"next/server\";\nimport { NextResponse } from \"next/server\";\nimport {\n handleStoreProxyRequest,\n type StoreProxyHandlerConfig,\n} from \"../../../../core/server/store-proxy-handler\";\n\nexport type StoreApiHandlerConfig = StoreProxyHandlerConfig;\n\n/**\n * Crea los handlers de ruta para montar la API de store como proxy en Next.js App Router.\n *\n * ```ts\n * // app/api/store/[...path]/route.ts\n * import { toNextJsStoreApiHandler } from \"@ecomiq/storefront/nextjs/api\";\n *\n * export const { GET, POST, PUT, PATCH, DELETE } = toNextJsStoreApiHandler({\n * apiBaseUrl: process.env.STOREFRONT_API_URL,\n * });\n * ```\n */\nexport function toNextJsStoreApiHandler(config: StoreApiHandlerConfig = {}) {\n const handler = async (\n request: NextRequest,\n context: { params: Promise<{ path?: string[] }> },\n ): Promise<NextResponse> => {\n const params = await context.params;\n const pathSegments = params.path ?? [];\n const response = await handleStoreProxyRequest(\n request,\n pathSegments,\n config,\n );\n return new NextResponse(response.body, {\n status: response.status,\n statusText: response.statusText,\n headers: response.headers,\n });\n };\n\n return {\n GET: handler,\n POST: handler,\n PUT: handler,\n PATCH: handler,\n DELETE: handler,\n };\n}\n\nexport { handleStoreProxyRequest } from \"../../../../core/server/store-proxy-handler\";\n"]}
|