@domain-first/handlers-svelte 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +72 -2
- package/dist/adapter.d.ts +8 -0
- package/dist/endpoints.d.ts +15 -0
- package/dist/index.cjs +125 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +84 -0
- package/package.json +60 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 alevnyacow
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,73 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Domain First Handlers (SvelteKit)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p>
|
|
4
|
+
<img src="https://img.shields.io/npm/v/%40domain-first%2Fhandlers-svelte" alt="version">
|
|
5
|
+
<img src="https://img.shields.io/badge/TypeScript-ready-3178C6?logo=typescript&logoColor=white?style=for-the-badge" alt="TypeScript ready">
|
|
6
|
+
<img src="https://img.shields.io/badge/semantic--release-angular-e10079?logo=semantic-release" alt="semver">
|
|
7
|
+
<img src="https://img.shields.io/npm/l/%40domain-first%2Fhandlers-svelte" alt="license">
|
|
8
|
+
</p>
|
|
9
|
+
|
|
10
|
+
Domain handlers as SvelteKit `+server.ts` request handlers.
|
|
11
|
+
|
|
12
|
+
## What is it?
|
|
13
|
+
|
|
14
|
+
[`@domain-first/handlers-rest`](https://www.npmjs.com/package/@domain-first/handlers-rest) describes how HTTP reaches your business logic: which request parts to validate and how to map them to a handler. It doesn't know about any framework: an adapter connects it to one.
|
|
15
|
+
|
|
16
|
+
Domain First Handlers (SvelteKit) is **the adapter for SvelteKit server routes**. Endpoints built with it have SvelteKit's own `RequestHandler` type: export them from `+server.ts` directly, or put them all behind one rest parameter route with `svelteEndpoints`, which matches methods and paths, path parameters included.
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
npm i @domain-first/handlers-svelte @domain-first/handlers-rest
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## API
|
|
23
|
+
|
|
24
|
+
The full technical specification lives in [API.md](./API.md).
|
|
25
|
+
|
|
26
|
+
## Guide
|
|
27
|
+
|
|
28
|
+
### How to create endpoints
|
|
29
|
+
|
|
30
|
+
Pass `svelteAdapter` to the endpoint generator.
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { createEndpoint } from "@domain-first/handlers-rest";
|
|
34
|
+
import { svelteAdapter } from "@domain-first/handlers-svelte";
|
|
35
|
+
|
|
36
|
+
const svelteEndpoint = createEndpoint()(svelteAdapter);
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Export an endpoint from `+server.ts` under its method. Folder names of dynamic segments must equal the parameter names of the route path.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
// src/routes/api/users/[userId]/+server.ts
|
|
43
|
+
import type { RequestHandler } from "./$types";
|
|
44
|
+
|
|
45
|
+
export const GET: RequestHandler = svelteEndpoint(findUser, {
|
|
46
|
+
route: { method: "get", path: ["users", ":userId"] }
|
|
47
|
+
})
|
|
48
|
+
.input(() => ({}))
|
|
49
|
+
.mapInput(({ params }) => ({ id: params.userId }));
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### How to serve all endpoints from one route
|
|
53
|
+
|
|
54
|
+
Create a rest parameter route named `[...path]` and pass the endpoints to `svelteEndpoints`. Route paths are relative to the rest parameter folder.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
// src/routes/api/[...path]/+server.ts
|
|
58
|
+
import { svelteEndpoints } from "@domain-first/handlers-svelte";
|
|
59
|
+
|
|
60
|
+
const handler = svelteEndpoints([findUser, findMe, createUser]);
|
|
61
|
+
|
|
62
|
+
export const GET = handler;
|
|
63
|
+
export const POST = handler;
|
|
64
|
+
export const PUT = handler;
|
|
65
|
+
export const PATCH = handler;
|
|
66
|
+
export const DELETE = handler;
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Unknown routes get `404`. Static segments always take priority over parameters, regardless of the order of endpoints: `/api/users/me` goes to `["users", "me"]`, not to `["users", ":userId"]`.
|
|
70
|
+
|
|
71
|
+
## License
|
|
72
|
+
|
|
73
|
+
MIT
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Adapter } from '@domain-first/handlers-rest';
|
|
2
|
+
import type { RequestEvent } from '@sveltejs/kit';
|
|
3
|
+
/**
|
|
4
|
+
* Endpoints built with this adapter have the SvelteKit
|
|
5
|
+
* `RequestHandler` signature: `(event: RequestEvent) => Promise<Response>`,
|
|
6
|
+
* so they can be exported from `+server.ts` directly.
|
|
7
|
+
*/
|
|
8
|
+
export declare const svelteAdapter: Adapter<[event: RequestEvent], Response>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Metadata, RawAPISchemas } from '@domain-first/handlers-rest';
|
|
2
|
+
import type { RequestHandler } from '@sveltejs/kit';
|
|
3
|
+
type Route = {
|
|
4
|
+
metadata: Metadata;
|
|
5
|
+
_api_schemas: RawAPISchemas;
|
|
6
|
+
} & Function;
|
|
7
|
+
/**
|
|
8
|
+
* Combines endpoints into one request handler for
|
|
9
|
+
* a rest parameter route `src/routes/<prefix>/[...path]/+server.ts`.
|
|
10
|
+
*
|
|
11
|
+
* @param routes endpoints to dispatch to
|
|
12
|
+
* @returns SvelteKit request handler
|
|
13
|
+
*/
|
|
14
|
+
export declare const svelteEndpoints: (routes: Array<Route>) => RequestHandler;
|
|
15
|
+
export {};
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
5
|
+
var define = (defs, kind)=>{
|
|
6
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
[kind]: defs[key]
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
define(getters, "get");
|
|
12
|
+
define(values, "value");
|
|
13
|
+
};
|
|
14
|
+
})();
|
|
15
|
+
(()=>{
|
|
16
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
17
|
+
})();
|
|
18
|
+
(()=>{
|
|
19
|
+
__webpack_require__.r = (exports1)=>{
|
|
20
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
21
|
+
value: 'Module'
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
24
|
+
value: true
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
})();
|
|
28
|
+
var __webpack_exports__ = {};
|
|
29
|
+
__webpack_require__.r(__webpack_exports__);
|
|
30
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
+
svelteAdapter: ()=>svelteAdapter,
|
|
32
|
+
svelteEndpoints: ()=>svelteEndpoints
|
|
33
|
+
});
|
|
34
|
+
function jsonResponse(data, init) {
|
|
35
|
+
return new Response(JSON.stringify(data), {
|
|
36
|
+
...init,
|
|
37
|
+
headers: {
|
|
38
|
+
'Content-Type': 'application/json',
|
|
39
|
+
...init?.headers || {}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
const svelteAdapter = {
|
|
44
|
+
input: {
|
|
45
|
+
body: (x)=>x.request.json(),
|
|
46
|
+
pathParams: (x)=>x.params ?? {},
|
|
47
|
+
queryParams: (x)=>Object.fromEntries(x.url.searchParams.entries()),
|
|
48
|
+
cookies: (x)=>x.cookies.getAll().reduce((acc, cur)=>{
|
|
49
|
+
acc[cur.name] = cur.value;
|
|
50
|
+
return acc;
|
|
51
|
+
}, {}),
|
|
52
|
+
formData: (x)=>x.request.formData(),
|
|
53
|
+
headers: (x)=>Object.fromEntries(x.request.headers.entries())
|
|
54
|
+
},
|
|
55
|
+
output: (x, event)=>{
|
|
56
|
+
if (x.success) {
|
|
57
|
+
if (x.cookies) {
|
|
58
|
+
const cookiesConfiguration = x.configuration?.cookies ?? {};
|
|
59
|
+
for (const [name, value] of Object.entries(x.cookies)){
|
|
60
|
+
const cookieConfig = cookiesConfiguration[name];
|
|
61
|
+
event.cookies.set(name, value, cookieConfig ? {
|
|
62
|
+
path: '/',
|
|
63
|
+
maxAge: cookieConfig.maxAge,
|
|
64
|
+
httpOnly: cookieConfig.httpOnly
|
|
65
|
+
} : {
|
|
66
|
+
path: '/'
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return jsonResponse(x.body, {
|
|
71
|
+
headers: x.headers ?? {},
|
|
72
|
+
status: x.statusCode
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return jsonResponse(x.error, {
|
|
76
|
+
status: x.statusCode
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
function matchPath(routePath, path) {
|
|
81
|
+
const segments = routePath.flatMap((segment)=>segment.split('/'));
|
|
82
|
+
if (segments.length !== path.length) return;
|
|
83
|
+
const params = {};
|
|
84
|
+
for (const [index, segment] of segments.entries())if (segment.startsWith(':')) params[segment.slice(1)] = path[index];
|
|
85
|
+
else if (segment !== path[index]) return;
|
|
86
|
+
return params;
|
|
87
|
+
}
|
|
88
|
+
function compareSpecificity(a, b) {
|
|
89
|
+
const isParam = (path)=>path.flatMap((segment)=>segment.split('/')).map((segment)=>segment.startsWith(':'));
|
|
90
|
+
const aParams = isParam(a);
|
|
91
|
+
const bParams = isParam(b);
|
|
92
|
+
for (const [index, aParam] of aParams.entries())if (aParam !== bParams[index]) return aParam ? 1 : -1;
|
|
93
|
+
return 0;
|
|
94
|
+
}
|
|
95
|
+
const svelteEndpoints = (routes)=>async (event)=>{
|
|
96
|
+
const rest = event.params.path;
|
|
97
|
+
if (void 0 === rest) throw new TypeError('svelteEndpoints: route must have a rest parameter named `path`, e.g. `[...path]`');
|
|
98
|
+
const path = '' === rest ? [] : rest.split('/');
|
|
99
|
+
let match;
|
|
100
|
+
for (const route of routes){
|
|
101
|
+
if (route.metadata.route.method.toLowerCase() !== event.request.method.toLowerCase()) continue;
|
|
102
|
+
const pathParams = matchPath(route.metadata.route.path, path);
|
|
103
|
+
if (pathParams && (!match || compareSpecificity(route.metadata.route.path, match.route.metadata.route.path) < 0)) match = {
|
|
104
|
+
route,
|
|
105
|
+
params: pathParams
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
if (!match) return new Response(void 0, {
|
|
109
|
+
status: 404
|
|
110
|
+
});
|
|
111
|
+
const matchedEvent = {
|
|
112
|
+
...event,
|
|
113
|
+
params: match.params
|
|
114
|
+
};
|
|
115
|
+
return match.route(matchedEvent);
|
|
116
|
+
};
|
|
117
|
+
exports.svelteAdapter = __webpack_exports__.svelteAdapter;
|
|
118
|
+
exports.svelteEndpoints = __webpack_exports__.svelteEndpoints;
|
|
119
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
120
|
+
"svelteAdapter",
|
|
121
|
+
"svelteEndpoints"
|
|
122
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
123
|
+
Object.defineProperty(exports, '__esModule', {
|
|
124
|
+
value: true
|
|
125
|
+
});
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
function jsonResponse(data, init) {
|
|
2
|
+
return new Response(JSON.stringify(data), {
|
|
3
|
+
...init,
|
|
4
|
+
headers: {
|
|
5
|
+
'Content-Type': 'application/json',
|
|
6
|
+
...init?.headers || {}
|
|
7
|
+
}
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
const svelteAdapter = {
|
|
11
|
+
input: {
|
|
12
|
+
body: (x)=>x.request.json(),
|
|
13
|
+
pathParams: (x)=>x.params ?? {},
|
|
14
|
+
queryParams: (x)=>Object.fromEntries(x.url.searchParams.entries()),
|
|
15
|
+
cookies: (x)=>x.cookies.getAll().reduce((acc, cur)=>{
|
|
16
|
+
acc[cur.name] = cur.value;
|
|
17
|
+
return acc;
|
|
18
|
+
}, {}),
|
|
19
|
+
formData: (x)=>x.request.formData(),
|
|
20
|
+
headers: (x)=>Object.fromEntries(x.request.headers.entries())
|
|
21
|
+
},
|
|
22
|
+
output: (x, event)=>{
|
|
23
|
+
if (x.success) {
|
|
24
|
+
if (x.cookies) {
|
|
25
|
+
const cookiesConfiguration = x.configuration?.cookies ?? {};
|
|
26
|
+
for (const [name, value] of Object.entries(x.cookies)){
|
|
27
|
+
const cookieConfig = cookiesConfiguration[name];
|
|
28
|
+
event.cookies.set(name, value, cookieConfig ? {
|
|
29
|
+
path: '/',
|
|
30
|
+
maxAge: cookieConfig.maxAge,
|
|
31
|
+
httpOnly: cookieConfig.httpOnly
|
|
32
|
+
} : {
|
|
33
|
+
path: '/'
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return jsonResponse(x.body, {
|
|
38
|
+
headers: x.headers ?? {},
|
|
39
|
+
status: x.statusCode
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return jsonResponse(x.error, {
|
|
43
|
+
status: x.statusCode
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
function matchPath(routePath, path) {
|
|
48
|
+
const segments = routePath.flatMap((segment)=>segment.split('/'));
|
|
49
|
+
if (segments.length !== path.length) return;
|
|
50
|
+
const params = {};
|
|
51
|
+
for (const [index, segment] of segments.entries())if (segment.startsWith(':')) params[segment.slice(1)] = path[index];
|
|
52
|
+
else if (segment !== path[index]) return;
|
|
53
|
+
return params;
|
|
54
|
+
}
|
|
55
|
+
function compareSpecificity(a, b) {
|
|
56
|
+
const isParam = (path)=>path.flatMap((segment)=>segment.split('/')).map((segment)=>segment.startsWith(':'));
|
|
57
|
+
const aParams = isParam(a);
|
|
58
|
+
const bParams = isParam(b);
|
|
59
|
+
for (const [index, aParam] of aParams.entries())if (aParam !== bParams[index]) return aParam ? 1 : -1;
|
|
60
|
+
return 0;
|
|
61
|
+
}
|
|
62
|
+
const svelteEndpoints = (routes)=>async (event)=>{
|
|
63
|
+
const rest = event.params.path;
|
|
64
|
+
if (void 0 === rest) throw new TypeError('svelteEndpoints: route must have a rest parameter named `path`, e.g. `[...path]`');
|
|
65
|
+
const path = '' === rest ? [] : rest.split('/');
|
|
66
|
+
let match;
|
|
67
|
+
for (const route of routes){
|
|
68
|
+
if (route.metadata.route.method.toLowerCase() !== event.request.method.toLowerCase()) continue;
|
|
69
|
+
const pathParams = matchPath(route.metadata.route.path, path);
|
|
70
|
+
if (pathParams && (!match || compareSpecificity(route.metadata.route.path, match.route.metadata.route.path) < 0)) match = {
|
|
71
|
+
route,
|
|
72
|
+
params: pathParams
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
if (!match) return new Response(void 0, {
|
|
76
|
+
status: 404
|
|
77
|
+
});
|
|
78
|
+
const matchedEvent = {
|
|
79
|
+
...event,
|
|
80
|
+
params: match.params
|
|
81
|
+
};
|
|
82
|
+
return match.route(matchedEvent);
|
|
83
|
+
};
|
|
84
|
+
export { svelteAdapter, svelteEndpoints };
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@domain-first/handlers-svelte",
|
|
3
|
+
"description": "SvelteKit Adapters for @domain-first/handlers and +server endpoints",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.cjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.cjs",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"svelte",
|
|
18
|
+
"sveltekit",
|
|
19
|
+
"adapter",
|
|
20
|
+
"domain-first"
|
|
21
|
+
],
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "https://github.com/alevnyacow/domain-first-handlers-svelte.git"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "rslib",
|
|
34
|
+
"check": "biome check --write",
|
|
35
|
+
"dev": "rslib --watch",
|
|
36
|
+
"format": "biome format --write",
|
|
37
|
+
"test": "rstest",
|
|
38
|
+
"test:watch": "rstest --watch"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@biomejs/biome": "^2.4.2",
|
|
42
|
+
"@domain-first/handlers": "^5.0.4",
|
|
43
|
+
"@domain-first/handlers-rest": "^9.2.0",
|
|
44
|
+
"@rslib/core": "^1.0.7",
|
|
45
|
+
"@rstest/adapter-rslib": "^0.12.6",
|
|
46
|
+
"@rstest/core": "^0.12.6",
|
|
47
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
48
|
+
"@semantic-release/git": "^10.0.1",
|
|
49
|
+
"@semantic-release/npm": "^13.2.0",
|
|
50
|
+
"@standard-schema/spec": "^1.1.0",
|
|
51
|
+
"@sveltejs/kit": "^3.3.1",
|
|
52
|
+
"@types/node": "^24.19.2",
|
|
53
|
+
"semantic-release": "^25.0.9",
|
|
54
|
+
"svelte": "^5.58.0",
|
|
55
|
+
"typescript": "^6.0.3",
|
|
56
|
+
"vite": "^8.3.8"
|
|
57
|
+
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"@sveltejs/kit": ">=2 <4"
|
|
60
|
+
}
|
|
61
|
+
}
|