revojs 0.0.87 → 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.
@@ -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";
@@ -30,7 +31,11 @@ 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
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>>;
33
36
  export declare function useCookies(scope: Scope): Record<string, string>;
37
+ export declare function useCookies<T extends Schema>(scope: Scope, schema: T): InferOutput<T>;
34
38
  export declare function useSetCookies(scope: Scope): Record<string, string>;
39
+ export declare function useSetCookies<T extends Schema>(scope: Scope, schema: T): InferOutput<T>;
35
40
  export declare function setCookie(scope: Scope, name: string, value: string, options?: CookieOptions): void;
36
41
  export declare function mimeType(file: string): MimeType;
package/dist/index.js CHANGED
@@ -122,6 +122,127 @@ function namespace(tag) {
122
122
  function isFailure(result) {
123
123
  return "issues" in result;
124
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();
125
246
 
126
247
  //#endregion
127
248
  //#region src/http/index.ts
@@ -163,12 +284,22 @@ function useUrl(scope, base) {
163
284
  function useQuery(scope, schema) {
164
285
  const { searchParams } = useUrl(scope);
165
286
  const entries = Object.fromEntries(searchParams);
166
- if (schema) {
167
- const result = schema["~standard"].validate(entries);
168
- if (isFailure(result)) throw sendBadRequest(scope, result.issues.join(", "));
169
- return result.value;
170
- }
171
- return entries;
287
+ return schema ? parseSchema(scope, schema, entries) : entries;
288
+ }
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;
172
303
  }
173
304
  function useCookies(scope, schema) {
174
305
  const { request } = useRuntime(scope);
@@ -177,12 +308,7 @@ function useCookies(scope, schema) {
177
308
  if (name && value) result[name] = decodeURIComponent(value);
178
309
  return result;
179
310
  }, {});
180
- if (schema) {
181
- const result = schema["~standard"].validate(entries);
182
- if (isFailure(result)) throw sendBadRequest(scope, result.issues.join(", "));
183
- return result.value;
184
- }
185
- return entries;
311
+ return schema ? parseSchema(scope, schema, entries) : entries;
186
312
  }
187
313
  function useSetCookies(scope, schema) {
188
314
  const { request } = useRuntime(scope);
@@ -191,12 +317,7 @@ function useSetCookies(scope, schema) {
191
317
  if (name && value) result[name] = decodeURIComponent(value);
192
318
  return result;
193
319
  }, {});
194
- if (schema) {
195
- const result = schema["~standard"].validate(entries);
196
- if (isFailure(result)) throw sendBadRequest(scope, result.issues.join(", "));
197
- return result.value;
198
- }
199
- return entries;
320
+ return schema ? parseSchema(scope, schema, entries) : entries;
200
321
  }
201
322
  function setCookie(scope, name, value, options) {
202
323
  const { response } = useRuntime(scope);
@@ -302,122 +423,6 @@ var Radix = class Radix {
302
423
  }
303
424
  };
304
425
 
305
- //#endregion
306
- //#region src/signals/index.ts
307
- var StopEvent = class extends Event {
308
- constructor() {
309
- super("stop");
310
- }
311
- };
312
- var Scope = class extends EventTarget {
313
- parentScope;
314
- context;
315
- constructor(parentScope) {
316
- super();
317
- this.parentScope = parentScope;
318
- this.parentScope?.onStop(() => this.stop());
319
- this.context = /* @__PURE__ */ new Map();
320
- }
321
- getContext(input) {
322
- let scope = this;
323
- while (scope) {
324
- if (scope.context.has(input)) return scope.context.get(input);
325
- scope = scope.parentScope;
326
- }
327
- return {};
328
- }
329
- setContext(input, value) {
330
- this.context.set(input, value);
331
- }
332
- onStop(input) {
333
- this.addEventListener("stop", input, { once: true });
334
- }
335
- stop() {
336
- return this.dispatchEvent(new StopEvent());
337
- }
338
- };
339
- var Compute = class extends Scope {
340
- invoke;
341
- constructor(parentScope, invoke) {
342
- super(parentScope);
343
- this.invoke = invoke;
344
- }
345
- run() {
346
- this.stop();
347
- return this.invoke(this);
348
- }
349
- };
350
- var Handler = class Handler {
351
- get(target, key) {
352
- const compute = activeCompute;
353
- if (compute) {
354
- const computes = targets.get(target) ?? /* @__PURE__ */ new Map();
355
- const set = computes.get(key) ?? /* @__PURE__ */ new Set();
356
- computes.set(key, set.add(compute));
357
- targets.set(target, computes);
358
- compute.parentScope?.onStop(() => {
359
- set.delete(compute);
360
- if (set.size === 0) {
361
- computes.delete(key);
362
- if (computes.size === 0) targets.delete(target);
363
- }
364
- });
365
- }
366
- const value = Reflect.get(target, key);
367
- if (value) {
368
- if (typeof value === "function" && !value.prototype) return value.bind(target);
369
- if (typeof value === "object") {
370
- const tag = Object.prototype.toString.call(value);
371
- 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());
372
- }
373
- }
374
- return value;
375
- }
376
- set(target, key, value) {
377
- const result = Reflect.set(target, key, value);
378
- for (const compute of targets.get(target)?.get(key) ?? []) compute.run();
379
- return result;
380
- }
381
- };
382
- function createState(value) {
383
- return new Proxy({ value }, new Handler());
384
- }
385
- function createCompute(scope, invoke) {
386
- let previous = activeCompute;
387
- activeCompute = new Compute(scope, invoke);
388
- const result = invoke(activeCompute);
389
- if (result instanceof Promise) return result.finally(() => activeCompute = previous);
390
- activeCompute = previous;
391
- return result;
392
- }
393
- function createMemo(scope, invoke) {
394
- let state;
395
- const compute = createCompute(scope, (scope$1) => {
396
- const value = invoke(scope$1);
397
- if (typeof state === "object") state.value = value;
398
- return value;
399
- });
400
- state = createState(compute);
401
- return state;
402
- }
403
- function fromValue(value) {
404
- if (value instanceof Function) return fromValue(value());
405
- return value;
406
- }
407
- function untrack(invoke) {
408
- let previous = activeCompute;
409
- activeCompute = void 0;
410
- const result = invoke();
411
- if (result instanceof Promise) return result.finally(() => activeCompute = previous);
412
- activeCompute = previous;
413
- return result;
414
- }
415
- function defineContext(key) {
416
- return key;
417
- }
418
- let activeCompute;
419
- const targets = /* @__PURE__ */ new WeakMap();
420
-
421
426
  //#endregion
422
427
  //#region src/runtime/index.ts
423
428
  function isRoute(value) {
@@ -1036,4 +1041,4 @@ function useLocale(scope, context) {
1036
1041
  const LOCALE_CONTEXT = defineContext("LOCALE_CONTEXT");
1037
1042
 
1038
1043
  //#endregion
1039
- 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, 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 };
@@ -1,3 +1,4 @@
1
+ import type { Scope } from "../signals";
1
2
  export type Issue = {
2
3
  readonly message: string;
3
4
  };
@@ -10,7 +11,7 @@ export type Failure = {
10
11
  export type Result<Output> = Success<Output> | Failure;
11
12
  export type Schema<T = unknown, TOutput = T> = {
12
13
  readonly "~standard": {
13
- readonly validate: (value: unknown) => Result<TOutput>;
14
+ readonly validate: (value: unknown) => Result<TOutput> | Promise<Result<TOutput>>;
14
15
  readonly types?: {
15
16
  readonly input: T;
16
17
  readonly output: TOutput;
@@ -20,3 +21,4 @@ export type Schema<T = unknown, TOutput = T> = {
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"];
22
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>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.0.87",
3
+ "version": "0.0.88",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",