diff --git a/server/auth.ts b/server/auth.ts index 590fd9a..11665e4 100644 --- a/server/auth.ts +++ b/server/auth.ts @@ -1,5 +1,11 @@ import { authErrorData } from "./error_data.ts"; -import { getJwtFromBearer, isArray, isPresent, type Payload } from "./deps.ts"; +import { + getJwtFromBearer, + isArray, + isFunction, + isPresent, + type Payload, +} from "./deps.ts"; import { type CreationInput } from "./creation.ts"; import { type AuthInput } from "./response.ts"; import { type ValidationSuccess } from "./validation.ts"; @@ -49,7 +55,7 @@ function processAuthData( ) { try { const verify = authData.verification; - if (typeof verify === "function") { + if (isFunction(verify)) { const jwt = getJwtFromBearer(authData.headers); const payload = await verify(jwt, authData.options); return { validationObject, methods, options, payload }; diff --git a/server/deps.ts b/server/deps.ts index a9808b0..a70c76f 100644 --- a/server/deps.ts +++ b/server/deps.ts @@ -10,6 +10,9 @@ export { } from "https://dev.zaubrik.com/portal@v0.2.7/functions/mod.ts"; export { isArray, + isFunction, + isNotNull, isObject, isPresent, + isString, } from "https://dev.zaubrik.com/sorcery@v0.1.5/type.js"; diff --git a/server/object.js b/server/object.js deleted file mode 100644 index 8113fe3..0000000 --- a/server/object.js +++ /dev/null @@ -1,11 +0,0 @@ -/** - * isObject. - * @param {unknown} input - * @returns {input is Record} - */ -export function isObject(input) { - return ( - input !== null && typeof input === "object" && - Array.isArray(input) === false - ); -} diff --git a/server/response.ts b/server/response.ts index e2fe219..f7bfa3b 100644 --- a/server/response.ts +++ b/server/response.ts @@ -1,11 +1,12 @@ -import { createRpcResponseOrBatch } from "./creation.ts"; -import { validateRequest } from "./validation.ts"; -import { type JsonValue } from "../rpc_types.ts"; import { type CryptoKeyOrUpdateInput, + isFunction, verifyJwt, type VerifyOptions, } from "./deps.ts"; +import { createRpcResponseOrBatch } from "./creation.ts"; +import { validateRequest } from "./validation.ts"; +import { type JsonValue } from "../rpc_types.ts"; export type Methods = { // deno-lint-ignore no-explicit-any @@ -32,7 +33,7 @@ export function ensureVerify(authInput: AuthInput): AuthInput { const verification = authInput.verification; return { ...authInput, - verification: typeof verification === "function" + verification: isFunction(verification) ? verification : verifyJwt(verification), }; diff --git a/server/validation.ts b/server/validation.ts index 8dbafc5..e405885 100644 --- a/server/validation.ts +++ b/server/validation.ts @@ -1,4 +1,4 @@ -import { isObject } from "./deps.ts"; +import { isFunction, isNotNull, isObject, isString } from "./deps.ts"; import { invalidParamsErrorData, invalidRequestErrorData, @@ -36,7 +36,7 @@ function isRpcVersion(input: unknown): input is "2.0" { } function isRpcMethod(input: unknown): input is string { - return typeof input === "string" && !input.startsWith("rpc."); + return isString(input) && !input.startsWith("rpc."); } export function isRpcParams(input: unknown): input is JsonArray | JsonObject { @@ -98,7 +98,7 @@ export function validateRpcRequestObject( id: isRpcId(decodedBody.id) ? decodedBody.id : null, isError: true, }; - } else if (typeof methods[decodedBody.method] !== "function") { + } else if (!(isFunction(methods[decodedBody.method]))) { return { id: decodedBody.id, isError: true, diff --git a/test_deps.ts b/test_deps.ts index ebcc1e7..dbc5c03 100644 --- a/test_deps.ts +++ b/test_deps.ts @@ -2,5 +2,5 @@ export { assertEquals, assertNotEquals, assertThrows, -} from "https://deno.land/std@0.223.0/testing/asserts.ts"; +} from "https://deno.land/std@0.224.0/testing/asserts.ts"; export { create, type Payload } from "https://deno.land/x/djwt@v3.0.2/mod.ts";