Deno: ServerRequest method should be an Typescript "enum"

Created on 18 Apr 2020  路  4Comments  路  Source: denoland/deno

I suggest to change the declaration of the method field from string to "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH" in the file "https://deno.land/[email protected]/http/server.ts". This makes switch statements easier and everything more fail-proof. Probably we should implement a type alias.

export class ServerRequest {
  url!: string;
  method!: "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH";
  proto!: string;
  protoMinor!: number;
  protoMajor!: number;
  headers!: Headers;

  ...
}

According to the following list:
mozilla.org - HTTP request methods

Most helpful comment

EDIT: What about the following?

"GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH" | string

Well that won't do anything. TS naturally resolves this to string even in intellisense.

All 4 comments

Does HTTP standard forbid inventing new method?

Correct, and some servers implement non-standard ones. We should not forbid non-standard HTTP methods. String literals can help, but we can't limit them.

Well, I is quite ugly and Node doesn't seem to support custom HTTP verbs either, but I guess you are right and we should be better. Let's keep it as it is 馃槶..

EDIT: What about the following?

"GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH" | string

EDIT: What about the following?

"GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH" | string

Well that won't do anything. TS naturally resolves this to string even in intellisense.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sh7dm picture sh7dm  路  3Comments

CruxCv picture CruxCv  路  3Comments

ry picture ry  路  3Comments

zugende picture zugende  路  3Comments

justjavac picture justjavac  路  3Comments