@fragno-dev/example-fragment 0.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.
@@ -0,0 +1,21 @@
1
+ $ tsdown
2
+ ℹ tsdown v0.15.5 powered by rolldown v1.0.0-beta.40
3
+ rollup
4
+ ℹ Using tsdown config: /home/runner/work/fragno/fragno/packages/example-fragment/tsdown.config.ts
5
+ ℹ entry: src/index.ts
6
+ ℹ tsconfig: tsconfig.json
7
+ ℹ entry: src/index.ts
8
+ ℹ tsconfig: tsconfig.json
9
+ ℹ Build start
10
+ ℹ dist/browser/index.js 1.58 kB │ gzip: 0.64 kB
11
+ ℹ dist/browser/index.js.map 5.66 kB │ gzip: 1.93 kB
12
+ ℹ dist/browser/index.d.ts.map 0.51 kB │ gzip: 0.26 kB
13
+ ℹ dist/browser/index.d.ts 1.82 kB │ gzip: 0.62 kB
14
+ ℹ 4 files, total: 9.57 kB
15
+ ℹ dist/node/index.js 2.90 kB │ gzip: 1.15 kB
16
+ ℹ dist/node/index.js.map 5.69 kB │ gzip: 1.96 kB
17
+ ℹ dist/node/index.d.ts.map 0.51 kB │ gzip: 0.26 kB
18
+ ℹ dist/node/index.d.ts 1.82 kB │ gzip: 0.62 kB
19
+ ℹ 4 files, total: 10.93 kB
20
+ ✔ Build complete in 11747ms
21
+ ✔ Build complete in 11755ms
@@ -0,0 +1 @@
1
+ $ tsc --noEmit
package/CHANGELOG.md ADDED
@@ -0,0 +1,29 @@
1
+ # @fragno-dev/example-fragment
2
+
3
+ ## 0.0.5
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [b9450b1]
8
+ - @fragno-dev/core@0.0.5
9
+
10
+ ## 0.0.4
11
+
12
+ ### Patch Changes
13
+
14
+ - Updated dependencies [1dae6f9]
15
+ - @fragno-dev/core@0.0.4
16
+
17
+ ## 0.0.3
18
+
19
+ ### Patch Changes
20
+
21
+ - Updated dependencies [604caff]
22
+ - @fragno-dev/core@0.0.3
23
+
24
+ ## 0.0.2
25
+
26
+ ### Patch Changes
27
+
28
+ - Updated dependencies [2052919]
29
+ - @fragno-dev/core@0.0.2
@@ -0,0 +1,31 @@
1
+ import * as _fragno_dev_core0 from "@fragno-dev/core";
2
+ import { FragnoPublicClientConfig, FragnoPublicConfig } from "@fragno-dev/core";
3
+ import * as _fragno_dev_core_client0 from "@fragno-dev/core/client";
4
+ import { z } from "zod";
5
+ import * as _fragno_dev_core_api0 from "@fragno-dev/core/api";
6
+ import { FragnoRouteConfig } from "@fragno-dev/core/api";
7
+
8
+ //#region src/index.d.ts
9
+ interface ExampleFragmentServerConfig {
10
+ initialData?: string;
11
+ }
12
+ declare function createExampleFragment(serverConfig?: ExampleFragmentServerConfig, fragnoConfig?: FragnoPublicConfig): _fragno_dev_core0.FragnoInstantiatedFragment<[_fragno_dev_core0.FragnoRouteConfig<"GET", "/hash", undefined, z.ZodString, string, string>, _fragno_dev_core0.FragnoRouteConfig<"GET", "/data", undefined, z.ZodString, string, "error">, _fragno_dev_core0.FragnoRouteConfig<"POST", "/sample", z.ZodObject<{
13
+ message: z.ZodString;
14
+ }, z.core.$strip>, z.ZodString, "MESSAGE_CANNOT_BE_DIGITS_ONLY", string>], {
15
+ serverSideData: {
16
+ value: string;
17
+ };
18
+ }, {
19
+ getData: () => string;
20
+ getHashFromHostsFileData: () => Promise<string | null>;
21
+ }>;
22
+ declare function createExampleFragmentClient(fragnoConfig?: FragnoPublicClientConfig): {
23
+ useHash: _fragno_dev_core_client0.FragnoClientHookData<"GET", "/hash", z.ZodString, string, string>;
24
+ useData: _fragno_dev_core_client0.FragnoClientHookData<"GET", "/data", z.ZodString, string, "error">;
25
+ useSampleMutator: _fragno_dev_core_client0.FragnoClientMutatorData<_fragno_dev_core_api0.NonGetHTTPMethod, "/sample", z.ZodObject<{
26
+ message: z.ZodString;
27
+ }, z.core.$strip> | undefined, z.ZodString | undefined, "MESSAGE_CANNOT_BE_DIGITS_ONLY", string>;
28
+ };
29
+ //#endregion
30
+ export { ExampleFragmentServerConfig, type FragnoRouteConfig, createExampleFragment, createExampleFragmentClient };
31
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;;;UAeiB,2BAAA;;;iBAkGD,qBAAA,gBACA,4CACA,uCAAuB,4BAAA,iBAAA,CAAA,6CAAA,CAAA,CAAA,4BAAA,iBAAA,CAAA,6CAAA,CAAA,CAAA,+CAAA,qCAAA,CAAA,CAAA;;;EApGtB,cAAA,EAAA;IAkGD,KAAA,EAAA,MAAA;EAAqB,CAAA;;SAErB,EAAA,GAAA,GAAA,MAAA;0BAAuB,EAAA,GAAA,UAAA,CAAA,MAAA,GAAA,IAAA,CAAA;;AAAA,iBAcvB,2BAAA,CAduB,YAAA,CAAA,EAcmB,wBAdnB,CAAA,EAAA;SAAA,+CAAA,CAAA,KAAA,EAAA,OAAA,aAAA,EAAA,MAAA,EAAA,MAAA,CAAA;;;IAAA,OAAA,aAAA"}
@@ -0,0 +1,50 @@
1
+ import { createFragment, defineFragment, defineRoute, defineRoutes } from "@fragno-dev/core";
2
+ import { createClientBuilder } from "@fragno-dev/core/client";
3
+ import { z } from "zod";
4
+
5
+ //#region src/index.ts
6
+ const exampleRoutesFactory = defineRoutes().create(({ deps }) => {
7
+ return [
8
+ defineRoute({
9
+ method: "GET",
10
+ path: "/hash",
11
+ outputSchema: z.string(),
12
+ handler: () => {}
13
+ }),
14
+ defineRoute({
15
+ method: "GET",
16
+ path: "/data",
17
+ outputSchema: z.string(),
18
+ queryParameters: ["error"],
19
+ handler: () => {}
20
+ }),
21
+ defineRoute({
22
+ method: "POST",
23
+ path: "/sample",
24
+ inputSchema: z.object({ message: z.string() }),
25
+ outputSchema: z.string(),
26
+ errorCodes: ["MESSAGE_CANNOT_BE_DIGITS_ONLY"],
27
+ handler: () => {}
28
+ })
29
+ ];
30
+ });
31
+ const exampleFragmentDefinition = defineFragment("example-fragment").withDependencies(() => {}).withServices(() => {});
32
+ function createExampleFragment(serverConfig = {}, fragnoConfig = {}) {
33
+ const config = { initialData: serverConfig.initialData ?? "Hello World! This is a server-side data." };
34
+ return createFragment(exampleFragmentDefinition, {
35
+ ...serverConfig,
36
+ ...config
37
+ }, [exampleRoutesFactory], fragnoConfig);
38
+ }
39
+ function createExampleFragmentClient(fragnoConfig = {}) {
40
+ const b = createClientBuilder(exampleFragmentDefinition, fragnoConfig, [exampleRoutesFactory]);
41
+ return {
42
+ useHash: b.createHook("/hash"),
43
+ useData: b.createHook("/data"),
44
+ useSampleMutator: b.createMutator("POST", "/sample")
45
+ };
46
+ }
47
+
48
+ //#endregion
49
+ export { createExampleFragment, createExampleFragmentClient };
50
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["defineFragment","defineRoute","defineRoutes","createFragment","FragnoPublicClientConfig","FragnoPublicConfig","createClientBuilder","z","ExampleFragmentServerConfig","initialData","ExampleRouteConfig","ExampleRouteDeps","serverSideData","value","exampleRoutesFactory","create","deps","method","path","outputSchema","string","handler","queryParameters","inputSchema","object","message","errorCodes","exampleFragmentDefinition","withDependencies","withServices","createExampleFragment","serverConfig","fragnoConfig","config","createExampleFragmentClient","b","useHash","createHook","useData","useSampleMutator","createMutator","FragnoRouteConfig"],"sources":["../../src/index.ts"],"sourcesContent":["import {\n defineFragment,\n defineRoute,\n defineRoutes,\n createFragment,\n type FragnoPublicClientConfig,\n type FragnoPublicConfig,\n} from \"@fragno-dev/core\";\nimport { createClientBuilder } from \"@fragno-dev/core/client\";\nimport { z } from \"zod\";\n\nimport { readFile } from \"node:fs/promises\";\nimport { platform } from \"node:os\";\nimport { createHash } from \"node:crypto\";\n\nexport interface ExampleFragmentServerConfig {\n initialData?: string;\n}\n\ntype ExampleRouteConfig = {\n initialData: string;\n};\n\ntype ExampleRouteDeps = {\n serverSideData: { value: string };\n};\n\nconst getHashFromHostsFileData = async () => {\n const hostsPath =\n platform() === \"win32\" ? \"C:\\\\Windows\\\\System32\\\\drivers\\\\etc\\\\hosts\" : \"/etc/hosts\";\n\n try {\n const data = await readFile(hostsPath, { encoding: \"utf8\" });\n return createHash(\"sha256\").update(data).digest(\"hex\");\n } catch {\n return null;\n }\n};\n\nconst exampleRoutesFactory = defineRoutes<ExampleRouteConfig, ExampleRouteDeps>().create(\n ({ deps }) => {\n const { serverSideData } = deps;\n\n return [\n defineRoute({\n method: \"GET\",\n path: \"/hash\",\n outputSchema: z.string(),\n handler: async (_, { json }) => {\n const hash = await getHashFromHostsFileData();\n return json(hash ? `The hash of your 'hosts' file is: ${hash}` : \"No hash found :(\");\n },\n }),\n\n defineRoute({\n method: \"GET\",\n path: \"/data\",\n outputSchema: z.string(),\n queryParameters: [\"error\"],\n handler: async ({ query }, { json, error }) => {\n if (query.get(\"error\")) {\n return error(\n {\n message: \"An error was triggered\",\n code: \"TEST_ERROR\",\n },\n 400,\n );\n }\n return json(serverSideData.value);\n },\n }),\n\n defineRoute({\n method: \"POST\",\n path: \"/sample\",\n inputSchema: z.object({ message: z.string() }),\n outputSchema: z.string(),\n errorCodes: [\"MESSAGE_CANNOT_BE_DIGITS_ONLY\"],\n handler: async ({ input }, { json, error }) => {\n const { message } = await input.valid();\n\n if (/^\\d+$/.test(message)) {\n return error(\n {\n message: \"Message cannot be digits only\",\n code: \"MESSAGE_CANNOT_BE_DIGITS_ONLY\",\n },\n 400,\n );\n }\n serverSideData.value = message;\n\n return json(message);\n },\n }),\n ];\n },\n);\n\nconst exampleFragmentDefinition = defineFragment<ExampleFragmentServerConfig>(\"example-fragment\")\n .withDependencies((config: ExampleFragmentServerConfig) => {\n return {\n serverSideData: { value: config.initialData ?? \"Hello World! This is a server-side data.\" },\n };\n })\n .withServices((_cfg, deps) => {\n return {\n getData: () => deps.serverSideData.value,\n getHashFromHostsFileData,\n };\n });\n\nexport function createExampleFragment(\n serverConfig: ExampleFragmentServerConfig = {},\n fragnoConfig: FragnoPublicConfig = {},\n) {\n const config: ExampleRouteConfig = {\n initialData: serverConfig.initialData ?? \"Hello World! This is a server-side data.\",\n };\n\n return createFragment(\n exampleFragmentDefinition,\n { ...serverConfig, ...config },\n [exampleRoutesFactory],\n fragnoConfig,\n );\n}\n\nexport function createExampleFragmentClient(fragnoConfig: FragnoPublicClientConfig = {}) {\n const b = createClientBuilder(exampleFragmentDefinition, fragnoConfig, [exampleRoutesFactory]);\n\n return {\n useHash: b.createHook(\"/hash\"),\n useData: b.createHook(\"/data\"),\n useSampleMutator: b.createMutator(\"POST\", \"/sample\"),\n };\n}\nexport type { FragnoRouteConfig } from \"@fragno-dev/core/api\";\n"],"mappings":";;;;;AAuCA,MAAMc,uBAAuBZ,cAAoD,CAACa,QAC/E,EAAEC,WAAW;AAGZ,QAAO;EACLf,YAAY;GACVgB,QAAQ;GACRC,MAAM;GACNC,cAAcZ,EAAEa,QAAQ;GACxBC,eAAO;GAIR,CAAC;EAEFpB,YAAY;GACVgB,QAAQ;GACRC,MAAM;GACNC,cAAcZ,EAAEa,QAAQ;GACxBE,iBAAiB,CAAC,QAAQ;GAC1BD,eAAO;GAYR,CAAC;EAEFpB,YAAY;GACVgB,QAAQ;GACRC,MAAM;GACNK,aAAahB,EAAEiB,OAAO,EAAEC,SAASlB,EAAEa,QAAO,EAAG,CAAC;GAC9CD,cAAcZ,EAAEa,QAAQ;GACxBM,YAAY,CAAC,gCAAgC;GAC7CL,eAAO;GAgBR,CAAC;EACH;EAEJ;AAED,MAAMM,4BAA4B3B,eAA4C,mBAAmB,CAC9F4B,uBAAgB,GAIf,CACDC,mBAAY,GAKX;AAEJ,SAAgBC,sBACdC,eAA4C,EAAE,EAC9CC,eAAmC,EAAE,EACrC;CACA,MAAMC,SAA6B,EACjCxB,aAAasB,aAAatB,eAAe,4CAC1C;AAED,QAAON,eACLwB,2BACA;EAAE,GAAGI;EAAc,GAAGE;EAAQ,EAC9B,CAACnB,qBAAqB,EACtBkB,aACD;;AAGH,SAAgBE,4BAA4BF,eAAyC,EAAE,EAAE;CACvF,MAAMG,IAAI7B,oBAAoBqB,2BAA2BK,cAAc,CAAClB,qBAAqB,CAAC;AAE9F,QAAO;EACLsB,SAASD,EAAEE,WAAW,QAAQ;EAC9BC,SAASH,EAAEE,WAAW,QAAQ;EAC9BE,kBAAkBJ,EAAEK,cAAc,QAAQ,UAAS;EACpD"}
@@ -0,0 +1,31 @@
1
+ import * as _fragno_dev_core0 from "@fragno-dev/core";
2
+ import { FragnoPublicClientConfig, FragnoPublicConfig } from "@fragno-dev/core";
3
+ import * as _fragno_dev_core_client0 from "@fragno-dev/core/client";
4
+ import { z } from "zod";
5
+ import * as _fragno_dev_core_api0 from "@fragno-dev/core/api";
6
+ import { FragnoRouteConfig } from "@fragno-dev/core/api";
7
+
8
+ //#region src/index.d.ts
9
+ interface ExampleFragmentServerConfig {
10
+ initialData?: string;
11
+ }
12
+ declare function createExampleFragment(serverConfig?: ExampleFragmentServerConfig, fragnoConfig?: FragnoPublicConfig): _fragno_dev_core0.FragnoInstantiatedFragment<[_fragno_dev_core0.FragnoRouteConfig<"GET", "/hash", undefined, z.ZodString, string, string>, _fragno_dev_core0.FragnoRouteConfig<"GET", "/data", undefined, z.ZodString, string, "error">, _fragno_dev_core0.FragnoRouteConfig<"POST", "/sample", z.ZodObject<{
13
+ message: z.ZodString;
14
+ }, z.core.$strip>, z.ZodString, "MESSAGE_CANNOT_BE_DIGITS_ONLY", string>], {
15
+ serverSideData: {
16
+ value: string;
17
+ };
18
+ }, {
19
+ getData: () => string;
20
+ getHashFromHostsFileData: () => Promise<string | null>;
21
+ }>;
22
+ declare function createExampleFragmentClient(fragnoConfig?: FragnoPublicClientConfig): {
23
+ useHash: _fragno_dev_core_client0.FragnoClientHookData<"GET", "/hash", z.ZodString, string, string>;
24
+ useData: _fragno_dev_core_client0.FragnoClientHookData<"GET", "/data", z.ZodString, string, "error">;
25
+ useSampleMutator: _fragno_dev_core_client0.FragnoClientMutatorData<_fragno_dev_core_api0.NonGetHTTPMethod, "/sample", z.ZodObject<{
26
+ message: z.ZodString;
27
+ }, z.core.$strip> | undefined, z.ZodString | undefined, "MESSAGE_CANNOT_BE_DIGITS_ONLY", string>;
28
+ };
29
+ //#endregion
30
+ export { ExampleFragmentServerConfig, type FragnoRouteConfig, createExampleFragment, createExampleFragmentClient };
31
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;;;UAeiB,2BAAA;;;iBAkGD,qBAAA,gBACA,4CACA,uCAAuB,4BAAA,iBAAA,CAAA,6CAAA,CAAA,CAAA,4BAAA,iBAAA,CAAA,6CAAA,CAAA,CAAA,+CAAA,qCAAA,CAAA,CAAA;;;EApGtB,cAAA,EAAA;IAkGD,KAAA,EAAA,MAAA;EAAqB,CAAA;;SAErB,EAAA,GAAA,GAAA,MAAA;0BAAuB,EAAA,GAAA,UAAA,CAAA,MAAA,GAAA,IAAA,CAAA;;AAAA,iBAcvB,2BAAA,CAduB,YAAA,CAAA,EAcmB,wBAdnB,CAAA,EAAA;SAAA,+CAAA,CAAA,KAAA,EAAA,OAAA,aAAA,EAAA,MAAA,EAAA,MAAA,CAAA;;;IAAA,OAAA,aAAA"}
@@ -0,0 +1,87 @@
1
+ import { createFragment, defineFragment, defineRoute, defineRoutes } from "@fragno-dev/core";
2
+ import { createClientBuilder } from "@fragno-dev/core/client";
3
+ import { z } from "zod";
4
+ import { readFile } from "node:fs/promises";
5
+ import { platform } from "node:os";
6
+ import { createHash } from "node:crypto";
7
+
8
+ //#region src/index.ts
9
+ const getHashFromHostsFileData = async () => {
10
+ const hostsPath = platform() === "win32" ? "C:\\Windows\\System32\\drivers\\etc\\hosts" : "/etc/hosts";
11
+ try {
12
+ const data = await readFile(hostsPath, { encoding: "utf8" });
13
+ return createHash("sha256").update(data).digest("hex");
14
+ } catch {
15
+ return null;
16
+ }
17
+ };
18
+ const exampleRoutesFactory = defineRoutes().create(({ deps }) => {
19
+ const { serverSideData } = deps;
20
+ return [
21
+ defineRoute({
22
+ method: "GET",
23
+ path: "/hash",
24
+ outputSchema: z.string(),
25
+ handler: async (_, { json }) => {
26
+ const hash = await getHashFromHostsFileData();
27
+ return json(hash ? `The hash of your 'hosts' file is: ${hash}` : "No hash found :(");
28
+ }
29
+ }),
30
+ defineRoute({
31
+ method: "GET",
32
+ path: "/data",
33
+ outputSchema: z.string(),
34
+ queryParameters: ["error"],
35
+ handler: async ({ query }, { json, error }) => {
36
+ if (query.get("error")) return error({
37
+ message: "An error was triggered",
38
+ code: "TEST_ERROR"
39
+ }, 400);
40
+ return json(serverSideData.value);
41
+ }
42
+ }),
43
+ defineRoute({
44
+ method: "POST",
45
+ path: "/sample",
46
+ inputSchema: z.object({ message: z.string() }),
47
+ outputSchema: z.string(),
48
+ errorCodes: ["MESSAGE_CANNOT_BE_DIGITS_ONLY"],
49
+ handler: async ({ input }, { json, error }) => {
50
+ const { message } = await input.valid();
51
+ if (/^\d+$/.test(message)) return error({
52
+ message: "Message cannot be digits only",
53
+ code: "MESSAGE_CANNOT_BE_DIGITS_ONLY"
54
+ }, 400);
55
+ serverSideData.value = message;
56
+ return json(message);
57
+ }
58
+ })
59
+ ];
60
+ });
61
+ const exampleFragmentDefinition = defineFragment("example-fragment").withDependencies((config) => {
62
+ return { serverSideData: { value: config.initialData ?? "Hello World! This is a server-side data." } };
63
+ }).withServices((_cfg, deps) => {
64
+ return {
65
+ getData: () => deps.serverSideData.value,
66
+ getHashFromHostsFileData
67
+ };
68
+ });
69
+ function createExampleFragment(serverConfig = {}, fragnoConfig = {}) {
70
+ const config = { initialData: serverConfig.initialData ?? "Hello World! This is a server-side data." };
71
+ return createFragment(exampleFragmentDefinition, {
72
+ ...serverConfig,
73
+ ...config
74
+ }, [exampleRoutesFactory], fragnoConfig);
75
+ }
76
+ function createExampleFragmentClient(fragnoConfig = {}) {
77
+ const b = createClientBuilder(exampleFragmentDefinition, fragnoConfig, [exampleRoutesFactory]);
78
+ return {
79
+ useHash: b.createHook("/hash"),
80
+ useData: b.createHook("/data"),
81
+ useSampleMutator: b.createMutator("POST", "/sample")
82
+ };
83
+ }
84
+
85
+ //#endregion
86
+ export { createExampleFragment, createExampleFragmentClient };
87
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["config: ExampleRouteConfig"],"sources":["../../src/index.ts"],"sourcesContent":["import {\n defineFragment,\n defineRoute,\n defineRoutes,\n createFragment,\n type FragnoPublicClientConfig,\n type FragnoPublicConfig,\n} from \"@fragno-dev/core\";\nimport { createClientBuilder } from \"@fragno-dev/core/client\";\nimport { z } from \"zod\";\n\nimport { readFile } from \"node:fs/promises\";\nimport { platform } from \"node:os\";\nimport { createHash } from \"node:crypto\";\n\nexport interface ExampleFragmentServerConfig {\n initialData?: string;\n}\n\ntype ExampleRouteConfig = {\n initialData: string;\n};\n\ntype ExampleRouteDeps = {\n serverSideData: { value: string };\n};\n\nconst getHashFromHostsFileData = async () => {\n const hostsPath =\n platform() === \"win32\" ? \"C:\\\\Windows\\\\System32\\\\drivers\\\\etc\\\\hosts\" : \"/etc/hosts\";\n\n try {\n const data = await readFile(hostsPath, { encoding: \"utf8\" });\n return createHash(\"sha256\").update(data).digest(\"hex\");\n } catch {\n return null;\n }\n};\n\nconst exampleRoutesFactory = defineRoutes<ExampleRouteConfig, ExampleRouteDeps>().create(\n ({ deps }) => {\n const { serverSideData } = deps;\n\n return [\n defineRoute({\n method: \"GET\",\n path: \"/hash\",\n outputSchema: z.string(),\n handler: async (_, { json }) => {\n const hash = await getHashFromHostsFileData();\n return json(hash ? `The hash of your 'hosts' file is: ${hash}` : \"No hash found :(\");\n },\n }),\n\n defineRoute({\n method: \"GET\",\n path: \"/data\",\n outputSchema: z.string(),\n queryParameters: [\"error\"],\n handler: async ({ query }, { json, error }) => {\n if (query.get(\"error\")) {\n return error(\n {\n message: \"An error was triggered\",\n code: \"TEST_ERROR\",\n },\n 400,\n );\n }\n return json(serverSideData.value);\n },\n }),\n\n defineRoute({\n method: \"POST\",\n path: \"/sample\",\n inputSchema: z.object({ message: z.string() }),\n outputSchema: z.string(),\n errorCodes: [\"MESSAGE_CANNOT_BE_DIGITS_ONLY\"],\n handler: async ({ input }, { json, error }) => {\n const { message } = await input.valid();\n\n if (/^\\d+$/.test(message)) {\n return error(\n {\n message: \"Message cannot be digits only\",\n code: \"MESSAGE_CANNOT_BE_DIGITS_ONLY\",\n },\n 400,\n );\n }\n serverSideData.value = message;\n\n return json(message);\n },\n }),\n ];\n },\n);\n\nconst exampleFragmentDefinition = defineFragment<ExampleFragmentServerConfig>(\"example-fragment\")\n .withDependencies((config: ExampleFragmentServerConfig) => {\n return {\n serverSideData: { value: config.initialData ?? \"Hello World! This is a server-side data.\" },\n };\n })\n .withServices((_cfg, deps) => {\n return {\n getData: () => deps.serverSideData.value,\n getHashFromHostsFileData,\n };\n });\n\nexport function createExampleFragment(\n serverConfig: ExampleFragmentServerConfig = {},\n fragnoConfig: FragnoPublicConfig = {},\n) {\n const config: ExampleRouteConfig = {\n initialData: serverConfig.initialData ?? \"Hello World! This is a server-side data.\",\n };\n\n return createFragment(\n exampleFragmentDefinition,\n { ...serverConfig, ...config },\n [exampleRoutesFactory],\n fragnoConfig,\n );\n}\n\nexport function createExampleFragmentClient(fragnoConfig: FragnoPublicClientConfig = {}) {\n const b = createClientBuilder(exampleFragmentDefinition, fragnoConfig, [exampleRoutesFactory]);\n\n return {\n useHash: b.createHook(\"/hash\"),\n useData: b.createHook(\"/data\"),\n useSampleMutator: b.createMutator(\"POST\", \"/sample\"),\n };\n}\nexport type { FragnoRouteConfig } from \"@fragno-dev/core/api\";\n"],"mappings":";;;;;;;;AA2BA,MAAM,2BAA2B,YAAY;CAC3C,MAAM,YACJ,UAAU,KAAK,UAAU,+CAA+C;AAE1E,KAAI;EACF,MAAM,OAAO,MAAM,SAAS,WAAW,EAAE,UAAU,QAAQ,CAAC;AAC5D,SAAO,WAAW,SAAS,CAAC,OAAO,KAAK,CAAC,OAAO,MAAM;SAChD;AACN,SAAO;;;AAIX,MAAM,uBAAuB,cAAoD,CAAC,QAC/E,EAAE,WAAW;CACZ,MAAM,EAAE,mBAAmB;AAE3B,QAAO;EACL,YAAY;GACV,QAAQ;GACR,MAAM;GACN,cAAc,EAAE,QAAQ;GACxB,SAAS,OAAO,GAAG,EAAE,WAAW;IAC9B,MAAM,OAAO,MAAM,0BAA0B;AAC7C,WAAO,KAAK,OAAO,qCAAqC,SAAS,mBAAmB;;GAEvF,CAAC;EAEF,YAAY;GACV,QAAQ;GACR,MAAM;GACN,cAAc,EAAE,QAAQ;GACxB,iBAAiB,CAAC,QAAQ;GAC1B,SAAS,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY;AAC7C,QAAI,MAAM,IAAI,QAAQ,CACpB,QAAO,MACL;KACE,SAAS;KACT,MAAM;KACP,EACD,IACD;AAEH,WAAO,KAAK,eAAe,MAAM;;GAEpC,CAAC;EAEF,YAAY;GACV,QAAQ;GACR,MAAM;GACN,aAAa,EAAE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC;GAC9C,cAAc,EAAE,QAAQ;GACxB,YAAY,CAAC,gCAAgC;GAC7C,SAAS,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY;IAC7C,MAAM,EAAE,YAAY,MAAM,MAAM,OAAO;AAEvC,QAAI,QAAQ,KAAK,QAAQ,CACvB,QAAO,MACL;KACE,SAAS;KACT,MAAM;KACP,EACD,IACD;AAEH,mBAAe,QAAQ;AAEvB,WAAO,KAAK,QAAQ;;GAEvB,CAAC;EACH;EAEJ;AAED,MAAM,4BAA4B,eAA4C,mBAAmB,CAC9F,kBAAkB,WAAwC;AACzD,QAAO,EACL,gBAAgB,EAAE,OAAO,OAAO,eAAe,4CAA4C,EAC5F;EACD,CACD,cAAc,MAAM,SAAS;AAC5B,QAAO;EACL,eAAe,KAAK,eAAe;EACnC;EACD;EACD;AAEJ,SAAgB,sBACd,eAA4C,EAAE,EAC9C,eAAmC,EAAE,EACrC;CACA,MAAMA,SAA6B,EACjC,aAAa,aAAa,eAAe,4CAC1C;AAED,QAAO,eACL,2BACA;EAAE,GAAG;EAAc,GAAG;EAAQ,EAC9B,CAAC,qBAAqB,EACtB,aACD;;AAGH,SAAgB,4BAA4B,eAAyC,EAAE,EAAE;CACvF,MAAM,IAAI,oBAAoB,2BAA2B,cAAc,CAAC,qBAAqB,CAAC;AAE9F,QAAO;EACL,SAAS,EAAE,WAAW,QAAQ;EAC9B,SAAS,EAAE,WAAW,QAAQ;EAC9B,kBAAkB,EAAE,cAAc,QAAQ,UAAU;EACrD"}
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@fragno-dev/example-fragment",
3
+ "version": "0.0.5",
4
+ "exports": {
5
+ ".": {
6
+ "development": {
7
+ "browser": "./dist/browser/index.js",
8
+ "default": "./src/index.ts"
9
+ },
10
+ "bun": "./src/index.ts",
11
+ "browser": "./dist/browser/index.js",
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/node/index.js"
14
+ }
15
+ },
16
+ "main": "./dist/node/index.js",
17
+ "module": "./dist/node/index.js",
18
+ "types": "./dist/node/index.d.ts",
19
+ "scripts": {
20
+ "build": "tsdown",
21
+ "build:watch": "tsdown --watch",
22
+ "types:check": "tsc --noEmit"
23
+ },
24
+ "type": "module",
25
+ "dependencies": {
26
+ "@fragno-dev/core": "0.0.5",
27
+ "zod": "^4.0.5"
28
+ },
29
+ "devDependencies": {
30
+ "@fragno-private/typescript-config": "0.0.1",
31
+ "@fragno-dev/unplugin-fragno": "0.0.1",
32
+ "@types/node": "^20"
33
+ }
34
+ }
package/src/index.ts ADDED
@@ -0,0 +1,139 @@
1
+ import {
2
+ defineFragment,
3
+ defineRoute,
4
+ defineRoutes,
5
+ createFragment,
6
+ type FragnoPublicClientConfig,
7
+ type FragnoPublicConfig,
8
+ } from "@fragno-dev/core";
9
+ import { createClientBuilder } from "@fragno-dev/core/client";
10
+ import { z } from "zod";
11
+
12
+ import { readFile } from "node:fs/promises";
13
+ import { platform } from "node:os";
14
+ import { createHash } from "node:crypto";
15
+
16
+ export interface ExampleFragmentServerConfig {
17
+ initialData?: string;
18
+ }
19
+
20
+ type ExampleRouteConfig = {
21
+ initialData: string;
22
+ };
23
+
24
+ type ExampleRouteDeps = {
25
+ serverSideData: { value: string };
26
+ };
27
+
28
+ const getHashFromHostsFileData = async () => {
29
+ const hostsPath =
30
+ platform() === "win32" ? "C:\\Windows\\System32\\drivers\\etc\\hosts" : "/etc/hosts";
31
+
32
+ try {
33
+ const data = await readFile(hostsPath, { encoding: "utf8" });
34
+ return createHash("sha256").update(data).digest("hex");
35
+ } catch {
36
+ return null;
37
+ }
38
+ };
39
+
40
+ const exampleRoutesFactory = defineRoutes<ExampleRouteConfig, ExampleRouteDeps>().create(
41
+ ({ deps }) => {
42
+ const { serverSideData } = deps;
43
+
44
+ return [
45
+ defineRoute({
46
+ method: "GET",
47
+ path: "/hash",
48
+ outputSchema: z.string(),
49
+ handler: async (_, { json }) => {
50
+ const hash = await getHashFromHostsFileData();
51
+ return json(hash ? `The hash of your 'hosts' file is: ${hash}` : "No hash found :(");
52
+ },
53
+ }),
54
+
55
+ defineRoute({
56
+ method: "GET",
57
+ path: "/data",
58
+ outputSchema: z.string(),
59
+ queryParameters: ["error"],
60
+ handler: async ({ query }, { json, error }) => {
61
+ if (query.get("error")) {
62
+ return error(
63
+ {
64
+ message: "An error was triggered",
65
+ code: "TEST_ERROR",
66
+ },
67
+ 400,
68
+ );
69
+ }
70
+ return json(serverSideData.value);
71
+ },
72
+ }),
73
+
74
+ defineRoute({
75
+ method: "POST",
76
+ path: "/sample",
77
+ inputSchema: z.object({ message: z.string() }),
78
+ outputSchema: z.string(),
79
+ errorCodes: ["MESSAGE_CANNOT_BE_DIGITS_ONLY"],
80
+ handler: async ({ input }, { json, error }) => {
81
+ const { message } = await input.valid();
82
+
83
+ if (/^\d+$/.test(message)) {
84
+ return error(
85
+ {
86
+ message: "Message cannot be digits only",
87
+ code: "MESSAGE_CANNOT_BE_DIGITS_ONLY",
88
+ },
89
+ 400,
90
+ );
91
+ }
92
+ serverSideData.value = message;
93
+
94
+ return json(message);
95
+ },
96
+ }),
97
+ ];
98
+ },
99
+ );
100
+
101
+ const exampleFragmentDefinition = defineFragment<ExampleFragmentServerConfig>("example-fragment")
102
+ .withDependencies((config: ExampleFragmentServerConfig) => {
103
+ return {
104
+ serverSideData: { value: config.initialData ?? "Hello World! This is a server-side data." },
105
+ };
106
+ })
107
+ .withServices((_cfg, deps) => {
108
+ return {
109
+ getData: () => deps.serverSideData.value,
110
+ getHashFromHostsFileData,
111
+ };
112
+ });
113
+
114
+ export function createExampleFragment(
115
+ serverConfig: ExampleFragmentServerConfig = {},
116
+ fragnoConfig: FragnoPublicConfig = {},
117
+ ) {
118
+ const config: ExampleRouteConfig = {
119
+ initialData: serverConfig.initialData ?? "Hello World! This is a server-side data.",
120
+ };
121
+
122
+ return createFragment(
123
+ exampleFragmentDefinition,
124
+ { ...serverConfig, ...config },
125
+ [exampleRoutesFactory],
126
+ fragnoConfig,
127
+ );
128
+ }
129
+
130
+ export function createExampleFragmentClient(fragnoConfig: FragnoPublicClientConfig = {}) {
131
+ const b = createClientBuilder(exampleFragmentDefinition, fragnoConfig, [exampleRoutesFactory]);
132
+
133
+ return {
134
+ useHash: b.createHook("/hash"),
135
+ useData: b.createHook("/data"),
136
+ useSampleMutator: b.createMutator("POST", "/sample"),
137
+ };
138
+ }
139
+ export type { FragnoRouteConfig } from "@fragno-dev/core/api";
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "@fragno-private/typescript-config/tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": ".",
6
+ "composite": true
7
+ },
8
+ "include": ["src"],
9
+ "exclude": ["node_modules", "dist"]
10
+ }
@@ -0,0 +1,18 @@
1
+ import { defineConfig } from "tsdown";
2
+ import unpluginFragno from "@fragno-dev/unplugin-fragno/rollup";
3
+
4
+ export default defineConfig([
5
+ {
6
+ entry: "./src/index.ts",
7
+ dts: true,
8
+ platform: "browser",
9
+ outDir: "./dist/browser",
10
+ plugins: [unpluginFragno({ platform: "browser" })],
11
+ },
12
+ {
13
+ entry: "./src/index.ts",
14
+ dts: true,
15
+ platform: "node",
16
+ outDir: "./dist/node",
17
+ },
18
+ ]);