44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
export const GENERATION_PURPOSE_HEADER = 'X-D3RO-Generation-Purpose'
|
|
export const GENERATION_ID_HEADER = 'X-D3RO-Generation-Id'
|
|
|
|
export type GenerationPurpose = 'talk_response' | 'command_response' | 'action_response'
|
|
|
|
const GENERATION_PURPOSES = new Set<string>([
|
|
'talk_response',
|
|
'command_response',
|
|
'action_response',
|
|
])
|
|
|
|
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
|
|
|
export class GenerationReceiptError extends Error {
|
|
constructor(readonly code: string) {
|
|
super(code)
|
|
this.name = 'GenerationReceiptError'
|
|
}
|
|
}
|
|
|
|
export function parseGenerationPurpose(value: string | null): GenerationPurpose | null {
|
|
if (value === null) return null
|
|
const normalized = value.trim()
|
|
if (!GENERATION_PURPOSES.has(normalized)) {
|
|
throw new GenerationReceiptError('invalid_generation_purpose')
|
|
}
|
|
return normalized as GenerationPurpose
|
|
}
|
|
|
|
export function parseGenerationReceiptId(value: unknown): string {
|
|
if (
|
|
typeof value !== 'object'
|
|
|| value === null
|
|
|| Array.isArray(value)
|
|
|| typeof (value as Record<string, unknown>).generationId !== 'string'
|
|
) {
|
|
throw new GenerationReceiptError('invalid_generation_receipt')
|
|
}
|
|
const generationId = ((value as Record<string, unknown>).generationId as string).toLowerCase()
|
|
if (!UUID_PATTERN.test(generationId)) {
|
|
throw new GenerationReceiptError('invalid_generation_receipt')
|
|
}
|
|
return generationId
|
|
}
|