import {z} from 'zod' export type GetApiCrbsCrbIdArgs = {crb_id: string}; export type GetApiCrbsCrbIdOptions = {baseUrl?: string | undefined, accessAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const uUID = z.string(); export const city = z.string(); export const eIN = z.string(); export const phoneNumber = z.string(); export const postalCode = z.string(); export const uSStates = z.enum(["AL", "AK", "AZ", "AR", "AS", "CA", "CO", "CT", "DE", "DC", "FM", "FL", "GA", "GU", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MH", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "MP", "OH", "OK", "OR", "PW", "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UM", "UT", "VT", "VA", "VI", "WA", "WV", "WI", "WY", "AA", "AE", "AP"]); export const website = z.string(); export const iANATimezones = z.enum(["America/New_York", "America/Detroit", "America/Kentucky/Louisville", "America/Kentucky/Monticello", "America/Indiana/Indianapolis", "America/Indiana/Vincennes", "America/Indiana/Winamac", "America/Indiana/Marengo", "America/Indiana/Petersburg", "America/Indiana/Vevay", "America/Chicago", "America/Indiana/Tell_City", "America/Indiana/Knox", "America/Menominee", "America/North_Dakota/Center", "America/North_Dakota/New_Salem", "America/North_Dakota/Beulah", "America/Denver", "America/Boise", "America/Phoenix", "America/Los_Angeles", "America/Anchorage", "America/Juneau", "America/Puerto_Rico", "America/Sitka", "America/St_Thomas", "America/Metlakatla", "America/Yakutat", "America/Nome", "America/Adak", "Pacific/Honolulu", "Pacific/Guam", "Pacific/Kosrae", "Pacific/Midway", "Pacific/Pago_Pago", "Pacific/Palau", "Pacific/Kwajalein", "Pacific/Saipan"]); export const shortDate = z.string(); export const nullablePositiveInteger = z.number().int().gte(0).nullable(); export const entityType = z.enum(["sole", "partnership", "corporation", "llc"]); export const cRB = z.object({id: uUID, name: z.string(), street_address: z.string(), street_address_2: z.string().nullable(), city: city.nullable(), ein: eIN.nullable(), phone_number: phoneNumber.nullable(), postal_code: postalCode.nullable(), country: z.string().nullable(), state: uSStates, website: website.nullable(), timezone: iANATimezones, mailing_street_address: z.string().nullable(), mailing_city: city.nullable(), mailing_state: uSStates.nullable(), mailing_postal_code: postalCode.nullable(), established_date: shortDate.nullable(), ftEmployees: nullablePositiveInteger, ptEmployees: nullablePositiveInteger, entityType: entityType.nullable(), dba: z.string().nullable(), monthlySales: z.string().nullable(), monthlyCustomers: nullablePositiveInteger}); 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 CRB * * GET /crbs/{crb_id} */ export const getApiCrbsCrbId = async (args: GetApiCrbsCrbIdArgs, options?: GetApiCrbsCrbIdOptions) => { const {baseUrl = "https://sandbox-api.greencheckverified.com", fetch: fetchFn = fetch, accessAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (accessAuth !== undefined) headers.set("Authorization", accessAuth) const url = `${baseUrl.replace(/\/+$/, '')}/crbs/${encodeURIComponent(String(args.crb_id))}${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(cRB, await res.json()) };