import { GitManager , Historian , ICredentials , BasicRestWrapper , getAuthorizationTokenFromCredentials , } from "@fluidframework/server-services-client" ; import { generateToken , getCorrelationId } from "@fluidframework/server-services-utils" ; import * as core from "@fluidframework/server-services-core" ; import Axios from "axios" ; import { fromUtf8ToBase64 } from "@fluidframework/common-utils" ; export class Tenant implements core . ITenant { public get id ( ) : string { return this . config . id ; } public get gitManager ( ) : GitManager { return this . manager ; } public get storage ( ) : core . ITenantStorage { return this . config . storage ; } public get orderer ( ) : core . ITenantOrderer { return this . config . orderer ; } constructor ( private readonly config , private readonly manager ) { } } /**
 * Manages a collection of tenants
 */ export class TenantManager implements core . ITenantManager { constructor ( private readonly endpoint ) { } public async createTenant ( tenantId ? ) { const result = await Axios . post < core . ITenantConfig & { key : string ; } > ( `${ this . endpoint }/api/tenants/${ encodeURIComponent ( tenantId || "" ) }` ) ; return result . data ; } public async getTenant ( tenantId , documentId , includeDisabledTenant = false ) { const [ details , key ] = await Promise . all ( [ Axios . get < core . ITenantConfig > ( `${ this . endpoint }/api/tenants/${ tenantId }` , { params : { includeDisabledTenant } } ) , this . getKey ( tenantId , includeDisabledTenant ) ] ) ; const defaultQueryString = { token : fromUtf8ToBase64 ( `${ tenantId }` ) } ; const getDefaultHeaders = ( ) => { const credentials = { password : generateToken ( tenantId , documentId , key , null ) , user : tenantId } ; return ( { Authorization : getAuthorizationTokenFromCredentials ( credentials ) } ) ; } ; const defaultHeaders = getDefaultHeaders ( ) ; const baseUrl = `${ details . data . storage . internalHistorianUrl }/repos/${ encodeURIComponent ( tenantId ) }` ; const restWrapper = new BasicRestWrapper ( baseUrl , defaultQueryString , undefined , undefined , defaultHeaders , undefined , undefined , getDefaultHeaders , getCorrelationId ) ; const historian = new Historian ( `${ details . data . storage . internalHistorianUrl }/repos/${ encodeURIComponent ( tenantId ) }` , true , false , restWrapper ) ; const gitManager = new GitManager ( historian ) ; const tenant = new Tenant ( details . data , gitManager ) ; return tenant ; } public async verifyToken ( tenantId , token ) { await Axios . post ( `${ this . endpoint }/api/tenants/${ encodeURIComponent ( tenantId ) }/validate` , { token } ) ; } public async getKey ( tenantId , includeDisabledTenant = false ) { const result = await Axios . get ( `${ this . endpoint }/api/tenants/${ encodeURIComponent ( tenantId ) }/key` , { params : { includeDisabledTenant } } ) ; return result . data ; } }