@thepassle/app-tools 0.7.0 → 0.7.2

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/api/index.js CHANGED
@@ -9,45 +9,12 @@ function handleStatus(response) {
9
9
  return response;
10
10
  }
11
11
 
12
- /**
13
- * @typedef {object} Config
14
- * @property {Plugin[]} [plugins=[]]
15
- * @property {'text'|'json'|'stream'|'blob'|'arrayBuffer'|'formData'|'stream'} [responseType=json]
16
- * @property {string} [baseURL]
17
- *
18
- * @typedef {(url: string, data?: object, opts?: RequestOptions) => Promise<FetchResponse>} BodyMethod
19
- * @typedef {(url: string, opts?: RequestOptions) => Promise<FetchResponse>} BodylessMethod
20
- * @typedef {Response & { [key: string]: any }} FetchResponse
21
- * @typedef {'GET'|'DELETE'|'HEAD'|'OPTIONS'|'POST'|'PUT'|'PATCH'} Method
22
- *
23
- * @typedef {{
24
- * beforeFetch?: (meta: MetaParams) => MetaParams | Promise<MetaParams> | void,
25
- * afterFetch?: (res: Response) => Response | Promise<Response>,
26
- * transform?: (data: any) => any,
27
- * name: string,
28
- * handleError?: (e: Error) => boolean
29
- * }} Plugin
30
- *
31
- * @typedef {Object} CustomRequestOptions
32
- * @property {(data: FetchResponse) => FetchResponse} [transform] - callback to transform the received data
33
- * @property {'text'|'json'|'stream'|'blob'|'arrayBuffer'|'formData'|'stream'} [responseType] - responseType of the request, will call res[responseType](). Defaults to 'json'
34
- * @property {Record<string, string>} [params] - An object to be queryParam-ified and added to the request url
35
- * @property {Plugin[]} [plugins] - Array of plugins. Plugins can be added on global level, or on a per request basis
36
- * @property {string} [baseURL] - BaseURL to resolve all requests from. Can be set globally, or on a per request basis. When set on a per request basis, will override the globally set baseURL (if set)
37
- *
38
- * @typedef {RequestInit & CustomRequestOptions} RequestOptions
39
- *
40
- * @typedef {{
41
- * responseType: string,
42
- * baseURL: string,
43
- * url: string,
44
- * method: Method,
45
- * headers: Headers,
46
- * opts?: RequestOptions,
47
- * data?: any,
48
- * fetchFn: typeof globalThis.fetch
49
- * }} MetaParams
50
- */
12
+ /** @typedef {import('./types.js').Config} Config */
13
+ /** @typedef {import('./types.js').Method} Method */
14
+ /** @typedef {import('./types.js').Plugin} Plugin */
15
+ /** @typedef {import('./types.js').CustomRequestOptions} CustomRequestOptions */
16
+ /** @typedef {import('./types.js').RequestOptions} RequestOptions */
17
+ /** @typedef {import('./types.js').MetaParams} MetaParams */
51
18
 
52
19
  /**
53
20
  * @example
@@ -115,7 +82,17 @@ export class Api {
115
82
  }
116
83
  }
117
84
 
118
- log(`Fetching ${method} ${url}`, { responseType, headers: Object.fromEntries(headers), fetchFn, baseURL, url, method, opts, data });
85
+ log(`Fetching ${method} ${url}`, {
86
+ responseType,
87
+ // @ts-ignore
88
+ headers: Object.fromEntries(headers),
89
+ fetchFn,
90
+ baseURL,
91
+ url,
92
+ method,
93
+ opts,
94
+ data
95
+ });
119
96
  return fetchFn(url, {
120
97
  method,
121
98
  headers,
@@ -167,19 +144,19 @@ export class Api {
167
144
  });
168
145
  }
169
146
 
170
- /** @type {BodylessMethod} */
147
+ /** @type {import('./types.js').BodylessMethod<object>} */
171
148
  get = (url, opts) => this.fetch(url, 'GET', opts);
172
- /** @type {BodylessMethod} */
149
+ /** @type {import('./types.js').BodylessMethod<object>} */
173
150
  options = (url, opts) => this.fetch(url, 'OPTIONS', opts);
174
- /** @type {BodylessMethod} */
151
+ /** @type {import('./types.js').BodylessMethod<object>} */
175
152
  delete = (url, opts) => this.fetch(url, 'DELETE', opts);
176
- /** @type {BodylessMethod} */
153
+ /** @type {import('./types.js').BodylessMethod<object>} */
177
154
  head = (url, opts) => this.fetch(url, 'HEAD', opts);
178
- /** @type {BodyMethod} */
155
+ /** @type {import('./types.js').BodyMethod<object>} */
179
156
  post = (url, data, opts) => this.fetch(url, 'POST', opts, data);
180
- /** @type {BodyMethod} */
157
+ /** @type {import('./types.js').BodyMethod<object>} */
181
158
  put = (url, data, opts) => this.fetch(url, 'PUT', opts, data);
182
- /** @type {BodyMethod} */
159
+ /** @type {import('./types.js').BodyMethod<object>} */
183
160
  patch = (url, data, opts) => this.fetch(url, 'PATCH', opts, data);
184
161
  }
185
162
 
package/dialog/index.js CHANGED
@@ -6,13 +6,10 @@ import { createLogger } from '../utils/log.js';
6
6
  const log = createLogger('dialog');
7
7
 
8
8
  /**
9
- * @typedef {HTMLDialogElement & { form: HTMLFormElement }} DialogNode
10
- * @typedef {Record<string, {
11
- * opening?: <Parameters>(opts: {dialog: DialogNode, parameters: Parameters}) => void,
12
- * opened?: <Parameters>(opts: {dialog: DialogNode, parameters: Parameters}) => void,
13
- * closing?: (opts: {dialog: DialogNode}) => void,
14
- * closed?: (opts: {dialog: DialogNode}) => void,
15
- * }>} Config
9
+ * @typedef {import('./types.js').DialogNode} DialogNode
10
+ * @typedef {import('./types.js').DialogCallbacks} DialogCallbacks
11
+ * @typedef {import('./types.js').Config} Config
12
+ * @typedef {import('./types.js').OpenDialogOptions} OpenDialogOptions
16
13
  */
17
14
 
18
15
  setupGlobalDialogStyles();
@@ -118,11 +115,7 @@ export class Dialog extends EventTarget {
118
115
  }
119
116
 
120
117
  /**
121
- *
122
- * @param {{
123
- * id: string,
124
- * parameters?: object
125
- * }} options
118
+ * @param {OpenDialogOptions} options
126
119
  * @returns
127
120
  */
128
121
  async open({id, parameters}) {
package/package.json CHANGED
@@ -1,27 +1,38 @@
1
1
  {
2
2
  "name": "@thepassle/app-tools",
3
- "version": "0.7.0",
3
+ "version": "0.7.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "test": "wtr **/*.test.js --node-resolve",
9
- "test:watch": "npm run test -- --watch"
9
+ "test:watch": "npm run test -- --watch",
10
+ "lint:types": "tsc",
11
+ "lint:types:watch": "tsc --watch"
10
12
  },
11
13
  "exports": {
12
14
  "./state.js": "./state.js",
13
15
  "./pwa.js": "./pwa.js",
14
- "./dialog.js": "./dialog.js",
15
- "./pwa/*.js": "./pwa/*.js",
16
- "./api.js": "./api.js",
17
- "./router.js": "./router.js",
18
- "./router/plugins/*.js": "./router/plugins/*.js",
19
- "./api/plugins/*.js": "./api/plugins/*.js",
16
+ "./dialog.js": {
17
+ "types": "./dialog/types.d.ts",
18
+ "default": "./dialog.js"
19
+ },
20
+ "./pwa/*": "./pwa/*",
21
+ "./api.js": {
22
+ "types": "./api/types.d.ts",
23
+ "default": "./api.js"
24
+ },
25
+ "./router.js": {
26
+ "types": "./router/types.d.ts",
27
+ "default": "./router.js"
28
+ },
29
+ "./router/plugins/*": "./router/plugins/*",
30
+ "./api/plugins/*": "./api/plugins/*",
20
31
  "./utils.js": "./utils.js",
21
- "./utils/*.js": "./utils/*.js",
32
+ "./utils/*": "./utils/*",
22
33
  "./env.js": {
23
- "development": "./env/env-dev.js",
24
- "default": "./env/env-prod.js"
34
+ "development": "./env/dev.js",
35
+ "default": "./env/prod.js"
25
36
  },
26
37
  "./package.json": "./package.json"
27
38
  },
@@ -57,6 +68,7 @@
57
68
  "devDependencies": {
58
69
  "@open-wc/testing": "^3.1.6",
59
70
  "@web/test-runner": "^0.13.31",
60
- "sinon": "^14.0.0"
71
+ "sinon": "^14.0.0",
72
+ "typescript": "^4.9.4"
61
73
  }
62
74
  }
package/pwa/index.js CHANGED
@@ -29,8 +29,7 @@ class Pwa extends EventTarget {
29
29
  installPrompt;
30
30
  /** @type {ServiceWorker | undefined} */
31
31
  __waitingServiceWorker;
32
- /** @type {boolean} */
33
- isInstalled = media.STANDALONE();
32
+ isInstalled = /** @type {boolean} */ (media.STANDALONE());
34
33
 
35
34
  /** Triggers the install prompt, when it's available. You can call this method when the `'installable'` event has fired. */
36
35
  triggerPrompt = async () => {
@@ -58,7 +57,7 @@ class Pwa extends EventTarget {
58
57
 
59
58
  /**
60
59
  * @param {string} swPath
61
- * @param {RegistrationOptions} opts
60
+ * @param {RegistrationOptions} [opts]
62
61
  * @returns {Promise<ServiceWorkerRegistration> | Promise<void>}
63
62
  */
64
63
  register(swPath, opts) {
package/router/index.js CHANGED
@@ -12,31 +12,11 @@ class RouteEvent extends Event {
12
12
  }
13
13
 
14
14
  /**
15
- * @typedef {{
16
- * name: string,
17
- * shouldNavigate?: (context: Context) => {
18
- * condition: () => boolean | (() => Promise<boolean>),
19
- * redirect: string
20
- * },
21
- * beforeNavigation?: (context: Context) => void,
22
- * afterNavigation?: (context: Context) => void,
23
- * }} Plugin
24
- * @typedef {{
25
- * title?: string,
26
- * query: Object,
27
- * params: Object,
28
- * url: URL,
29
- * [key: string]: any
30
- * }} Context
31
- * @typedef {{
32
- * path: string,
33
- * title: string | ((context: Context) => string),
34
- * render?: <RenderResult>(context: Context) => RenderResult
35
- * plugins?: Plugin[]
36
- * }} RouteDefinition
37
- * @typedef {RouteDefinition & {
38
- * urlPattern?: any,
39
- * }} Route
15
+ * @typedef {import('./types.js').Plugin} Plugin
16
+ * @typedef {import('./types.js').Context} Context
17
+ * @typedef {import('./types.js').RouteDefinition} RouteDefinition
18
+ * @typedef {import('./types.js').Route} Route
19
+ * @typedef {import('./types.js').Config} Config
40
20
  */
41
21
 
42
22
  export class Router extends EventTarget {
@@ -48,11 +28,7 @@ export class Router extends EventTarget {
48
28
  }
49
29
 
50
30
  /**
51
- * @param {{
52
- * fallback?: string,
53
- * plugins?: Plugin[],
54
- * routes: RouteDefinition[]
55
- * }} config
31
+ * @param {Config} config
56
32
  */
57
33
  constructor(config) {
58
34
  super();
@@ -62,6 +38,7 @@ export class Router extends EventTarget {
62
38
  this.routes = config.routes.map((route) => {
63
39
  const r = /** @type {unknown} */ ({
64
40
  ...route,
41
+ // @ts-ignore
65
42
  urlPattern: new URLPattern({
66
43
  pathname: route.path,
67
44
  baseURL: window.location.href,
package/state/index.js CHANGED
@@ -22,7 +22,7 @@ export class State extends EventTarget {
22
22
 
23
23
  setState(state) {
24
24
  log('Before: ', this.#state);
25
- this.#state = typeof state === 'function' ? state(this.#state) : state;
25
+ this.#state = typeof state === 'function' ? state?.(this.#state) : state;
26
26
  log('After: ', this.#state);
27
27
  this.dispatchEvent(new StateEvent(this.#state));
28
28
  }
package/env/env-dev.js DELETED
@@ -1,2 +0,0 @@
1
- export const DEV = true;
2
- export const PROD = false;
package/env/env-prod.js DELETED
@@ -1,2 +0,0 @@
1
- export const DEV = false;
2
- export const PROD = true;