import {z} from 'zod' export type CustomMetadataDataType = 'boolean' | 'date' | 'link' | 'multi_select' | 'number' | 'single_select' | 'string' | 'text'; export type GetApiAccountsAccountIdCustomMetadataPropertiesArgs = {'X-PhraseApp-OTP'?: string | undefined, account_id: string, data_type?: CustomMetadataDataType | undefined, project_id?: string | undefined, page?: number | undefined, per_page?: number | undefined, q?: string | undefined, sort?: string | undefined, order?: string | undefined}; export type GetApiAccountsAccountIdCustomMetadataPropertiesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const customMetadataDataType = z.enum(["boolean", "date", "link", "multi_select", "number", "single_select", "string", "text"]); export const userPreview = z.object({id: z.string().optional(), username: z.string().optional(), name: z.string().optional(), gravatar_uid: z.string().optional()}); export const projectShort = z.object({id: z.string().optional(), name: z.string().optional(), main_format: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const customMetadataProperty = z.object({id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), data_type: customMetadataDataType.optional(), user: userPreview.optional(), projects: z.array(projectShort).optional(), value_options: z.array(z.string()).optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const getApiAccountsAccountIdCustomMetadataPropertiesResponse = z.array(customMetadataProperty); 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 }; /** * List properties * * GET /accounts/{account_id}/custom_metadata/properties */ export const getApiAccountsAccountIdCustomMetadataProperties = async (args: GetApiAccountsAccountIdCustomMetadataPropertiesArgs, options?: GetApiAccountsAccountIdCustomMetadataPropertiesOptions) => { const {baseUrl = "https://eu.phrase.com/control-hub", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-PhraseApp-OTP'] != null) headers.set("X-PhraseApp-OTP", String(args['X-PhraseApp-OTP'])) if (args.data_type != null) query.append("data_type", String(args.data_type)) if (args.project_id != null) query.append("project_id", String(args.project_id)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.q != null) query.append("q", String(args.q)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.order != null) query.append("order", String(args.order)) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.account_id))}/custom_metadata/properties${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(getApiAccountsAccountIdCustomMetadataPropertiesResponse, await res.json()) };