@spirobel/mininext 0.4.0 → 0.4.1

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
@@ -187,12 +187,21 @@ export declare class url {
187
187
  static link<X>(Url: string, qs?: string[] | string, settings?: LinkSettings): (mini: Mini<X>) => string;
188
188
  static currylink(Url: string, qs: string[] | string, req: Request, settings?: LinkSettings): string;
189
189
  /**
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.
190
+ * users expect links to work with or without a trailing slash.
191
+ * Developers expect that that links work with or without a preceding slash.
192
+ * We make sure that these expectations are met when using url.set and url.get.
193
+ * (by adding all the variations to the url.direct_handlers Map)
194
+ * @param {string} inputString - the url
195
+ * @returns {string[]} - returns array of variations (added slash in the beginning, added, removed slash at the end)
196
+ */
197
+ static generateVariations(inputString: string): string[];
198
+ /**
199
+ * This method retrieves a url from the urls array. If the url does not exist in the urls array, null will be returned.
191
200
  * @param {string} Url - The url to retrieve.
192
201
  * @return {string} - The retrieved url.
193
202
  * @throws Will throw an Error if the provided url is not found in the urls array.
194
203
  */
195
- static get(Url: string): string;
204
+ static get(Url: string): string | null;
196
205
  static match(req: Request, reqPath?: string): Promise<Response | undefined>;
197
206
  /**
198
207
  * 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,18 @@ 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
+ }
188
190
  }
189
191
  /**
190
192
  * wrap your handlers in this if you mutate something to prevent CSRF issues.
@@ -280,7 +282,7 @@ export class url {
280
282
  // Create a new URL object from the current location
281
283
  // https://github.com/whatwg/url/issues/531#issuecomment-1337050285
282
284
  const GOOFY_HACK = "http://goofyhack.com";
283
- const updatedUrl = new URL(url.get(Url), GOOFY_HACK);
285
+ const updatedUrl = new URL(url.get(Url) || "/url_not_found_error", GOOFY_HACK);
284
286
  for (const q of qs) {
285
287
  // Use URLSearchParams to set the name query parameter
286
288
  const reqParam = new URL(req.url).searchParams.get(q);
@@ -298,15 +300,47 @@ export class url {
298
300
  return updatedUrl.toString().slice(GOOFY_HACK.length);
299
301
  }
300
302
  /**
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.
303
+ * users expect links to work with or without a trailing slash.
304
+ * Developers expect that that links work with or without a preceding slash.
305
+ * We make sure that these expectations are met when using url.set and url.get.
306
+ * (by adding all the variations to the url.direct_handlers Map)
307
+ * @param {string} inputString - the url
308
+ * @returns {string[]} - returns array of variations (added slash in the beginning, added, removed slash at the end)
309
+ */
310
+ static generateVariations(inputString) {
311
+ const variations = [];
312
+ // Special case for the index route
313
+ if (inputString === "/") {
314
+ variations.push("/");
315
+ return variations;
316
+ }
317
+ // Check if the string starts with a slash and add/remove variations accordingly
318
+ if (inputString.startsWith("/")) {
319
+ variations.push(inputString); // With leading slash
320
+ }
321
+ else {
322
+ inputString = "/" + inputString;
323
+ variations.push(inputString); // With leading slash
324
+ }
325
+ // Check if the string ends with a slash and add/remove variations accordingly
326
+ if (inputString.endsWith("/")) {
327
+ variations.push(inputString.slice(0, -1)); // Without trailing slash
328
+ }
329
+ else {
330
+ variations.push(inputString + "/"); // With trailing slash
331
+ }
332
+ return variations;
333
+ }
334
+ /**
335
+ * This method retrieves a url from the urls array. If the url does not exist in the urls array, null will be returned.
302
336
  * @param {string} Url - The url to retrieve.
303
337
  * @return {string} - The retrieved url.
304
338
  * @throws Will throw an Error if the provided url is not found in the urls array.
305
339
  */
306
340
  static get(Url) {
307
- const foundUrl = url.direct_handlers_html.get(Url);
341
+ const foundUrl = url.direct_handlers_html.get(url.generateVariations(Url)[0]);
308
342
  if (!foundUrl) {
309
- throw new Error(`URL "${html `${Url}`}" was not set.`);
343
+ return null;
310
344
  }
311
345
  return Url;
312
346
  }
@@ -432,10 +466,6 @@ export class url {
432
466
  return res;
433
467
  //handle svg file serving
434
468
  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
469
  if (res)
440
470
  return res;
441
471
  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.1",
15
15
  "devDependencies": {
16
16
  "@types/bun": "latest"
17
17
  },