import {z} from 'zod' export type OAuthLinkCreateRequest = {access_control?: string | undefined, expires_in_days?: (number | null) | undefined, mcp_instance_id: string, provider_config?: {[key: string]: unknown} | undefined}; export type CreateApiV1WorkspacesWorkspaceMcpOauthLinksArgs = {workspace: string, body: OAuthLinkCreateRequest}; export type CreateApiV1WorkspacesWorkspaceMcpOauthLinksOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const oAuthLinkResponse = z.object({access_control: z.string(), access_count: z.number().int(), created_at: z.string(), expires_at: z.string().nullable(), id: z.string(), is_active: z.boolean(), last_accessed_at: z.string().nullable(), mcp_instance_id: z.string(), token: z.string()}); 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 }; /** * Create Oauth Link * * POST /v1/workspaces/{workspace}/mcp-oauth-links/ */ export const createApiV1WorkspacesWorkspaceMcpOauthLinks = async (args: CreateApiV1WorkspacesWorkspaceMcpOauthLinksArgs, options: CreateApiV1WorkspacesWorkspaceMcpOauthLinksOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workspaces/${encodeURIComponent(String(args.workspace))}/mcp-oauth-links/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(oAuthLinkResponse, await res.json()) };