import {z} from 'zod' export type CreateApiOrganizationsArgs = {'Idempotency-Key'?: string | undefined, body: {name: string, website?: string | undefined, linkedin?: string | undefined, address?: ({region?: string | undefined, country: string, formatted?: string | undefined, street_address?: string | undefined, locality?: string | undefined, postal_code?: string | undefined}) | undefined, legal_structure?: ('sole_proprietor' | 'single_member_llc' | 'limited_liability_company' | 'general_partnership' | 'unlisted_corporation' | 'publicly_traded_corporation' | 'association' | 'non_profit' | 'government_organization' | 'revocable_trust' | 'irrevocable_trust' | 'estate') | undefined, industry?: string | undefined, formation_date?: string | undefined, size?: ('1-24' | '25-99' | '100-499' | '500+') | undefined, yc_id?: ('no' | 'claimed') | undefined, yc_url?: string | undefined}}; export type CreateApiOrganizationsOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiOrganizationsResponse = z.object({name: z.string().min(1), website: z.string().optional(), linkedin: z.string().optional(), address: z.object({region: z.string().min(1).optional(), country: z.string(), formatted: z.string().min(1).optional(), street_address: z.string().min(1).optional(), locality: z.string().min(1).optional(), postal_code: z.string().min(1).optional()}).optional(), legal_structure: z.enum(["sole_proprietor", "single_member_llc", "limited_liability_company", "general_partnership", "unlisted_corporation", "publicly_traded_corporation", "association", "non_profit", "government_organization", "revocable_trust", "irrevocable_trust", "estate"]).optional(), industry: z.string().optional(), formation_date: z.string().optional(), size: z.enum(["1-24", "25-99", "100-499", "500+"]).optional(), yc_id: z.union([z.string(), z.enum(["no", "claimed"])]).optional(), aiprise_business_id: z.string().optional(), monato_business_id: z.string().optional(), picture: z.string(), id: z.string(), created_at: z.string(), created_by: z.string(), updated_at: z.string(), updated_by: z.string()}); 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 an organization * * POST /organizations * * Also accepts auth: OAuth2 */ export const createApiOrganizations = async (args: CreateApiOrganizationsArgs, options?: CreateApiOrganizationsOptions) => { const {baseUrl = "https://core.sivo.com", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (APIKeyAuth !== undefined) headers.set("Authorization", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/organizations${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(createApiOrganizationsResponse, await res.json()) };