Skip to content

Commit 0c6935b

Browse files
feat: Add explicit types for resource schemas and a getSchema helper function to the useResourceSchemas composable.
1 parent 206cce8 commit 0c6935b

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/runtime/composables/useResourceSchemas.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
import { useAsyncData, useRequestHeaders } from '#imports'
2+
import type { Ref } from 'vue'
23

3-
export const useResourceSchemas = async () => {
4-
const { data: schemas, status, error, refresh } = await useAsyncData('resource-schemas', () => $fetch<Record<string, { resource: string, fields: { name: string, type: string, required?: boolean, selectOptions?: string[] }[] }>>('/api/_schema', {
4+
export interface ResourceField {
5+
name: string
6+
type: string
7+
required?: boolean
8+
selectOptions?: string[]
9+
}
10+
11+
export interface ResourceSchema {
12+
resource: string
13+
fields: ResourceField[]
14+
}
15+
16+
export type ResourceSchemas = Record<string, ResourceSchema>
17+
18+
export const useResourceSchemas = async (): Promise<{
19+
schemas: Ref<ResourceSchemas | null | undefined>
20+
getSchema: (resource: string) => ResourceSchema | undefined
21+
status: Ref<'idle' | 'pending' | 'success' | 'error'>
22+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
23+
error: Ref<any>
24+
refresh: () => Promise<void>
25+
}> => {
26+
const { data: schemas, status, error, refresh } = await useAsyncData<ResourceSchemas>('resource-schemas', () => $fetch('/api/_schema', {
527
headers: useRequestHeaders(['cookie']),
628
}))
729

@@ -15,6 +37,6 @@ export const useResourceSchemas = async () => {
1537
getSchema,
1638
status,
1739
error,
18-
refresh,
40+
refresh: refresh as unknown as () => Promise<void>,
1941
}
2042
}

0 commit comments

Comments
 (0)