@nexpress/plugin-block-stats 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nexpress
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.
@@ -0,0 +1,5 @@
1
+ import * as _nexpress_plugin_sdk from '@nexpress/plugin-sdk';
2
+
3
+ declare const statsBlockPlugin: _nexpress_plugin_sdk.NpResolvedPlugin<Record<string, unknown>>;
4
+
5
+ export { statsBlockPlugin as default, statsBlockPlugin };
package/dist/index.js ADDED
@@ -0,0 +1,92 @@
1
+ // src/index.tsx
2
+ import { definePlugin } from "@nexpress/plugin-sdk";
3
+ import { jsx, jsxs } from "react/jsx-runtime";
4
+ function readString(value, fallback) {
5
+ return typeof value === "string" && value.trim().length > 0 ? value : fallback;
6
+ }
7
+ function formatNumber(value) {
8
+ return new Intl.NumberFormat("en-US").format(value);
9
+ }
10
+ async function StatsCounterBody({
11
+ collection,
12
+ label,
13
+ ctx
14
+ }) {
15
+ const count = await ctx.content.count(collection);
16
+ const wrapperStyle = {
17
+ display: "inline-flex",
18
+ flexDirection: "column",
19
+ gap: "0.25rem",
20
+ padding: "1.25rem 1.5rem",
21
+ margin: "1rem 0",
22
+ borderRadius: "0.75rem",
23
+ backgroundColor: "#f8fafc",
24
+ border: "1px solid #e2e8f0",
25
+ minWidth: "12rem"
26
+ };
27
+ const valueStyle = {
28
+ fontSize: "2.25rem",
29
+ fontWeight: 700,
30
+ lineHeight: 1.1,
31
+ color: "#0f172a",
32
+ fontVariantNumeric: "tabular-nums"
33
+ };
34
+ return /* @__PURE__ */ jsxs("div", { className: "np-block-stats", style: wrapperStyle, children: [
35
+ /* @__PURE__ */ jsx("span", { style: valueStyle, children: formatNumber(count) }),
36
+ /* @__PURE__ */ jsx("span", { style: { fontSize: "0.875rem", color: "#475569", textTransform: "uppercase", letterSpacing: "0.04em" }, children: label })
37
+ ] });
38
+ }
39
+ var statsCounterBlock = {
40
+ type: "stats.counter",
41
+ label: "Stats counter",
42
+ description: "Live document count for any collection (server-rendered).",
43
+ icon: "\u{1F522}",
44
+ defaultProps: {
45
+ collection: "posts",
46
+ label: "Total posts"
47
+ },
48
+ propsSchema: [
49
+ {
50
+ name: "collection",
51
+ label: "Collection",
52
+ type: "collection",
53
+ required: true,
54
+ defaultValue: "posts"
55
+ },
56
+ {
57
+ name: "label",
58
+ label: "Display label",
59
+ type: "text",
60
+ defaultValue: "Total posts"
61
+ }
62
+ ],
63
+ render: (props, _children, ctx) => {
64
+ const collection = readString(props.collection, "posts");
65
+ const label = readString(props.label, "Total posts");
66
+ if (!ctx) {
67
+ return /* @__PURE__ */ jsx("div", { className: "np-block-stats np-block-stats--no-ctx", children: /* @__PURE__ */ jsxs("span", { children: [
68
+ label,
69
+ ": data ctx unavailable"
70
+ ] }) });
71
+ }
72
+ return /* @__PURE__ */ jsx(StatsCounterBody, { collection, label, ctx });
73
+ }
74
+ };
75
+ var statsBlockPlugin = definePlugin({
76
+ manifest: {
77
+ id: "block-stats",
78
+ version: "0.1.0",
79
+ name: "Stats blocks",
80
+ description: "Adds a live stats-counter block.",
81
+ author: { name: "NexPress" },
82
+ license: "MIT",
83
+ nexpress: { minVersion: "0.1.0" }
84
+ },
85
+ blocks: [statsCounterBlock]
86
+ });
87
+ var index_default = statsBlockPlugin;
88
+ export {
89
+ index_default as default,
90
+ statsBlockPlugin
91
+ };
92
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.tsx"],"sourcesContent":["import type { CSSProperties } from \"react\";\n\nimport type { NpBlockDefinition, NpBlockRenderContext } from \"@nexpress/blocks\";\nimport { definePlugin } from \"@nexpress/plugin-sdk\";\n\nfunction readString(value: unknown, fallback: string): string {\n return typeof value === \"string\" && value.trim().length > 0 ? value : fallback;\n}\n\nfunction formatNumber(value: number): string {\n return new Intl.NumberFormat(\"en-US\").format(value);\n}\n\nasync function StatsCounterBody({\n collection,\n label,\n ctx,\n}: {\n collection: string;\n label: string;\n ctx: NpBlockRenderContext;\n}) {\n // No try/catch — `renderBlocks` wraps every block in `SafeBlock`, which\n // catches both sync throws AND awaited rejections from a `Promise<ReactElement>`\n // return value. A failed `ctx.content.count` (collection doesn't exist,\n // DB unreachable, etc.) surfaces as the framework's red error placeholder\n // in dev and a hidden empty div in prod — the page itself still ships.\n const count = await ctx.content.count(collection);\n\n const wrapperStyle: CSSProperties = {\n display: \"inline-flex\",\n flexDirection: \"column\",\n gap: \"0.25rem\",\n padding: \"1.25rem 1.5rem\",\n margin: \"1rem 0\",\n borderRadius: \"0.75rem\",\n backgroundColor: \"#f8fafc\",\n border: \"1px solid #e2e8f0\",\n minWidth: \"12rem\",\n };\n\n const valueStyle: CSSProperties = {\n fontSize: \"2.25rem\",\n fontWeight: 700,\n lineHeight: 1.1,\n color: \"#0f172a\",\n fontVariantNumeric: \"tabular-nums\",\n };\n\n return (\n <div className=\"np-block-stats\" style={wrapperStyle}>\n <span style={valueStyle}>{formatNumber(count)}</span>\n <span style={{ fontSize: \"0.875rem\", color: \"#475569\", textTransform: \"uppercase\", letterSpacing: \"0.04em\" }}>\n {label}\n </span>\n </div>\n );\n}\n\nconst statsCounterBlock: NpBlockDefinition = {\n type: \"stats.counter\",\n label: \"Stats counter\",\n description: \"Live document count for any collection (server-rendered).\",\n icon: \"🔢\",\n defaultProps: {\n collection: \"posts\",\n label: \"Total posts\",\n },\n propsSchema: [\n {\n name: \"collection\",\n label: \"Collection\",\n type: \"collection\",\n required: true,\n defaultValue: \"posts\",\n },\n {\n name: \"label\",\n label: \"Display label\",\n type: \"text\",\n defaultValue: \"Total posts\",\n },\n ],\n render: (props, _children, ctx) => {\n const collection = readString(props.collection, \"posts\");\n const label = readString(props.label, \"Total posts\");\n if (!ctx) {\n // No ctx supplied (legacy renderer call site) — fall back to a\n // static placeholder rather than throwing. Matches what the host\n // renders for unknown blocks.\n return (\n <div className=\"np-block-stats np-block-stats--no-ctx\">\n <span>{label}: data ctx unavailable</span>\n </div>\n );\n }\n return <StatsCounterBody collection={collection} label={label} ctx={ctx} />;\n },\n};\n\nexport const statsBlockPlugin = definePlugin({\n manifest: {\n id: \"block-stats\",\n version: \"0.1.0\",\n name: \"Stats blocks\",\n description: \"Adds a live stats-counter block.\",\n author: { name: \"NexPress\" },\n license: \"MIT\",\n nexpress: { minVersion: \"0.1.0\" },\n },\n blocks: [statsCounterBlock],\n});\n\nexport default statsBlockPlugin;\n"],"mappings":";AAGA,SAAS,oBAAoB;AA+CzB,SACE,KADF;AA7CJ,SAAS,WAAW,OAAgB,UAA0B;AAC5D,SAAO,OAAO,UAAU,YAAY,MAAM,KAAK,EAAE,SAAS,IAAI,QAAQ;AACxE;AAEA,SAAS,aAAa,OAAuB;AAC3C,SAAO,IAAI,KAAK,aAAa,OAAO,EAAE,OAAO,KAAK;AACpD;AAEA,eAAe,i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;AAAA,SAAsB,GACrC;AAAA,IAEJ;AACA,WAAO,oBAAC,oBAAiB,YAAwB,OAAc,KAAU;AAAA,EAC3E;AACF;AAEO,IAAM,mBAAmB,aAAa;AAAA,EAC3C,UAAU;AAAA,IACR,IAAI;AAAA,IACJ,SAAS;AAAA,IACT,MAAM;AAAA,IACN,aAAa;AAAA,IACb,QAAQ,EAAE,MAAM,WAAW;AAAA,IAC3B,SAAS;AAAA,IACT,UAAU,EAAE,YAAY,QAAQ;AAAA,EAClC;AAAA,EACA,QAAQ,CAAC,iBAAiB;AAC5B,CAAC;AAED,IAAO,gBAAQ;","names":[]}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@nexpress/plugin-block-stats",
3
+ "version": "0.1.0",
4
+ "description": "Stats counter block — live document counts for any collection. Sample plugin showcasing data-bound blocks.",
5
+ "license": "MIT",
6
+ "author": "Nexpress",
7
+ "type": "module",
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "default": "./dist/index.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "engines": {
21
+ "node": ">=20"
22
+ },
23
+ "peerDependencies": {
24
+ "react": "^19.0.0"
25
+ },
26
+ "dependencies": {
27
+ "@nexpress/blocks": "0.1.0",
28
+ "@nexpress/plugin-sdk": "0.1.0"
29
+ },
30
+ "devDependencies": {
31
+ "@types/node": "^22.0.0",
32
+ "@types/react": "^19.0.0",
33
+ "tsup": "^8.5.0",
34
+ "typescript": "^5.8.0"
35
+ },
36
+ "scripts": {
37
+ "build": "tsup",
38
+ "dev": "tsup --watch --no-clean",
39
+ "clean": "rm -rf dist",
40
+ "typecheck": "tsc --noEmit",
41
+ "lint": "eslint . --cache --cache-location node_modules/.cache/eslint"
42
+ }
43
+ }