{"id":20252376,"url":"https://github.com/bewinxed/svetch","last_synced_at":"2025-05-09T02:44:50.675Z","repository":{"id":181600388,"uuid":"667007638","full_name":"Bewinxed/svetch","owner":"Bewinxed","description":"Auto-Generated typesafe client \u0026 API docs generator for your Serverless Application (Svelte First)","archived":false,"fork":false,"pushed_at":"2024-09-08T03:37:48.000Z","size":35857,"stargazers_count":71,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-19T22:53:28.223Z","etag":null,"topics":["codegen","openapi","svelte","sveltekit","swagger"],"latest_commit_sha":null,"homepage":"https://svetch-dev.vercel.app/","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/Bewinxed.png","metadata":{"files":{"readme":"README.md","changelog":null,"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,"publiccode":null,"codemeta":null}},"created_at":"2023-07-16T10:33:11.000Z","updated_at":"2025-04-17T01:30:47.000Z","dependencies_parsed_at":"2024-09-08T04:33:04.348Z","dependency_job_id":null,"html_url":"https://github.com/Bewinxed/svetch","commit_stats":null,"previous_names":["bewinxed/svetch"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bewinxed%2Fsvetch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bewinxed%2Fsvetch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bewinxed%2Fsvetch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bewinxed%2Fsvetch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Bewinxed","download_url":"https://codeload.github.com/Bewinxed/svetch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253180885,"owners_count":21866988,"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":["codegen","openapi","svelte","sveltekit","swagger"],"created_at":"2024-11-14T10:16:25.929Z","updated_at":"2025-05-09T02:44:50.648Z","avatar_url":"https://github.com/Bewinxed.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![npm](https://img.shields.io/npm/v/svetch.ts)](https://www.npmjs.com/package/svetch.ts)\n![svetch-chan (1)](https://github.com/Bewinxed/svetch/assets/9145989/67c36f21-a21e-42f1-ba50-42f457948c46)\n\n# 🚀 Svetch.ts: Zero-Effort client/types/schema/OpenAPI Schema/Docs generator for your API endpoints\n\n_Typesafety, Minus the typing_\n\n👉 Check it out @ (https://svetch-dev.vercel.app/)\nEffortlessly generate a typesafe Fetch client for your Svelte/Sveltekit applications, complete with automatic input/output zod validation and autogenerated types \u0026 API Docs.\n\n# What is this?\n\nSvetch automatically scans your `+server.ts` files in /src/routes/api (or whatever directory you specify) and generates a typesafe Fetch client that has intellisense for path, query, body parameters \u0026 all the possible responses (and errors)\n\n# 🧙‍♂️ Automatic Detection\n\n- ❓ **Query Params** =\u003e Detected using any declarations of `url.searchParams.get`\n- 📂 **Path Params** =\u003e Detected from the file directory\n- 📦 **Payload Definition** =\u003e inferred from `const payload: X = await request.json` or `as X`\n- 💬 **Response Definition** =\u003e inferred from any return statement with `json(X) (sveltekit utility)` or `new Response(X)`\n- 📛 **Error Definitions** =\u003e inferred from any throw statement with `throw error()` or `throw new Error` or `return error(HTTP_CODE, error_message)`\n\n# ⏬ How to run\n\n`$ npx svetch.ts@latest`\n\n### Make sure you have a path alias for src in your `svelte.config.js`\n\n```diff\nimport adapter from '@sveltejs/adapter-auto';\nimport { vitePreprocess } from '@sveltejs/kit/vite';\n\n/** @type {import('@sveltejs/kit').Config} */\nconst config = {\n\t// Consult https://kit.svelte.dev/docs/integrations#preprocessors\n\t// for more information about preprocessors\n\tpreprocess: vitePreprocess(),\n\tkit: {\n\t\t// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.\n\t\t// If your environment is not supported or you settled on a specific environment, switch out the adapter.\n\t\t// See https://kit.svelte.dev/docs/adapters for more information about adapters.\n\t\tadapter: adapter(),\n+\t\talias: {\n+\t\t\t\"src\": \"./src\",\n\t\t},\n\t}\n};\n\nexport default config;\n\n```\n\n# 🌟 Advantages\n\n- 😍 **ALMOST** no code changes required to your existing API code\n- 🚀 Almost no learning curve, Augments a regular FETCH client with **data** and **error** along with the rest of the fetch client properties, you can use it just like a regular fetch client.\n- 🔎 **Intellisense** for your api paths.\n- ✅ Typesafety for api inputs \u0026 outputs.\n- 📚 Generates an OpenAPI Schema for your API, alongside with Swagger docs.\n- 📃 Can use **JSDocs** to add more info to the endpoint documentation (WIP)\n- 🤖 Handles multiple possible responses per http code.\n\n## Autogenerated Docs\n\nThe library will generate an OpenAPI compatible schema, alongside with swagger docs, by default in /docs/+page.svelte\n\n# ⚙ Config\n\n`.svetchrc`\n\n```json\n{\n  \"framework\": \"sveltekit\", // currently the only option\n  \"input\": \"src/routes/api\", // the directory you want the generator to traverse\n  \"out\": \"src/lib/api\", // the directory to output the types \u0026 the client to\n  \"docs\": \"src/routes/docs\", // where to output docs\n  \"tsconfig\": \"tsconfig.json\" // your tsconfig directory\n}\n```\n\n# 🔎 Detection\n\n1. Svetch will traverse your input directory, will scan for any +server.ts with exported GET/POST/PUT/DELETE functions.\n2. For each endpoint it will detect...\n   1. **path parameters**: based on the file name, e.g. `user/[user_id].ts` will have a path parameter of `user_id`\n   2. **query parameters**: based on any parameters instantiated like `url.searchParams.get('param')`\n   3. **body parameters**: based on the type of the payload `Must be assigned to a const called` **payload** **⚠ IMPORTANT**\n   4. **response types**: will pickup any top-level return statement that is instantiated like **json(xxx)** or **new Response(xxx)** along with status code\n\n## In client code\n\n```ts\nimport { Svetch } from \"src/lib/api/client\"; // or wherever you chose to generate the client\n\nconst svetch = new Svetch({\n  baseUrl: \"/api\", // default is '/api'\n  fetch, // default is window.fetch, pass the global fetch to it in node, etc...\n  validate: false, // default is false, uses Zod to validate payload + response (ON CLIENT THIS CAN MAKE THE IMPORT SIZE HUGE)\n});\n\nawait svetch\n  .post(\"user/[user_id]\", {\n    path: {\n      user_id: 1,\n    },\n    body: {\n      text: foodInput,\n      journalId: $journal.id,\n      today: new Date(),\n    },\n  })\n  .then((response) =\u003e {\n    if (response.error) throw new Error(response.error);\n    food = response.data;\n    loading = false;\n  })\n  .catch((error) =\u003e {\n    throw new Error(error);\n  });\n```\n\n## In load functions\n\n```ts\nimport { Svetch } from \"src/lib/api/client\"; // or wherever you chose to generate the client\n\nconst svetch = new Svetch({\n  baseUrl: \"/api\", // default is '/api'\n  fetch, // pass the load function's fetch to it\n});\n\nexport async function load({ fetch, session }) {\n  const user = await svetch.get(\"user\").then((response) =\u003e {\n    if (response.error) throw new Error(response.error);\n    return response.data;\n  });\n  return {\n    props: {\n      user,\n    },\n  };\n}\n```\n\n# License\n\nThis library is **Free** for personal use, If it's useful to you, please consider purchasing a license @ https://petrasolutions.lemonsqueezy.com/checkout/buy/19210e05-ae3c-41a0-920c-324e3083618d\nRedistribution/Forking is **Not** Allowed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbewinxed%2Fsvetch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbewinxed%2Fsvetch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbewinxed%2Fsvetch/lists"}