diff --git a/src/api/v2/queries/history.ts b/src/api/v2/queries/history.ts index 5a157f9..d4f1df2 100644 --- a/src/api/v2/queries/history.ts +++ b/src/api/v2/queries/history.ts @@ -3,6 +3,7 @@ import { getCeramicClient } from "../../../util/config.js"; import { resolveHistory, type CeramicClient } from "@desci-labs/desci-codex-lib"; import parentLogger from "../../../logger.js"; import { resolveDpid } from "../resolvers/dpid.js"; +import { isDpid } from "../../../util/validation.js"; const logger = parentLogger.child({ module: "api/v2/queries/history", @@ -95,9 +96,6 @@ export const getVersions = async (ceramic: CeramicClient, streamId: string) => { })); }; -/** Consider ID a dPID if it looks like a number */ -const isDpid = (id: string) => /^[0-9]+$/.test(id); - const getCodexHistories = async (streamIds: string[]) => { if (streamIds.length === 0) return []; diff --git a/src/api/v2/resolvers/generic.ts b/src/api/v2/resolvers/generic.ts index 1834bac..56a6d63 100644 --- a/src/api/v2/resolvers/generic.ts +++ b/src/api/v2/resolvers/generic.ts @@ -7,6 +7,7 @@ import analytics, { LogEventType } from "../../../analytics.js"; import { getIpfsGateway, getNodesUrl } from "../../../util/config.js"; import { DpidResolverError, resolveDpid } from "./dpid.js"; import type { HistoryQueryResult } from "../queries/history.js"; +import { isDpid, isVersionString } from "../../../util/validation.js"; const MODULE_PATH = "/api/v2/resolvers/generic" as const; @@ -68,7 +69,7 @@ export const resolveGenericHandler = async ( logger.info({ path, query }, `Resolving path: ${path}`); const [dpid, ...rest] = path.split("/"); - if (!matchPlainDpid(dpid)) { + if (!isDpid(dpid)) { logger.error({ path }, "invalid dpid"); return res.status(400).send({ error: "invalid dpid", @@ -95,7 +96,7 @@ export const resolveGenericHandler = async ( let suffix = ""; if (rest.length > 0) { const maybeVersionString = rest[0]; - if (matchVersion(maybeVersionString)) { + if (isVersionString(maybeVersionString)) { versionIx = getVersionIndex(maybeVersionString); suffix = rest.slice(1).join("/"); logger.info({ dpid, path, versionIx, suffix }, "extracted version from path"); @@ -235,10 +236,6 @@ export const resolveGenericHandler = async ( } }; -const matchPlainDpid = (path: string) => /\d+/.test(path); - -const matchVersion = (path: string) => /v?\d+/.test(path); - const getVersionIndex = (versionString: string): number => { let index; if (versionString.startsWith("v")) { diff --git a/src/util/validation.ts b/src/util/validation.ts new file mode 100644 index 0000000..e882dc8 --- /dev/null +++ b/src/util/validation.ts @@ -0,0 +1,3 @@ +export const isDpid = (id: string) => /^[0-9]+$/.test(id); + +export const isVersionString = (maybeVersion: string) => /v?\d/.test(maybeVersion);