Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

1.6 KB
Raw Blame
import type { ClientLink } from '@orpc/client'
import type { AnyContractRouter } from '@orpc/contract'
import type { ConsoleClientContext } from './index'
import { onError } from '@orpc/client'
import { OpenAPILink } from '@orpc/openapi-client/fetch'
import { API_PREFIX } from '@/config'
// oxlint-disable-next-line no-restricted-imports -- The Console HTTP adapter must preserve existing auth refresh, CSRF, and error handling.
import { request } from '../base'
import { reportClientError } from '../client-error'
import { createConsoleContractLink } from './contract-loader'
import { validateDefinedErrors } from './defined-errors'
import { normalizeConsoleOpenAPIURL } from './openapi-url'

function createBrowserLink(contract: AnyContractRouter): ClientLink<ConsoleClientContext> {
  return new OpenAPILink<ConsoleClientContext>(contract, {
    url: () => new URL(API_PREFIX, window.location.origin),
    fetch: async (input, init, options) => {
      const requestInit = options.context.keepalive ? { ...init, keepalive: true } : init
      const normalizedURL = normalizeConsoleOpenAPIURL(input.url)
      const normalizedRequest =
        normalizedURL === input.url ? input : new Request(normalizedURL, input)
      const response = await request<Response>(normalizedURL, requestInit, {
        fetchCompat: true,
        request: normalizedRequest,
        silent: options.context.silent,
      })
      options.context.onResponseHeaders?.(response.headers)
      return response
    },
    interceptors: [validateDefinedErrors(contract), onError((error) => reportClientError(error))],
  })
}

export const consoleBrowserLink = createConsoleContractLink(createBrowserLink)