import {z} from 'zod' export type StringFilter = {eq?: string | undefined, ne?: string | undefined, in?: string | undefined, nin?: string | undefined, between?: string | undefined, contains?: string | undefined, starts?: string | undefined, ends?: string | undefined}; export type DateTimeFilter = {eq?: string | undefined, ne?: string | undefined, between?: string | undefined, gt?: string | undefined, gte?: string | undefined, lt?: string | undefined, lte?: string | undefined}; export type GetApiUsersUserIdOrganizationsArgs = {user_id: string, 'filter[id]'?: StringFilter | undefined, 'filter[type]'?: StringFilter | undefined, 'filter[vendor]'?: StringFilter | undefined, 'filter[status]'?: StringFilter | undefined, 'filter[updated_at]'?: DateTimeFilter | undefined, 'page[size]'?: number | undefined, 'page[before]'?: string | undefined, 'page[after]'?: string | undefined, sort?: string | undefined}; export type GetApiUsersUserIdOrganizationsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const organization = z.object({id: z.string().optional(), type: z.enum(["fixed", "flexible"]).optional(), owner_id: z.string().optional(), namespace: z.string().optional(), name: z.string().optional(), label: z.string().optional(), country: z.string().max(2).optional(), capabilities: z.array(z.string()).optional(), vendor: z.string().optional(), billing_account_id: z.string().optional(), billing_legacy: z.boolean().optional(), security_contact: z.string().optional(), status: z.enum(["active", "restricted", "suspended", "deleted"]).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), _links: z.object({self: z.object({href: z.string().optional()}).optional(), update: z.object({href: z.string().optional(), method: z.string().optional()}).optional(), delete: z.object({href: z.string().optional(), method: z.string().optional()}).optional(), members: z.object({href: z.string().optional()}).optional(), "create-member": z.object({href: z.string().optional(), method: z.string().optional()}).optional(), address: z.object({href: z.string().optional()}).optional(), profile: z.object({href: z.string().optional()}).optional(), "payment-source": z.object({href: z.string().optional()}).optional(), orders: z.object({href: z.string().optional()}).optional(), vouchers: z.object({href: z.string().optional()}).optional(), "apply-voucher": z.object({href: z.string().optional(), method: z.string().optional()}).optional(), subscriptions: z.object({href: z.string().optional()}).optional(), "create-subscription": z.object({href: z.string().optional(), method: z.string().optional()}).optional(), "estimate-subscription": z.object({href: z.string().optional()}).optional(), "mfa-enforcement": z.object({href: z.string().optional()}).optional()}).optional()}); export const link = z.object({href: z.string().optional()}); export const listLinks = z.object({self: link.optional(), previous: link.optional(), next: link.optional()}); export const getApiUsersUserIdOrganizationsResponse = z.object({items: z.array(organization).optional(), _links: listLinks.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 }; /** * User organizations * * GET /users/{user_id}/organizations */ export const getApiUsersUserIdOrganizations = async (args: GetApiUsersUserIdOrganizationsArgs, options?: GetApiUsersUserIdOrganizationsOptions) => { const {baseUrl = "https://api.upsun.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['filter[id]'] != null) query.append("filter[id]", JSON.stringify(args['filter[id]'])) if (args['filter[type]'] != null) query.append("filter[type]", JSON.stringify(args['filter[type]'])) if (args['filter[vendor]'] != null) query.append("filter[vendor]", JSON.stringify(args['filter[vendor]'])) if (args['filter[status]'] != null) query.append("filter[status]", JSON.stringify(args['filter[status]'])) if (args['filter[updated_at]'] != null) query.append("filter[updated_at]", JSON.stringify(args['filter[updated_at]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['page[before]'] != null) query.append("page[before]", String(args['page[before]'])) if (args['page[after]'] != null) query.append("page[after]", String(args['page[after]'])) if (args.sort != null) query.append("sort", String(args.sort)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/users/${encodeURIComponent(String(args.user_id))}/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(getApiUsersUserIdOrganizationsResponse, await res.json()) };