import { EntityManager } from "typeorm" ; import "reflect-metadata" ; import { MetadataArgsStorage } from "./metadata-args/MetadataArgsStorage" ; import { VesperFramework } from "./VesperFramework" ; import { VesperFrameworkOptions } from "./options/VesperFrameworkOptions" ; import { NextFunction , Request , Response } from "express" ; import { SchemaBuilder } from "./SchemaBuilder" ; import { SchemaBuilderOptions } from "./options/SchemaBuilderOptions" ; import { Container } from "typedi" ; import { HttpQueryError , runHttpQuery } from "apollo-server-core" ; import { GraphQLSchema } from "graphql" ; import { CurrentRequest } from "./token/CurrentRequest" ; import { CurrentResponse } from "./token/CurrentResponse" ; declare global { interface RequestInit { } } export * from "./decorator/ArgsValidator" ; export * from "./decorator/Authorized" ; export * from "./decorator/Controller" ; export * from "./decorator/Mutation" ; export * from "./decorator/Query" ; export * from "./decorator/Resolve" ; export * from "./decorator/Resolver" ; export * from "./decorator/Subscription" ; export * from "./interface/Action" ; export * from "./interface/ArgsValidatorInterface" ; export * from "./interface/ResolverInterface" ; export * from "./interface/RoleCheckerInterface" ; export * from "./options/GraphModule" ; export * from "./options/GraphModuleControllerAction" ; export * from "./options/GraphModuleResolver" ; export * from "./options/GraphModuleResolverMethod" ; export * from "./options/VesperFrameworkOptions" ; export * from "./options/SchemaBuilderOptions" ; export * from "./token/CurrentResponse" ; export * from "./token/CurrentRequest" ; export * from "./util/ResolverUtils" ; export * from "./VesperFramework" ; /**
 * Gets metadata args storage.
 * Metadata args storage follows the best practices and stores metadata in a global variable.
 */ export function getMetadataArgsStorage ( ) { if ( ! ( global as any ) . vesperMetadataArgsStorage ) ( global as any ) . vesperMetadataArgsStorage = new MetadataArgsStorage ( ) ; return ( global as any ) . vesperMetadataArgsStorage ; } /**
 * Bootstraps framework the easiest way.
 */ export function bootstrap ( options ? ) { const framework = new VesperFramework ( options ) ; return framework . start ( ) . then ( ( ) => framework ) ; } /**
 * Builds GraphQLSchema based on provided options.
 */ export function buildVesperSchema ( options ? ) { const middleware = new SchemaBuilder ( options ) ; return middleware . build ( ) ; } /**
 * Vesper Express middleware.
 * You can use it in your own express setup.
 */ export function vesper ( schema , options ? ) { const allOptions = { context : { } , schema : schema , ... ( options || { } ) } ; return ( req , res , next ) => { const container = Container . of ( req ) ; container . set ( CurrentRequest , req ) ; container . set ( CurrentResponse , res ) ; allOptions . context . container = container ; allOptions . context . dataLoaders = { } ; return runHttpQuery ( [ req , res ] , { method : req . method , options : allOptions , query : req . method === "POST" ? req . body : req . query , } ) . then ( ( gqlResponse ) => { const transactionEntityManager = container . has ( EntityManager ) ? container . get ( EntityManager ) : undefined ; if ( transactionEntityManager && transactionEntityManager . connection . options . type !== "mongodb" && transactionEntityManager . queryRunner && transactionEntityManager . queryRunner . isTransactionActive && transactionEntityManager . queryRunner . isReleased === false ) { return transactionEntityManager . queryRunner . commitTransaction ( ) . then ( ( ) => transactionEntityManager . queryRunner . release ( ) ) . then ( ( ) => gqlResponse ) ; } return gqlResponse ; } ) . then ( ( gqlResponse ) => { res . setHeader ( "Content-Type" , "application/json; charset=utf-8" ) ; res . setHeader ( "Content-Length" , String ( Buffer . byteLength ( gqlResponse , "utf8" ) ) ) ; res . write ( gqlResponse ) ; res . end ( ) ; Container . reset ( req ) ; } ) . catch ( ( error ) => { const transactionEntityManager = container . has ( EntityManager ) ? container . get ( EntityManager ) : undefined ; if ( transactionEntityManager && transactionEntityManager . connection . options . type !== "mongodb" && transactionEntityManager . queryRunner && transactionEntityManager . queryRunner . isTransactionActive && transactionEntityManager . queryRunner . isReleased === false ) { return transactionEntityManager . queryRunner . rollbackTransaction ( ) . then ( ( ) => transactionEntityManager . queryRunner . release ( ) ) . then ( ( ) => { throw error ; } ) ; } throw error ; } ) . catch ( ( error ) => { if ( "HttpQueryError" !== error . name ) return next ( error ) ; if ( error . headers ) { Object . keys ( error . headers ) . forEach ( header => { res . setHeader ( header , error . headers [ header ] ) ; } ) ; } res . statusCode = error . statusCode ; res . write ( error . message ) ; res . end ( ) ; Container . reset ( req ) ; } ) ; } ; }