import { ModelProxy } from "modelproxy" ; import { FetchEngine } from "modelproxy-engine-fetch" ; import { IProxyCtx } from "modelproxy/out/models/proxyctx" ; import { InterfaceFactory } from "modelproxy/out/libs/interface.factory" ; import sfSchemaConfig from "./common.json" ; import { IInterfaceModel } from "modelproxy/out/models/interface" ; /**
 * 接口的配置以及初始化
 * 这里有fetch和jsonp的接口转发器
 */ let proxy = new ModelProxy ( ) ; let fetchEngine = new FetchEngine ( ) ; let fetchSchemaEngine = new FetchEngine ( ) ; /**
 * fetch接口引擎初始化
 */ fetchEngine . init ( ) ; fetchEngine . use ( async ( ctx , next ) => { if ( ! ctx . result . ok || ctx . result . status !== 200 ) { throw new Error ( ctx . result . statusText ) ; } ctx . result = await ctx . result . json ( ) ; await next ( ) ; } ) ; fetchEngine . use ( async ( ctx , next ) => { if ( ctx . result . code !== undefined && ctx . result . code !== 200 ) { throw new Error ( ctx . result . message ) ; } ctx . result = ctx . result . data ; await next ( ) ; } ) ; fetchSchemaEngine . init ( ) ; fetchSchemaEngine . use ( async ( ctx , next ) => { if ( ! ctx . result . ok || ctx . result . status !== 200 ) { throw new Error ( ctx . result . statusText ) ; } ctx . result = await ctx . result . json ( ) ; await next ( ) ; } ) ; let state = "dev" ; proxy . loadConfig ( sfSchemaConfig , { engine : "json" , state } ) ; proxy . addEngines ( { "fetch" : fetchEngine , "json" : fetchSchemaEngine } ) ; export const schemaNs = proxy . getNs ( "sf" ) as InterfaceFactory ; export const getSchema = schemaNs . get ( "schema" ) as IInterfaceModel ; export default proxy ;