@elcarte/core 0.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/LICENSE +21 -0
- package/README.md +37 -0
- package/dist/components/PageBuilder.d.ts +7 -0
- package/dist/components/PageBuilder.jsx +32 -0
- package/dist/components/ProductDetail.d.ts +6 -0
- package/dist/components/ProductDetail.jsx +18 -0
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.js +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/lib/data.d.ts +2 -0
- package/dist/lib/data.js +127 -0
- package/dist/types.d.ts +41 -0
- package/dist/types.js +1 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Elcarte
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# @elcarte/core
|
|
2
|
+
|
|
3
|
+
Core SDK for Elcarte e-commerce platform - API client, hooks, middleware for Next.js.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @elcarte/core @elcarte/blocks
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
// In your Next.js app
|
|
15
|
+
import { getResolvedData, BlockRenderer } from '@elcarte/core';
|
|
16
|
+
|
|
17
|
+
export default async function Page({ params }) {
|
|
18
|
+
const data = await getResolvedData(params.slug);
|
|
19
|
+
|
|
20
|
+
if (!data) {
|
|
21
|
+
notFound();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return <BlockRenderer blocks={data.blocks} />;
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Environment Variables
|
|
29
|
+
|
|
30
|
+
```env
|
|
31
|
+
# Optional: API URL for server-side requests (default: http://127.0.0.1:8080)
|
|
32
|
+
ELCARTE_API_URL=http://127.0.0.1:8080
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## License
|
|
36
|
+
|
|
37
|
+
MIT
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
import React from 'react';
|
|
13
|
+
import { Hero, ProductGrid, TextBlock } from '@elcarte/blocks';
|
|
14
|
+
var DEFAULT_REGISTRY = {
|
|
15
|
+
hero: Hero,
|
|
16
|
+
product_grid: ProductGrid,
|
|
17
|
+
text_block: TextBlock,
|
|
18
|
+
};
|
|
19
|
+
export var PageBuilder = function (_a) {
|
|
20
|
+
var blocks = _a.blocks, _b = _a.customRegistry, customRegistry = _b === void 0 ? {} : _b;
|
|
21
|
+
var registry = __assign(__assign({}, DEFAULT_REGISTRY), customRegistry);
|
|
22
|
+
return (<>
|
|
23
|
+
{blocks.map(function (block) {
|
|
24
|
+
var Component = registry[block.type];
|
|
25
|
+
if (!Component) {
|
|
26
|
+
console.warn("Unknown block type: ".concat(block.type));
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
return <Component key={block.id} {...block.data}/>;
|
|
30
|
+
})}
|
|
31
|
+
</>);
|
|
32
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export var ProductDetail = function (_a) {
|
|
3
|
+
var data = _a.data, locale = _a.locale;
|
|
4
|
+
return (<div className="container mx-auto p-4">
|
|
5
|
+
<div className="bg-white shadow rounded-lg p-6 max-w-2xl mx-auto">
|
|
6
|
+
<h1 className="text-3xl font-bold mb-4">{data.title}</h1>
|
|
7
|
+
<p className="text-gray-600 mb-4">{data.description}</p>
|
|
8
|
+
<div className="flex items-center justify-between mt-6">
|
|
9
|
+
<span className="text-2xl font-bold text-green-600">
|
|
10
|
+
{new Intl.NumberFormat(locale === 'cs' ? 'cs-CZ' : 'en-US', { style: 'currency', currency: locale === 'cs' ? 'CZK' : 'USD' }).format(data.price)}
|
|
11
|
+
</span>
|
|
12
|
+
<button className="px-6 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 transition">
|
|
13
|
+
{locale === 'cs' ? 'Přidat do košíku' : 'Add to Cart'}
|
|
14
|
+
</button>
|
|
15
|
+
</div>
|
|
16
|
+
</div>
|
|
17
|
+
</div>);
|
|
18
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ProductDetail';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ProductDetail';
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/lib/data.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
12
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
import { headers as nextHeaders } from 'next/headers';
|
|
38
|
+
/**
|
|
39
|
+
* Get the API base URL for server-side requests.
|
|
40
|
+
*
|
|
41
|
+
* Architecture:
|
|
42
|
+
* - Nginx routes external requests to Rust backend (port 8080)
|
|
43
|
+
* - Rust backend proxies frontend requests to Next.js (port 3000)
|
|
44
|
+
* - Next.js SSR needs to call back to Rust backend for data
|
|
45
|
+
*
|
|
46
|
+
* The URL is configurable via ELCARTE_API_URL environment variable.
|
|
47
|
+
* Default: http://127.0.0.1:8080 (internal loopback, not external)
|
|
48
|
+
*/
|
|
49
|
+
function getApiBaseUrl() {
|
|
50
|
+
// Only used server-side; client-side uses relative URLs
|
|
51
|
+
return process.env.ELCARTE_API_URL || 'http://127.0.0.1:8080';
|
|
52
|
+
}
|
|
53
|
+
export function getResolvedData(slug, host) {
|
|
54
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
55
|
+
var path, endpoint, isServer, baseUrl, resolvedHost, protocol, headersList, forwardedProto, referer, _a, headers, res, error_1;
|
|
56
|
+
return __generator(this, function (_b) {
|
|
57
|
+
switch (_b.label) {
|
|
58
|
+
case 0:
|
|
59
|
+
path = slug && slug.length > 0 ? slug.join('/') : '';
|
|
60
|
+
endpoint = path ? "/data/resolve/".concat(path) : '/data/resolve';
|
|
61
|
+
_b.label = 1;
|
|
62
|
+
case 1:
|
|
63
|
+
_b.trys.push([1, 10, , 11]);
|
|
64
|
+
isServer = typeof window === 'undefined';
|
|
65
|
+
baseUrl = isServer ? getApiBaseUrl() : '';
|
|
66
|
+
resolvedHost = host;
|
|
67
|
+
protocol = 'http';
|
|
68
|
+
if (!isServer) return [3 /*break*/, 6];
|
|
69
|
+
_b.label = 2;
|
|
70
|
+
case 2:
|
|
71
|
+
_b.trys.push([2, 4, , 5]);
|
|
72
|
+
return [4 /*yield*/, nextHeaders()];
|
|
73
|
+
case 3:
|
|
74
|
+
headersList = _b.sent();
|
|
75
|
+
forwardedProto = headersList.get('x-forwarded-proto');
|
|
76
|
+
referer = headersList.get('referer');
|
|
77
|
+
if (forwardedProto && forwardedProto !== 'http') {
|
|
78
|
+
protocol = forwardedProto;
|
|
79
|
+
}
|
|
80
|
+
else if (referer) {
|
|
81
|
+
// Extract protocol from referer URL (e.g., "https://a.elcarte.test/")
|
|
82
|
+
protocol = referer.startsWith('https') ? 'https' : 'http';
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
protocol = forwardedProto || 'http';
|
|
86
|
+
}
|
|
87
|
+
if (!resolvedHost) {
|
|
88
|
+
resolvedHost = headersList.get('host') || undefined;
|
|
89
|
+
}
|
|
90
|
+
return [3 /*break*/, 5];
|
|
91
|
+
case 4:
|
|
92
|
+
_a = _b.sent();
|
|
93
|
+
return [3 /*break*/, 5];
|
|
94
|
+
case 5: return [3 /*break*/, 7];
|
|
95
|
+
case 6:
|
|
96
|
+
protocol = window.location.protocol.replace(':', '');
|
|
97
|
+
resolvedHost = resolvedHost || window.location.host;
|
|
98
|
+
_b.label = 7;
|
|
99
|
+
case 7:
|
|
100
|
+
headers = {};
|
|
101
|
+
if (resolvedHost) {
|
|
102
|
+
headers['X-Forwarded-Host'] = resolvedHost;
|
|
103
|
+
}
|
|
104
|
+
if (protocol) {
|
|
105
|
+
headers['X-Forwarded-Proto'] = protocol;
|
|
106
|
+
}
|
|
107
|
+
return [4 /*yield*/, fetch("".concat(baseUrl).concat(endpoint), {
|
|
108
|
+
next: { tags: ['pages'] },
|
|
109
|
+
headers: headers
|
|
110
|
+
})];
|
|
111
|
+
case 8:
|
|
112
|
+
res = _b.sent();
|
|
113
|
+
if (!res.ok) {
|
|
114
|
+
console.error("Failed to fetch data for ".concat(path, ": ").concat(res.status));
|
|
115
|
+
return [2 /*return*/, null];
|
|
116
|
+
}
|
|
117
|
+
return [4 /*yield*/, res.json()];
|
|
118
|
+
case 9: return [2 /*return*/, _b.sent()];
|
|
119
|
+
case 10:
|
|
120
|
+
error_1 = _b.sent();
|
|
121
|
+
console.error('Error fetching data:', error_1);
|
|
122
|
+
return [2 /*return*/, null];
|
|
123
|
+
case 11: return [2 /*return*/];
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export interface BlockData {
|
|
3
|
+
id: string;
|
|
4
|
+
type: string;
|
|
5
|
+
data: any;
|
|
6
|
+
}
|
|
7
|
+
export type Block = BlockData;
|
|
8
|
+
export interface ComponentRegistry {
|
|
9
|
+
[key: string]: React.FC<any>;
|
|
10
|
+
}
|
|
11
|
+
export interface ProductData {
|
|
12
|
+
id: number;
|
|
13
|
+
title: string;
|
|
14
|
+
price: number;
|
|
15
|
+
description: string;
|
|
16
|
+
}
|
|
17
|
+
export interface LayoutResponse {
|
|
18
|
+
header: Block[];
|
|
19
|
+
footer: Block[];
|
|
20
|
+
}
|
|
21
|
+
export type ResolveResponse = {
|
|
22
|
+
type: 'page';
|
|
23
|
+
locale: string;
|
|
24
|
+
available_languages: string[];
|
|
25
|
+
default_language: string;
|
|
26
|
+
alternate_urls: Record<string, string>;
|
|
27
|
+
layout: LayoutResponse;
|
|
28
|
+
data: {
|
|
29
|
+
page_title: string;
|
|
30
|
+
meta_description: string;
|
|
31
|
+
blocks: Block[];
|
|
32
|
+
};
|
|
33
|
+
} | {
|
|
34
|
+
type: 'product';
|
|
35
|
+
locale: string;
|
|
36
|
+
available_languages: string[];
|
|
37
|
+
default_language: string;
|
|
38
|
+
alternate_urls: Record<string, string>;
|
|
39
|
+
layout: LayoutResponse;
|
|
40
|
+
data: ProductData;
|
|
41
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@elcarte/core",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Core SDK for Elcarte e-commerce platform - API client, hooks, middleware",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"prepublishOnly": "npm run build"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/elcarte/elcarte-core.git"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/elcarte/elcarte-core#readme",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/elcarte/elcarte-core/issues"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"elcarte",
|
|
24
|
+
"nextjs",
|
|
25
|
+
"sdk",
|
|
26
|
+
"ecommerce",
|
|
27
|
+
"cms",
|
|
28
|
+
"api-client"
|
|
29
|
+
],
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"next": ">=14.0.0",
|
|
36
|
+
"react": ">=18.0.0",
|
|
37
|
+
"react-dom": ">=18.0.0"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@elcarte/blocks": "^0.0.1"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "^25.0.3",
|
|
44
|
+
"@types/react": "^19.0.0",
|
|
45
|
+
"@types/react-dom": "^19.0.0",
|
|
46
|
+
"typescript": "^5.0.0"
|
|
47
|
+
}
|
|
48
|
+
}
|