mock-adapter.ts 1.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
import mockAdapter1 from 'axios-mock-adapter';
import axios from 'axios';


/**
 * Http net 对象
 * 调用 getInstance() 获取实例
 *
 * @class Http
 */
export class MockAdapter {

    /**
     * Creates an instance of MockAdapter.
     * 
     * @memberof MockAdapter
     */
    private constructor() {
    }

    /**
     * 获取 Http 单例对象
     *
     * @static
     * @returns {Http}
     * @memberof Http
     */
    public static getInstance(): mockAdapter1 {
        if (!MockAdapter.mockAdapter) {
            MockAdapter.mockAdapter = new mockAdapter1(axios,{delayResponse:500});
        }
        return this.mockAdapter;
    }

    /**
     * 模拟请求结果状态
     *
     * @static
     * @param {*} config
     * @returns {number}
     * @memberof MockAdapter
     */
    public static  mockStatus(config: any): number {
        let status = 200;
        const { headers: _headers } = config;
        // status = _headers.Authorization && !Object.is(_headers.Authorization, '') ? 200 : 401;
        return status;
    }

    /**
     * 单例变量声明
     *
     * @private
     * @static
     * @type {Http}
     * @memberof Http
     */
    private static mockAdapter: mockAdapter1;
}