-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmethod.ts
70 lines (66 loc) · 1.28 KB
/
method.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
61
62
63
64
65
66
67
68
69
70
// Copyright 2018-2024 the oak authors. All rights reserved. MIT license.
/**
* Contains the constant {@linkcode HTTP_METHODS} and the type
* {@linkcode HttpMethod} and the type guard {@linkcode isHttpMethod} for
* working with HTTP methods with type safety.
*
* @example
* ```ts
* import { isHttpMethod } from "jsr:@oak/commons/method";
*
* console.log(isMethod("GET")); // true
* ```
*
* @module
*/
/**
* A constant array of common HTTP methods.
*
* This list is compatible with Node.js `http` module.
*/
export const HTTP_METHODS = [
"ACL",
"BIND",
"CHECKOUT",
"CONNECT",
"COPY",
"DELETE",
"GET",
"HEAD",
"LINK",
"LOCK",
"M-SEARCH",
"MERGE",
"MKACTIVITY",
"MKCALENDAR",
"MKCOL",
"MOVE",
"NOTIFY",
"OPTIONS",
"PATCH",
"POST",
"PROPFIND",
"PROPPATCH",
"PURGE",
"PUT",
"REBIND",
"REPORT",
"SEARCH",
"SOURCE",
"SUBSCRIBE",
"TRACE",
"UNBIND",
"UNLINK",
"UNLOCK",
"UNSUBSCRIBE",
] as const;
/**
* A type representing string literals of each of the common HTTP method.
*/
export type HttpMethod = typeof HTTP_METHODS[number];
/**
* A type guard that determines if a value is a valid HTTP method.
*/
export function isHttpMethod(value: unknown): value is HttpMethod {
return HTTP_METHODS.includes(value as HttpMethod);
}