export type MunicipalityProfile = { profileId: string jCode: string displayName: string | null updatedAt: string updatedBy: string legacyName: string | null legacyMailingAddress: string | null legacyCityStateZip: string | null } export type MunicipalityProfileValidationError = { error: string } export async function fetchMunicipalityProfiles( fetcher: typeof fetch = fetch, ): Promise { const response = await fetcher('/api/municipalities/profiles') if (!response.ok) { throw new Error(`Failed to load municipality profiles (${response.status})`) } return (await response.json()) as MunicipalityProfile[] } export async function createMunicipalityProfile( jCode: string, displayName: string | null, fetcher: typeof fetch = fetch, ): Promise { const response = await fetcher('/api/municipalities/profiles', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ jCode, displayName }), }) if (response.status === 422) { const problem = (await response.json()) as MunicipalityProfileValidationError throw new MunicipalityValidationError(problem.error ?? 'Validation failed.') } if (!response.ok) { throw new Error(`Failed to create municipality profile (${response.status})`) } return (await response.json()) as MunicipalityProfile } export async function updateMunicipalityProfile( profileId: string, displayName: string | null, fetcher: typeof fetch = fetch, ): Promise { const response = await fetcher(`/api/municipalities/profiles/${profileId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ displayName }), }) if (response.status === 422) { const problem = (await response.json()) as MunicipalityProfileValidationError throw new MunicipalityValidationError(problem.error ?? 'Validation failed.') } if (!response.ok) { throw new Error(`Failed to update municipality profile (${response.status})`) } return (await response.json()) as MunicipalityProfile } export type LegacyJurisdiction = { jCode: string name: string | null } export async function fetchAvailableJurisdictions( fetcher: typeof fetch = fetch, ): Promise { const response = await fetcher('/api/municipalities/jurisdictions') if (!response.ok) { throw new Error(`Failed to load jurisdictions (${response.status})`) } return (await response.json()) as LegacyJurisdiction[] } export class MunicipalityValidationError extends Error { constructor(message: string) { super(message) this.name = 'MunicipalityValidationError' } }