import {z} from 'zod' export type GetApi10TimeOffsTimeOffIdArgs = {timeOffId: number, includeFields?: (Array<'note' | 'notifyUsers'>) | undefined, includeAllFields?: boolean | undefined}; export type GetApi10TimeOffsTimeOffIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const timeOffUserPublicAPIResponseEntity = z.object({emailId: z.string().optional(), userId: z.number().int().optional(), firstName: z.string().optional(), lastName: z.string().optional()}); export const timeOffOtherUsersPublicAPIResponse = z.object({emailId: z.string().optional(), userId: z.number().int().optional(), firstName: z.string().optional(), lastName: z.string().optional()}); export const tImeOffNotifyUsersPublicAPIResponse = z.object({projectOwners: z.boolean().optional(), others: z.array(timeOffOtherUsersPublicAPIResponse).optional()}); export const timeOffCreatedByPublicResponse = z.object({emailId: z.string().optional(), userId: z.number().int().optional(), firstName: z.string().optional(), lastName: z.string().optional()}); export const timeOffPublicAPIResponseEntity = z.object({timeOffId: z.number().int().gte(1).optional(), user: timeOffUserPublicAPIResponseEntity.optional(), note: z.string().optional(), startDate: z.string().optional(), endDate: z.string().optional(), durationInMinutes: z.number().int().optional(), type: z.enum(["FULL_DAY", "HALF_DAY", "CUSTOM"]).optional(), notifyUsers: tImeOffNotifyUsersPublicAPIResponse.optional(), createdAt: z.number().int().optional(), createdBy: timeOffCreatedByPublicResponse.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 time-off by Id * * GET /1.0/time-offs/{timeOffId} */ export const getApi10TimeOffsTimeOffId = async (args: GetApi10TimeOffsTimeOffIdArgs, options?: GetApi10TimeOffsTimeOffIdOptions) => { 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/time-offs/${encodeURIComponent(String(args.timeOffId))}${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(timeOffPublicAPIResponseEntity, await res.json()) };