-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathutil.ts
More file actions
39 lines (36 loc) · 1.17 KB
/
util.ts
File metadata and controls
39 lines (36 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import type { Request } from "express";
/**
* Merges multiple URLSearchParams or FormData instances into the first one passed.
*/
export function mergeParams(
destination: URLSearchParams,
...sources: URLSearchParams[]
): URLSearchParams;
export function mergeParams(
destination: FormData,
...sources: FormData[] | URLSearchParams[]
): FormData;
export function mergeParams(
destination: URLSearchParams | FormData,
...sources: URLSearchParams[] | FormData[]
) {
for (const source of sources) {
for (const [k, v] of source.entries()) {
// Skip file inputs in case of FormData
if (typeof v === "string") destination.set(k, v);
}
}
return destination;
}
/**
* Merges an Express request's body and query params into one URLSearchParams object.
*/
export function mergeRequestParams(req: Request) {
const queryParams = new URLSearchParams(
req.url.includes("?") ? req.url.slice(req.url.indexOf("?")) : "",
);
if (!req.body) return queryParams;
return mergeParams(new URLSearchParams(req.body), queryParams);
}
/** Returns the current (local) date in YYYY-MM-DD format. */
export const getShortIsoDate = () => new Date().toISOString().slice(0, 10);