import {z} from 'zod' export type GetApiOrganizationsMyOptions = {baseUrl?: string | undefined, orgToken?: string | undefined, fetch?: typeof fetch | undefined}; export const organization = z.object({id: z.string().optional(), name: z.string().optional(), subdomain: z.string().optional(), custom_domain: z.string().nullable().optional(), locale: z.string().optional(), timezone: z.string().optional(), currency: z.string().optional(), suspended_at: z.string().nullable().optional(), operational_address_id: z.string().nullable().optional()}); export const getApiOrganizationsMyResponse = z.object({organization: organization.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 }; /** * Obtener mi organización * * GET /organizations/my */ export const getApiOrganizationsMy = async (options?: GetApiOrganizationsMyOptions) => { const {baseUrl = "https://mi-organizacion.routes.zipnova.app/api/v1", fetch: fetchFn = fetch, orgToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (orgToken !== undefined) headers.set('authorization', `Bearer ${orgToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/organizations/my${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(getApiOrganizationsMyResponse, await res.json()) };