-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnormalize.ts
60 lines (54 loc) · 1.77 KB
/
normalize.ts
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { format } from "./isomorphic.node";
import { ErrorLike, OnoOptions } from "./types";
/**
* Normalizes Ono options, accounting for defaults and optional options.
*/
export function normalizeOptions(options?: OnoOptions): OnoOptions {
options = options || {};
return {
concatMessages: options.concatMessages === undefined ? true : Boolean(options.concatMessages),
format: options.format === undefined ? format
: (typeof options.format === "function" ? options.format : false),
};
}
/**
* Normalizes the Ono arguments, accounting for defaults, options, and optional arguments.
*/
export function normalizeArgs<TError extends ErrorLike, TProps extends object>(args: unknown[], options: OnoOptions) {
let originalError: TError | undefined;
let props: TProps | undefined;
let formatArgs: unknown[];
let message = "";
// Determine which arguments were actually specified
if (typeof args[0] === "string") {
formatArgs = args;
}
else if (typeof args[1] === "string") {
if (args[0] instanceof Error) {
originalError = args[0] as TError;
}
else {
props = args[0] as TProps;
}
formatArgs = args.slice(1);
}
else {
originalError = args[0] as TError;
props = args[1] as TProps;
formatArgs = args.slice(2);
}
// If there are any format arguments, then format the error message
if (formatArgs.length > 0) {
if (options.format) {
message = options.format.apply(undefined, formatArgs);
}
else {
message = formatArgs.join(" ");
}
}
if (options.concatMessages && originalError && originalError.message) {
// The inner-error's message will be added to the new message
message += (message ? " \n" : "") + originalError.message;
}
return { originalError, props, message };
}