@spirobel/mininext 0.3.9 → 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 +13 -2
- package/dist/url.js +58 -17
- package/package.json +1 -1
package/dist/url.d.ts
CHANGED
|
@@ -45,6 +45,8 @@ export declare class Mini<X = unknown> {
|
|
|
45
45
|
params: URLSearchParams;
|
|
46
46
|
form: Form;
|
|
47
47
|
requrl: Readonly<URL>;
|
|
48
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/redirect_static) */
|
|
49
|
+
redirect: (url: string | URL, status?: number) => void;
|
|
48
50
|
constructor(mini: Mini<unknown>, data: X);
|
|
49
51
|
}
|
|
50
52
|
/**
|
|
@@ -185,12 +187,21 @@ export declare class url {
|
|
|
185
187
|
static link<X>(Url: string, qs?: string[] | string, settings?: LinkSettings): (mini: Mini<X>) => string;
|
|
186
188
|
static currylink(Url: string, qs: string[] | string, req: Request, settings?: LinkSettings): string;
|
|
187
189
|
/**
|
|
188
|
-
*
|
|
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.
|
|
189
200
|
* @param {string} Url - The url to retrieve.
|
|
190
201
|
* @return {string} - The retrieved url.
|
|
191
202
|
* @throws Will throw an Error if the provided url is not found in the urls array.
|
|
192
203
|
*/
|
|
193
|
-
static get(Url: string): string;
|
|
204
|
+
static get(Url: string): string | null;
|
|
194
205
|
static match(req: Request, reqPath?: string): Promise<Response | undefined>;
|
|
195
206
|
/**
|
|
196
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
|
@@ -21,6 +21,8 @@ export class Mini {
|
|
|
21
21
|
params;
|
|
22
22
|
form;
|
|
23
23
|
requrl;
|
|
24
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/redirect_static) */
|
|
25
|
+
redirect;
|
|
24
26
|
constructor(mini, data) {
|
|
25
27
|
Object.assign(this, mini);
|
|
26
28
|
this.html = (html);
|
|
@@ -50,7 +52,7 @@ export class url {
|
|
|
50
52
|
static websocket = undefined;
|
|
51
53
|
static server;
|
|
52
54
|
// direct mapping of "url string" -> function leads to Html Response
|
|
53
|
-
static direct_handlers_html;
|
|
55
|
+
static direct_handlers_html = new Map();
|
|
54
56
|
// An array of the uncompiled frontend files, example frontends[0] = "index.tsx" -> frontend/index.tsx (from the project root)
|
|
55
57
|
static frontends = [];
|
|
56
58
|
static svgs = new Map();
|
|
@@ -173,16 +175,18 @@ export class url {
|
|
|
173
175
|
};
|
|
174
176
|
}
|
|
175
177
|
static set(entries, handler) {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
url.direct_handlers_html.set(
|
|
179
|
-
}
|
|
180
|
-
else {
|
|
181
|
-
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);
|
|
182
181
|
}
|
|
183
182
|
}
|
|
183
|
+
if (typeof entries === "string" && handler) {
|
|
184
|
+
addUrl(entries, handler);
|
|
185
|
+
}
|
|
184
186
|
if (typeof entries !== "string")
|
|
185
|
-
|
|
187
|
+
for (const [entryUrl, entryHandler] of entries) {
|
|
188
|
+
addUrl(entryUrl, entryHandler);
|
|
189
|
+
}
|
|
186
190
|
}
|
|
187
191
|
/**
|
|
188
192
|
* wrap your handlers in this if you mutate something to prevent CSRF issues.
|
|
@@ -278,7 +282,7 @@ export class url {
|
|
|
278
282
|
// Create a new URL object from the current location
|
|
279
283
|
// https://github.com/whatwg/url/issues/531#issuecomment-1337050285
|
|
280
284
|
const GOOFY_HACK = "http://goofyhack.com";
|
|
281
|
-
const updatedUrl = new URL(url.get(Url), GOOFY_HACK);
|
|
285
|
+
const updatedUrl = new URL(url.get(Url) || "/url_not_found_error", GOOFY_HACK);
|
|
282
286
|
for (const q of qs) {
|
|
283
287
|
// Use URLSearchParams to set the name query parameter
|
|
284
288
|
const reqParam = new URL(req.url).searchParams.get(q);
|
|
@@ -296,15 +300,47 @@ export class url {
|
|
|
296
300
|
return updatedUrl.toString().slice(GOOFY_HACK.length);
|
|
297
301
|
}
|
|
298
302
|
/**
|
|
299
|
-
*
|
|
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.
|
|
300
336
|
* @param {string} Url - The url to retrieve.
|
|
301
337
|
* @return {string} - The retrieved url.
|
|
302
338
|
* @throws Will throw an Error if the provided url is not found in the urls array.
|
|
303
339
|
*/
|
|
304
340
|
static get(Url) {
|
|
305
|
-
const foundUrl = url.direct_handlers_html.get(Url);
|
|
341
|
+
const foundUrl = url.direct_handlers_html.get(url.generateVariations(Url)[0]);
|
|
306
342
|
if (!foundUrl) {
|
|
307
|
-
|
|
343
|
+
return null;
|
|
308
344
|
}
|
|
309
345
|
return Url;
|
|
310
346
|
}
|
|
@@ -315,7 +351,8 @@ export class url {
|
|
|
315
351
|
}
|
|
316
352
|
const handler = url.direct_handlers_html.get(reqPath);
|
|
317
353
|
if (handler) {
|
|
318
|
-
|
|
354
|
+
let redirectTarget = null;
|
|
355
|
+
let redirectStatus = undefined;
|
|
319
356
|
let handlerHead = undefined;
|
|
320
357
|
let handlerOptions = {
|
|
321
358
|
headers: {
|
|
@@ -340,6 +377,7 @@ export class url {
|
|
|
340
377
|
if (post && (urlencoded || multipart)) {
|
|
341
378
|
formData = await req.formData();
|
|
342
379
|
}
|
|
380
|
+
//this is the source of mini
|
|
343
381
|
const mini = new Mini({
|
|
344
382
|
requrl: miniurl,
|
|
345
383
|
data: undefined,
|
|
@@ -381,8 +419,15 @@ export class url {
|
|
|
381
419
|
options: (options) => {
|
|
382
420
|
handlerOptions = options;
|
|
383
421
|
},
|
|
422
|
+
redirect: (url, status) => {
|
|
423
|
+
redirectTarget = url;
|
|
424
|
+
redirectStatus = status;
|
|
425
|
+
},
|
|
384
426
|
}, undefined);
|
|
385
427
|
const unresolved = await handler(mini); //passing mini
|
|
428
|
+
if (redirectTarget) {
|
|
429
|
+
return Response.redirect(redirectTarget, redirectStatus);
|
|
430
|
+
}
|
|
386
431
|
return htmlResponder(mini, unresolved, handlerHead, handlerOptions);
|
|
387
432
|
}
|
|
388
433
|
}
|
|
@@ -421,10 +466,6 @@ export class url {
|
|
|
421
466
|
return res;
|
|
422
467
|
//handle svg file serving
|
|
423
468
|
res = url.serveSvg(req);
|
|
424
|
-
if (res)
|
|
425
|
-
return res;
|
|
426
|
-
// go through all the Htmlhandlers again with added slash at the end.
|
|
427
|
-
res = await url.match(req, new URL(req.url).pathname + "/");
|
|
428
469
|
if (res)
|
|
429
470
|
return res;
|
|
430
471
|
return new Response("No matching url found", { status: 404 });
|