gemi 0.4.18 → 0.4.19
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/dist/app/App.d.ts +0 -1
- package/dist/app/App.d.ts.map +1 -1
- package/dist/app/App.test.d.ts +20 -0
- package/dist/app/App.test.d.ts.map +1 -0
- package/dist/app/createComponentTree.test.d.ts +2 -0
- package/dist/app/createComponentTree.test.d.ts.map +1 -0
- package/dist/app/createFlatApiRoutes.d.ts.map +1 -1
- package/dist/app/createFlatApiRoutes.test.d.ts +2 -0
- package/dist/app/createFlatApiRoutes.test.d.ts.map +1 -0
- package/dist/app/createFlatViewRoutes.test.d.ts +2 -0
- package/dist/app/createFlatViewRoutes.test.d.ts.map +1 -0
- package/dist/app/createRouteManifest.test.d.ts +2 -0
- package/dist/app/createRouteManifest.test.d.ts.map +1 -0
- package/dist/app/index.d.ts +2 -0
- package/dist/app/index.d.ts.map +1 -0
- package/dist/app/index.js +1 -504
- package/dist/chunk-0d0f764773c0f6b8.js +2 -0
- package/dist/chunk-0e3473f7e43f4876.js +2 -0
- package/dist/chunk-847629c9a7550cc3.js +2 -0
- package/dist/email/index.js +1 -68
- package/dist/facades/index.js +1 -59
- package/dist/http/index.js +1 -454
- package/dist/kernel/index.js +2 -13352
- package/dist/server/index.js +5 -6479
- package/dist/vite/index.js +1 -17
- package/package.json +1 -1
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var l="GEMI_REQUEST_BREAKER_ERROR";class a extends Error{constructor(){super(...arguments)}kind="GEMI_REQUEST_BREAKER_ERROR";payload={api:{},view:{}}}import{AsyncLocalStorage as p} from"async_hooks";class e{t;i;s;constructor(t,i,s={}){this.name=t;this.value=i;this.options=s}toString(){return[`${this.name}=${this.value}`,this.options.maxAge?`Max-Age=${this.options.maxAge}`:"",this.options.httpOnly?"HttpOnly":"",this.options.secure?"Secure":"",this.options.sameSite?`SameSite=${this.options.sameSite}`:"SameSite=Strict",this.options.path?`Path=${this.options.path}`:"Path=/",this.options.domain?`Domain=${this.options.domain}`:"",this.options.expires?`Expires=${this.options.expires.toUTCString()}`:"",this.options.partitioned?"Partitioned":""].filter((t)=>t!=="").join("; ")}}var o=new p;class r{cookies=new Set;user=null;req=null;constructor(){}setCookie(t,i,s={}){this.cookies.add(new e(t,i,s))}setUser(t){this.user=t}setRequest(t){this.req=t}}class R{static getStore(){return o.getStore()}static setRequest(t){o.getStore().req=t}static async run(t){return o.run(new r,t)}}import{AsyncLocalStorage as u} from"async_hooks";var n=new u;class c{static getStore=()=>n.getStore()}
|
|
2
|
+
export{l,a as m,R as n,n as o,c as p};
|
package/dist/email/index.js
CHANGED
|
@@ -1,69 +1,2 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
import"../http/index.js";
|
|
3
|
-
import"../facades/index.js";
|
|
4
|
-
import {
|
|
5
|
-
EmailServiceProvider,
|
|
6
|
-
Kernel
|
|
7
|
-
} from "../kernel/index.js";
|
|
8
|
-
import"../chunk-fa40971555060604.js";
|
|
9
|
-
import"../chunk-970e033f764e29fc.js";
|
|
10
|
-
|
|
11
|
-
// email/Email.tsx
|
|
12
|
-
import {renderToStaticMarkup} from "react-dom/server";
|
|
13
|
-
var render = (component) => {
|
|
14
|
-
const doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
|
|
15
|
-
const markup = renderToStaticMarkup(component);
|
|
16
|
-
const document = `${doctype}${markup}`;
|
|
17
|
-
return document;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
class Email {
|
|
21
|
-
static async send(Template, params) {
|
|
22
|
-
const template = new Template;
|
|
23
|
-
const {
|
|
24
|
-
to = template.to,
|
|
25
|
-
from = template.from,
|
|
26
|
-
subject = template.subject,
|
|
27
|
-
cc = template.cc,
|
|
28
|
-
bcc = template.bcc,
|
|
29
|
-
attachments = template.attachments,
|
|
30
|
-
data
|
|
31
|
-
} = params;
|
|
32
|
-
const html = render(template.render(data));
|
|
33
|
-
if (process.env.EMAIL_DEBUG) {
|
|
34
|
-
const fileName = `${process.env.ROOT_DIR}/.debug/emails/${new Date().toISOString()}${subject}.html`;
|
|
35
|
-
await Bun.write(fileName, html);
|
|
36
|
-
Bun.spawnSync(["open", fileName]);
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
await Kernel.getContext().emailServiceProvider.send({
|
|
40
|
-
bcc,
|
|
41
|
-
cc,
|
|
42
|
-
from,
|
|
43
|
-
subject,
|
|
44
|
-
to,
|
|
45
|
-
attachments,
|
|
46
|
-
html
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
// email/EmailTemplate.ts
|
|
51
|
-
import {createElement, Fragment} from "react";
|
|
52
|
-
|
|
53
|
-
class EmailTemplate {
|
|
54
|
-
from = "doe@gemijs.dev";
|
|
55
|
-
to = ["hi@gemijs.dev"];
|
|
56
|
-
subject = "Welcome";
|
|
57
|
-
cc = [];
|
|
58
|
-
bcc = [];
|
|
59
|
-
attachments = [];
|
|
60
|
-
render(_props) {
|
|
61
|
-
console.log("EmailTemplate");
|
|
62
|
-
return createElement(Fragment, null, "EmailTemplate");
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
export {
|
|
66
|
-
EmailTemplate,
|
|
67
|
-
EmailServiceProvider,
|
|
68
|
-
Email
|
|
69
|
-
};
|
|
2
|
+
import"../http/index.js";import"../facades/index.js";import{h as w,i as c} from"../kernel/index.js";import"../chunk-847629c9a7550cc3.js";import"../chunk-0d0f764773c0f6b8.js";import{renderToStaticMarkup as u} from"react-dom/server";var x=(t)=>{return`<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">${u(t)}`};class s{static async send(t,r){const e=new t,{to:a=e.to,from:l=e.from,subject:m=e.subject,cc:p=e.cc,bcc:T=e.bcc,attachments:d=e.attachments,data:E}=r,n=x(e.render(E));if(process.env.EMAIL_DEBUG){const o=`${process.env.ROOT_DIR}/.debug/emails/${(new Date()).toISOString()}${m}.html`;await Bun.write(o,n),Bun.spawnSync(["open",o]);return}await c.getContext().emailServiceProvider.send({bcc:T,cc:p,from:l,subject:m,to:a,attachments:d,html:n})}}import{createElement as f,Fragment as g} from"react";class i{from="doe@gemijs.dev";to=["hi@gemijs.dev"];subject="Welcome";cc=[];bcc=[];attachments=[];render(t){return console.log("EmailTemplate"),f(g,null,"EmailTemplate")}}export{i as EmailTemplate,w as EmailServiceProvider,s as Email};
|
package/dist/facades/index.js
CHANGED
|
@@ -1,60 +1,2 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
import {
|
|
3
|
-
KernelContext,
|
|
4
|
-
RequestBreakerError,
|
|
5
|
-
RequestContext
|
|
6
|
-
} from "../chunk-fa40971555060604.js";
|
|
7
|
-
import"../chunk-970e033f764e29fc.js";
|
|
8
|
-
|
|
9
|
-
// facades/Auth.ts
|
|
10
|
-
class Auth {
|
|
11
|
-
static async user() {
|
|
12
|
-
const requestContextStore = RequestContext.getStore();
|
|
13
|
-
const accessToken = requestContextStore.req.cookies.get("access_token");
|
|
14
|
-
let user = requestContextStore.user;
|
|
15
|
-
if (!user) {
|
|
16
|
-
const adapter = KernelContext.getStore().authenticationServiceProvider.adapter;
|
|
17
|
-
const session = await adapter.findSession({
|
|
18
|
-
token: accessToken,
|
|
19
|
-
userAgent: requestContextStore.req.headers.get("User-Agent")
|
|
20
|
-
});
|
|
21
|
-
user = session?.user;
|
|
22
|
-
requestContextStore.setUser(user);
|
|
23
|
-
}
|
|
24
|
-
if (user) {
|
|
25
|
-
return user;
|
|
26
|
-
}
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
// facades/Redirect.ts
|
|
31
|
-
class RedirectError extends RequestBreakerError {
|
|
32
|
-
constructor(path) {
|
|
33
|
-
super("Redirect error");
|
|
34
|
-
this.name = "RedirectError";
|
|
35
|
-
this.payload = {
|
|
36
|
-
api: {
|
|
37
|
-
status: 302,
|
|
38
|
-
data: { error: "Redirect error" }
|
|
39
|
-
},
|
|
40
|
-
view: {
|
|
41
|
-
status: 302,
|
|
42
|
-
headers: {
|
|
43
|
-
"Cache-Control": "private, no-cache, no-store, max-age=0, must-revalidate",
|
|
44
|
-
Location: path
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
class Redirect {
|
|
52
|
-
static to(path) {
|
|
53
|
-
throw new RedirectError(path);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
export {
|
|
57
|
-
Redirect,
|
|
58
|
-
Auth
|
|
59
|
-
};
|
|
60
|
-
export { Auth };
|
|
2
|
+
import{m as d,n as c,p as i} from"../chunk-847629c9a7550cc3.js";import"../chunk-0d0f764773c0f6b8.js";class o{static async user(){const r=c.getStore(),p=r.req.cookies.get("access_token");let a=r.user;if(!a)a=(await i.getStore().authenticationServiceProvider.adapter.findSession({token:p,userAgent:r.req.headers.get("User-Agent")}))?.user,r.setUser(a);if(a)return a;return null}}class m extends d{constructor(r){super("Redirect error");this.name="RedirectError",this.payload={api:{status:302,data:{error:"Redirect error"}},view:{status:302,headers:{"Cache-Control":"private, no-cache, no-store, max-age=0, must-revalidate",Location:r}}}}}class h{static to(r){throw new m(r)}}export{h as Redirect,o as Auth};export{o as g};
|
package/dist/http/index.js
CHANGED
|
@@ -1,455 +1,2 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
import {
|
|
3
|
-
KernelContext,
|
|
4
|
-
RequestBreakerError,
|
|
5
|
-
RequestContext
|
|
6
|
-
} from "../chunk-fa40971555060604.js";
|
|
7
|
-
import"../chunk-970e033f764e29fc.js";
|
|
8
|
-
|
|
9
|
-
// http/Controller.ts
|
|
10
|
-
class Controller {
|
|
11
|
-
requests = {};
|
|
12
|
-
static kind = "controller";
|
|
13
|
-
constructor() {
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
class ResourceController extends Controller {
|
|
18
|
-
constructor() {
|
|
19
|
-
super(...arguments);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
// internal/isConstructor.ts
|
|
23
|
-
function isConstructor(value) {
|
|
24
|
-
return typeof value === "function" && value.prototype !== undefined;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// http/ApiRouter.ts
|
|
28
|
-
var isController = function(candidate) {
|
|
29
|
-
return isConstructor(candidate);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
class RouteHandler {
|
|
33
|
-
method;
|
|
34
|
-
handler;
|
|
35
|
-
methodName;
|
|
36
|
-
middlewares = [];
|
|
37
|
-
constructor(method, handler, methodName) {
|
|
38
|
-
this.method = method;
|
|
39
|
-
this.handler = handler;
|
|
40
|
-
this.methodName = methodName;
|
|
41
|
-
this.handler = handler;
|
|
42
|
-
this.methodName = methodName;
|
|
43
|
-
this.method = method;
|
|
44
|
-
}
|
|
45
|
-
run(req) {
|
|
46
|
-
let httpRequest = req;
|
|
47
|
-
if (isController(this.handler)) {
|
|
48
|
-
const controller = new this.handler;
|
|
49
|
-
const handler = controller[this.methodName].bind(controller);
|
|
50
|
-
httpRequest = controller.requests[this.methodName] ? new controller.requests[this.methodName](req.rawRequest, req.params) : httpRequest;
|
|
51
|
-
return handler(httpRequest);
|
|
52
|
-
} else {
|
|
53
|
-
return this.handler(req);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
middleware(middlewareList) {
|
|
57
|
-
this.middlewares = middlewareList;
|
|
58
|
-
return this;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
class ApiRouter {
|
|
63
|
-
routes = {};
|
|
64
|
-
middlewares = [];
|
|
65
|
-
middleware(_req) {
|
|
66
|
-
}
|
|
67
|
-
get(handler, methodName) {
|
|
68
|
-
return new RouteHandler("GET", handler, methodName);
|
|
69
|
-
}
|
|
70
|
-
post(handler, methodName) {
|
|
71
|
-
return new RouteHandler("POST", handler, methodName);
|
|
72
|
-
}
|
|
73
|
-
put(handler, methodName) {
|
|
74
|
-
return new RouteHandler("PUT", handler, methodName);
|
|
75
|
-
}
|
|
76
|
-
patch(handler, methodName) {
|
|
77
|
-
return new RouteHandler("PATCH", handler, methodName);
|
|
78
|
-
}
|
|
79
|
-
delete(handler, methodName) {
|
|
80
|
-
return new RouteHandler("DELETE", handler, methodName);
|
|
81
|
-
}
|
|
82
|
-
resource(Controller2) {
|
|
83
|
-
|
|
84
|
-
class ResourceRouter extends ApiRouter {
|
|
85
|
-
constructor() {
|
|
86
|
-
super(...arguments);
|
|
87
|
-
}
|
|
88
|
-
routes = {
|
|
89
|
-
"/": {
|
|
90
|
-
list: this.get(Controller2, "list"),
|
|
91
|
-
create: this.post(Controller2, "create")
|
|
92
|
-
},
|
|
93
|
-
"/:id": {
|
|
94
|
-
show: this.get(Controller2, "show"),
|
|
95
|
-
update: this.put(Controller2, "update"),
|
|
96
|
-
delete: this.delete(Controller2, "delete")
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
return ResourceRouter;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
// http/ViewRouter.ts
|
|
104
|
-
class ViewRouter {
|
|
105
|
-
routes = {};
|
|
106
|
-
middlewares = [];
|
|
107
|
-
middleware(req) {
|
|
108
|
-
}
|
|
109
|
-
layout(viewPath, handler, children) {
|
|
110
|
-
function prepare(middlewares = []) {
|
|
111
|
-
let _children = children ?? {};
|
|
112
|
-
if (handler && handler.constructor === Object) {
|
|
113
|
-
_children = handler;
|
|
114
|
-
}
|
|
115
|
-
return {
|
|
116
|
-
exec: async (req) => {
|
|
117
|
-
let _handler = () => Promise.resolve({
|
|
118
|
-
data: { [viewPath]: {} },
|
|
119
|
-
headers: {},
|
|
120
|
-
head: {}
|
|
121
|
-
});
|
|
122
|
-
if (typeof handler === "function") {
|
|
123
|
-
_handler = handler;
|
|
124
|
-
}
|
|
125
|
-
if (Array.isArray(handler)) {
|
|
126
|
-
const [controller, methodName] = handler;
|
|
127
|
-
const instance = new controller(app);
|
|
128
|
-
_handler = instance[methodName].bind(instance);
|
|
129
|
-
}
|
|
130
|
-
const data = await _handler(req);
|
|
131
|
-
return { [viewPath]: data };
|
|
132
|
-
},
|
|
133
|
-
children: _children,
|
|
134
|
-
viewPath,
|
|
135
|
-
middlewares,
|
|
136
|
-
kind: "layout"
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
return {
|
|
140
|
-
prepare,
|
|
141
|
-
middleware: (middlewares) => ({
|
|
142
|
-
prepare: () => prepare(middlewares)
|
|
143
|
-
})
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
view(viewPath, handler, children) {
|
|
147
|
-
function prepare(middlewares = []) {
|
|
148
|
-
let _children = children ?? {};
|
|
149
|
-
if (handler && handler.constructor === Object) {
|
|
150
|
-
_children = handler;
|
|
151
|
-
}
|
|
152
|
-
return {
|
|
153
|
-
exec: async (req) => {
|
|
154
|
-
let _handler = () => Promise.resolve({
|
|
155
|
-
data: { [viewPath]: {} },
|
|
156
|
-
headers: {},
|
|
157
|
-
head: {}
|
|
158
|
-
});
|
|
159
|
-
if (typeof handler === "function") {
|
|
160
|
-
_handler = handler;
|
|
161
|
-
}
|
|
162
|
-
if (Array.isArray(handler)) {
|
|
163
|
-
const [controller, methodName] = handler;
|
|
164
|
-
const instance = new controller(app);
|
|
165
|
-
_handler = instance[methodName].bind(instance);
|
|
166
|
-
}
|
|
167
|
-
const data = await _handler(req);
|
|
168
|
-
return { [viewPath]: data };
|
|
169
|
-
},
|
|
170
|
-
children: _children,
|
|
171
|
-
viewPath,
|
|
172
|
-
middlewares,
|
|
173
|
-
kind: "view"
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
return {
|
|
177
|
-
prepare,
|
|
178
|
-
middleware: (middlewares) => ({
|
|
179
|
-
prepare: () => prepare(middlewares)
|
|
180
|
-
})
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
// http/Router.ts
|
|
185
|
-
class AuthenticationError extends RequestBreakerError {
|
|
186
|
-
constructor() {
|
|
187
|
-
super("Authentication error");
|
|
188
|
-
this.name = "AuthenticationError";
|
|
189
|
-
}
|
|
190
|
-
payload = {
|
|
191
|
-
api: {
|
|
192
|
-
status: 401,
|
|
193
|
-
data: { error: "Authentication error" }
|
|
194
|
-
},
|
|
195
|
-
view: {
|
|
196
|
-
status: 302,
|
|
197
|
-
headers: {
|
|
198
|
-
"Cache-Control": "private, no-cache, no-store, max-age=0, must-revalidate",
|
|
199
|
-
Location: "/auth/sign-in"
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
class ValidationError extends RequestBreakerError {
|
|
206
|
-
errors = {};
|
|
207
|
-
constructor(errors) {
|
|
208
|
-
super("Validation error");
|
|
209
|
-
this.name = "ValidationError";
|
|
210
|
-
this.errors = errors;
|
|
211
|
-
this.payload = {
|
|
212
|
-
api: {
|
|
213
|
-
status: 400,
|
|
214
|
-
data: {
|
|
215
|
-
error: {
|
|
216
|
-
kind: "validation_error",
|
|
217
|
-
messages: errors
|
|
218
|
-
}
|
|
219
|
-
},
|
|
220
|
-
headers: {
|
|
221
|
-
"Content-Type": "application/json"
|
|
222
|
-
}
|
|
223
|
-
},
|
|
224
|
-
view: {
|
|
225
|
-
status: 400
|
|
226
|
-
}
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
// http/HttpRequest.ts
|
|
231
|
-
var validate = function(ruleName) {
|
|
232
|
-
const [rule, param] = ruleName.split(":");
|
|
233
|
-
switch (rule) {
|
|
234
|
-
case "required":
|
|
235
|
-
return (value) => {
|
|
236
|
-
return value !== null && value !== undefined && value?.length > 0;
|
|
237
|
-
};
|
|
238
|
-
case "password":
|
|
239
|
-
return (value) => {
|
|
240
|
-
const passwordRegex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).{8,}$/;
|
|
241
|
-
return passwordRegex.test(value);
|
|
242
|
-
};
|
|
243
|
-
case "number":
|
|
244
|
-
return (value) => {
|
|
245
|
-
if (typeof value !== "number")
|
|
246
|
-
return false;
|
|
247
|
-
return !isNaN(value);
|
|
248
|
-
};
|
|
249
|
-
case "min":
|
|
250
|
-
return (value) => {
|
|
251
|
-
return value?.length >= parseInt(param);
|
|
252
|
-
};
|
|
253
|
-
case "max":
|
|
254
|
-
return (value) => {
|
|
255
|
-
return value?.length <= parseInt(param);
|
|
256
|
-
};
|
|
257
|
-
case "email":
|
|
258
|
-
return (value) => {
|
|
259
|
-
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
260
|
-
return emailRegex.test(value);
|
|
261
|
-
};
|
|
262
|
-
default:
|
|
263
|
-
return () => true;
|
|
264
|
-
}
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
class Input {
|
|
268
|
-
data;
|
|
269
|
-
constructor(data) {
|
|
270
|
-
this.data = data;
|
|
271
|
-
}
|
|
272
|
-
get(key) {
|
|
273
|
-
return this.data[key];
|
|
274
|
-
}
|
|
275
|
-
set(key, value) {
|
|
276
|
-
this.data[key] = value;
|
|
277
|
-
}
|
|
278
|
-
has(key) {
|
|
279
|
-
return this.data[key] !== undefined;
|
|
280
|
-
}
|
|
281
|
-
toJSON() {
|
|
282
|
-
return this.data;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
class HttpRequest {
|
|
287
|
-
rawRequest;
|
|
288
|
-
headers;
|
|
289
|
-
cookies;
|
|
290
|
-
schema = {};
|
|
291
|
-
params;
|
|
292
|
-
ctx = RequestContext.getStore();
|
|
293
|
-
constructor(req, params) {
|
|
294
|
-
this.params = params;
|
|
295
|
-
this.rawRequest = req;
|
|
296
|
-
this.headers = req.headers;
|
|
297
|
-
const cookie = this.rawRequest.headers.get("Cookie");
|
|
298
|
-
const cookies = new Map;
|
|
299
|
-
if (cookie) {
|
|
300
|
-
const cookieArray = cookie.split(";");
|
|
301
|
-
for (const c of cookieArray) {
|
|
302
|
-
const [key, value] = c.split("=");
|
|
303
|
-
cookies.set(key.trim(), value.trim());
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
this.cookies = cookies;
|
|
307
|
-
}
|
|
308
|
-
async parseBody() {
|
|
309
|
-
const inputMap = new Input({});
|
|
310
|
-
if (this.rawRequest.headers.get("Content-Type") === "application/json") {
|
|
311
|
-
const body = await this.rawRequest.json();
|
|
312
|
-
for (const [key, value] of Object.entries(body)) {
|
|
313
|
-
inputMap.set(key, value);
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
if (this.rawRequest.headers.get("Content-Type") === "application/x-www-form-urlencoded") {
|
|
317
|
-
const body = await this.rawRequest.formData();
|
|
318
|
-
for (const [key, value] of body) {
|
|
319
|
-
inputMap.set(key, value);
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
if (this.rawRequest.headers.get("Content-Type").startsWith("multipart/form-data")) {
|
|
323
|
-
const body = await this.rawRequest.formData();
|
|
324
|
-
for (const [key, value] of body) {
|
|
325
|
-
if (inputMap.has(key)) {
|
|
326
|
-
const currentValue = inputMap.get(key);
|
|
327
|
-
if (Array.isArray(currentValue)) {
|
|
328
|
-
currentValue.push(value);
|
|
329
|
-
inputMap.set(key, currentValue);
|
|
330
|
-
} else {
|
|
331
|
-
inputMap.set(key, [currentValue, value]);
|
|
332
|
-
}
|
|
333
|
-
} else {
|
|
334
|
-
inputMap.set(key, value);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
return inputMap;
|
|
339
|
-
}
|
|
340
|
-
validateInput(input) {
|
|
341
|
-
const errors = {};
|
|
342
|
-
for (const [key, rules] of Object.entries(this.schema)) {
|
|
343
|
-
for (const [rule, message] of Object.entries(rules)) {
|
|
344
|
-
const validator = validate(rule);
|
|
345
|
-
if (!validator(input.get(key))) {
|
|
346
|
-
if (!errors[key]) {
|
|
347
|
-
errors[key] = [];
|
|
348
|
-
}
|
|
349
|
-
if (rule === "required") {
|
|
350
|
-
errors[key] = [String(message)];
|
|
351
|
-
break;
|
|
352
|
-
} else {
|
|
353
|
-
errors[key].push(String(message));
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
if (Object.keys(errors).length > 0) {
|
|
359
|
-
throw new ValidationError(errors);
|
|
360
|
-
} else {
|
|
361
|
-
return input;
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
async input() {
|
|
365
|
-
return this.validateInput(await this.parseBody());
|
|
366
|
-
}
|
|
367
|
-
async safeInput() {
|
|
368
|
-
const input = await this.parseBody();
|
|
369
|
-
try {
|
|
370
|
-
this.validateInput(input);
|
|
371
|
-
return {
|
|
372
|
-
isValid: true,
|
|
373
|
-
errors: {},
|
|
374
|
-
input
|
|
375
|
-
};
|
|
376
|
-
} catch (err) {
|
|
377
|
-
if (!(err instanceof ValidationError)) {
|
|
378
|
-
throw err;
|
|
379
|
-
}
|
|
380
|
-
return {
|
|
381
|
-
isValid: false,
|
|
382
|
-
errors: err.errors,
|
|
383
|
-
input
|
|
384
|
-
};
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
async terminate(params) {
|
|
388
|
-
throw "not implemented";
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
// http/Middleware.ts
|
|
392
|
-
class Middleware {
|
|
393
|
-
async run(_req) {
|
|
394
|
-
return {};
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
// http/getCookies.ts
|
|
398
|
-
function getCookies(req) {
|
|
399
|
-
const cookies = req.headers.get("cookie");
|
|
400
|
-
if (!cookies) {
|
|
401
|
-
return new Map;
|
|
402
|
-
}
|
|
403
|
-
const _cookies = new Map;
|
|
404
|
-
const cookieStrings = cookies.split(";");
|
|
405
|
-
for (const cookieString of cookieStrings) {
|
|
406
|
-
const [name, value] = cookieString.split("=");
|
|
407
|
-
_cookies.set(name.trim(), value);
|
|
408
|
-
}
|
|
409
|
-
return _cookies;
|
|
410
|
-
}
|
|
411
|
-
// http/MiddlewareServiceProvider.ts
|
|
412
|
-
class MiddlewareServiceProvider {
|
|
413
|
-
aliases = {};
|
|
414
|
-
}
|
|
415
|
-
// http/AuthenticationMiddlware.ts
|
|
416
|
-
class AuthenticationMiddleware extends Middleware {
|
|
417
|
-
constructor() {
|
|
418
|
-
super(...arguments);
|
|
419
|
-
}
|
|
420
|
-
async run(req) {
|
|
421
|
-
const requestContextStore = RequestContext.getStore();
|
|
422
|
-
const accessToken = requestContextStore.req.cookies.get("access_token");
|
|
423
|
-
if (!accessToken) {
|
|
424
|
-
throw new AuthenticationError;
|
|
425
|
-
}
|
|
426
|
-
let user = requestContextStore.user;
|
|
427
|
-
if (!user) {
|
|
428
|
-
const session = await KernelContext.getStore().authenticationServiceProvider.adapter.findSession({
|
|
429
|
-
token: accessToken,
|
|
430
|
-
userAgent: requestContextStore.req.headers.get("User-Agent")
|
|
431
|
-
});
|
|
432
|
-
if (!session) {
|
|
433
|
-
throw new AuthenticationError;
|
|
434
|
-
}
|
|
435
|
-
user = session?.user;
|
|
436
|
-
requestContextStore.setUser(user);
|
|
437
|
-
}
|
|
438
|
-
return {};
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
export {
|
|
442
|
-
getCookies,
|
|
443
|
-
ViewRouter,
|
|
444
|
-
ValidationError,
|
|
445
|
-
ResourceController,
|
|
446
|
-
RequestBreakerError,
|
|
447
|
-
MiddlewareServiceProvider,
|
|
448
|
-
Middleware,
|
|
449
|
-
HttpRequest,
|
|
450
|
-
Controller,
|
|
451
|
-
AuthenticationMiddleware,
|
|
452
|
-
AuthenticationError,
|
|
453
|
-
ApiRouter
|
|
454
|
-
};
|
|
455
|
-
export { Controller, isConstructor, ApiRouter, ViewRouter, HttpRequest, MiddlewareServiceProvider };
|
|
2
|
+
import{m as c,n as T,p as M} from"../chunk-847629c9a7550cc3.js";import"../chunk-0d0f764773c0f6b8.js";class x{requests={};static kind="controller";constructor(){}}class C extends x{constructor(){super(...arguments)}}function P(e){return typeof e==="function"&&e.prototype!==void 0}var b=function(e){return P(e)};class d{e;t;n;middlewares=[];constructor(e,t,n){this.method=e;this.handler=t;this.methodName=n;this.handler=t,this.methodName=n,this.method=e}run(e){let t=e;if(b(this.handler)){const n=new this.handler,r=n[this.methodName].bind(n);return t=n.requests[this.methodName]?new n.requests[this.methodName](e.rawRequest,e.params):t,r(t)}else return this.handler(e)}middleware(e){return this.middlewares=e,this}}class R{routes={};middlewares=[];middleware(e){}get(e,t){return new d("GET",e,t)}post(e,t){return new d("POST",e,t)}put(e,t){return new d("PUT",e,t)}patch(e,t){return new d("PATCH",e,t)}delete(e,t){return new d("DELETE",e,t)}resource(e){class t extends R{constructor(){super(...arguments)}routes={"/":{list:this.get(e,"list"),create:this.post(e,"create")},"/:id":{show:this.get(e,"show"),update:this.put(e,"update"),delete:this.delete(e,"delete")}}}return t}}class f{routes={};middlewares=[];middleware(e){}layout(e,t,n){function r(o=[]){let s=n??{};if(t&&t.constructor===Object)s=t;return{exec:async(i)=>{let a=()=>Promise.resolve({data:{[e]:{}},headers:{},head:{}});if(typeof t==="function")a=t;if(Array.isArray(t)){const[h,m]=t,l=new h(app);a=l[m].bind(l)}const w=await a(i);return{[e]:w}},children:s,viewPath:e,middlewares:o,kind:"layout"}}return{prepare:r,middleware:(o)=>({prepare:()=>r(o)})}}view(e,t,n){function r(o=[]){let s=n??{};if(t&&t.constructor===Object)s=t;return{exec:async(i)=>{let a=()=>Promise.resolve({data:{[e]:{}},headers:{},head:{}});if(typeof t==="function")a=t;if(Array.isArray(t)){const[h,m]=t,l=new h(app);a=l[m].bind(l)}const w=await a(i);return{[e]:w}},children:s,viewPath:e,middlewares:o,kind:"view"}}return{prepare:r,middleware:(o)=>({prepare:()=>r(o)})}}}class p extends c{constructor(){super("Authentication error");this.name="AuthenticationError"}payload={api:{status:401,data:{error:"Authentication error"}},view:{status:302,headers:{"Cache-Control":"private, no-cache, no-store, max-age=0, must-revalidate",Location:"/auth/sign-in"}}}}class u extends c{errors={};constructor(e){super("Validation error");this.name="ValidationError",this.errors=e,this.payload={api:{status:400,data:{error:{kind:"validation_error",messages:e}},headers:{"Content-Type":"application/json"}},view:{status:400}}}}var O=function(e){const[t,n]=e.split(":");switch(t){case"required":return(r)=>{return r!==null&&r!==void 0&&r?.length>0};case"password":return(r)=>{return/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).{8,}$/.test(r)};case"number":return(r)=>{if(typeof r!=="number")return!1;return!isNaN(r)};case"min":return(r)=>{return r?.length>=parseInt(n)};case"max":return(r)=>{return r?.length<=parseInt(n)};case"email":return(r)=>{return/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(r)};default:return()=>!0}};class H{e;constructor(e){this.data=e}get(e){return this.data[e]}set(e,t){this.data[e]=t}has(e){return this.data[e]!==void 0}toJSON(){return this.data}}class g{rawRequest;headers;cookies;schema={};params;ctx=T.getStore();constructor(e,t){this.params=t,this.rawRequest=e,this.headers=e.headers;const n=this.rawRequest.headers.get("Cookie"),r=new Map;if(n){const o=n.split(";");for(let s of o){const[i,a]=s.split("=");r.set(i.trim(),a.trim())}}this.cookies=r}async parseBody(){const e=new H({});if(this.rawRequest.headers.get("Content-Type")==="application/json"){const t=await this.rawRequest.json();for(let[n,r]of Object.entries(t))e.set(n,r)}if(this.rawRequest.headers.get("Content-Type")==="application/x-www-form-urlencoded"){const t=await this.rawRequest.formData();for(let[n,r]of t)e.set(n,r)}if(this.rawRequest.headers.get("Content-Type").startsWith("multipart/form-data")){const t=await this.rawRequest.formData();for(let[n,r]of t)if(e.has(n)){const o=e.get(n);if(Array.isArray(o))o.push(r),e.set(n,o);else e.set(n,[o,r])}else e.set(n,r)}return e}validateInput(e){const t={};for(let[n,r]of Object.entries(this.schema))for(let[o,s]of Object.entries(r))if(!O(o)(e.get(n))){if(!t[n])t[n]=[];if(o==="required"){t[n]=[String(s)];break}else t[n].push(String(s))}if(Object.keys(t).length>0)throw new u(t);else return e}async input(){return this.validateInput(await this.parseBody())}async safeInput(){const e=await this.parseBody();try{return this.validateInput(e),{isValid:!0,errors:{},input:e}}catch(t){if(!(t instanceof u))throw t;return{isValid:!1,errors:t.errors,input:e}}}async terminate(e){throw"not implemented"}}class y{async run(e){return{}}}function I(e){const t=e.headers.get("cookie");if(!t)return new Map;const n=new Map,r=t.split(";");for(let o of r){const[s,i]=o.split("=");n.set(s.trim(),i)}return n}class K{aliases={}}class q extends y{constructor(){super(...arguments)}async run(e){const t=T.getStore(),n=t.req.cookies.get("access_token");if(!n)throw new p;let r=t.user;if(!r){const o=await M.getStore().authenticationServiceProvider.adapter.findSession({token:n,userAgent:t.req.headers.get("User-Agent")});if(!o)throw new p;r=o?.user,t.setUser(r)}return{}}}export{I as getCookies,f as ViewRouter,u as ValidationError,C as ResourceController,c as RequestBreakerError,K as MiddlewareServiceProvider,y as Middleware,g as HttpRequest,x as Controller,q as AuthenticationMiddleware,p as AuthenticationError,R as ApiRouter};export{x as a,P as b,R as c,f as d,g as e,K as f};
|