{"id":15315859,"url":"https://github.com/wobsoriano/nuxt-remote-fn","last_synced_at":"2025-04-14T07:08:15.861Z","repository":{"id":64017160,"uuid":"571854627","full_name":"wobsoriano/nuxt-remote-fn","owner":"wobsoriano","description":"Remote Functions. Instead of Event Handlers.","archived":false,"fork":false,"pushed_at":"2024-04-06T04:01:04.000Z","size":1033,"stargazers_count":101,"open_issues_count":4,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-14T07:08:07.939Z","etag":null,"topics":["api","http","nuxt","proxy","vue"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wobsoriano.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-11-29T02:50:25.000Z","updated_at":"2025-03-30T21:03:58.000Z","dependencies_parsed_at":"2023-12-29T06:31:51.605Z","dependency_job_id":"4128a6c9-812e-4603-aaf5-0e2417b03bf5","html_url":"https://github.com/wobsoriano/nuxt-remote-fn","commit_stats":null,"previous_names":[],"tags_count":40,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wobsoriano%2Fnuxt-remote-fn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wobsoriano%2Fnuxt-remote-fn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wobsoriano%2Fnuxt-remote-fn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wobsoriano%2Fnuxt-remote-fn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wobsoriano","download_url":"https://codeload.github.com/wobsoriano/nuxt-remote-fn/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248837280,"owners_count":21169374,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","http","nuxt","proxy","vue"],"created_at":"2024-10-01T08:52:35.369Z","updated_at":"2025-04-14T07:08:15.834Z","avatar_url":"https://github.com/wobsoriano.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# nuxt-remote-fn\n\n`nuxt-remote-fn` allows you to call your backend-functions from the frontend, as if they were local. No need for an extra language or DSL to learn and maintain.\n\n## Install\n\n```bash\nnpm install nuxt-remote-fn\n```\n\n```ts\nexport default defineNuxtConfig({\n  modules: [\n    'nuxt-remote-fn',\n  ],\n})\n```\n\n## Usage\n\nExport your remote functions in `*.server.{ts,js,mjs}` files:\n\n```ts\n// lib/todo.server.ts\n\nimport { PrismaClient } from '@prisma/client'\n\nconst prisma = new PrismaClient()\n\nexport async function getTodos () {\n  const todos = await prisma.todo.findMany()\n  return todos\n}\n```\n\nDirectly use any SQL/ORM query to retrieve \u0026 mutate data on client.\n\n```vue\n\u003cscript setup lang=\"ts\"\u003e\nimport { getTodos } from '@/lib/todo.server'\n\nconst todos = await getTodos()\n\u003c/script\u003e\n\n\u003ctemplate\u003e\n  \u003cTodoList :todos=\"todos\" /\u003e\n\u003c/template\u003e\n```\n\nThe `.server` part of the filename informs the module that this code should never end up in the browser and to convert it to an API call instead (`POST /api/__remote/todo/getTodos`).\n\nCheckout [the playground example](/playground).\n\n## H3 Event\n\nThe `useH3Event` hook provides the `event` object of the current request. You can use it to check headers, log requests, or extend the event's request object.\n\n```ts\nimport { useH3Event } from 'nuxt-remote-fn/server'\nimport { getRequestHeader, createError } from 'h3'\nimport { decodeAndVerifyJwtToken } from '~/somewhere/in/utils'\n\nexport async function addTodo(todo: Todo) {\n  const event = useH3Event()\n\n  async function getUserFromHeader() {\n    const authorization = getRequestHeader(event, 'authorization')\n    if (authorization) {\n      const user = await decodeAndVerifyJwtToken(authorization.split(' ')[1])\n      return user\n    }\n    return null\n  }\n\n  const user = await getUserFromHeader()\n\n  if (!user) {\n    throw createError({ statusCode: 401 })\n  }\n\n  const result = await prisma.todo.create({\n    data: {\n      ...todo,\n      userId: user.id\n    }\n  })\n\n  return result\n}\n```\n\nYou can use all built-in [h3 utilities](https://github.com/unjs/h3#utilities) inside your exported functions.\n\n## createContext\n\nEach `.server.` file can also export a `createContext` function that is called for each incoming request:\n\n```ts\nexport function createContext() {\n  const event = useH3Event()\n\n  async function getUserFromHeader() {\n    const authorization = getRequestHeader(event, 'authorization')\n    if (authorization) {\n      const user = await decodeAndVerifyJwtToken(authorization.split(' ')[1])\n      return user\n    }\n    return null\n  }\n\n  event.context.user = await getUserFromHeader()\n}\n\nexport async function addTodo(todo: Todo) {\n  const event = useH3Event()\n\n  if (!event.context.user) {\n    throw createError({ statusCode: 401 })\n  }\n\n  // addTodo logic\n}\n```\n\n## useAsyncData\n\n`nuxt-remote-fn` can work seamlessly with [`useAsyncData`](https://nuxt.com/docs/api/composables/use-async-data/):\n\n```vue\n\u003cscript setup lang=\"ts\"\u003e\nimport { getTodos } from '@/lib/todo.server'\n\nconst { data: todos } = useAsyncData('todos', () =\u003e getTodos())\n\u003c/script\u003e\n```\n\n## Fetch options:\n\nSince `nuxt.config.ts` file doesn't accept functions as values, you can use the client directly to add `$fetch` options:\n\n```ts\nimport type { RemoteFunction } from '#build/remote-handler'\nimport { createClient } from 'nuxt-remote-fn/client'\n\nconst client = createClient\u003cRemoteFunction\u003e({\n  fetchOptions: {\n    onRequest({ request }) {\n      // do something\n    }\n  }\n})\n\nconst todo = await client.todo.getTodo(1)\n```\n\n## Why this module\n\nSharing data from server to client involves a lot of ceremony. i.e. an `eventHandler` needs to be set up and `useFetch` needs to be used in the browser.\n\nWouldn't it be nice if all of that was automatically handled and all you'd need to do is import `getTodos` on the client, just like you do in `eventHandler`'s? That's where `nuxt-remote-fn` comes in. With `nuxt-remote-fn`, all exported functions from `.server.` files automatically become available to the browser as well.\n\n## Development\n\n- Run `cp playground/.env.example playground/.env`\n- Run `pnpm dev:prepare` to generate type stubs.\n- Use `pnpm dev` to start [playground](./playground) in development mode.\n\n## Credits\n\nThis project is inspired by [tRPC](http://trpc.io/), [Telefunc](https://telefunc.com) and [nuxt-server-fn](https://github.com/antfu/nuxt-server-fn).\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwobsoriano%2Fnuxt-remote-fn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwobsoriano%2Fnuxt-remote-fn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwobsoriano%2Fnuxt-remote-fn/lists"}