import { parse } from "url" ; import { assert , PromiseCache } from "@fluidframework/common-utils" ; import { IRequest , } from "@fluidframework/core-interfaces" ; import { IFluidResolvedUrl , IResolvedUrl , IUrlResolver , } from "@fluidframework/driver-definitions" ; import { default as Axios , AxiosInstance } from "axios" ; export class ContainerUrlResolver implements IUrlResolver { private readonly cache = new PromiseCache < string , IResolvedUrl > ( ) ; constructor ( private readonly baseUrl , private readonly jwt , cache ? , private readonly axios = Axios ) { if ( cache !== undefined ) { for ( const [ key , value ] of cache ) { this . cache . addValue ( key , value ) ; } } } public async resolve ( request ) { const fetchResolvedUrl = async ( ) => { const headers = { Authorization : `Bearer ${ this . jwt }` , } ; const resolvedUrl = await this . axios . post < IResolvedUrl > ( `${ this . baseUrl }/api/v1/load` , { url : request . url , } , { headers , } ) ; return resolvedUrl . data ; } ; return this . cache . addOrGet ( request . url , fetchResolvedUrl ) ; } public async getAbsoluteUrl ( resolvedUrl , relativeUrl ) { const fluidResolvedUrl = resolvedUrl as IFluidResolvedUrl ; const parsedUrl = parse ( fluidResolvedUrl . url ) ; assert ( parsedUrl . pathname !== null , 0x0b7 ) ; const [ , tenantId , documentId ] = parsedUrl . pathname . split ( "/" ) ; assert ( ! ! tenantId && ! ! documentId , 0x0b8 ) ; let url = relativeUrl ; if ( url . startsWith ( "/" ) ) { url = url . substr ( 1 ) ; } return `${ this . baseUrl }/${ encodeURIComponent ( tenantId ) }/${ encodeURIComponent ( documentId ) }${ url ? `/${ url }` : `` }` ; } }