revojs 0.0.86 → 0.0.88
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/http/index.d.ts +6 -3
- package/dist/index.js +152 -123
- package/dist/schema/index.d.ts +3 -0
- package/package.json +1 -1
package/dist/http/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type InferOutput, type Schema } from "../schema";
|
|
1
2
|
import { Scope } from "../signals";
|
|
2
3
|
export type CookiePriority = "Low" | "Medium" | "High";
|
|
3
4
|
export type CookieSameSite = "Lax" | "Strict" | "None";
|
|
@@ -29,10 +30,12 @@ export declare function sendRedirect(scope: Scope, path: string): Response;
|
|
|
29
30
|
export declare function sendBadRequest(scope: Scope, text: string): Response;
|
|
30
31
|
export declare function sendUnauthorized(scope: Scope): Response;
|
|
31
32
|
export declare function useUrl(scope: Scope, base?: string): URL;
|
|
32
|
-
export declare function useQuery(scope: Scope):
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
export declare function useQuery(scope: Scope): Record<string, string>;
|
|
34
|
+
export declare function useQuery<T extends Schema>(scope: Scope, schema: T): InferOutput<T>;
|
|
35
|
+
export declare function useCookie<T extends Schema>(scope: Scope, name: string, schema: T, options?: CookieOptions): import("..").State<InferOutput<T>>;
|
|
35
36
|
export declare function useCookies(scope: Scope): Record<string, string>;
|
|
37
|
+
export declare function useCookies<T extends Schema>(scope: Scope, schema: T): InferOutput<T>;
|
|
36
38
|
export declare function useSetCookies(scope: Scope): Record<string, string>;
|
|
39
|
+
export declare function useSetCookies<T extends Schema>(scope: Scope, schema: T): InferOutput<T>;
|
|
37
40
|
export declare function setCookie(scope: Scope, name: string, value: string, options?: CookieOptions): void;
|
|
38
41
|
export declare function mimeType(file: string): MimeType;
|
package/dist/index.js
CHANGED
|
@@ -117,6 +117,133 @@ function namespace(tag) {
|
|
|
117
117
|
return svgElements.has(tag) ? "http://www.w3.org/2000/svg" : "http://www.w3.org/1999/xhtml";
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
+
//#endregion
|
|
121
|
+
//#region src/schema/index.ts
|
|
122
|
+
function isFailure(result) {
|
|
123
|
+
return "issues" in result;
|
|
124
|
+
}
|
|
125
|
+
function parseSchema(scope, schema, value) {
|
|
126
|
+
const result = schema["~standard"].validate(value);
|
|
127
|
+
if (isFailure(result)) throw sendBadRequest(scope, result.issues.map((issue) => issue.message).join(", "));
|
|
128
|
+
return result.value;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region src/signals/index.ts
|
|
133
|
+
var StopEvent = class extends Event {
|
|
134
|
+
constructor() {
|
|
135
|
+
super("stop");
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
var Scope = class extends EventTarget {
|
|
139
|
+
parentScope;
|
|
140
|
+
context;
|
|
141
|
+
constructor(parentScope) {
|
|
142
|
+
super();
|
|
143
|
+
this.parentScope = parentScope;
|
|
144
|
+
this.parentScope?.onStop(() => this.stop());
|
|
145
|
+
this.context = /* @__PURE__ */ new Map();
|
|
146
|
+
}
|
|
147
|
+
getContext(input) {
|
|
148
|
+
let scope = this;
|
|
149
|
+
while (scope) {
|
|
150
|
+
if (scope.context.has(input)) return scope.context.get(input);
|
|
151
|
+
scope = scope.parentScope;
|
|
152
|
+
}
|
|
153
|
+
return {};
|
|
154
|
+
}
|
|
155
|
+
setContext(input, value) {
|
|
156
|
+
this.context.set(input, value);
|
|
157
|
+
}
|
|
158
|
+
onStop(input) {
|
|
159
|
+
this.addEventListener("stop", input, { once: true });
|
|
160
|
+
}
|
|
161
|
+
stop() {
|
|
162
|
+
return this.dispatchEvent(new StopEvent());
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
var Compute = class extends Scope {
|
|
166
|
+
invoke;
|
|
167
|
+
constructor(parentScope, invoke) {
|
|
168
|
+
super(parentScope);
|
|
169
|
+
this.invoke = invoke;
|
|
170
|
+
}
|
|
171
|
+
run() {
|
|
172
|
+
this.stop();
|
|
173
|
+
return this.invoke(this);
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
var Handler = class Handler {
|
|
177
|
+
get(target, key) {
|
|
178
|
+
const compute = activeCompute;
|
|
179
|
+
if (compute) {
|
|
180
|
+
const computes = targets.get(target) ?? /* @__PURE__ */ new Map();
|
|
181
|
+
const set = computes.get(key) ?? /* @__PURE__ */ new Set();
|
|
182
|
+
computes.set(key, set.add(compute));
|
|
183
|
+
targets.set(target, computes);
|
|
184
|
+
compute.parentScope?.onStop(() => {
|
|
185
|
+
set.delete(compute);
|
|
186
|
+
if (set.size === 0) {
|
|
187
|
+
computes.delete(key);
|
|
188
|
+
if (computes.size === 0) targets.delete(target);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
const value = Reflect.get(target, key);
|
|
193
|
+
if (value) {
|
|
194
|
+
if (typeof value === "function" && !value.prototype) return value.bind(target);
|
|
195
|
+
if (typeof value === "object") {
|
|
196
|
+
const tag = Object.prototype.toString.call(value);
|
|
197
|
+
if (tag === "[object Object]" || tag === "[object Array]" || tag === "[object Map]" || tag === "[object Set]" || tag === "[object WeakMap]" || tag === "[object WeakSet]") return new Proxy(value, new Handler());
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return value;
|
|
201
|
+
}
|
|
202
|
+
set(target, key, value) {
|
|
203
|
+
const result = Reflect.set(target, key, value);
|
|
204
|
+
for (const compute of targets.get(target)?.get(key) ?? []) compute.run();
|
|
205
|
+
return result;
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
function createState(value) {
|
|
209
|
+
return new Proxy({ value }, new Handler());
|
|
210
|
+
}
|
|
211
|
+
function createCompute(scope, invoke) {
|
|
212
|
+
let previous = activeCompute;
|
|
213
|
+
activeCompute = new Compute(scope, invoke);
|
|
214
|
+
const result = invoke(activeCompute);
|
|
215
|
+
if (result instanceof Promise) return result.finally(() => activeCompute = previous);
|
|
216
|
+
activeCompute = previous;
|
|
217
|
+
return result;
|
|
218
|
+
}
|
|
219
|
+
function createMemo(scope, invoke) {
|
|
220
|
+
let state;
|
|
221
|
+
const compute = createCompute(scope, (scope$1) => {
|
|
222
|
+
const value = invoke(scope$1);
|
|
223
|
+
if (typeof state === "object") state.value = value;
|
|
224
|
+
return value;
|
|
225
|
+
});
|
|
226
|
+
state = createState(compute);
|
|
227
|
+
return state;
|
|
228
|
+
}
|
|
229
|
+
function fromValue(value) {
|
|
230
|
+
if (value instanceof Function) return fromValue(value());
|
|
231
|
+
return value;
|
|
232
|
+
}
|
|
233
|
+
function untrack(invoke) {
|
|
234
|
+
let previous = activeCompute;
|
|
235
|
+
activeCompute = void 0;
|
|
236
|
+
const result = invoke();
|
|
237
|
+
if (result instanceof Promise) return result.finally(() => activeCompute = previous);
|
|
238
|
+
activeCompute = previous;
|
|
239
|
+
return result;
|
|
240
|
+
}
|
|
241
|
+
function defineContext(key) {
|
|
242
|
+
return key;
|
|
243
|
+
}
|
|
244
|
+
let activeCompute;
|
|
245
|
+
const targets = /* @__PURE__ */ new WeakMap();
|
|
246
|
+
|
|
120
247
|
//#endregion
|
|
121
248
|
//#region src/http/index.ts
|
|
122
249
|
function sendText(scope, text) {
|
|
@@ -154,25 +281,43 @@ function useUrl(scope, base) {
|
|
|
154
281
|
const { request } = useRuntime(scope);
|
|
155
282
|
return new URL(request?.url ?? window?.location.href, base);
|
|
156
283
|
}
|
|
157
|
-
function useQuery(scope) {
|
|
284
|
+
function useQuery(scope, schema) {
|
|
158
285
|
const { searchParams } = useUrl(scope);
|
|
159
|
-
|
|
286
|
+
const entries = Object.fromEntries(searchParams);
|
|
287
|
+
return schema ? parseSchema(scope, schema, entries) : entries;
|
|
160
288
|
}
|
|
161
|
-
function
|
|
289
|
+
function useCookie(scope, name, schema, options) {
|
|
290
|
+
const cookies = useCookies(scope);
|
|
291
|
+
const state = createState(parseSchema(scope, schema, cookies[name]));
|
|
292
|
+
createCompute(scope, () => {
|
|
293
|
+
switch (typeof state.value) {
|
|
294
|
+
case "string":
|
|
295
|
+
case "number":
|
|
296
|
+
case "bigint":
|
|
297
|
+
case "boolean":
|
|
298
|
+
case "symbol": return setCookie(scope, name, state.value.toString(), options);
|
|
299
|
+
case "object": return setCookie(scope, name, JSON.stringify(state.value), options);
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
return state;
|
|
303
|
+
}
|
|
304
|
+
function useCookies(scope, schema) {
|
|
162
305
|
const { request } = useRuntime(scope);
|
|
163
|
-
|
|
306
|
+
const entries = (isClient() ? document.cookie : request.headers.get("Cookie") ?? "").split("; ").reduce((result, cookie) => {
|
|
164
307
|
const [name, value] = cookie.split("=");
|
|
165
308
|
if (name && value) result[name] = decodeURIComponent(value);
|
|
166
309
|
return result;
|
|
167
310
|
}, {});
|
|
311
|
+
return schema ? parseSchema(scope, schema, entries) : entries;
|
|
168
312
|
}
|
|
169
|
-
function useSetCookies(scope) {
|
|
313
|
+
function useSetCookies(scope, schema) {
|
|
170
314
|
const { request } = useRuntime(scope);
|
|
171
|
-
|
|
315
|
+
const entries = request.headers.getSetCookie().reduce((result, cookie) => {
|
|
172
316
|
const [name, value] = cookie.split("=");
|
|
173
317
|
if (name && value) result[name] = decodeURIComponent(value);
|
|
174
318
|
return result;
|
|
175
319
|
}, {});
|
|
320
|
+
return schema ? parseSchema(scope, schema, entries) : entries;
|
|
176
321
|
}
|
|
177
322
|
function setCookie(scope, name, value, options) {
|
|
178
323
|
const { response } = useRuntime(scope);
|
|
@@ -278,122 +423,6 @@ var Radix = class Radix {
|
|
|
278
423
|
}
|
|
279
424
|
};
|
|
280
425
|
|
|
281
|
-
//#endregion
|
|
282
|
-
//#region src/signals/index.ts
|
|
283
|
-
var StopEvent = class extends Event {
|
|
284
|
-
constructor() {
|
|
285
|
-
super("stop");
|
|
286
|
-
}
|
|
287
|
-
};
|
|
288
|
-
var Scope = class extends EventTarget {
|
|
289
|
-
parentScope;
|
|
290
|
-
context;
|
|
291
|
-
constructor(parentScope) {
|
|
292
|
-
super();
|
|
293
|
-
this.parentScope = parentScope;
|
|
294
|
-
this.parentScope?.onStop(() => this.stop());
|
|
295
|
-
this.context = /* @__PURE__ */ new Map();
|
|
296
|
-
}
|
|
297
|
-
getContext(input) {
|
|
298
|
-
let scope = this;
|
|
299
|
-
while (scope) {
|
|
300
|
-
if (scope.context.has(input)) return scope.context.get(input);
|
|
301
|
-
scope = scope.parentScope;
|
|
302
|
-
}
|
|
303
|
-
return {};
|
|
304
|
-
}
|
|
305
|
-
setContext(input, value) {
|
|
306
|
-
this.context.set(input, value);
|
|
307
|
-
}
|
|
308
|
-
onStop(input) {
|
|
309
|
-
this.addEventListener("stop", input, { once: true });
|
|
310
|
-
}
|
|
311
|
-
stop() {
|
|
312
|
-
return this.dispatchEvent(new StopEvent());
|
|
313
|
-
}
|
|
314
|
-
};
|
|
315
|
-
var Compute = class extends Scope {
|
|
316
|
-
invoke;
|
|
317
|
-
constructor(parentScope, invoke) {
|
|
318
|
-
super(parentScope);
|
|
319
|
-
this.invoke = invoke;
|
|
320
|
-
}
|
|
321
|
-
run() {
|
|
322
|
-
this.stop();
|
|
323
|
-
return this.invoke(this);
|
|
324
|
-
}
|
|
325
|
-
};
|
|
326
|
-
var Handler = class Handler {
|
|
327
|
-
get(target, key) {
|
|
328
|
-
const compute = activeCompute;
|
|
329
|
-
if (compute) {
|
|
330
|
-
const computes = targets.get(target) ?? /* @__PURE__ */ new Map();
|
|
331
|
-
const set = computes.get(key) ?? /* @__PURE__ */ new Set();
|
|
332
|
-
computes.set(key, set.add(compute));
|
|
333
|
-
targets.set(target, computes);
|
|
334
|
-
compute.parentScope?.onStop(() => {
|
|
335
|
-
set.delete(compute);
|
|
336
|
-
if (set.size === 0) {
|
|
337
|
-
computes.delete(key);
|
|
338
|
-
if (computes.size === 0) targets.delete(target);
|
|
339
|
-
}
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
-
const value = Reflect.get(target, key);
|
|
343
|
-
if (value) {
|
|
344
|
-
if (typeof value === "function" && !value.prototype) return value.bind(target);
|
|
345
|
-
if (typeof value === "object") {
|
|
346
|
-
const tag = Object.prototype.toString.call(value);
|
|
347
|
-
if (tag === "[object Object]" || tag === "[object Array]" || tag === "[object Map]" || tag === "[object Set]" || tag === "[object WeakMap]" || tag === "[object WeakSet]") return new Proxy(value, new Handler());
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
return value;
|
|
351
|
-
}
|
|
352
|
-
set(target, key, value) {
|
|
353
|
-
const result = Reflect.set(target, key, value);
|
|
354
|
-
for (const compute of targets.get(target)?.get(key) ?? []) compute.run();
|
|
355
|
-
return result;
|
|
356
|
-
}
|
|
357
|
-
};
|
|
358
|
-
function createState(value) {
|
|
359
|
-
return new Proxy({ value }, new Handler());
|
|
360
|
-
}
|
|
361
|
-
function createCompute(scope, invoke) {
|
|
362
|
-
let previous = activeCompute;
|
|
363
|
-
activeCompute = new Compute(scope, invoke);
|
|
364
|
-
const result = invoke(activeCompute);
|
|
365
|
-
if (result instanceof Promise) return result.finally(() => activeCompute = previous);
|
|
366
|
-
activeCompute = previous;
|
|
367
|
-
return result;
|
|
368
|
-
}
|
|
369
|
-
function createMemo(scope, invoke) {
|
|
370
|
-
let state;
|
|
371
|
-
const compute = createCompute(scope, (scope$1) => {
|
|
372
|
-
const value = invoke(scope$1);
|
|
373
|
-
if (typeof state === "object") state.value = value;
|
|
374
|
-
return value;
|
|
375
|
-
});
|
|
376
|
-
state = createState(compute);
|
|
377
|
-
return state;
|
|
378
|
-
}
|
|
379
|
-
function fromValue(value) {
|
|
380
|
-
if (value instanceof Function) return fromValue(value());
|
|
381
|
-
return value;
|
|
382
|
-
}
|
|
383
|
-
function untrack(invoke) {
|
|
384
|
-
let previous = activeCompute;
|
|
385
|
-
activeCompute = void 0;
|
|
386
|
-
const result = invoke();
|
|
387
|
-
if (result instanceof Promise) return result.finally(() => activeCompute = previous);
|
|
388
|
-
activeCompute = previous;
|
|
389
|
-
return result;
|
|
390
|
-
}
|
|
391
|
-
function defineContext(key) {
|
|
392
|
-
return key;
|
|
393
|
-
}
|
|
394
|
-
let activeCompute;
|
|
395
|
-
const targets = /* @__PURE__ */ new WeakMap();
|
|
396
|
-
|
|
397
426
|
//#endregion
|
|
398
427
|
//#region src/runtime/index.ts
|
|
399
428
|
function isRoute(value) {
|
|
@@ -1012,4 +1041,4 @@ function useLocale(scope, context) {
|
|
|
1012
1041
|
const LOCALE_CONTEXT = defineContext("LOCALE_CONTEXT");
|
|
1013
1042
|
|
|
1014
1043
|
//#endregion
|
|
1015
|
-
export { $fetch, AfterNavigateEvent, CLIENT, Compute, HOST_CONTEXT, Handler, LOCALE_CONTEXT, MountedEvent, NavigateEvent, Page, ROUTER_CONTEXT, ROUTE_CONTEXT, RUNTIME_CONTEXT, Radix, SERVER, STATES, Scope, StopEvent, activeCompute, activeViewTransition, components, createApp, createCompute, createElement, createMemo, createRuntime, createState, defineComponent, defineContext, defineMiddleware, defineRoute, fileName, fromValue, hydrate, isClient, isComponent, isCustomElement, isRoute, isServer, isTemplate, mergeObjects, mimeType, onMounted, onViewTransition, preventDefault, provideLocaleContext, provideRouterContext, registerComponent, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, startViewTransition, stopImmediatePropagation, stopPropagation, targets, toArray, toCustomElement, toFragment, toPath, toRange, toString, untrack, useAssets, useAsync, useCookies, useEvent, useFetch, useHost, useLocale, useLocales, useQuery, useRoute, useRouter, useRoutes, useRuntime, useSetCookies, useState, useUrl };
|
|
1044
|
+
export { $fetch, AfterNavigateEvent, CLIENT, Compute, HOST_CONTEXT, Handler, LOCALE_CONTEXT, MountedEvent, NavigateEvent, Page, ROUTER_CONTEXT, ROUTE_CONTEXT, RUNTIME_CONTEXT, Radix, SERVER, STATES, Scope, StopEvent, activeCompute, activeViewTransition, components, createApp, createCompute, createElement, createMemo, createRuntime, createState, defineComponent, defineContext, defineMiddleware, defineRoute, fileName, fromValue, hydrate, isClient, isComponent, isCustomElement, isFailure, isRoute, isServer, isTemplate, mergeObjects, mimeType, onMounted, onViewTransition, parseSchema, preventDefault, provideLocaleContext, provideRouterContext, registerComponent, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, startViewTransition, stopImmediatePropagation, stopPropagation, targets, toArray, toCustomElement, toFragment, toPath, toRange, toString, untrack, useAssets, useAsync, useCookie, useCookies, useEvent, useFetch, useHost, useLocale, useLocales, useQuery, useRoute, useRouter, useRoutes, useRuntime, useSetCookies, useState, useUrl };
|
package/dist/schema/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Scope } from "../signals";
|
|
1
2
|
export type Issue = {
|
|
2
3
|
readonly message: string;
|
|
3
4
|
};
|
|
@@ -19,3 +20,5 @@ export type Schema<T = unknown, TOutput = T> = {
|
|
|
19
20
|
};
|
|
20
21
|
export type InferInput<T extends Schema> = NonNullable<T["~standard"]["types"]>["input"];
|
|
21
22
|
export type InferOutput<T extends Schema> = NonNullable<T["~standard"]["types"]>["output"];
|
|
23
|
+
export declare function isFailure<T>(result: Result<T>): result is Failure;
|
|
24
|
+
export declare function parseSchema<T extends Schema>(scope: Scope, schema: T, value: unknown): InferOutput<T>;
|