@spirobel/mininext 0.4.0 → 0.4.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/dist/url.d.ts CHANGED
@@ -152,6 +152,18 @@ export declare class url {
152
152
  */
153
153
  static set<K extends string>(entries: [K, HtmlHandler][]): void;
154
154
  static set(urlPath: string, handler: HtmlHandler): void;
155
+ /**
156
+ * use this to remove routes.
157
+ * @param urlPath - the route to remove
158
+ * @example
159
+ * ``` js
160
+ * let perma_link_defined_by_editor_or_admin_user_that_changed = "/haha"
161
+ * url.remove(perma_link_defined_by_editor_or_admin_user_that_changed);
162
+ * // add new url after removing the old one (that might come from a sqlite db)
163
+ * url.set("/huhu", (mini)=> mini.html`huhu`)
164
+ * ```
165
+ */
166
+ static remove(urlPath: string): void;
155
167
  /**
156
168
  * wrap your handlers in this if you mutate something to prevent CSRF issues.
157
169
  * @param handler - normal html handler with mini as the argument
@@ -187,12 +199,21 @@ export declare class url {
187
199
  static link<X>(Url: string, qs?: string[] | string, settings?: LinkSettings): (mini: Mini<X>) => string;
188
200
  static currylink(Url: string, qs: string[] | string, req: Request, settings?: LinkSettings): string;
189
201
  /**
190
- * This method retrieves a url from the urls array. If the url does not exist in the urls array, an error will be thrown.
202
+ * users expect links to work with or without a trailing slash.
203
+ * Developers expect that that links work with or without a preceding slash.
204
+ * We make sure that these expectations are met when using url.set and url.get.
205
+ * (by adding all the variations to the url.direct_handlers Map)
206
+ * @param {string} inputString - the url
207
+ * @returns {string[]} - returns array of variations (added slash in the beginning, added, removed slash at the end)
208
+ */
209
+ static generateVariations(inputString: string): string[];
210
+ /**
211
+ * This method retrieves a url from the urls array. If the url does not exist in the urls array, null will be returned.
191
212
  * @param {string} Url - The url to retrieve.
192
213
  * @return {string} - The retrieved url.
193
214
  * @throws Will throw an Error if the provided url is not found in the urls array.
194
215
  */
195
- static get(Url: string): string;
216
+ static get(Url: string): string | null;
196
217
  static match(req: Request, reqPath?: string): Promise<Response | undefined>;
197
218
  /**
198
219
  * user this to set the Websocket object. Check out [the bun docs](https://bun.sh/docs/api/websockets) for more details.
package/dist/url.js CHANGED
@@ -52,7 +52,7 @@ export class url {
52
52
  static websocket = undefined;
53
53
  static server;
54
54
  // direct mapping of "url string" -> function leads to Html Response
55
- static direct_handlers_html;
55
+ static direct_handlers_html = new Map();
56
56
  // An array of the uncompiled frontend files, example frontends[0] = "index.tsx" -> frontend/index.tsx (from the project root)
57
57
  static frontends = [];
58
58
  static svgs = new Map();
@@ -175,16 +175,34 @@ export class url {
175
175
  };
176
176
  }
177
177
  static set(entries, handler) {
178
- if (typeof entries === "string" && handler) {
179
- if (url.direct_handlers_html) {
180
- url.direct_handlers_html.set(entries, handler);
181
- }
182
- else {
183
- url.direct_handlers_html = new Map([[entries, handler]]);
178
+ function addUrl(entryUrl, entryHandler) {
179
+ for (const u of url.generateVariations(entryUrl)) {
180
+ url.direct_handlers_html.set(u, entryHandler);
184
181
  }
185
182
  }
183
+ if (typeof entries === "string" && handler) {
184
+ addUrl(entries, handler);
185
+ }
186
186
  if (typeof entries !== "string")
187
- url.direct_handlers_html = new Map(entries);
187
+ for (const [entryUrl, entryHandler] of entries) {
188
+ addUrl(entryUrl, entryHandler);
189
+ }
190
+ }
191
+ /**
192
+ * use this to remove routes.
193
+ * @param urlPath - the route to remove
194
+ * @example
195
+ * ``` js
196
+ * let perma_link_defined_by_editor_or_admin_user_that_changed = "/haha"
197
+ * url.remove(perma_link_defined_by_editor_or_admin_user_that_changed);
198
+ * // add new url after removing the old one (that might come from a sqlite db)
199
+ * url.set("/huhu", (mini)=> mini.html`huhu`)
200
+ * ```
201
+ */
202
+ static remove(urlPath) {
203
+ for (const u of url.generateVariations(urlPath)) {
204
+ url.direct_handlers_html.delete(u);
205
+ }
188
206
  }
189
207
  /**
190
208
  * wrap your handlers in this if you mutate something to prevent CSRF issues.
@@ -280,7 +298,7 @@ export class url {
280
298
  // Create a new URL object from the current location
281
299
  // https://github.com/whatwg/url/issues/531#issuecomment-1337050285
282
300
  const GOOFY_HACK = "http://goofyhack.com";
283
- const updatedUrl = new URL(url.get(Url), GOOFY_HACK);
301
+ const updatedUrl = new URL(url.get(Url) || "/url_not_found_error", GOOFY_HACK);
284
302
  for (const q of qs) {
285
303
  // Use URLSearchParams to set the name query parameter
286
304
  const reqParam = new URL(req.url).searchParams.get(q);
@@ -298,15 +316,47 @@ export class url {
298
316
  return updatedUrl.toString().slice(GOOFY_HACK.length);
299
317
  }
300
318
  /**
301
- * This method retrieves a url from the urls array. If the url does not exist in the urls array, an error will be thrown.
319
+ * users expect links to work with or without a trailing slash.
320
+ * Developers expect that that links work with or without a preceding slash.
321
+ * We make sure that these expectations are met when using url.set and url.get.
322
+ * (by adding all the variations to the url.direct_handlers Map)
323
+ * @param {string} inputString - the url
324
+ * @returns {string[]} - returns array of variations (added slash in the beginning, added, removed slash at the end)
325
+ */
326
+ static generateVariations(inputString) {
327
+ const variations = [];
328
+ // Special case for the index route
329
+ if (inputString === "/") {
330
+ variations.push("/");
331
+ return variations;
332
+ }
333
+ // Check if the string starts with a slash and add/remove variations accordingly
334
+ if (inputString.startsWith("/")) {
335
+ variations.push(inputString); // With leading slash
336
+ }
337
+ else {
338
+ inputString = "/" + inputString;
339
+ variations.push(inputString); // With leading slash
340
+ }
341
+ // Check if the string ends with a slash and add/remove variations accordingly
342
+ if (inputString.endsWith("/")) {
343
+ variations.push(inputString.slice(0, -1)); // Without trailing slash
344
+ }
345
+ else {
346
+ variations.push(inputString + "/"); // With trailing slash
347
+ }
348
+ return variations;
349
+ }
350
+ /**
351
+ * This method retrieves a url from the urls array. If the url does not exist in the urls array, null will be returned.
302
352
  * @param {string} Url - The url to retrieve.
303
353
  * @return {string} - The retrieved url.
304
354
  * @throws Will throw an Error if the provided url is not found in the urls array.
305
355
  */
306
356
  static get(Url) {
307
- const foundUrl = url.direct_handlers_html.get(Url);
357
+ const foundUrl = url.direct_handlers_html.get(url.generateVariations(Url)[0]);
308
358
  if (!foundUrl) {
309
- throw new Error(`URL "${html `${Url}`}" was not set.`);
359
+ return null;
310
360
  }
311
361
  return Url;
312
362
  }
@@ -432,10 +482,6 @@ export class url {
432
482
  return res;
433
483
  //handle svg file serving
434
484
  res = url.serveSvg(req);
435
- if (res)
436
- return res;
437
- // go through all the Htmlhandlers again with added slash at the end.
438
- res = await url.match(req, new URL(req.url).pathname + "/");
439
485
  if (res)
440
486
  return res;
441
487
  return new Response("No matching url found", { status: 404 });
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "clean":"rm -rf ./dist"
12
12
  },
13
13
  "files": ["dist"],
14
- "version": "0.4.0",
14
+ "version": "0.4.2",
15
15
  "devDependencies": {
16
16
  "@types/bun": "latest"
17
17
  },