2025-06-09 11:17:34 +00:00
|
|
|
import { HttpError } from '@agentic/platform-core'
|
|
|
|
|
2025-06-05 16:00:07 +00:00
|
|
|
import type { ParsedProjectIdentifier, ParseIdentifierOptions } from './types'
|
|
|
|
import { parseDeploymentIdentifier } from './parse-deployment-identifier'
|
|
|
|
import { coerceIdentifier } from './utils'
|
|
|
|
|
|
|
|
const projectIdentifierRe = /^@([a-z0-9-]{1,256})\/([a-z0-9-]{1,256})$/
|
|
|
|
|
|
|
|
export function parseProjectIdentifier(
|
|
|
|
identifier?: string,
|
2025-06-09 11:17:34 +00:00
|
|
|
{ strict = true, errorStatusCode = 400 }: ParseIdentifierOptions = {}
|
|
|
|
): ParsedProjectIdentifier {
|
|
|
|
const inputIdentifier = identifier
|
2025-06-05 16:00:07 +00:00
|
|
|
|
2025-06-09 11:17:34 +00:00
|
|
|
if (!strict) {
|
|
|
|
try {
|
|
|
|
return parseDeploymentIdentifier(identifier, {
|
|
|
|
strict,
|
|
|
|
errorStatusCode
|
|
|
|
})
|
|
|
|
} catch {
|
|
|
|
// ignore
|
2025-06-05 16:00:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strict) {
|
|
|
|
identifier = coerceIdentifier(identifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!identifier?.length) {
|
2025-06-09 11:17:34 +00:00
|
|
|
throw new HttpError({
|
|
|
|
statusCode: errorStatusCode,
|
|
|
|
message: `Invalid project identifier "${inputIdentifier}"`
|
|
|
|
})
|
2025-06-05 16:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const match = identifier.match(projectIdentifierRe)
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
return {
|
|
|
|
projectIdentifier: `@${match[1]!}/${match[2]!}`,
|
|
|
|
projectNamespace: match[1]!,
|
|
|
|
projectName: match[2]!
|
|
|
|
}
|
|
|
|
}
|
2025-06-09 11:17:34 +00:00
|
|
|
|
|
|
|
throw new HttpError({
|
|
|
|
statusCode: errorStatusCode,
|
|
|
|
message: `Invalid project identifier "${inputIdentifier}"`
|
|
|
|
})
|
2025-06-05 16:00:07 +00:00
|
|
|
}
|