JacobLinCool / JacobLinCool/sveltekit-api

Overall feedback

Open
#60 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
56
Forks
6
PR merge metrics
No merged PRs in 30d

Description

Hey, I'm looking for ways to implement openapi into sveltekit, I found this repository and I'm currently testing.

I find it pretty interesting, nice work!

There is specially one thing that bothers me, is that you suggest to duplicate routes in different locations.
Inside `routes` and inside a custom folder (you name it `api`). I wonder why you implemented this way.
Doesn't it make more sense to have the routes into a single location only?

For example:
Having a catch all into `routes/api/[...route]` and pointing all requests into the api.
```ts
// routes/api/[...route]/+server.ts

import api from '$api';
import type { RequestHandler } from './$types';

export const GET: RequestHandler = (evt) => api.handle(evt);
export const POST: RequestHandler = (evt) => api.handle(evt);
export const PUT: RequestHandler = (evt) => api.handle(evt);
export const DELETE: RequestHandler = (evt) => api.handle(evt);
export const OPTIONS: RequestHandler = (evt) => api.handle(evt);
```

and then control the routes in a different or the same location.

----

Another thing I just noticed is that this library isn't capable to deal with group routes
E.g.: `/routes/(protected)/api/some-route`
The example above will be read from the library as `(protected)/api/some-route`
![image](https://github.com/JacobLinCool/sveltekit-api/assets/11768109/2551c105-3e04-4336-891b-60242bfe4e6b)

I tried adding a new replace into the `parse_module` function and it seems to work well
[At this line](https://github.com/JacobLinCool/sveltekit-api/blob/main/src/lib/api.ts#L640), add `.replace(/\(.+\)\//g, "")`

----

For last, don't you find it easier to export an object from each route/method instead of exports individual variables?
For someone that isn't aware of the library, it's more difficult to remember each variable to export.
My suggestion would be something like this:

```ts
import api from '$api';

// a function is used for type-safety purposes
export default api.defineEndpoint({
query: z.object({
password: z.string().optional(),
}),
param: z.object({
id: z.string(),
}),
input: z.object({
title: z.string(),
content: z.string(),
author: z.string(),
}),
output: z.object({
id: z.string(),
title: z.string(),
content: z.string(),
author: z.string(),
date: z.string(),
}) satisfies z.ZodSchema,
error: {
404: error(404, "Post not found"),
403: error(403, "Forbidden"),
},
endpoint: 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;
}
})
```

I'm able to fork and submit a pull request if you wish, I had like to discuss first.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reviewing src/lib/api.ts, especially parse_module around line 640, and compare it with the routes/api/[...route]/+server.ts pattern described in the issue. The requested work spans route discovery, grouped routes, and endpoint exports; done would require an agreed design and corresponding implementation scope, which this discussion does not yet define.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.