import {z} from 'zod' export type FieldOptionPublicRequest = {optionColor: 'RED' | 'YELLOW' | 'GREEN' | 'TEAL' | 'CYAN' | 'BLUE' | 'PURPLE' | 'MAGENTA' | 'GRAY' | 'COOL_GRAY', optionLabel: string}; export type FieldPublicRequest = {fieldId?: number | undefined, fieldLabel: string, fieldDescription?: string | undefined, fieldType: 'TEXT' | 'MULTI_LINE_TEXT' | 'YES_OR_NO' | 'DATE' | 'SINGLE_CHOICE' | 'MULTIPLE_CHOICE' | 'SINGLE_USER' | 'MULTIPLE_USER' | 'NUMBER' | 'NOTE' | 'RATING', objectType: 'PROJECT' | 'TASK' | 'USER' | 'BUDGET' | 'COMPANY', fieldOptions?: Array | undefined, ratingScale?: ('THREE' | 'FIVE' | 'SEVEN' | 'TEN') | undefined, enabled?: boolean | undefined, private?: boolean | undefined}; export type CreateApi10FieldsArgs = {includeFields?: Array<'options'> | undefined, includeAllFields?: boolean | undefined, body: FieldPublicRequest}; export type CreateApi10FieldsOptions = {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 }; /** * Create a Field * * POST /1.0/fields */ export const createApi10Fields = async (args: CreateApi10FieldsArgs, options?: CreateApi10FieldsOptions) => { const {baseUrl = "https://api.rocketlane.com/api", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) 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()) };