
Security News
Open Source Maintainers Feeling the Weight of the EU’s Cyber Resilience Act
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
sveltekit-api
Advanced tools
Handles all kinds of SvelteKit data flows in one place, and automatically generate OpenAPI documentation.
Handles all kinds of SvelteKit data flows in one place, and automatically generate OpenAPI documentation.
API
: Manage API endpoints and automatically generate OpenAPI documentationpnpm i -D sveltekit-api
These projects are using SvelteKit-API and can be used as examples:
API
Add $api
to your svelte.config.js
:
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
alias: {
"$api": "./src/api",
},
},
};
Create the API endpoints in the structure like src/api
.
// for example:
src
├── api
│ ├── index.ts
│ └── post
│ ├── GET.ts
│ ├── POST.ts
│ ├── [...id]
│ │ └── GET.ts
│ └── search
│ └── GET.ts
├── lib
│ └── ...
└── routes
└── ...
// file: src/api/index.ts
import { API } from "sveltekit-api";
export default new API(import.meta.glob("./**/*.ts"), {
openapi: "3.0.0",
info: {
title: "Simple Post API",
version: "1.0.0",
description: "An example API",
},
});
// file: src/api/post/[...id]/PUT.ts
import { Endpoint, z, error } from "sveltekit-api";
import { posts, type Post } from "../../db.js";
export const Query = z.object({
password: z.string().optional(),
});
export const Param = z.object({
id: z.string(),
});
export const Input = z.object({
title: z.string(),
content: z.string(),
author: z.string(),
});
export const Output = z.object({
id: z.string(),
title: z.string(),
content: z.string(),
author: z.string(),
date: z.string(),
}) satisfies z.ZodSchema<Post>;
export const Error = {
404: error(404, "Post not found"),
403: error(403, "Forbidden"),
};
export default new Endpoint({ Param, Query, Input, Output, Error }).handle(async (param) => {
const post = posts.get(param.id);
if (!post) {
throw Error[404];
}
if (post.password && post.password !== param.password) {
throw Error[403];
}
post.title = param.title;
post.content = param.content;
post.author = param.author;
return post;
});
Call the API handler and OpenAPI generator in your routes like src/routes/api
.
// file: src/routes/+server.ts
import api from "$api";
import { json } from "@sveltejs/kit";
export const prerender = true;
export const GET = async (evt) => json(await api.openapi(evt));
// file: src/routes/api/post/+server.ts
import api from "$api";
export const GET = async (evt) => api.handle(evt);
export const POST = async (evt) => api.handle(evt);
export const OPTIONS = async (evt) => api.handle(evt);
0.6.1
1518e995e7da1a8d9ef5964df7385f147e9bc72b
Thanks @JinIgarashi! - fix: clone evt.request in parse_body so request can be accessible in endpoint function laterFAQs
Handles all kinds of SvelteKit data flows in one place, and automatically generate OpenAPI documentation.
The npm package sveltekit-api receives a total of 123 weekly downloads. As such, sveltekit-api popularity was classified as not popular.
We found that sveltekit-api demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.