-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
39 lines (35 loc) · 1.04 KB
/
utils.ts
File metadata and controls
39 lines (35 loc) · 1.04 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 { createDefine } from "fresh";
import { XError } from "@/lib.ts";
// This specifies the type of "ctx.state" which is used to share
// data among middlewares, layouts and routes.
export interface State {
shared: string;
}
export const define = createDefine<State>();
export function errorResponse(status: number, error: Error): Response;
export function errorResponse(status: number, error: string, message?: string): Response;
export function errorResponse(status: number, error: string | Error, message?: string): Response {
if (error instanceof Error) {
return new Response(
JSON.stringify({
error: error.constructor.name,
message: error.message,
} as XError),
{ status: status },
);
} else {
return new Response(
JSON.stringify({
error: error,
message: message,
} as XError),
{ status: status },
);
}
}
/** Returns 'An' if str starts with a vowel. */
export function aOrAn(upper: boolean, str: string) {
if (str.match(/^\s*[AaEeIiOoUu]/)) {
return upper ? "An" : "an";
} else return upper ? "A" : "a";
}