import {z} from 'zod' export type StatusOrganizationRequest = {code?: string | undefined, description?: (string | null) | undefined}; export type Metadata = {[key: string]: string | number | boolean}; export type CreateLedgerRequest = {name: string, status?: StatusOrganizationRequest | undefined, metadata?: Metadata | undefined, settings?: ({accounting?: ({validateAccountType?: boolean | undefined, validateRoutes?: boolean | undefined}) | undefined}) | undefined}; export type CreateApiV1OrganizationsOrganizationIdLedgersArgs = {organization_id: string, 'Content-Type'?: string | undefined, 'X-Request-Id'?: string | undefined, Authorization?: string | undefined, body: CreateLedgerRequest}; export type CreateApiV1OrganizationsOrganizationIdLedgersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const statusOrganization = z.object({code: z.string().max(100).optional(), description: z.string().max(256).nullable().optional()}); export const metadata = z.record(z.string(), z.union([z.string().max(2000), z.number(), z.boolean()])); export const ledgerSettingsResponse = z.object({accounting: z.object({validateAccountType: z.boolean().optional(), validateRoutes: z.boolean().optional()}).optional()}); export const createLedgerResponse = z.object({id: z.string().optional(), organizationId: z.string().optional(), name: z.string().max(256).optional(), status: statusOrganization.optional(), metadata: metadata.optional(), settings: ledgerSettingsResponse.optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), deletedAt: z.string().nullable().optional()}); 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 a Ledger * * POST /v1/organizations/{organization_id}/ledgers */ export const createApiV1OrganizationsOrganizationIdLedgers = async (args: CreateApiV1OrganizationsOrganizationIdLedgersArgs, options?: CreateApiV1OrganizationsOrganizationIdLedgersOptions) => { const {baseUrl = "https://identity.sandbox.lerian.net", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (args['X-Request-Id'] != null) headers.set("X-Request-Id", String(args['X-Request-Id'])) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/organizations/${encodeURIComponent(String(args.organization_id))}/ledgers${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(createLedgerResponse, await res.json()) };