Skip to content

Commit

Permalink
Merge pull request #47 from ty-ras/issue/45-add-http-methods
Browse files Browse the repository at this point in the history
#45 During the work on enabling non-200 code, lon…
  • Loading branch information
stazz authored Dec 8, 2022
2 parents 2e3b9aa + fe3e376 commit 439826f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion data/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ty-ras/data",
"version": "0.12.3",
"version": "0.13.0",
"author": {
"name": "Stanislav Muhametsin",
"email": "[email protected]",
Expand Down
24 changes: 24 additions & 0 deletions data/src/__test__/methods.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import test from "ava";
import * as spec from "../methods";

test("Validate isMethodWithoutRequestBody works", (t) => {
t.plan(8);
for (const [method, result] of Object.entries(methodRequestBodySpec)) {
t.deepEqual(
spec.isMethodWithoutRequestBody(method as spec.HttpMethod),
result,
`The return value of isMethodWithoutRequestBody for ${method} must be expected`,
);
}
});

const methodRequestBodySpec: Record<spec.HttpMethod, boolean> = {
OPTIONS: true,
GET: true,
POST: false,
PUT: false,
DELETE: false,
HEAD: true,
PATCH: false,
TRACE: true,
};
1 change: 1 addition & 0 deletions data/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type {
DataValidatorResultSuccess,
OneOrMany,
} from "./common";
export * from "./methods";
export * from "./utils";
export * from "./combine";
export type {
Expand Down
32 changes: 32 additions & 0 deletions data/src/methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export type HttpMethod =
| typeof METHOD_GET
| typeof METHOD_HEAD
| typeof METHOD_POST
| typeof METHOD_PUT
| typeof METHOD_PATCH
| typeof METHOD_DELETE
| typeof METHOD_OPTIONS
| typeof METHOD_TRACE;

export type HttpMethodWithoutBody = keyof typeof HttpMethodsWithoutBody;
export type HttpMethodWithBody = Exclude<HttpMethod, HttpMethodWithoutBody>;

export const isMethodWithoutRequestBody = (
method: HttpMethod,
): method is HttpMethodWithoutBody => method in HttpMethodsWithoutBody;

export const METHOD_GET = "GET";
export const METHOD_HEAD = "HEAD";
export const METHOD_POST = "POST";
export const METHOD_PUT = "PUT";
export const METHOD_PATCH = "PATCH";
export const METHOD_DELETE = "DELETE";
export const METHOD_OPTIONS = "OPTIONS";
export const METHOD_TRACE = "TRACE";

const HttpMethodsWithoutBody = {
[METHOD_TRACE]: true,
[METHOD_GET]: true,
[METHOD_OPTIONS]: true,
[METHOD_HEAD]: true,
} as const;
2 changes: 1 addition & 1 deletion protocol/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ty-ras/protocol",
"version": "0.12.1",
"version": "0.13.0",
"author": {
"name": "Stanislav Muhametsin",
"email": "[email protected]",
Expand Down

0 comments on commit 439826f

Please sign in to comment.