Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(common): add express http verb method name string literal type #2754

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* A collection of all the HTTP verb method names that Express.js supports.
* Directly taken from the Express.js type definitions file of the @types/express
* package.
*/
export const ALL_EXPRESS_HTTP_VERB_METHOD_NAMES = [
"all",
"get",
"post",
"put",
"delete",
"patch",
"options",
"head",
] as const;

/**
* A type that represents all the HTTP verb method names that Express.js supports.
*/
export type ExpressHttpVerbMethodName =
typeof ALL_EXPRESS_HTTP_VERB_METHOD_NAMES[number];

/**
* Custom (user-defined) typescript type-guard that checks whether the given
* value is an Express.js HTTP verb method name or not.
* Useful for verifying at runtime if we can safely call a method on the Express.js
* application object where the method's name is the value of the given variable.
*
* Example:
*
* ```typescript
* import express from "express";
* import { isExpressHttpVerbMethodName } from "@hyperledger/cactus-core-api-server";
* const app = express();
* const methodName = "get";
* if (isExpressHttpVerbMethodName(methodName)) {
* app[methodName]("/foo", (req, res) => {
* res.send("Hello World!");
* });
* ```
*
* @param x Any value that may or may not be an Express.js HTTP verb method name.
* @returns Whether the given value is an Express.js HTTP verb method name.
*/
export function isExpressHttpVerbMethodName(
x: unknown,
): x is ExpressHttpVerbMethodName {
return (
typeof x === "string" && ALL_EXPRESS_HTTP_VERB_METHOD_NAMES.includes(x as never)
);
}
16 changes: 14 additions & 2 deletions packages/cactus-common/src/main/typescript/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ export {
export { isRecord } from "./types/is-record";
export { hasKey } from "./types/has-key";

export { asError, coerceUnknownToError } from "./exception/coerce-unknown-to-error";
export { createRuntimeErrorWithCause, newRex } from "./exception/create-runtime-error-with-cause";
export {
asError,
coerceUnknownToError,
} from "./exception/coerce-unknown-to-error";
export {
createRuntimeErrorWithCause,
newRex,
} from "./exception/create-runtime-error-with-cause";
export { ErrorFromUnknownThrowable } from "./exception/error-from-unknown-throwable";
export { ErrorFromSymbol } from "./exception/error-from-symbol";

export {
ALL_EXPRESS_HTTP_VERB_METHOD_NAMES,
ExpressHttpVerbMethodName,
isExpressHttpVerbMethodName,
} from "./http/express-http-verb-method-name";
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
ALL_EXPRESS_HTTP_VERB_METHOD_NAMES,
ExpressHttpVerbMethodName,
isExpressHttpVerbMethodName,
} from "../../../../main/typescript/public-api";

describe("isExpressHttpVerbMethodName", () => {
it("should return true for valid HTTP verb method names", () => {
ALL_EXPRESS_HTTP_VERB_METHOD_NAMES.forEach((methodName) => {
expect(isExpressHttpVerbMethodName(methodName)).toBe(true);
});
});

it("should return false for invalid values", () => {
const invalidValues = [
123, // Not a string
0, // Falsy Number
"invalid", // Not a valid HTTP verb method name
"gEt", // Case-sensitive
"", // Empty string
null, // Null value
undefined, // Undefined value
{}, // Object
[], // Array
];

invalidValues.forEach((value) => {
expect(isExpressHttpVerbMethodName(value)).toBe(false);
});
});
});