import {z} from 'zod' export type GetApiServiceOwnerUsernameUsersUserUsernameOrOwneridArgs = {owner_username: string, service: 'bitbucket' | 'bitbucket_server' | 'github' | 'github_enterprise' | 'gitlab' | 'gitlab_enterprise', user_username_or_ownerid: string}; export type GetApiServiceOwnerUsernameUsersUserUsernameOrOwneridOptions = {baseUrl?: string | undefined, tokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceEnum = z.enum(["github", "gitlab", "bitbucket", "github_enterprise", "gitlab_enterprise", "bitbucket_server"]); export const user = z.object({service: serviceEnum, username: z.string().nullable().optional(), name: z.string().nullable().optional(), activated: z.boolean(), is_admin: z.boolean(), email: 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 }; /** * User detail * * GET /{service}/{owner_username}/users/{user_username_or_ownerid}/ */ export const getApiServiceOwnerUsernameUsersUserUsernameOrOwnerid = async (args: GetApiServiceOwnerUsernameUsersUserUsernameOrOwneridArgs, options?: GetApiServiceOwnerUsernameUsersUserUsernameOrOwneridOptions) => { const {baseUrl = "https://api.codecov.io/api/v2", fetch: fetchFn = fetch, tokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (tokenAuth !== undefined) headers.set('authorization', `Bearer ${tokenAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.service))}/${encodeURIComponent(String(args.owner_username))}/users/${encodeURIComponent(String(args.user_username_or_ownerid))}/${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(user, await res.json()) };