import {z} from 'zod' export type GetApiAuthConnectorsConnectorIdConsentAdminArgs = {connector_id: number, tenant_id?: number | undefined, cookie_name?: (string | null) | undefined}; export type GetApiAuthConnectorsConnectorIdConsentAdminOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const connectorOut = z.object({id: z.number().int(), name: z.string(), provider: z.string(), caps: z.array(z.string()), scopes: z.array(z.string())}); export const connectorConsentOut = z.object({consent_url: z.string(), connector: connectorOut}); 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 }; /** * Initiate admin connector consent * * GET /auth/connectors/{connector_id}/consent/admin */ export const getApiAuthConnectorsConnectorIdConsentAdmin = async (args: GetApiAuthConnectorsConnectorIdConsentAdminArgs, options?: GetApiAuthConnectorsConnectorIdConsentAdminOptions) => { const {baseUrl = "https://api.your-domain.com", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.tenant_id != null) query.append("tenant_id", String(args.tenant_id)) if (args.cookie_name != null) query.append("cookie_name", String(args.cookie_name)) if (APIKeyHeader !== undefined) headers.set("X-API-KEY", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/auth/connectors/${encodeURIComponent(String(args.connector_id))}/consent/admin${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(connectorConsentOut, await res.json()) };