import {z} from 'zod' export type GetApiV4OrganizationsCustomdataLabelsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const customDataFieldLabelResponse = z.object({fieldLabelId: z.string().nullable().optional(), fieldLabelName: z.string().nullable().optional(), isActive: z.boolean().optional(), order: z.number().int().optional()}); export const customDataLabelsResponse = z.object({organizationId: z.string().nullable().optional(), fieldLabels: z.array(customDataFieldLabelResponse).nullable().optional(), createdOn: z.string().nullable().optional(), createdBy: z.string().nullable().optional(), updatedOn: z.string().nullable().optional(), updatedBy: z.string().nullable().optional(), version: z.number().int().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get a list of Custom Data Labels * * GET /v4/organizations/customdata/labels */ export const getApiV4OrganizationsCustomdataLabels = async (options?: GetApiV4OrganizationsCustomdataLabelsOptions) => { const {baseUrl = "https://submittable-api.submittable.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v4/organizations/customdata/labels${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(customDataLabelsResponse, await res.json()) };