import {z} from 'zod' export type GetApi10CompaniesArgs = {pageSize?: number | undefined, pageToken?: string | undefined, includeFields?: (Array<'companyUrl' | 'notes'>) | undefined, includeAllFields?: boolean | undefined, sortBy?: ('companyName' | 'createdAt' | 'updatedAt') | undefined, sortOrder?: ('ASC' | 'DESC') | undefined, match?: ('all' | 'any') | undefined, 'companyName.eq'?: string | undefined, 'companyName.cn'?: string | undefined, 'companyName.nc'?: string | undefined, 'companyUrl.eq'?: string | undefined, 'companyUrl.cn'?: string | undefined, 'companyUrl.nc'?: string | undefined, 'accountOwner.eq'?: string | undefined, 'accountOwner.oneOf'?: string | undefined, 'accountOwner.noneOf'?: string | undefined}; export type GetApi10CompaniesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const companyCreatedByUserPublicAPI = z.object({emailId: z.string().optional(), userId: z.number().int().optional(), firstName: z.string().optional(), lastName: z.string().optional()}); export const companyUpdatedByUserPublicAPI = z.object({emailId: z.string().optional(), userId: z.number().int().optional(), firstName: z.string().optional(), lastName: z.string().optional()}); export const companyAccountOwnerPublicResponse = z.object({emailId: z.string().optional(), userId: z.number().int().optional(), firstName: z.string().optional(), lastName: z.string().optional()}); export const companyFieldPublicResponseAPIEntity = z.object({fieldId: z.number().int().gte(1).optional(), fieldLabel: z.string().optional(), fieldValue: z.string().optional(), fieldValueLabel: z.string().optional()}); export const companyNoteTagPublicResponseEntity = z.object({tagId: z.number().int().optional(), label: z.string().optional(), color: z.string().optional()}); export const companyNotePublicResponseAPIEntity = z.object({noteId: z.number().int().optional(), fieldId: z.number().int().optional(), fieldLabel: z.string().optional(), noteTitle: z.string().optional(), noteText: z.string().optional(), tags: z.array(companyNoteTagPublicResponseEntity).optional(), createdAt: z.number().int().optional(), updatedAt: z.number().int().optional()}); export const customerCompanyPublicAPIResponseEntity = z.object({companyId: z.number().int().gte(1).optional(), companyName: z.string().optional(), companyUrl: z.string().optional(), visibility: z.enum(["EVERYONE", "MEMBERS", "GROUP", "EVERYONE", "MEMBERS", "GROUP"]).optional(), createdAt: z.number().int().optional(), updatedAt: z.number().int().optional(), createdBy: companyCreatedByUserPublicAPI.optional(), updatedBy: companyUpdatedByUserPublicAPI.optional(), description: z.string().optional(), accountOwner: companyAccountOwnerPublicResponse.optional(), fields: z.array(companyFieldPublicResponseAPIEntity).optional(), notes: z.array(companyNotePublicResponseAPIEntity).optional()}); export const examplePaginationCompanies = z.object({pageSize: z.number().int().optional(), hasMore: z.boolean().optional(), totalRecordCount: z.number().int().optional(), nextPage: z.string().optional(), nextPageToken: z.string().optional()}); export const exampleAPIResponseCompanies = z.object({data: z.array(customerCompanyPublicAPIResponseEntity).optional(), pagination: examplePaginationCompanies.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 all companies * * GET /1.0/companies */ export const getApi10Companies = async (args: GetApi10CompaniesArgs, options?: GetApi10CompaniesOptions) => { 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.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) if (args.includeFields != null) query.append("includeFields", args.includeFields.join(',')) if (args.includeAllFields != null) query.append("includeAllFields", String(args.includeAllFields)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (args.match != null) query.append("match", String(args.match)) if (args['companyName.eq'] != null) query.append("companyName.eq", String(args['companyName.eq'])) if (args['companyName.cn'] != null) query.append("companyName.cn", String(args['companyName.cn'])) if (args['companyName.nc'] != null) query.append("companyName.nc", String(args['companyName.nc'])) if (args['companyUrl.eq'] != null) query.append("companyUrl.eq", String(args['companyUrl.eq'])) if (args['companyUrl.cn'] != null) query.append("companyUrl.cn", String(args['companyUrl.cn'])) if (args['companyUrl.nc'] != null) query.append("companyUrl.nc", String(args['companyUrl.nc'])) if (args['accountOwner.eq'] != null) query.append("accountOwner.eq", String(args['accountOwner.eq'])) if (args['accountOwner.oneOf'] != null) query.append("accountOwner.oneOf", String(args['accountOwner.oneOf'])) if (args['accountOwner.noneOf'] != null) query.append("accountOwner.noneOf", String(args['accountOwner.noneOf'])) if (apiKey !== undefined) headers.set("api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/1.0/companies${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(exampleAPIResponseCompanies, await res.json()) };