import {z} from 'zod' export type PartnersTypeEnum = 'consortium' | 'hostAgency' | 'agency'; export type TeamEnum = 'west' | 'east' | 'midAmerica' | 'PM' | 'nonUS'; export type CreateCompanySettingDto = {showExtraDetailsFlow?: boolean | undefined, minDaysFromToday?: number | undefined, excludedFlowKeys?: Array | undefined}; export type CreatePartnerDto = {name: string, type: PartnersTypeEnum, email?: string | undefined, accountManagerEmail: string, primaryAdmEmail?: string | undefined, state: string, consortiumName?: string | undefined, externalId?: string | undefined, source?: Record | undefined, team?: TeamEnum | undefined, partnerIdentifier?: string | undefined, api?: boolean | undefined, utm_source?: string | undefined, utm_medium?: string | undefined, insuranceProducts?: Array | undefined, defaultInsuranceProduct?: string | undefined, companySetting?: CreateCompanySettingDto | undefined, parentCompanyExternalId?: string | undefined}; export type CreateApiV1PartnerArgs = {Authorization?: string | undefined, body: CreatePartnerDto}; export type CreateApiV1PartnerOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const partnersTypeEnum = z.enum(["consortium", "hostAgency", "agency"]); export const teamEnum = z.enum(["west", "east", "midAmerica", "PM", "nonUS"]); export const createCompanySettingDto = z.object({showExtraDetailsFlow: z.boolean().optional(), minDaysFromToday: z.number().optional(), excludedFlowKeys: z.array(z.string()).optional()}); export const createPartnerDto = z.object({name: z.string(), type: partnersTypeEnum, email: z.string().optional(), accountManagerEmail: z.string(), primaryAdmEmail: z.string().optional(), state: z.string(), consortiumName: z.string().optional(), externalId: z.string().optional(), source: z.object({}).optional(), team: teamEnum.optional(), partnerIdentifier: z.string().optional(), api: z.boolean().optional(), utm_source: z.string().optional(), utm_medium: z.string().optional(), insuranceProducts: z.array(z.string()).optional(), defaultInsuranceProduct: z.string().optional(), companySetting: createCompanySettingDto.optional(), parentCompanyExternalId: z.string().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 }; /** * Generate a new partner * * POST /v1/partner */ export const createApiV1Partner = async (args: CreateApiV1PartnerArgs, options?: CreateApiV1PartnerOptions) => { const {baseUrl = "https://partnerapi.withfaye.com", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/partner${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(createPartnerDto, await res.json()) };