-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathutils.ts
More file actions
35 lines (32 loc) · 1.02 KB
/
Copy pathutils.ts
File metadata and controls
35 lines (32 loc) · 1.02 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
import type { UrlWithStringQuery } from 'node:url';
import type { Rewrite } from 'connect-history-api-fallback';
import type { Page } from './api-types';
/**
* This function simply converts the arguments to an array and returns them.
* It helps creating pages configuration with type hints independently outside plugin function.
*/
export function createPages<
Name extends string,
Filename extends string,
Tpl extends string,
>(pages: Page<Name, Filename, Tpl> | Page<Name, Filename, Tpl>[]) {
return Array.isArray(pages) ? pages : [pages];
}
/**
* @see https://github.com/bripkens/connect-history-api-fallback/blob/6b58bc97d4a2ff2be0a68dc661df5c7857758a55/lib/index.js#L89-L101
*/
export function evaluateRewriteRule(
parsedUrl: UrlWithStringQuery,
match: RegExpMatchArray,
rule: Rewrite['to'],
) {
if (typeof rule === 'string') {
return rule;
} else if (typeof rule !== 'function') {
throw new Error('Rewrite rule can only be of type string or function.');
}
return rule({
parsedUrl,
match,
});
}