import {z} from 'zod' export type GetApiOrganizationsArgs = {limit?: number | undefined, offset?: number | undefined, q?: (string | null) | undefined, unfederated?: boolean | undefined}; export type GetApiOrganizationsOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const v1OrganizationType = z.enum(["neobank", "community_bank", "credit_union", "regional_bank", "national_bank", "fintech", "smb_lender", "merchant_cash_advanced", "processor", "infrastructure", "other"]); export const v1WebhooksVersion = z.enum(["0.8", "1.0", "2.0", "2.0"]); export const v1ContactResponse = z.object({id: z.string(), name: z.string().nullable().optional(), email: z.string().nullable().optional()}); export const v1FederationResponse = z.object({id: z.string(), name: z.string().nullable().optional(), contact: v1ContactResponse.nullable().optional()}); export const v1ApplicationType = z.enum(["sandbox", "production"]); export const v1ApplicationResponse = z.object({id: z.string(), name: z.string().nullable().optional(), type: v1ApplicationType, enabled: z.boolean(), external_id: z.string().nullable().optional()}); export const v1StateAbbreviation = z.enum(["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY", "PR", "VI", "AE", "AA", "AP", "GU", "AS"]); export const v1BusinessStructure = z.enum(["SOLE_PROPRIETORSHIP", "GENERAL_PARTNERSHIP", "LLC", "LLP", "LLLP", "LP", "C_CORPORATION", "S_CORPORATION", "B_CORPORATION", "NONPROFIT", "COOPERATIVE", "TRUST", "PROFESSIONAL_ASSOCIATION", "PROFESSIONAL_CORPORATION", "TRADE_NAME", "BANK", "CREDIT_UNION", "INSURANCE", "OTHER"]); export const v1LienFilingOrganizationSecuredParty = z.object({mailing_address: z.string().max(128).nullable().optional(), city: z.string().max(128).nullable().optional(), state: v1StateAbbreviation.nullable().optional(), postal_code: z.string().max(16).nullable().optional(), masked: z.boolean().nullable().optional(), is_assignor: z.boolean().nullable().optional(), organization_name: z.string().max(128).nullable().optional(), organization_type: v1BusinessStructure.nullable().optional(), type: z.literal("ORGANIZATION")}); export const v1LienFilingCollateralStatementTemplate = z.object({title: z.string(), text: z.string()}); export const v1OrganizationDetailResponse = z.object({id: z.string(), name: z.string().nullable(), domain_name: z.string().nullable(), type: v1OrganizationType, external_id: z.string().nullable(), webhooks_version: v1WebhooksVersion, federation: v1FederationResponse.nullable(), applications: z.array(v1ApplicationResponse), lien_filing_correspondence_email: z.string().nullable().optional(), lien_filing_secured_party: v1LienFilingOrganizationSecuredParty.nullable().optional(), lien_filing_collateral_statement: z.string().nullable().optional(), lien_filing_collateral_statement_templates: z.array(v1LienFilingCollateralStatementTemplate).optional()}); export const getApiOrganizationsResponse = z.array(v1OrganizationDetailResponse); 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 Organizations Within Scope * * GET /organizations */ export const getApiOrganizations = async (args: GetApiOrganizationsArgs, options?: GetApiOrganizationsOptions) => { const {baseUrl = "https://api.baselayer.com", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.q != null) query.append("q", String(args.q)) if (args.unfederated != null) query.append("unfederated", String(args.unfederated)) if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/organizations${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(getApiOrganizationsResponse, await res.json()) };