import { Server } from './server' ; export async function getDatabaseStructure ( connectionUrl ) { const request = { method : 'POST' , body : JSON . stringify ( { connectionUrl } ) } ; const response = await Server . makeRequest ( '/jupyterlab-sql/database' , request ) ; if ( ! response . ok ) { return Private . createErrorResponse ( response . status ) ; } const data = await response . json ( ) ; return data ; } export interface DatabaseObjects { tables : Array < string > ; views : Array < string > ; } export namespace DatabaseStructureResponse { export type Type = ErrorResponse | SuccessResponse ; interface ErrorResponse { responseType : 'error' ; responseData : ErrorResponseData ; } interface SuccessResponse { responseType : 'success' ; responseData : SuccessResponseData ; } type SuccessResponseData = { tables : Array < string > ; views ? : Array < string > ; } ; type ErrorResponseData = { message : string ; } ; export function createError ( message ) { return { responseType : 'error' , responseData : { message } } ; } export function createNotFoundError ( ) { const errorMessage = 'Failed to reach server endpoints. ' + 'Is the server extension installed correctly?' ; return createError ( errorMessage ) ; } export function match < U > ( response , onSuccess , onError ) { if ( response . responseType === 'error' ) { return onError ( response . responseData ) ; } else if ( response . responseType === 'success' ) { const { responseData } = response ; const tables = responseData . tables ; const views = responseData . views || [ ] ; const databaseObjects = { tables , views } ; return onSuccess ( databaseObjects ) ; } } } namespace Private { export function createErrorResponse ( responseStatus ) { if ( responseStatus === 404 ) { return DatabaseStructureResponse . createNotFoundError ( ) ; } else { const errorMessage = 'Unexpected response status from server' ; return DatabaseStructureResponse . createError ( errorMessage ) ; } } }