mirror of
https://github.com/langgenius/dify.git
synced 2026-07-16 01:48:40 +08:00
Co-authored-by: yunlu.wen <yunlu.wen@dify.ai> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Yunlu Wen <wylswz@163.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Yanli 盐粒 <yanli@dify.ai> Co-authored-by: 盐粒 Yanli <beautyyuyanli@gmail.com> Co-authored-by: zyssyz123 <916125788@qq.com> Co-authored-by: 盐粒 Yanli <mail@yanli.one>
30 lines
1.0 KiB
TypeScript
30 lines
1.0 KiB
TypeScript
import type { ClientContext, ClientLink } from '@orpc/client'
|
|
import type { AnyContractRouter } from '@orpc/contract'
|
|
import { DynamicLink } from '@orpc/client'
|
|
import { loadConsoleContractForSegment } from './console-router-loader'
|
|
|
|
export function createConsoleDynamicLink<TContext extends ClientContext>(
|
|
createLink: (contract: AnyContractRouter) => ClientLink<TContext>,
|
|
) {
|
|
const routerLinkPromises = new Map<string, Promise<ClientLink<TContext>>>()
|
|
|
|
function getRouterLink(path: readonly string[]) {
|
|
const segment = path[0]
|
|
if (!segment)
|
|
throw new Error('Console contract path is empty.')
|
|
|
|
let routerLinkPromise = routerLinkPromises.get(segment)
|
|
if (!routerLinkPromise) {
|
|
routerLinkPromise = loadConsoleContractForSegment(segment).then(createLink).catch((error) => {
|
|
routerLinkPromises.delete(segment)
|
|
throw error
|
|
})
|
|
routerLinkPromises.set(segment, routerLinkPromise)
|
|
}
|
|
|
|
return routerLinkPromise
|
|
}
|
|
|
|
return new DynamicLink<TContext>((_options, path) => getRouterLink(path))
|
|
}
|