import {z} from 'zod' export type GetApiV1OrganizationsArgs = {'Content-Type'?: string | undefined, 'X-Request-Id'?: string | undefined, Authorization?: string | undefined, limit?: number | undefined, start_date?: string | undefined, end_date?: string | undefined, sort_order?: ('asc' | 'desc') | undefined, page?: number | undefined, 'metadata.key'?: string | undefined, legal_name?: string | undefined, doing_business_as?: string | undefined, status?: ('ACTIVE' | 'INACTIVE') | undefined, legal_document?: string | undefined}; export type GetApiV1OrganizationsOptions = {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 addressOrganization = z.object({line1: z.string(), line2: z.string().optional(), zipCode: z.string(), city: z.string(), state: z.string(), country: z.string(), description: z.string().max(100).optional()}); export const metadata = z.record(z.string(), z.union([z.string().max(2000), z.number(), z.boolean()])); export const createOrganizationResponse = z.object({id: z.string().optional(), legalName: z.string().max(256).optional(), parentOrganizationId: z.string().nullable().optional(), doingBusinessAs: z.string().max(256).nullable().optional(), legalDocument: z.string().max(256).optional(), status: statusOrganization.optional(), address: addressOrganization.optional(), metadata: metadata.optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), deletedAt: z.string().nullable().optional()}); export const getApiV1OrganizationsResponse = z.object({items: z.array(createOrganizationResponse).optional(), page: z.number().int().optional(), limit: z.number().int().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 }; /** * List Organizations * * GET /v1/organizations */ export const getApiV1Organizations = async (args: GetApiV1OrganizationsArgs, options?: GetApiV1OrganizationsOptions) => { const {baseUrl = "https://identity.sandbox.lerian.net", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.page != null) query.append("page", String(args.page)) if (args['metadata.key'] != null) query.append("metadata.key", String(args['metadata.key'])) if (args.legal_name != null) query.append("legal_name", String(args.legal_name)) if (args.doing_business_as != null) query.append("doing_business_as", String(args.doing_business_as)) if (args.status != null) query.append("status", String(args.status)) if (args.legal_document != null) query.append("legal_document", String(args.legal_document)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(getApiV1OrganizationsResponse, await res.json()) };