|
1 | 1 | // SPDX-FileCopyrightText: 2026 LibreCode coop and LibreCode contributors |
2 | 2 | // SPDX-License-Identifier: AGPL-3.0-or-later |
3 | 3 |
|
4 | | -export type FieldType = 'text' | 'number' |
5 | | - |
6 | | -export type FieldVisibility = 'private' | 'users' | 'public' |
7 | | - |
8 | | -export interface FieldDefinition { |
9 | | - id: number |
10 | | - field_key: string |
11 | | - label: string |
12 | | - type: FieldType |
13 | | - admin_only: boolean |
14 | | - user_editable: boolean |
15 | | - user_visible: boolean |
16 | | - initial_visibility: FieldVisibility |
17 | | - sort_order: number |
18 | | - active: boolean |
19 | | - created_at: string |
20 | | - updated_at: string |
21 | | -} |
22 | | - |
23 | | -export interface FieldValuePayload { |
24 | | - value?: string | number | null |
| 4 | +import type { components as ApiComponents, operations as ApiOperations } from './types/openapi/openapi-full' |
| 5 | + |
| 6 | +type ApiJsonBody<TRequestBody> = TRequestBody extends { |
| 7 | + content: { |
| 8 | + 'application/json': infer Body |
| 9 | + } |
25 | 10 | } |
| 11 | + ? Body |
| 12 | + : never |
26 | 13 |
|
27 | | -export interface FieldValueRecord { |
28 | | - id: number |
29 | | - field_definition_id: number |
30 | | - user_uid: string |
31 | | - value: FieldValuePayload |
32 | | - current_visibility: FieldVisibility |
33 | | - updated_by_uid: string |
34 | | - updated_at: string |
| 14 | +type ApiOperationRequestBody<TOperation> = TOperation extends { |
| 15 | + requestBody?: infer RequestBody |
35 | 16 | } |
| 17 | + ? NonNullable<RequestBody> |
| 18 | + : never |
| 19 | + |
| 20 | +type ApiRequestJsonBody<TOperation> = ApiJsonBody<ApiOperationRequestBody<TOperation>> |
36 | 21 |
|
37 | | -export interface EditableField { |
| 22 | +export type FieldType = ApiComponents['schemas']['Type'] |
| 23 | +export type FieldVisibility = ApiComponents['schemas']['Visibility'] |
| 24 | +export type FieldDefinition = ApiComponents['schemas']['Definition'] |
| 25 | + |
| 26 | +// openapi-typescript collapses the loose `value: mixed` schema to Record<string, never>. |
| 27 | +// Keep the surrounding contract generated and widen only this payload leaf for frontend use. |
| 28 | +export type FieldValuePayload = Omit<ApiComponents['schemas']['ValuePayload'], 'value'> & { |
| 29 | + value?: string | number | null |
| 30 | +} |
| 31 | +export type FieldValueRecord = Omit<ApiComponents['schemas']['ValueRecord'], 'value'> & { |
| 32 | + value: FieldValuePayload |
| 33 | +} |
| 34 | +export type EditableField = Omit<ApiComponents['schemas']['EditableField'], 'definition' | 'value'> & { |
38 | 35 | definition: FieldDefinition |
39 | 36 | value: FieldValueRecord | null |
40 | | - can_edit: boolean |
41 | 37 | } |
| 38 | +export type LookupField = Omit<ApiComponents['schemas']['LookupField'], 'definition' | 'value'> & { |
| 39 | + definition: FieldDefinition |
| 40 | + value: FieldValueRecord |
| 41 | +} |
| 42 | +export type LookupResult = Omit<ApiComponents['schemas']['LookupResult'], 'fields'> & { |
| 43 | + fields: Record<string, LookupField> |
| 44 | +} |
| 45 | + |
| 46 | +export type CreateDefinitionPayload = ApiRequestJsonBody<ApiOperations['field_definition_api-create']> |
| 47 | +export type UpdateDefinitionPayload = ApiRequestJsonBody<ApiOperations['field_definition_api-update']> |
| 48 | +export type UpsertOwnValuePayload = ApiRequestJsonBody<ApiOperations['field_value_api-upsert']> |
| 49 | +export type UpdateOwnVisibilityPayload = ApiRequestJsonBody<ApiOperations['field_value_api-update-visibility']> |
| 50 | +export type UpsertAdminUserValuePayload = ApiRequestJsonBody<ApiOperations['field_value_admin_api-upsert']> |
42 | 51 |
|
43 | | -export interface AdminEditableField { |
| 52 | +export type AdminEditableField = { |
44 | 53 | definition: FieldDefinition |
45 | 54 | value: FieldValueRecord | null |
46 | 55 | } |
47 | 56 |
|
48 | | -export interface ApiError { |
| 57 | +export type ApiError = { |
49 | 58 | message: string |
50 | 59 | } |
0 commit comments