feat : add applications CRUD service
This commit is contained in:
38
frontend/services/applications.ts
Normal file
38
frontend/services/applications.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
export function getApplications(): Promise<Application[]> {
|
||||
const api = useApi()
|
||||
return api.get<Application[]>('/applications', undefined, {
|
||||
toast: false,
|
||||
}).then((response) => {
|
||||
if (Array.isArray(response)) {
|
||||
return response
|
||||
}
|
||||
return extractHydraMembers(response as HydraCollection<Application>)
|
||||
})
|
||||
}
|
||||
|
||||
export function getApplication(slug: string): Promise<Application> {
|
||||
return useApi().get<Application>(`/applications/${slug}`, undefined, {
|
||||
toast: false,
|
||||
})
|
||||
}
|
||||
|
||||
export function createApplication(data: ApplicationWrite): Promise<Application> {
|
||||
return useApi().post<Application>('/applications', data, {
|
||||
toastSuccessKey: 'success.applications.create',
|
||||
toastErrorKey: 'errors.applications.create',
|
||||
})
|
||||
}
|
||||
|
||||
export function updateApplication(slug: string, data: Partial<ApplicationWrite>): Promise<Application> {
|
||||
return useApi().patch<Application>(`/applications/${slug}`, data, {
|
||||
toastSuccessKey: 'success.applications.update',
|
||||
toastErrorKey: 'errors.applications.update',
|
||||
})
|
||||
}
|
||||
|
||||
export function deleteApplication(slug: string): Promise<void> {
|
||||
return useApi().delete<void>(`/applications/${slug}`, undefined, {
|
||||
toastSuccessKey: 'success.applications.delete',
|
||||
toastErrorKey: 'errors.applications.delete',
|
||||
})
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
import type { ManagedApplication, ManagedApplicationCollection } from './dto/managed-application'
|
||||
|
||||
function normalizeManagedApplications(response: ManagedApplication[] | ManagedApplicationCollection): ManagedApplication[] {
|
||||
if (Array.isArray(response)) {
|
||||
return response
|
||||
}
|
||||
|
||||
return response['hydra:member'] ?? response.member ?? []
|
||||
}
|
||||
|
||||
export async function getManagedApplications(): Promise<ManagedApplication[]> {
|
||||
const api = useApi()
|
||||
const response = await api.get<ManagedApplication[] | ManagedApplicationCollection>('/applications')
|
||||
|
||||
return normalizeManagedApplications(response)
|
||||
}
|
||||
|
||||
export function setApplicationMaintenance(slug: string, maintenance: boolean) {
|
||||
const api = useApi()
|
||||
|
||||
return api.post<ManagedApplication>(
|
||||
`/applications/${slug}/maintenance`,
|
||||
{ maintenance },
|
||||
{
|
||||
toastSuccessKey: maintenance
|
||||
? 'success.applications.activateMaintenance'
|
||||
: 'success.applications.deactivateMaintenance',
|
||||
toastErrorKey: maintenance
|
||||
? 'errors.applications.activateMaintenance'
|
||||
: 'errors.applications.deactivateMaintenance'
|
||||
}
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user