@thepassle/app-tools 0.9.5 → 0.9.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thepassle/app-tools",
3
- "version": "0.9.5",
3
+ "version": "0.9.7",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -51,6 +51,7 @@
51
51
  "default": "./utils/*"
52
52
  },
53
53
  "./env.js": {
54
+ "types": "./types/env/dev.js",
54
55
  "development": "./env/dev.js",
55
56
  "default": "./env/prod.js"
56
57
  },
package/router/index.js CHANGED
@@ -79,11 +79,10 @@ export class Router extends EventTarget {
79
79
 
80
80
  /**
81
81
  * @template RenderResult
82
- * @returns {RenderResult}
83
82
  */
84
83
  render() {
85
- log(`Rendering route ${this.context.url.pathname}${this.context.url.search}`, { context: this.context, route: this.route });
86
- return this.route?.render?.(this.context);
84
+ log(`Rendering route ${this.context.url.pathname}${this.context.url.search}${this.context.url.hash}`, { context: this.context, route: this.route });
85
+ return /** @type {RenderResult} */ (this.route?.render?.(this.context));
87
86
  }
88
87
 
89
88
  /**
@@ -107,7 +106,7 @@ export class Router extends EventTarget {
107
106
  return route;
108
107
  }
109
108
  }
110
- log(`No route matched for ${url.pathname}${url.search}`, url);
109
+ log(`No route matched for ${url.pathname}${url.search}${url.hash}`, url);
111
110
  return null;
112
111
  }
113
112
 
@@ -155,6 +154,13 @@ export class Router extends EventTarget {
155
154
  this.navigate(url);
156
155
  }
157
156
 
157
+ _collectPlugins(route) {
158
+ return [
159
+ ...(this.config?.plugins ?? []),
160
+ ...(route?.plugins ?? []),
161
+ ]
162
+ }
163
+
158
164
  /**
159
165
  * @param {string | URL} url The URL to navigate to.
160
166
  * @param {{
@@ -166,14 +172,11 @@ export class Router extends EventTarget {
166
172
  url = new URL(url, this.baseUrl);
167
173
  }
168
174
 
169
- this.route = this._matchRoute(url) || this._matchRoute(this.fallback);
170
- log(`Navigating to ${url.pathname}${url.search}`, { context: this.context, route: this.route });
175
+ let route = this._matchRoute(url) || this._matchRoute(this.fallback);
176
+ log(`Navigating to ${url.pathname}${url.search}${url.hash}`, { context: this.context, route: this.route });
171
177
 
172
178
  /** @type {Plugin[]} */
173
- const plugins = [
174
- ...(this.config?.plugins ?? []),
175
- ...(this.route?.plugins ?? []),
176
- ];
179
+ let plugins = this._collectPlugins(route);
177
180
 
178
181
  for (const plugin of plugins) {
179
182
  try {
@@ -182,7 +185,8 @@ export class Router extends EventTarget {
182
185
  const condition = await result.condition();
183
186
  if (!condition) {
184
187
  url = new URL(result.redirect, this.baseUrl);
185
- this.route = this._matchRoute(url) || this._matchRoute(this.fallback);
188
+ route = this._matchRoute(url) || this._matchRoute(this.fallback);
189
+ plugins = this._collectPlugins(route);
186
190
  log('Redirecting', { context: this.context, route: this.route });
187
191
  }
188
192
  }
@@ -192,6 +196,8 @@ export class Router extends EventTarget {
192
196
  }
193
197
  }
194
198
 
199
+ this.route = route;
200
+
195
201
  if (!this.route) {
196
202
  throw new Error(`[ROUTER] No route or fallback matched for url ${url}`);
197
203
  }
@@ -206,7 +212,7 @@ export class Router extends EventTarget {
206
212
  }
207
213
 
208
214
  if (!options.backNav) {
209
- window.history.pushState(null, '', `${url.pathname}${url.search}`);
215
+ window.history.pushState(null, '', `${url.pathname}${url.search}${url.hash}`);
210
216
  }
211
217
  document.title = this.context.title;
212
218
  this._notifyUrlChanged();
@@ -0,0 +1,34 @@
1
+ export type ResponseType = 'text' | 'json' | 'stream' | 'blob' | 'arrayBuffer' | 'formData' | 'stream';
2
+ export interface Config {
3
+ plugins?: Plugin[];
4
+ responseType?: ResponseType;
5
+ baseURL?: string;
6
+ }
7
+ export type BodyMethod<R> = (url: string, data?: object, opts?: RequestOptions) => Promise<R>;
8
+ export type BodylessMethod<R> = (url: string, opts?: RequestOptions) => Promise<R>;
9
+ export type Method = 'GET' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'POST' | 'PUT' | 'PATCH';
10
+ export interface Plugin {
11
+ beforeFetch?: (meta: MetaParams) => MetaParams | Promise<MetaParams> | void;
12
+ afterFetch?: (res: Response) => void | Promise<void> | Response | Promise<Response>;
13
+ transform?: (data: any) => any;
14
+ name: string;
15
+ handleError?: (e: Error) => boolean;
16
+ }
17
+ export interface CustomRequestOptions {
18
+ transform?: (data: object) => object;
19
+ responseType?: ResponseType;
20
+ params?: Record<string, string>;
21
+ plugins?: Plugin[];
22
+ baseURL?: string;
23
+ }
24
+ export type RequestOptions = RequestInit & CustomRequestOptions;
25
+ export interface MetaParams {
26
+ responseType: string;
27
+ baseURL: string;
28
+ url: string;
29
+ method: Method;
30
+ headers: Headers;
31
+ opts?: RequestOptions;
32
+ data?: any;
33
+ fetchFn: typeof globalThis.fetch;
34
+ }
@@ -0,0 +1,19 @@
1
+ export type DialogNode = HTMLDialogElement & {
2
+ form: HTMLFormElement;
3
+ };
4
+ export interface DialogCallbacks {
5
+ opening?: <Parameters>(context: Context) => void;
6
+ opened?: <Parameters>(context: Context) => void;
7
+ closing?: (context: Context) => void;
8
+ closed?: (context: Context) => void;
9
+ }
10
+ export type Config = Record<string, DialogCallbacks>;
11
+ export interface OpenDialogOptions {
12
+ id: string;
13
+ parameters?: object;
14
+ }
15
+ export interface Context {
16
+ dialog: DialogNode | undefined;
17
+ id: string;
18
+ parameters?: Record<string, any>;
19
+ }
@@ -25,7 +25,6 @@ export class Router extends EventTarget {
25
25
  get baseUrl(): URL;
26
26
  /**
27
27
  * @template RenderResult
28
- * @returns {RenderResult}
29
28
  */
30
29
  render<RenderResult>(): RenderResult;
31
30
  /**
@@ -0,0 +1,32 @@
1
+ export interface Config {
2
+ fallback?: string;
3
+ plugins?: Plugin[];
4
+ routes: RouteDefinition[];
5
+ }
6
+ export interface Plugin {
7
+ name: string;
8
+ shouldNavigate?: (context: Context) => {
9
+ redirect: string;
10
+ condition: () => boolean | (() => Promise<Boolean>);
11
+ };
12
+ beforeNavigation?: (context: Context) => void;
13
+ afterNavigation?: (context: Context) => void;
14
+ }
15
+ export interface Context {
16
+ title?: string;
17
+ query: Record<string, string>;
18
+ params: Record<string, string>;
19
+ url: URL;
20
+ [key: string]: any;
21
+ }
22
+ export type Render<RenderResult> = (context: Context) => RenderResult;
23
+ export interface RouteDefinition<RenderResult = unknown> {
24
+ path: string;
25
+ title: string | ((context: Partial<Context>) => string);
26
+ render?: Render<RenderResult>;
27
+ plugins?: Plugin[];
28
+ }
29
+ /** @TODO URLPattern type */
30
+ export type Route = RouteDefinition & {
31
+ urlPattern?: any;
32
+ };