revojs 0.1.7 → 0.1.8

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/index.d.ts CHANGED
@@ -57,6 +57,7 @@ type StatusCode = 100 | 101 | 102 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 20
57
57
  type MimeType = "text/plain" | "text/css" | "text/html" | "text/csv" | "text/javascript" | "application/json" | "application/xml" | "image/jpeg" | "image/png" | "image/gif" | "image/webp" | "image/svg+xml" | "image/bmp" | "image/x-icon" | "font/ttf" | "font/otf" | "font/woff" | "font/woff2" | "audio/mpeg" | "audio/wav" | "audio/ogg" | "audio/mp4" | "video/mp4" | "video/webm" | "video/ogg" | "video/quicktime" | "video/x-msvideo" | "application/zip" | "application/vnd.rar" | "application/x-tar" | "application/gzip" | "application/x-7z-compressed" | "application/pdf" | "application/msword" | "application/vnd.openxmlformats-officedocument.wordprocessingml.document" | "application/vnd.ms-excel" | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | "application/vnd.ms-powerpoint" | "application/vnd.openxmlformats-officedocument.presentationml.presentation" | "application/vnd.microsoft.portable-executable" | "application/vnd.android.package-archive";
58
58
  type Result = void | Response | Promise<void | Response>;
59
59
  type Node<T> = WildcardNode<T> | ParameterNode<T> | PathNode<T>;
60
+ type States = Record<string, unknown>;
60
61
  interface CookieOptions {
61
62
  domain?: string;
62
63
  expires?: Date;
@@ -78,6 +79,7 @@ interface RouterContext {
78
79
  parameters: Record<string, string>;
79
80
  }
80
81
  interface ServerContext<T extends Context = Context> {
82
+ states: States;
81
83
  request: Request;
82
84
  response: ResponseConfig;
83
85
  variables: T;
@@ -120,6 +122,7 @@ declare class Router extends Radix<Route> implements Middleware {
120
122
  }
121
123
  declare function defineRoute<T extends Route>(route: T): T;
122
124
  declare function defineMiddleware<T extends Middleware>(middleware: T): T;
125
+ declare function useRouter(scope: Scope): RouterContext;
123
126
  declare function useServer<T extends Context>(scope: Scope): ServerContext<T>;
124
127
  declare function useUrl(scope: Scope, base?: string): URL;
125
128
  declare function useQuery(scope: Scope): Record<string, string>;
@@ -129,6 +132,8 @@ declare function useCookies<T extends Schema>(scope: Scope, schema: T): InferOut
129
132
  declare function useSetCookies(scope: Scope): Record<string, string>;
130
133
  declare function useSetCookies<T extends Schema>(scope: Scope, schema: T): InferOutput<T>;
131
134
  declare function setCookie(scope: Scope, name: string, value: string, options?: CookieOptions): void;
135
+ declare function getState<T>(scope: Scope, name: string): T;
136
+ declare function setState<T>(scope: Scope, name: string, value: T): void;
132
137
  declare function sendText(scope: Scope, text: string, config?: Mergeable<ResponseConfig>): Response;
133
138
  declare function sendHtml(scope: Scope, text: string, config?: Mergeable<ResponseConfig>): Response;
134
139
  declare function sendJson<T>(scope: Scope, value: T, config?: Mergeable<ResponseConfig>): Response;
@@ -143,6 +148,7 @@ declare const ROUTER_CONTEXT: Descriptor<RouterContext>;
143
148
  declare const SERVER_CONTEXT: Descriptor<ServerContext<Context>>;
144
149
  declare const WILDCARD = "$";
145
150
  declare const PARAMETER = ":";
151
+ declare let STATES: States;
146
152
  declare const mimeTypes: Record<string, MimeType>;
147
153
  //#endregion
148
154
  //#region src/app/index.d.ts
@@ -180,4 +186,4 @@ declare const CLIENT = "client";
180
186
  //#region src/client/index.d.ts
181
187
  declare function $fetch<T>(scope: Scope, input: string | URL, options?: RequestInit): Promise<T>;
182
188
  //#endregion
183
- export { $fetch, App, CLIENT, Config, Context, CookieOptions, CookiePriority, CookieSameSite, Descriptor, DevelopmentConfig, Encoding, Environment, Failure, HttpMethod, InferInput, InferOutput, Issue, Mergeable, Middleware, MimeType, Module, Node, Output, PARAMETER, ParameterNode, PathNode, ROUTER_CONTEXT, Radix, ResponseConfig, Result, Route, Router, RouterContext, SERVER, SERVER_CONTEXT, Schema, Scope, Server, ServerContext, Source, StatusCode, StopEvent, Success, Virtual, WILDCARD, WildcardNode, createApp, createServer, defineContext, defineMiddleware, defineRoute, invoke, isFailure, mergeObjects, mimeType, mimeTypes, parseSchema, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, toRoutePath, useCookies, useQuery, useServer, useSetCookies, useUrl };
189
+ export { $fetch, App, CLIENT, Config, Context, CookieOptions, CookiePriority, CookieSameSite, Descriptor, DevelopmentConfig, Encoding, Environment, Failure, HttpMethod, InferInput, InferOutput, Issue, Mergeable, Middleware, MimeType, Module, Node, Output, PARAMETER, ParameterNode, PathNode, ROUTER_CONTEXT, Radix, ResponseConfig, Result, Route, Router, RouterContext, SERVER, SERVER_CONTEXT, STATES, Schema, Scope, Server, ServerContext, Source, States, StatusCode, StopEvent, Success, Virtual, WILDCARD, WildcardNode, createApp, createServer, defineContext, defineMiddleware, defineRoute, getState, invoke, isFailure, mergeObjects, mimeType, mimeTypes, parseSchema, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, setState, toRoutePath, useCookies, useQuery, useRouter, useServer, useSetCookies, useUrl };
package/dist/index.js CHANGED
@@ -88,6 +88,9 @@ function defineRoute(route) {
88
88
  function defineMiddleware(middleware) {
89
89
  return middleware;
90
90
  }
91
+ function useRouter(scope) {
92
+ return scope.getContext(ROUTER_CONTEXT);
93
+ }
91
94
  function useServer(scope) {
92
95
  return scope.getContext(SERVER_CONTEXT);
93
96
  }
@@ -132,6 +135,30 @@ function setCookie(scope, name, value, options) {
132
135
  if (import.meta.client) document.cookie = cookie;
133
136
  else response.headers.append("Set-Cookie", cookie);
134
137
  }
138
+ function getState(scope, name) {
139
+ if (import.meta.server) {
140
+ const { states } = useServer(scope);
141
+ return states[name];
142
+ } else {
143
+ if (STATES === void 0) {
144
+ const element = document.getElementById("STATES");
145
+ STATES = element?.textContent ? JSON.parse(element.textContent) : {};
146
+ }
147
+ return STATES[name];
148
+ }
149
+ }
150
+ function setState(scope, name, value) {
151
+ if (import.meta.server) {
152
+ const { states } = useServer(scope);
153
+ states[name] = value;
154
+ } else {
155
+ if (STATES === void 0) {
156
+ const element = document.getElementById("STATES");
157
+ STATES = element?.textContent ? JSON.parse(element.textContent) : {};
158
+ }
159
+ STATES[name] = value;
160
+ }
161
+ }
135
162
  function sendText(scope, text, config) {
136
163
  const { response } = useServer(scope);
137
164
  response.headers.set("Content-Type", "text/plain");
@@ -203,6 +230,7 @@ const ROUTER_CONTEXT = defineContext("ROUTER_CONTEXT");
203
230
  const SERVER_CONTEXT = defineContext("SERVER_CONTEXT");
204
231
  const WILDCARD = "$";
205
232
  const PARAMETER = ":";
233
+ let STATES;
206
234
  const mimeTypes = {
207
235
  txt: "text/plain",
208
236
  css: "text/css",
@@ -346,9 +374,10 @@ const CLIENT = "client";
346
374
  async function $fetch(scope, input, options) {
347
375
  let response;
348
376
  if (import.meta.server) {
349
- const { request, variables } = useServer(scope);
377
+ const { states, request, variables } = useServer(scope);
350
378
  const next = new Scope();
351
379
  next.setContext(SERVER_CONTEXT, {
380
+ states,
352
381
  request: new Request(new URL(input.toString(), request.url), options),
353
382
  response: { headers: new Headers() },
354
383
  variables
@@ -364,4 +393,4 @@ async function $fetch(scope, input, options) {
364
393
  }
365
394
 
366
395
  //#endregion
367
- export { $fetch, CLIENT, PARAMETER, ROUTER_CONTEXT, Radix, Router, SERVER, SERVER_CONTEXT, Scope, StopEvent, WILDCARD, createApp, createServer, defineContext, defineMiddleware, defineRoute, invoke, isFailure, mergeObjects, mimeType, mimeTypes, parseSchema, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, toRoutePath, useCookies, useQuery, useServer, useSetCookies, useUrl };
396
+ export { $fetch, CLIENT, PARAMETER, ROUTER_CONTEXT, Radix, Router, SERVER, SERVER_CONTEXT, STATES, Scope, StopEvent, WILDCARD, createApp, createServer, defineContext, defineMiddleware, defineRoute, getState, invoke, isFailure, mergeObjects, mimeType, mimeTypes, parseSchema, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, setState, toRoutePath, useCookies, useQuery, useRouter, useServer, useSetCookies, useUrl };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",