import {z} from 'zod' export type GetApiOrgsOrgTeamsSearchArgs = {org: string, q?: string | undefined, include_desc?: boolean | undefined, page?: number | undefined, limit?: number | undefined}; export type GetApiOrgsOrgTeamsSearchOptions = {baseUrl: string, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const organization = z.object({avatar_url: z.string().optional(), description: z.string().optional(), email: z.string().optional(), full_name: z.string().optional(), id: z.number().int().optional(), location: z.string().optional(), name: z.string().optional(), repo_admin_change_team_access: z.boolean().optional(), username: z.string().optional(), visibility: z.enum(["public", "limited", "private"]).optional(), website: z.string().optional()}); export const team = z.object({can_create_org_repo: z.boolean().optional(), description: z.string().optional(), id: z.number().int().optional(), includes_all_repositories: z.boolean().optional(), name: z.string().optional(), organization: organization.optional(), permission: z.enum(["none", "read", "write", "admin", "owner"]).optional(), units: z.array(z.string()).optional(), units_map: z.record(z.string(), z.string()).optional(), visibility: z.enum(["public", "limited", "private"]).optional()}); export const getApiOrgsOrgTeamsSearchResponse = z.object({data: z.array(team).optional(), ok: z.boolean().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 }; /** * Search for teams within an organization * * GET /orgs/{org}/teams/search * * Also accepts auth: Token, AccessToken, AuthorizationHeaderToken, SudoParam, SudoHeader, TOTPHeader */ export const getApiOrgsOrgTeamsSearch = async (args: GetApiOrgsOrgTeamsSearchArgs, options: GetApiOrgsOrgTeamsSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", String(args.q)) if (args.include_desc != null) query.append("include_desc", String(args.include_desc)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/orgs/${encodeURIComponent(String(args.org))}/teams/search${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(getApiOrgsOrgTeamsSearchResponse, await res.json()) };