import {z} from 'zod' export type GetApiV1HostingWebsitesWebsiteIdUserInfoArgs = {website_id: number, 'x-api-key': string}; export type GetApiV1HostingWebsitesWebsiteIdUserInfoOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const userInfoResponse = z.object({status: z.enum(["ok", "error"]).optional(), data: z.object({mysql: z.object({db_name: z.string().optional(), password: z.string().optional(), path: z.string().optional(), type: z.literal("mysql").optional(), username: z.string().optional()}).optional(), sftp: z.object({password: z.string().optional(), port: z.number().int().optional(), type: z.literal("sftp").optional(), username: z.string().optional()}).optional()}).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 }; /** * Get user info for a website * * GET /v1/hosting/websites/{website_id}/user_info */ export const getApiV1HostingWebsitesWebsiteIdUserInfo = async (args: GetApiV1HostingWebsitesWebsiteIdUserInfoArgs, options?: GetApiV1HostingWebsitesWebsiteIdUserInfoOptions) => { const {baseUrl = "https://api.10web.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/hosting/websites/${encodeURIComponent(String(args.website_id))}/user_info${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(userInfoResponse, await res.json()) };