107 lines
2.7 KiB
TypeScript
107 lines
2.7 KiB
TypeScript
|
|
import { adminApiClient } from './client'
|
||
|
|
import type { AxiosError } from 'axios'
|
||
|
|
|
||
|
|
export interface FeatureFlag {
|
||
|
|
id: string
|
||
|
|
key: string
|
||
|
|
enabled: boolean
|
||
|
|
description?: string | null
|
||
|
|
updatedAt: string
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface UpdateFeatureFlagDto {
|
||
|
|
enabled: boolean
|
||
|
|
description?: string
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface FeatureFlagsApiError {
|
||
|
|
message: string
|
||
|
|
statusCode?: number
|
||
|
|
originalError?: unknown
|
||
|
|
name: 'FeatureFlagsApiError'
|
||
|
|
}
|
||
|
|
|
||
|
|
export function createFeatureFlagsApiError(
|
||
|
|
message: string,
|
||
|
|
statusCode?: number,
|
||
|
|
originalError?: unknown
|
||
|
|
): FeatureFlagsApiError {
|
||
|
|
return {
|
||
|
|
message,
|
||
|
|
statusCode,
|
||
|
|
originalError,
|
||
|
|
name: 'FeatureFlagsApiError',
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function isFeatureFlagsApiError(error: unknown): error is FeatureFlagsApiError {
|
||
|
|
return (
|
||
|
|
typeof error === 'object' &&
|
||
|
|
error !== null &&
|
||
|
|
'name' in error &&
|
||
|
|
error.name === 'FeatureFlagsApiError'
|
||
|
|
)
|
||
|
|
}
|
||
|
|
|
||
|
|
export const featureFlagsApi = {
|
||
|
|
getFeatureFlags: async (): Promise<FeatureFlag[]> => {
|
||
|
|
try {
|
||
|
|
const response = await adminApiClient.get('/api/admin/feature-flags')
|
||
|
|
return response.data
|
||
|
|
} catch (error) {
|
||
|
|
const axiosError = error as AxiosError<{ message?: string }>
|
||
|
|
throw createFeatureFlagsApiError(
|
||
|
|
axiosError.response?.data?.message || 'Failed to load feature flags',
|
||
|
|
axiosError.response?.status,
|
||
|
|
error
|
||
|
|
)
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
getFeatureFlag: async (key: string): Promise<FeatureFlag> => {
|
||
|
|
try {
|
||
|
|
const response = await adminApiClient.get(`/api/admin/feature-flags/${key}`)
|
||
|
|
return response.data
|
||
|
|
} catch (error) {
|
||
|
|
const axiosError = error as AxiosError<{ message?: string }>
|
||
|
|
if (axiosError.response?.status === 404) {
|
||
|
|
throw createFeatureFlagsApiError(
|
||
|
|
`Feature flag "${key}" not found`,
|
||
|
|
axiosError.response.status,
|
||
|
|
error
|
||
|
|
)
|
||
|
|
}
|
||
|
|
throw createFeatureFlagsApiError(
|
||
|
|
axiosError.response?.data?.message || `Failed to load feature flag ${key}`,
|
||
|
|
axiosError.response?.status,
|
||
|
|
error
|
||
|
|
)
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
updateFeatureFlag: async (
|
||
|
|
key: string,
|
||
|
|
data: UpdateFeatureFlagDto
|
||
|
|
): Promise<FeatureFlag> => {
|
||
|
|
try {
|
||
|
|
const response = await adminApiClient.put(`/api/admin/feature-flags/${key}`, data)
|
||
|
|
return response.data
|
||
|
|
} catch (error) {
|
||
|
|
const axiosError = error as AxiosError<{ message?: string }>
|
||
|
|
if (axiosError.response?.status === 404) {
|
||
|
|
throw createFeatureFlagsApiError(
|
||
|
|
`Feature flag "${key}" not found`,
|
||
|
|
axiosError.response.status,
|
||
|
|
error
|
||
|
|
)
|
||
|
|
}
|
||
|
|
throw createFeatureFlagsApiError(
|
||
|
|
axiosError.response?.data?.message || `Failed to update feature flag ${key}`,
|
||
|
|
axiosError.response?.status,
|
||
|
|
error
|
||
|
|
)
|
||
|
|
}
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|