@@ -9,18 +9,18 @@ var Mock = _interopDefault(require('mockjs'));
9
9
var fs = require ( 'fs' ) ;
10
10
11
11
/*! *****************************************************************************
12
- Copyright (c) Microsoft Corporation. All rights reserved.
13
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use
14
- this file except in compliance with the License. You may obtain a copy of the
15
- License at http://www.apache.org/licenses/LICENSE-2.0
12
+ Copyright (c) Microsoft Corporation.
16
13
17
- THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18
- KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
19
- WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
20
- MERCHANTABLITY OR NON-INFRINGEMENT.
14
+ Permission to use, copy, modify, and/or distribute this software for any
15
+ purpose with or without fee is hereby granted.
21
16
22
- See the Apache Version 2.0 License for specific language governing permissions
23
- and limitations under the License.
17
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
18
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
19
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
20
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
21
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
22
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23
+ PERFORMANCE OF THIS SOFTWARE.
24
24
***************************************************************************** */
25
25
/* global Reflect, Promise */
26
26
@@ -170,6 +170,12 @@ var Http = /** @class */ (function () {
170
170
} ;
171
171
return Http ;
172
172
} ( ) ) ;
173
+ var noop = function ( func ) {
174
+ if ( typeof func === 'function' ) {
175
+ return func ;
176
+ }
177
+ return function ( ) { } ;
178
+ } ;
173
179
var getInfo = function ( req , option , headers ) {
174
180
var url = req . _parsedUrl [ / g e t / i. test ( req . method ) ? 'pathname' : 'href' ] . replace ( / ^ \/ / , '' ) ;
175
181
return {
@@ -335,15 +341,20 @@ var methodHandler = function (_a, next) {
335
341
var server$1 = jsonServer . create ( ) ;
336
342
// 路径从根目录开始?
337
343
var router = jsonServer . router ( path . resolve ( process . cwd ( ) , 'db.json' ) ) ;
338
- var middlewares = jsonServer . defaults ( {
339
- static : path . resolve ( __dirname , './public' )
340
- } ) ;
341
- server$1 . use ( middlewares ) ;
342
344
var createServer = function ( option , callback ) {
343
345
var config = option . https ;
344
346
config = / ^ ( b o o l e a n | n u m b e r ) $ / . test ( typeof config ) ? config && { } : config ;
345
347
var currentServer ;
346
348
if ( config instanceof Object ) {
349
+ if ( typeof config . static === 'function' ) {
350
+ config . static ( jsonServer , server$1 ) ;
351
+ }
352
+ else {
353
+ var middlewares = jsonServer . defaults ( {
354
+ static : typeof config . static === 'string' ? config . static : path . resolve ( process . cwd ( ) , './public' )
355
+ } ) ;
356
+ server$1 . use ( middlewares ) ;
357
+ }
347
358
if ( typeof config . key !== 'string' || typeof config . cert !== 'string' || config . key . length + config . cert . length === 0 ) {
348
359
config . key = fs . readFileSync ( path . join ( __dirname , 'ssl/key.pem' ) ) ;
349
360
config . cert = fs . readFileSync ( path . join ( __dirname , 'ssl/cert.pem' ) ) ;
@@ -397,6 +408,9 @@ var Server$1 = function (option, callback) {
397
408
// 路由映射
398
409
server$1 . use ( jsonServer . rewriter ( option . rules instanceof Object ? option . rules : rules ) ) ;
399
410
server$1 . use ( function ( req , res , next ) {
411
+ if ( noop ( option . interceptor ) ( req , res , next ) === false ) {
412
+ return ;
413
+ }
400
414
var http = new Http ( res ) ;
401
415
var _a = getInfo ( req , option , config$1 . crossDomain ) , url = _a . url , data = _a . data , method = _a . method , urlKey = _a . urlKey , params = _a . params , headConfig = _a . headConfig ;
402
416
var result = { } ;
0 commit comments