import {z} from 'zod' export type StatusOrganizationRequest = {code?: string | undefined, description?: (string | null) | undefined}; export type AddressOrganization = {line1: string, line2?: string | undefined, zipCode: string, city: string, state: string, country: string, description?: string | undefined}; export type Metadata = {[key: string]: string | number | boolean}; export type CreateOrganizationRequest = {legalName: string, parentOrganizationId?: (string | null) | undefined, doingBusinessAs?: (string | null) | undefined, legalDocument: string, status?: StatusOrganizationRequest | undefined, address?: AddressOrganization | undefined, metadata?: Metadata | undefined}; export type CreateApiV1OrganizationsArgs = {'Content-Type'?: string | undefined, 'X-Request-Id'?: string | undefined, Authorization?: string | undefined, body: CreateOrganizationRequest}; export type CreateApiV1OrganizationsOptions = {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 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 an Organization * * POST /v1/organizations */ export const createApiV1Organizations = async (args: CreateApiV1OrganizationsArgs, options?: CreateApiV1OrganizationsOptions) => { 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${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(createOrganizationResponse, await res.json()) };