Skip to content

Commit

Permalink
feat(method): add method utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Mar 1, 2023
1 parent 2a011d0 commit 44a025c
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 2 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,39 @@ assertEquals(isResponse({}), false);
assertEquals(isResponse(null), false);
```

## isSafeMethod

Whether the method is safe method or not.

Defined in
[RFC 9110, 9.2.1. Safe Methods](https://www.rfc-editor.org/rfc/rfc9110.html#name-safe-methods).

```ts
import { isSafeMethod } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assert(isSafeMethod("GET"));
assert(isSafeMethod("HEAD"));
assert(isSafeMethod("OPTIONS"));
assert(isSafeMethod("TRACE"));
```

## isIdempotentMethod

Whether the method is idempotent method or not.

Defined in
[RFC 9110, 9.2.2 Idempotent Methods](https://www.rfc-editor.org/rfc/rfc9110.html#name-idempotent-methods).

```ts
import { isIdempotentMethod } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";

assert(isIdempotentMethod("GET"));
assert(isIdempotentMethod("PUT"));
assert(isIdempotentMethod("DELETE"));
```

## License

Copyright © 2023-present [httpland](https://github.com/httpland).
Expand Down
3 changes: 2 additions & 1 deletion dev_deps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "https://deno.land/std@0.159.0/testing/bdd.ts";
export { describe, it } from "https://deno.land/std@0.178.0/testing/bdd.ts";
export { assertEquals } from "https://deno.land/std@0.178.0/testing/asserts.ts";
import {
defineExpect,
jestMatcherMap,
Expand Down
49 changes: 49 additions & 0 deletions method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,52 @@ export const enum Method {

/** HTTP request method. */
export type HttpMethod = Uppercase<keyof typeof Method>;

/** HTTP method that is read-only.
* @see [RFC 9110, 9.2.1. Safe Methods](https://www.rfc-editor.org/rfc/rfc9110.html#name-safe-methods)
*/
export type SafeMethod =
| Method.Get
| Method.Head
| Method.Options
| Method.Trace;

/** HTTP method that is idempotent.
* @see [RFC 9110, 9.2.2 Idempotent Methods](https://www.rfc-editor.org/rfc/rfc9110.html#name-idempotent-methods)
*/
export type IdempotentMethod = Method.Put | Method.Delete | SafeMethod;

/** Whether the method is {@link SafeMethod} or not.
*
* @example
* ```ts
* import { isSafeMethod } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
* import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
*
* assert(isSafeMethod("GET"));
* assert(isSafeMethod("HEAD"));
* assert(isSafeMethod("OPTIONS"));
* assert(isSafeMethod("TRACE"));
* ```
*/
export function isSafeMethod(method: string): method is SafeMethod {
return ([Method.Get, Method.Head, Method.Options, Method.Trace] as string[])
.includes(method);
}

/** Whether the method is {@link IdempotentMethod} or not.
*
* @example
* ```ts
* import { isIdempotentMethod } from "https://deno.land/x/http_utils@$VERSION/mod.ts";
* import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
*
* assert(isIdempotentMethod("GET"));
* assert(isIdempotentMethod("PUT"));
* assert(isIdempotentMethod("DELETE"));
* ```
*/
export function isIdempotentMethod(method: string): method is IdempotentMethod {
return isSafeMethod(method) ||
([Method.Put, Method.Delete] as string[]).includes(method);
}
54 changes: 54 additions & 0 deletions method_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { assertEquals, describe, Fn, it } from "./dev_deps.ts";
import { isIdempotentMethod, isSafeMethod } from "./method.ts";

describe("isSafeMethod", () => {
it("should pass cases", () => {
const table: Fn<typeof isSafeMethod>[] = [
["", false],
["get", false],
["head", false],
["post", false],
["put", false],
["POST", false],
["PUT", false],
["DELETE", false],
["CONNECT", false],
["PATCH", false],

["GET", true],
["HEAD", true],
["OPTIONS", true],
["TRACE", true],
];

table.forEach(([method, expected]) => {
assertEquals(isSafeMethod(method), expected);
});
});
});

describe("isIdempotentMethod", () => {
it("should pass cases", () => {
const table: Fn<typeof isIdempotentMethod>[] = [
["", false],
["get", false],
["head", false],
["post", false],
["put", false],
["POST", false],
["CONNECT", false],
["PATCH", false],

["GET", true],
["HEAD", true],
["OPTIONS", true],
["TRACE", true],
["PUT", true],
["DELETE", true],
];

table.forEach(([method, expected]) => {
assertEquals(isIdempotentMethod(method), expected);
});
});
});
9 changes: 8 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ export {
parseFieldValue,
} from "./headers.ts";
export { equalsResponse, isResponse, safeResponse } from "./responses.ts";
export { type HttpMethod, Method } from "./method.ts";
export {
type HttpMethod,
type IdempotentMethod,
isIdempotentMethod,
isSafeMethod,
Method,
type SafeMethod,
} from "./method.ts";

0 comments on commit 44a025c

Please sign in to comment.