π¨ DEPRECATED π¨
This repository is deprecated in favour of a new and improved project: sveltekit-api-gen.
Please migrate to the replacement package: sveltekit-openapi-generator and visit the project homepage.
For migration instructions and continued updates, see the new project's GitHub repository.
This project has been superseded by a more robust and automated solution. The new project uses a Vite plugin to automatically generate OpenAPI specs from SvelteKit server endpoints using JSDoc @swagger annotations, offering features like Hot Module Replacement, TypeScript support, SvelteKit-native route handling, shared schemas, and seamless Swagger UI integration.
@swagger JSDoc blocks.npm install -D sveltekit-openapi-generator.vite.config.js (see the new project's README for details).@swagger annotations in your +server.ts/js files.For full instructions, visit the new project's GitHub repository.
A Svelte library that auto-generates OpenAPI (Swagger) documentation for HTTP handlers in SvelteKit projects. This library scans your projectβs routes to create a dynamic, accessible Swagger UI page that documents your API endpoints.
Install the package via npm:
npm install @obele-michael/swagger-ui-svelte
After installing the library, you can start using it by configuring your SvelteKit routes and initializing the OpenAPI generator.
Define HTTP method handlers (e.g., GET, POST) in your SvelteKit route files (+server.ts/js). Hereβs an example:
// src/routes/example/+server.ts
import type { RequestHandler } from './$types';
export const GET: RequestHandler = async () => {
return new Response(JSON.stringify({ message: 'Hello from GET!' }), {
status: 200,
headers: { 'Content-Type': 'application/json' }
});
};
export const POST: RequestHandler = async ({ request }) => {
const data = await request.json();
return new Response(JSON.stringify({ message: 'Received data', data }), {
status: 200,
headers: { 'Content-Type': 'application/json' }
});
};
Run the OpenAPI generator script to scan your routes and create the openapi.json file. The openapi.json is generated in the static folder of your project:
bun run generate:openapi
This command will create or update static/openapi.json, documenting all detected endpoints in your project.
In your SvelteKit app, import and display Swagger UI using the provided component from @obele-michael/swagger-ui-svelte:
<script lang="ts">
import { SwaggerUI } from '@obele-michael/swagger-ui-svelte';
</script>
<SwaggerUI url="/openapi.json" />
After setting up, visit your app to view the Swagger UI, which will render the documentation for your API endpoints based on openapi.json.
GET, POST, PUT, DELETE, PATCH, HEAD, and OPTIONS handlers in SvelteKit.Hereβs a sample project structure to help illustrate the usage:
project-root/
βββ src/
β βββ lib/
β β βββ index.ts # Entry point for the library
β β βββ script/
β β β βββ generateOpenApi.ts # Script for OpenAPI generation
β βββ routes/
β β βββ example/
β β β βββ +server.ts # Route file with HTTP handlers
β βββ app.svelte # Svelte app file
βββ static/
βββ openapi.json # Generated OpenAPI specification
Contributions, issues, and feature requests are welcome! Feel free to check out the issues page if you have any suggestions or run into problems.
This project is licensed under the MIT License.