npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-shauns-api-generator

v0.1.3

Published

Generates a Swagger UI and API client based on your SvelteKit API endpoints defined in +server.ts files

Downloads

6

Readme

Contributors Stargazers Issues MIT License

About The Project

The main goal of this project is to provide type-safety for SvelteKit's API endpoints which can be defined in +server.ts files. This project will generate an OpenAPI schema from your API endpoints and provide a Swagger UI to explore your API. It will also generate a type-safe client library for your API. This is achieved by using a Vite plugin.

This project is still in the early stages of development. It is not yet ready for production use. Please feel free to contribute!

The inspiration for this project came from my codebase increasingly relying on SvelteKit's API endpoints for fetching mostly JSON data. Similar to SvelteKit's PageData for load functions, I wanted "automatic" type safety for my API endpoints. In addition, as my codebase grows in size, I wanted to use Swagger UI to explore my API endpoints without manually defining an OpenAPI schema.

Why is this a Vite plugin?

This project is a Vite plugin because it needs to be able to parse your SvelteKit API endpoints. Once an OpenAPI schema is generated, a Swagger UI is served - this is made possible by using the configureServer hook provided by the Vite Plugin API.

Built With

  • Vite
  • TS Morph
  • Swagger UI

Getting Started

A vite plugin is used to generate the OpenAPI schema and client library. The plugin will also serve the Swagger UI.

Prerequisites

  • Create a SvelteKit project, if not already existing
    npm create svelte@latest
  • Use TypeScript to define your +server.ts files in your SvelteKit project

Installation

  1. Install the plugin

    npm install --save-dev vite-plugin-sveltekit-api-generator
  2. Add the plugin to your vite.config.ts file

    import { defineConfig } from 'vite';
    import { sveltekit } from '@sveltejs/kit/vite';
       
    import sveltekitApiGenerator from 'vite-plugin-sveltekit-api-generator';
    
    // https://vitejs.dev/config/
    export default defineConfig({
        plugins: [sveltekit(), sveltekitApiGenerator()],
    });

Usage

Upon running npm run dev, the vite plugin will:

  1. Generate an OpenAPI schema from your +server.ts files, and serve it at http://localhost:5173/swagger-ui.json
  2. Generate a type-safe client library for your API and create it at src/lib/api.ts
  3. Serve the Swagger UI at http://localhost:5173/swagger-ui

What endpoints are type safe?

Only endpoints that make use of SvelteKit's json() helper function will have type safety for the response body. For example, the following endpoint will have type safety for the response body:

export async function GET({ params }) {
    const { id } = params;
    const user: {
        id: number;
        name: string;
    } = await getUser(id);
    return json(data);
}

If you do not use the json() helper function, the response will be typed as any.

What else is type safe?

If you use url.searchParams.get() or url.searchParams.getAll(), the OpenAPI schema will include query parameters.

Roadmap

  • [ ] Add route parameters to OpenAPI schema
  • [ ] Add type safety for request.formData() and request.json() usage
  • [ ] Redesign API client to leverage types with a smaller, generic runtime
  • [ ] Add tests

See the open issues for a full list of proposed features (and known issues).

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

License

Distributed under the MIT License. See LICENSE.txt for more information.