@spirobel/mininext 0.4.1 → 0.4.3
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/mininext.d.ts +2 -2
- package/dist/mininext.js +2 -2
- package/dist/url.d.ts +26 -1
- package/dist/url.js +32 -1
- package/package.json +1 -1
package/dist/mininext.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="bun-types" />
|
|
2
2
|
/// <reference types="bun-types" />
|
|
3
|
-
import { url, Mini, type HtmlHandler } from "./url";
|
|
3
|
+
import { url, Mini, has, type HtmlHandler } from "./url";
|
|
4
4
|
import { isError, HtmlString, BasedHtml, head, commonHead, cssReset, basedHtml as html } from "./html";
|
|
5
5
|
import type { Server, WebSocketHandler } from "bun";
|
|
6
6
|
declare global {
|
|
@@ -12,4 +12,4 @@ declare function makeEntrypoint(): Promise<{
|
|
|
12
12
|
fetch: (req: Request, server: Server) => Promise<Response>;
|
|
13
13
|
websocket: WebSocketHandler;
|
|
14
14
|
}>;
|
|
15
|
-
export { html, url, head, build, makeEntrypoint, isError, BasedHtml, HtmlString, type HtmlHandler, Mini, standardDevReloader, commonHead, cssReset, };
|
|
15
|
+
export { has, html, url, head, build, makeEntrypoint, isError, BasedHtml, HtmlString, type HtmlHandler, Mini, standardDevReloader, commonHead, cssReset, };
|
package/dist/mininext.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { url, Mini } from "./url";
|
|
1
|
+
import { url, Mini, has } from "./url";
|
|
2
2
|
import { isError, HtmlString, BasedHtml, head, commonHead, cssReset, basedHtml as html, } from "./html";
|
|
3
3
|
import { watch } from "fs/promises";
|
|
4
4
|
import * as path from "path";
|
|
@@ -182,4 +182,4 @@ async function makeEntrypoint() {
|
|
|
182
182
|
}
|
|
183
183
|
return module.default();
|
|
184
184
|
}
|
|
185
|
-
export { html, url, head, build, makeEntrypoint, isError, BasedHtml, HtmlString, Mini, standardDevReloader, commonHead, cssReset, };
|
|
185
|
+
export { has, html, url, head, build, makeEntrypoint, isError, BasedHtml, HtmlString, Mini, standardDevReloader, commonHead, cssReset, };
|
package/dist/url.d.ts
CHANGED
|
@@ -3,11 +3,24 @@
|
|
|
3
3
|
import type { Server, WebSocketHandler } from "bun";
|
|
4
4
|
import { html, json, dangerjson, HtmlString } from "./html";
|
|
5
5
|
import type { BasedHtml, DangerJsonInHtml, JsonString, JsonStringValues } from "./html";
|
|
6
|
+
/**
|
|
7
|
+
* A helper function that helps narrow unknown objects
|
|
8
|
+
* @param object - the object of type unknown that is to be narrowed
|
|
9
|
+
* @param key - the key that may or may not exist in object
|
|
10
|
+
* @returns true if the key is present and false if not
|
|
11
|
+
* @example
|
|
12
|
+
* ``` js
|
|
13
|
+
* has(this.form.formJson, "formName") &&
|
|
14
|
+
* this.form.formJson.formName === this.form.formName
|
|
15
|
+
* ```
|
|
16
|
+
* https://stackoverflow.com/questions/70028907/narrowing-an-object-of-type-unknown
|
|
17
|
+
*/
|
|
18
|
+
export declare function has<T, K extends string>(object: T, key: K): object is T & object & Record<K, unknown>;
|
|
6
19
|
export type Form = {
|
|
7
20
|
post: boolean;
|
|
8
21
|
urlencoded: boolean;
|
|
9
22
|
multipart: boolean;
|
|
10
|
-
formJson?:
|
|
23
|
+
formJson?: unknown;
|
|
11
24
|
formData?: FormData;
|
|
12
25
|
formName?: string;
|
|
13
26
|
hiddenField?: HtmlString;
|
|
@@ -152,6 +165,18 @@ export declare class url {
|
|
|
152
165
|
*/
|
|
153
166
|
static set<K extends string>(entries: [K, HtmlHandler][]): void;
|
|
154
167
|
static set(urlPath: string, handler: HtmlHandler): void;
|
|
168
|
+
/**
|
|
169
|
+
* use this to remove routes.
|
|
170
|
+
* @param urlPath - the route to remove
|
|
171
|
+
* @example
|
|
172
|
+
* ``` js
|
|
173
|
+
* let perma_link_defined_by_editor_or_admin_user_that_changed = "/haha"
|
|
174
|
+
* url.remove(perma_link_defined_by_editor_or_admin_user_that_changed);
|
|
175
|
+
* // add new url after removing the old one (that might come from a sqlite db)
|
|
176
|
+
* url.set("/huhu", (mini)=> mini.html`huhu`)
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
static remove(urlPath: string): void;
|
|
155
180
|
/**
|
|
156
181
|
* wrap your handlers in this if you mutate something to prevent CSRF issues.
|
|
157
182
|
* @param handler - normal html handler with mini as the argument
|
package/dist/url.js
CHANGED
|
@@ -1,4 +1,19 @@
|
|
|
1
1
|
import { htmlResponder, html, json, dangerjson, HtmlString } from "./html";
|
|
2
|
+
/**
|
|
3
|
+
* A helper function that helps narrow unknown objects
|
|
4
|
+
* @param object - the object of type unknown that is to be narrowed
|
|
5
|
+
* @param key - the key that may or may not exist in object
|
|
6
|
+
* @returns true if the key is present and false if not
|
|
7
|
+
* @example
|
|
8
|
+
* ``` js
|
|
9
|
+
* has(this.form.formJson, "formName") &&
|
|
10
|
+
* this.form.formJson.formName === this.form.formName
|
|
11
|
+
* ```
|
|
12
|
+
* https://stackoverflow.com/questions/70028907/narrowing-an-object-of-type-unknown
|
|
13
|
+
*/
|
|
14
|
+
export function has(object, key) {
|
|
15
|
+
return typeof object === "object" && object !== null && key in object;
|
|
16
|
+
}
|
|
2
17
|
/**
|
|
3
18
|
* Mini - the data object can be filled with url.data
|
|
4
19
|
* @example
|
|
@@ -37,7 +52,7 @@ export class Mini {
|
|
|
37
52
|
this.form.formData.get("formName") === this.form.formName) {
|
|
38
53
|
return cb();
|
|
39
54
|
}
|
|
40
|
-
else if (this.form.formJson &&
|
|
55
|
+
else if (has(this.form.formJson, "formName") &&
|
|
41
56
|
this.form.formJson.formName === this.form.formName) {
|
|
42
57
|
return cb();
|
|
43
58
|
}
|
|
@@ -188,6 +203,22 @@ export class url {
|
|
|
188
203
|
addUrl(entryUrl, entryHandler);
|
|
189
204
|
}
|
|
190
205
|
}
|
|
206
|
+
/**
|
|
207
|
+
* use this to remove routes.
|
|
208
|
+
* @param urlPath - the route to remove
|
|
209
|
+
* @example
|
|
210
|
+
* ``` js
|
|
211
|
+
* let perma_link_defined_by_editor_or_admin_user_that_changed = "/haha"
|
|
212
|
+
* url.remove(perma_link_defined_by_editor_or_admin_user_that_changed);
|
|
213
|
+
* // add new url after removing the old one (that might come from a sqlite db)
|
|
214
|
+
* url.set("/huhu", (mini)=> mini.html`huhu`)
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
static remove(urlPath) {
|
|
218
|
+
for (const u of url.generateVariations(urlPath)) {
|
|
219
|
+
url.direct_handlers_html.delete(u);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
191
222
|
/**
|
|
192
223
|
* wrap your handlers in this if you mutate something to prevent CSRF issues.
|
|
193
224
|
* @param handler - normal html handler with mini as the argument
|