import {z} from 'zod' export type GetApi10FieldsFieldIdArgs = {fieldId: number, includeFields?: Array<'options'> | undefined, includeAllFields?: boolean | undefined}; export type GetApi10FieldsFieldIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const optionsPublicAPIResponseEntity = z.object({optionValue: z.number().int().gte(1).optional(), optionLabel: z.string().optional(), optionColor: z.enum(["RED", "YELLOW", "GREEN", "TEAL", "CYAN", "BLUE", "PURPLE", "MAGENTA", "GRAY", "COOL_GRAY"]).optional()}); export const fieldCreatedByUserPublicAPI = z.object({emailId: z.string().optional(), userId: z.number().int().optional(), firstName: z.string().optional(), lastName: z.string().optional()}); export const fieldUpdatedByUserPublicAPI = z.object({emailId: z.string().optional(), userId: z.number().int().optional(), firstName: z.string().optional(), lastName: z.string().optional()}); export const fieldModelPublicResponseAPIEntity = z.object({fieldId: z.number().int().gte(1).optional(), fieldLabel: z.string().optional(), fieldDescription: z.string().optional(), fieldType: z.enum(["TEXT", "MULTI_LINE_TEXT", "YES_OR_NO", "DATE", "SINGLE_CHOICE", "MULTIPLE_CHOICE", "SINGLE_USER", "MULTIPLE_USER", "NUMBER", "NOTE", "RATING"]).optional(), objectType: z.string().optional(), fieldOptions: z.array(optionsPublicAPIResponseEntity).optional(), ratingScale: z.enum(["THREE", "FIVE", "SEVEN", "TEN"]).optional(), createdBy: fieldCreatedByUserPublicAPI.optional(), updatedBy: fieldUpdatedByUserPublicAPI.optional(), createdAt: z.number().int().optional(), updatedAt: z.number().int().optional(), enabled: z.boolean().optional(), private: z.boolean().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 field by Id * * GET /1.0/fields/{fieldId} */ export const getApi10FieldsFieldId = async (args: GetApi10FieldsFieldIdArgs, options?: GetApi10FieldsFieldIdOptions) => { const {baseUrl = "https://api.rocketlane.com/api", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.includeFields != null) query.append("includeFields", args.includeFields.join(',')) if (args.includeAllFields != null) query.append("includeAllFields", String(args.includeAllFields)) if (apiKey !== undefined) headers.set("api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/1.0/fields/${encodeURIComponent(String(args.fieldId))}${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(fieldModelPublicResponseAPIEntity, await res.json()) };