import {z} from 'zod' export type V1PEPSearchRequest = {name: string, similarity_threshold?: number | undefined, limit?: number | undefined}; export type CreateApiPepSearchesArgs = {body: V1PEPSearchRequest}; export type CreateApiPepSearchesOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const pEPRecordType = z.enum(["PERSON", "ORGANIZATION"]); export const gender = z.enum(["M", "F"]); export const iSO2CountryCode = z.enum(["AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CW", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY", "QA", "RE", "RO", "RS", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST", "SV", "SX", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", "WS", "XK", "YE", "YT", "ZA", "ZM", "ZW"]); export const pEPRiskCategory = z.enum(["GOVERNMENT_EXECUTIVE", "GOVERNMENT_FINANCIAL", "GOVERNMENT_HEAD", "INTERGOVERNMENTAL_ORGANIZATION", "GOVERNMENT_JUDICIAL", "GOVERNMENT_LEGISLATIVE", "GOVERNMENT_MUNICIPAL", "GOVERNMENT_NATIONAL", "GOVERNMENT_SECURITY", "STATE_OWNED_ENTERPRISE", "STATE_GOVERNMENT", "MILITARY", "PERSON_OF_INTEREST", "POLITICAL_PARTY", "DIPLOMAT", "JUDGE", "POLITICALLY_EXPOSED_PERSON", "RELATIVES_OR_CLOSE_ASSOCIATES", "WANTED"]); export const pEPPositionStatus = z.enum(["CURRENT", "ENDED", "UNKNOWN"]); export const pEPPosition = z.object({name: z.string(), country: iSO2CountryCode.nullable().optional(), subnational_area: z.string().nullable().optional(), categories: z.array(pEPRiskCategory).optional(), start_date: z.string().nullable().optional(), end_date: z.string().nullable().optional(), status: pEPPositionStatus.nullable().optional()}); export const v1PEPMatch = z.object({name: z.string(), alternative_names: z.array(z.string()).optional(), addresses: z.array(z.string()).optional(), similarity_score: z.number().gte(0).lte(1), record_type: pEPRecordType.nullable().optional(), gender: gender.nullable().optional(), date_of_birth: z.string().nullable().optional(), first_name: z.string().nullable().optional(), middle_name: z.string().nullable().optional(), last_name: z.string().nullable().optional(), title: z.string().nullable().optional(), classification: z.string().nullable().optional(), political_party: z.string().nullable().optional(), birth_place: z.string().nullable().optional(), death_date: z.string().nullable().optional(), notes: z.string().nullable().optional(), wikidata_id: z.string().nullable().optional(), positions: z.array(pEPPosition).optional(), risks: z.array(pEPRiskCategory).optional(), associated_countries: z.array(iSO2CountryCode).optional(), emails: z.array(z.string()).optional(), phone_numbers: z.array(z.string()).optional(), websites: z.array(z.string()).optional(), source_links: z.array(z.string()).optional()}); export const v1PEPSearchResponse = z.object({matches: z.array(v1PEPMatch).optional(), total_matches: z.number().int()}); 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 }; /** * Search Pep * * POST /pep/searches */ export const createApiPepSearches = async (args: CreateApiPepSearchesArgs, options?: CreateApiPepSearchesOptions) => { const {baseUrl = "https://api.baselayer.com", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/pep/searches${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(v1PEPSearchResponse, await res.json()) };