{"plugin":{"id":16,"name":"server-object","icon":"✨","description":"Access server-side [object, function] client-side","longDescription":"# frame-master-plugin-server-object\n\nFrame-Master plugin to expose server-side objects or functions to the client, with automatic serialization and dynamic fetching support. Enables seamless sharing of server logic or data with the client in Bun-based Frame-Master projects.\n\n## Installation\n\n```bash\nbun add frame-master-plugin-server-object superjson@2.2.6\n```\n\n## Usage\n\n```typescript\nimport type { FrameMasterConfig } from \"frame-master/server/types\";\nimport framemasterpluginserverobject from \"frame-master-plugin-server-object\";\n\nconst config: FrameMasterConfig = {\n  HTTPServer: { port: 3000 },\n  plugins: [framemasterpluginserverobject()],\n};\n\nexport default config;\n```\n\n## Features\n\n- Exposes server-side objects or functions to the client, using a configurable prefix (default: `Server`).\n- Supports both static (build-time) and dynamic (runtime) server object exports.\n- Automatically serializes and deserializes server objects using [superjson](https://github.com/blitz-js/superjson).\n- Dynamic server objects are fetched via a special API route and executed on the server at runtime.\n- Caches dynamic server object functions for efficient repeated access.\n- Configurable source directory and export prefixes.\n- Get Request in Dynamic Server Object with `getRequest(arguments)`.\n- Custom client-side parsers for advanced response handling with `customClientParser`.\n- Custom server-side parsers for build-time transformation with `customServerParser`.\n- Optional runtime parsing support for custom server parsers.\n\n## How It Works\n\n1. During build, scans your source files for exports starting with a specified prefix (default: `Server`) or an optional dynamic prefix.\n2. Static server objects are serialized and inlined for client use.\n3. Dynamic server objects are replaced with a client function that fetches and executes the server object at runtime arguments must be serializable.\n4. A special server route is set up to handle dynamic object requests, executing the function and returning the result: `/__server_object_dynamic`\n\n## Options\n\nYou can configure the plugin with the following options:\n\n```typescript\nframemasterpluginserverobject({\n  prefix: \"Server\", // Export prefix to identify server objects (default: \"Server\")\n  dynamicPrefix: \"DynamicServer\", // Optional dynamic prefix for runtime-fetched server objects\n  src: \"src/server\", // Optional base path to scan for server objects\n  customClientParser: [\n    {\n      prefix: \"MyClient\",\n      parser: ({ object, req, props }) => {\n        // Custom parsing logic for client-side response\n        return JSON.stringify(object);\n      },\n      buildParser: (res) => res.json(), // Optional: transform response at build time\n    },\n  ],\n  customServerParser: [\n    {\n      prefix: \"MyServer\",\n      parser: (object, filePath, objectName) => {\n        // Return string representation for client-side access\n        return JSON.stringify(object);\n      },\n      enableRuntimeParsing: false, // Enable runtime parsing (default: false)\n    },\n  ],\n});\n```\n\n### Usecase\n\n```typescript\nexport const DynamicServerGetter = async (id: string) => {\n  // SQL query or fetch external data\n  return await fetch(`https://exemple.com/api/${id}`).then((res) => res.json()); // must be serializable\n};\n\nexport const ServerObject = Bun.file(\"./my-file.txt\").text(); // must be serializable\n\n// client side\nconsole.log(ServerObject, await DynamicServerGetter(\"random-id\"));\n```\n\n## License\n\nMIT\n\n```\n\n```\n","version":"0.1.0","compatibleVersions":"^1.3.0","author":"shpaw415","ownerId":"c166cdde-5f5c-40e5-a3ff-236652fa88a9","category":"api","tags":["server-action","server-object"],"published":true,"npmPackage":"frame-master-plugin-server-object","githubUrl":"https://github.com/shpaw415/frame-master-plugin-server-object","docsUrl":"https://github.com/shpaw415/frame-master-plugin-server-object","installation":"bun add frame-master-plugin-server-object","quickStart":"\n// build time\nimport { getRequest } from \"frame-master-plugin-server-object/utils\";\nexport ServerObject = await Bun.file(\"file.txt\").text();\n\nexport DynamicServerCall = async (param: string) => {\n  const master = getRequest(arguments);\n  // server side\n  return \"Hello from Server\" + ${param};\n};\n\n// client side\nconsole.log(ServerObject);\nconsole.log(await DynamicServerCall(\"Hello from client\"))","configuration":"framemasterpluginserverobject({\n  prefix: \"Server\", // Export prefix to identify server objects (default: \"Server\")\n  dynamicPrefix: \"DynamicServer\", // Optional dynamic prefix for runtime-fetched server objects\n  src: \"src/server\", // Optional base path to scan for server objects\n  customClientParser: [\n    {\n      prefix: \"MyClient\",\n      parser: ({ object, req, props }) => {\n        // Custom parsing logic for client-side response\n        return JSON.stringify(object);\n      },\n      buildParser: (res) => res.json(), // Optional: transform response at build time\n    },\n  ],\n  customServerParser: [\n    {\n      prefix: \"MyServer\",\n      parser: (object, filePath, objectName) => {\n        // Return string representation for client-side access\n        return JSON.stringify(object);\n      },\n      enableRuntimeParsing: false, // Enable runtime parsing (default: false)\n    },\n  ],\n})","upvote":0,"downvote":0,"createdAt":"2026-01-15T18:06:28.000Z","updatedAt":"2026-01-15T18:07:27.000Z","dependencies":[]}}