|
| 1 | +import type { |
| 2 | + FormForgeCategory, |
| 3 | + FormForgeCategoryCreateInput, |
| 4 | + FormForgeCategoryListQuery, |
| 5 | + FormForgeCategoryListResponse, |
| 6 | + FormForgeCategoryUpdateInput, |
| 7 | + FormForgeHttpAdapter, |
| 8 | + FormForgeJsonObject |
| 9 | +} from '../types' |
| 10 | +import type { FormForgeMutationOptions } from './management' |
| 11 | +import { normalizeFormForgeCategory, normalizeFormForgeCategoryListResponse } from '../utils/category' |
| 12 | +import { pickFormForgeDataEnvelope } from '../utils/object' |
| 13 | +import { resolveEndpointPath, type FormForgeRequestOptions } from './request' |
| 14 | + |
| 15 | +function withMutationHeaders(options: FormForgeMutationOptions = {}): Record<string, string> { |
| 16 | + if (options.idempotencyKey === undefined || options.idempotencyKey === '') { |
| 17 | + return {} |
| 18 | + } |
| 19 | + |
| 20 | + return { |
| 21 | + 'Idempotency-Key': options.idempotencyKey |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function toJsonObject(input: FormForgeCategoryCreateInput | FormForgeCategoryUpdateInput): FormForgeJsonObject { |
| 26 | + return JSON.parse(JSON.stringify(input)) as FormForgeJsonObject |
| 27 | +} |
| 28 | + |
| 29 | +function normalizeSingleCategory(payload: FormForgeJsonObject): FormForgeCategory { |
| 30 | + const dataEnvelope = pickFormForgeDataEnvelope(payload) |
| 31 | + const normalized = normalizeFormForgeCategory(dataEnvelope) |
| 32 | + |
| 33 | + if (normalized !== null) { |
| 34 | + return normalized |
| 35 | + } |
| 36 | + |
| 37 | + return { |
| 38 | + id: 0, |
| 39 | + key: '', |
| 40 | + name: '', |
| 41 | + description: null, |
| 42 | + is_active: false, |
| 43 | + meta: {}, |
| 44 | + created_at: null, |
| 45 | + updated_at: null |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +export async function fetchFormForgeCategories( |
| 50 | + http: FormForgeHttpAdapter, |
| 51 | + query: FormForgeCategoryListQuery = {}, |
| 52 | + options: FormForgeRequestOptions = {} |
| 53 | +): Promise<FormForgeCategoryListResponse> { |
| 54 | + const resolvedQuery: Record<string, string | number | boolean | undefined> = { |
| 55 | + per_page: query.per_page, |
| 56 | + search: query.search, |
| 57 | + is_active: query.is_active === undefined ? undefined : (query.is_active ? 1 : 0) |
| 58 | + } |
| 59 | + |
| 60 | + const response = await http<FormForgeJsonObject>({ |
| 61 | + path: resolveEndpointPath(options.endpoint, '/categories', {}, options.scope), |
| 62 | + method: 'GET', |
| 63 | + query: resolvedQuery |
| 64 | + }) |
| 65 | + |
| 66 | + return normalizeFormForgeCategoryListResponse(response.data) |
| 67 | +} |
| 68 | + |
| 69 | +export async function fetchFormForgeCategory( |
| 70 | + http: FormForgeHttpAdapter, |
| 71 | + categoryKey: string, |
| 72 | + options: FormForgeRequestOptions = {} |
| 73 | +): Promise<FormForgeCategory> { |
| 74 | + const response = await http<FormForgeJsonObject>({ |
| 75 | + path: resolveEndpointPath(options.endpoint, `/categories/${categoryKey}`, { |
| 76 | + categoryKey |
| 77 | + }, options.scope), |
| 78 | + method: 'GET' |
| 79 | + }) |
| 80 | + |
| 81 | + return normalizeSingleCategory(response.data) |
| 82 | +} |
| 83 | + |
| 84 | +export async function createFormForgeCategory( |
| 85 | + http: FormForgeHttpAdapter, |
| 86 | + input: FormForgeCategoryCreateInput, |
| 87 | + options: FormForgeMutationOptions = {} |
| 88 | +): Promise<FormForgeCategory> { |
| 89 | + const response = await http<FormForgeJsonObject>({ |
| 90 | + path: resolveEndpointPath(options.endpoint, '/categories', {}, options.scope), |
| 91 | + method: 'POST', |
| 92 | + headers: withMutationHeaders(options), |
| 93 | + json: toJsonObject(input) |
| 94 | + }) |
| 95 | + |
| 96 | + return normalizeSingleCategory(response.data) |
| 97 | +} |
| 98 | + |
| 99 | +export async function patchFormForgeCategory( |
| 100 | + http: FormForgeHttpAdapter, |
| 101 | + categoryKey: string, |
| 102 | + input: FormForgeCategoryUpdateInput, |
| 103 | + options: FormForgeMutationOptions = {} |
| 104 | +): Promise<FormForgeCategory> { |
| 105 | + const response = await http<FormForgeJsonObject>({ |
| 106 | + path: resolveEndpointPath(options.endpoint, `/categories/${categoryKey}`, { |
| 107 | + categoryKey |
| 108 | + }, options.scope), |
| 109 | + method: 'PATCH', |
| 110 | + headers: withMutationHeaders(options), |
| 111 | + json: toJsonObject(input) |
| 112 | + }) |
| 113 | + |
| 114 | + return normalizeSingleCategory(response.data) |
| 115 | +} |
| 116 | + |
| 117 | +export async function deleteFormForgeCategory( |
| 118 | + http: FormForgeHttpAdapter, |
| 119 | + categoryKey: string, |
| 120 | + options: FormForgeMutationOptions = {} |
| 121 | +): Promise<FormForgeCategory> { |
| 122 | + const response = await http<FormForgeJsonObject>({ |
| 123 | + path: resolveEndpointPath(options.endpoint, `/categories/${categoryKey}`, { |
| 124 | + categoryKey |
| 125 | + }, options.scope), |
| 126 | + method: 'DELETE', |
| 127 | + headers: withMutationHeaders(options) |
| 128 | + }) |
| 129 | + |
| 130 | + return normalizeSingleCategory(response.data) |
| 131 | +} |
0 commit comments