burger-api 0.5.1 → 0.5.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.
@@ -1,30 +1,66 @@
1
+ /**
2
+ * Extracts the pathname from a full URL string, removing query parameters.
3
+ * @param url - The full URL string (e.g., "http://localhost:4000/api/users/123/profile?id=1")
4
+ * @returns The extracted pathname (e.g., "/api/users/123/profile")
5
+ */
1
6
  export function extractPathnameFromUrl(url) {
7
+ // Find where the path starts (after protocol and domain)
8
+ // Example: "http://localhost:4000/api/..." → protocolEnd = 4 (after "http")
2
9
  const protocolEnd = url.indexOf('://');
10
+ // Find first "/" after the domain
11
+ // Example: "http://localhost:4000/api/..." → pathStart = 21 (the "/" before "api")
3
12
  const pathStart = url.indexOf('/', protocolEnd + 3);
13
+ // Find where query parameters start (if any)
14
+ // Example: "/api/users/123?id=1" → pathEnd = 15 (at the "?")
4
15
  const pathEnd = url.indexOf('?', pathStart);
16
+ // Extract just the pathname without query parameters
17
+ // Example: "/api/users/123/profile"
5
18
  return pathEnd === -1
6
- ? url.substring(pathStart)
7
- : url.substring(pathStart, pathEnd);
19
+ ? url.substring(pathStart) // No query params
20
+ : url.substring(pathStart, pathEnd); // Has query params, stop at "?"
8
21
  }
22
+ /**
23
+ * Extracts wildcard parameters from a request pathname and sets them on the request object.
24
+ * @param request - The request object to modify
25
+ * @param pathname - The pathname extracted from the URL (e.g., "/api/users/123/profile")
26
+ * @param baseSegmentCount - Number of segments before the wildcard (e.g., 3 for "/api/users/:userId/*")
27
+ */
9
28
  export function extractWildcardParams(request, pathname, baseSegmentCount) {
10
29
  if (baseSegmentCount === 0) {
30
+ // Fast path: The entire path is wildcard
31
+ // Example: "/api/docs/*" where all segments after domain are wildcards
32
+ // Just split everything and we're done!
11
33
  request.wildcardParams = pathname.split('/').filter(Boolean);
12
34
  }
13
35
  else {
36
+ // Optimized path: Skip base segments and only collect wildcard segments
37
+ // Example: "/api/users/123/settings/privacy"
38
+ // - We need to skip 3 segments (api, users, 123)
39
+ // - Only collect: ["settings", "privacy"]
14
40
  const wildcardParams = [];
15
- let segmentCount = 0;
16
- let start = pathname[0] === '/' ? 1 : 0;
41
+ let segmentCount = 0; // Counts how many segments we've seen
42
+ let start = pathname[0] === '/' ? 1 : 0; // Start after leading "/" if present
43
+ // Loop through each character in the pathname
44
+ // When we hit "/" or end of string, we know we found a segment
17
45
  for (let i = start; i <= pathname.length; i++) {
46
+ // Found end of a segment (either "/" or end of string)
18
47
  if (i === pathname.length || pathname[i] === '/') {
48
+ // Make sure it's not an empty segment (from double slashes)
19
49
  if (i > start) {
50
+ // Only save segments AFTER we've skipped the base segments
51
+ // Example: If baseSegmentCount=3, save segment 3, 4, 5, etc.
20
52
  if (segmentCount >= baseSegmentCount) {
53
+ // Extract this segment from the pathname
54
+ // Example: pathname.substring(5, 13) might give "settings"
21
55
  wildcardParams.push(pathname.substring(start, i));
22
56
  }
23
- segmentCount++;
57
+ segmentCount++; // Increment count for next segment
24
58
  }
25
- start = i + 1;
59
+ start = i + 1; // Move start position to character after "/"
26
60
  }
27
61
  }
62
+ // Done! wildcardParams now contains only the wildcard segments
63
+ // Example: ["settings", "privacy"] instead of ["api", "users", "123", "settings", "privacy"]
28
64
  request.wildcardParams = wildcardParams;
29
65
  }
30
66
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "burger-api",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "author": "Isfhan Ahmed",
5
5
  "type": "module",
6
6
  "module": "dist/src/index.js",