import {z} from 'zod' export type OrganizationType = 'neobank' | 'community_bank' | 'credit_union' | 'regional_bank' | 'national_bank' | 'fintech' | 'smb_lender' | 'merchant_cash_advanced' | 'processor' | 'infrastructure' | 'other'; export type ContactRequest = {name: string, email: string}; export type CreateOrganizationRequest = {name: string, domain_name?: (string | null) | undefined, type: OrganizationType, external_id?: (string | null) | undefined, contact?: (ContactRequest | null) | undefined}; export type CreateApiOrganizationsArgs = {body: CreateOrganizationRequest}; export type CreateApiOrganizationsOptions = {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 v1OrganizationResponse = 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)}); 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 Organization * * POST /organizations */ export const createApiOrganizations = async (args: CreateApiOrganizationsArgs, options?: CreateApiOrganizationsOptions) => { 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(/\/+$/, '')}/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(v1OrganizationResponse, await res.json()) };