request-pre
Advanced tools
Comparing version 0.0.1 to 0.1.0
@@ -17,8 +17,10 @@ "use strict"; | ||
var mockInfo = mockStatus && mock && mock[mockStatus]; | ||
if (process.env.NODE_ENV === 'development' && mockInfo) { | ||
console.log(requestInfo); | ||
return new Promise(function (res, rej) { | ||
var action = mockInfo.success ? res : rej; | ||
action(mockInfo.data); | ||
}); | ||
if (process.env.NODE_ENV === 'development') { | ||
if (mockInfo) { | ||
console.log(requestInfo); | ||
return new Promise(function (res, rej) { | ||
var action = mockInfo.success ? res : rej; | ||
action(typeof mockInfo.data === 'function' ? mockInfo.data(config) : mockInfo.data); | ||
}); | ||
} | ||
} | ||
@@ -25,0 +27,0 @@ return target.requester(requestInfo); |
@@ -11,3 +11,4 @@ "use strict"; | ||
methods["DELETE"] = "DELETE"; | ||
methods["OPTIONS"] = "OPTIONS"; | ||
})(methods || (methods = {})); | ||
exports.default = methods; |
@@ -21,3 +21,2 @@ "use strict"; | ||
serviceConfig = serviceConfig || {}; | ||
var requestObjURL = requestObj.url = requestObj.url || {}; | ||
var apiSchemaURLInfo = __assign({}, apiSchemaURL); | ||
@@ -33,4 +32,4 @@ var apiSchemaInfo = { | ||
} | ||
if (requestObjURL.path) { | ||
apiSchemaURLInfo.path = utils_1.resolvePath(apiSchemaURL.path, requestObjURL.path); | ||
if (requestObj.path) { | ||
apiSchemaURLInfo.path = utils_1.resolvePath(apiSchemaURL.path, requestObj.path); | ||
} | ||
@@ -40,10 +39,15 @@ if (serviceConfig.prefix) { | ||
} | ||
if ((apiSchemaURL.body || requestObjURL.body) && bodyMethods.includes(apiSchemaURLInfo.method)) { | ||
apiSchemaURLInfo.body = __assign({}, apiSchemaURL.body, requestObjURL.body); | ||
if ((apiSchemaURL.body || requestObj.body)) { | ||
apiSchemaURLInfo.body = __assign({}, apiSchemaURL.body, requestObj.body); | ||
if (process.env.NODE_ENV === 'development') { | ||
if (!bodyMethods.includes(apiSchemaURLInfo.method)) { | ||
console.warn('HTTP methods like post,patch,put require a body.'); | ||
} | ||
} | ||
} | ||
if (apiSchemaURL.query || requestObjURL.query) { | ||
apiSchemaURLInfo.query = __assign({}, apiSchemaURL.query, requestObjURL.query); | ||
if (apiSchemaURL.query || requestObj.query) { | ||
apiSchemaURLInfo.query = __assign({}, apiSchemaURL.query, requestObj.query); | ||
} | ||
if (apiSchemaURL.headers || requestObjURL.headers || serviceConfig.headers) { | ||
apiSchemaURLInfo.headers = __assign({}, serviceConfig.headers, apiSchemaURL.headers, requestObjURL.headers); | ||
if (apiSchemaURL.headers || requestObj.headers || serviceConfig.headers) { | ||
apiSchemaURLInfo.headers = __assign({}, serviceConfig.headers, apiSchemaURL.headers, requestObj.headers); | ||
} | ||
@@ -50,0 +54,0 @@ return apiSchemaInfo; |
@@ -36,4 +36,3 @@ import methods from './methods'; | ||
} | ||
export interface ApiSchemaData { | ||
url?: UrlSchemaData; | ||
export interface ApiSchemaData extends UrlSchemaData { | ||
config?: ApiSchemaConfig; | ||
@@ -40,0 +39,0 @@ mock?: ApiSchemaMock; |
@@ -7,4 +7,5 @@ declare enum methods { | ||
PATCH = "PATCH", | ||
DELETE = "DELETE" | ||
DELETE = "DELETE", | ||
OPTIONS = "OPTIONS" | ||
} | ||
export default methods; |
const service = require('./services'); | ||
service.list({ | ||
url: { | ||
path: { | ||
id: 2, | ||
}, | ||
query: { | ||
status: 4, | ||
}, | ||
body: { | ||
xx: 2, | ||
}, | ||
headers: { | ||
'x-option': 'custom', | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}, | ||
path: { | ||
id: 2, | ||
}, | ||
query: { | ||
status: 4, | ||
}, | ||
body: { | ||
xx: 2, | ||
}, | ||
headers: { | ||
'x-option': 'custom', | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}, | ||
config: { | ||
@@ -19,0 +17,0 @@ mock: 'list.success', |
@@ -7,3 +7,4 @@ module.exports = { | ||
testRegex: '/tests/.*\\.test\\.ts$', | ||
setupFilesAfterEnv: ["<rootDir>/tests/setup.js"], | ||
coverageReporters: ['html'], | ||
}; |
{ | ||
"name": "request-pre", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "process format data before request send", | ||
@@ -44,2 +44,3 @@ "scripts": { | ||
"npm run test", | ||
"npm run lib", | ||
"git add" | ||
@@ -46,0 +47,0 @@ ] |
@@ -94,3 +94,3 @@ # request-pre | ||
success: true, | ||
data: [], | ||
data: [], // or () => [], | ||
}, | ||
@@ -106,17 +106,15 @@ }, | ||
services.list({ | ||
url: { | ||
path: { | ||
id: 2, | ||
}, | ||
query: { | ||
status: 4, | ||
}, | ||
body: { | ||
xx: 2, | ||
}, | ||
headers: { | ||
'x-option': 'custom', | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}, | ||
path: { | ||
id: 2, | ||
}, | ||
query: { | ||
status: 4, | ||
}, | ||
body: { | ||
xx: 2, | ||
}, | ||
headers: { | ||
'x-option': 'custom', | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}, | ||
config: { | ||
@@ -123,0 +121,0 @@ mock: 'list.success', |
@@ -36,4 +36,3 @@ import methods from './methods'; | ||
} | ||
export interface ApiSchemaData { | ||
url?: UrlSchemaData; | ||
export interface ApiSchemaData extends UrlSchemaData{ | ||
config?: ApiSchemaConfig; | ||
@@ -40,0 +39,0 @@ mock?: ApiSchemaMock; |
@@ -28,8 +28,10 @@ import { ApiSchemaList, ApiSchemaData, ServiceConfig } from './api'; | ||
const mockInfo = mockStatus && mock && mock[mockStatus]; | ||
if (process.env.NODE_ENV === 'development' && mockInfo) { | ||
console.log(requestInfo); | ||
return new Promise((res, rej): void => { | ||
const action = mockInfo.success ? res : rej; | ||
action(mockInfo.data); | ||
}); | ||
if (process.env.NODE_ENV === 'development') { | ||
if (mockInfo) { | ||
console.log(requestInfo); | ||
return new Promise((res, rej): void => { | ||
const action = mockInfo.success ? res : rej; | ||
action(typeof mockInfo.data === 'function' ? mockInfo.data(config) : mockInfo.data); | ||
}); | ||
} | ||
} | ||
@@ -36,0 +38,0 @@ return target.requester(requestInfo); |
@@ -7,4 +7,5 @@ enum methods { | ||
PATCH = 'PATCH', | ||
DELETE = 'DELETE' | ||
DELETE = 'DELETE', | ||
OPTIONS = 'OPTIONS' | ||
} | ||
export default methods; |
@@ -9,3 +9,2 @@ import { ApiSchema, ApiSchemaData, ServiceConfig } from '../api'; | ||
serviceConfig = serviceConfig || {}; | ||
const requestObjURL: ApiSchemaData["url"] = requestObj.url = requestObj.url || {}; | ||
const apiSchemaURLInfo = { | ||
@@ -27,4 +26,4 @@ ...apiSchemaURL, | ||
} | ||
if (requestObjURL.path) { | ||
apiSchemaURLInfo.path = resolvePath(apiSchemaURL.path, requestObjURL.path); | ||
if (requestObj.path) { | ||
apiSchemaURLInfo.path = resolvePath(apiSchemaURL.path, requestObj.path); | ||
} | ||
@@ -34,19 +33,24 @@ if (serviceConfig.prefix) { | ||
} | ||
if ((apiSchemaURL.body || requestObjURL.body) && bodyMethods.includes(apiSchemaURLInfo.method)) { | ||
if ((apiSchemaURL.body || requestObj.body)) { | ||
apiSchemaURLInfo.body = { | ||
...apiSchemaURL.body, | ||
...requestObjURL.body, | ||
...requestObj.body, | ||
}; | ||
if (process.env.NODE_ENV === 'development') { | ||
if (!bodyMethods.includes(apiSchemaURLInfo.method)) { | ||
console.warn('HTTP methods like post,patch,put require a body.'); | ||
} | ||
} | ||
} | ||
if (apiSchemaURL.query || requestObjURL.query) { | ||
if (apiSchemaURL.query || requestObj.query) { | ||
apiSchemaURLInfo.query = { | ||
...apiSchemaURL.query, | ||
...requestObjURL.query, | ||
...requestObj.query, | ||
}; | ||
} | ||
if (apiSchemaURL.headers || requestObjURL.headers || serviceConfig.headers) { | ||
if (apiSchemaURL.headers || requestObj.headers || serviceConfig.headers) { | ||
apiSchemaURLInfo.headers = { | ||
...serviceConfig.headers, | ||
...apiSchemaURL.headers, | ||
...requestObjURL.headers, | ||
...requestObj.headers, | ||
}; | ||
@@ -53,0 +57,0 @@ } |
@@ -16,3 +16,3 @@ import Service from '../src/index'; | ||
success: true, | ||
data: [], | ||
data: () => [], | ||
}, | ||
@@ -61,3 +61,3 @@ 'list.fail': { | ||
}).then((data) => { | ||
expect(data).toBe(apiSchemaList.list.mock["list.success"].data); | ||
expect(data).toStrictEqual(apiSchemaList.list.mock["list.success"].data()); | ||
}); | ||
@@ -76,3 +76,3 @@ service.list({ | ||
}).then((data) => { | ||
expect(data).toBe(apiSchemaList.list.mock["list.success"].data); | ||
expect(data).toStrictEqual(apiSchemaList.list.mock["list.success"].data()); | ||
}); | ||
@@ -142,3 +142,3 @@ }); | ||
}).then((data) => { | ||
expect(data).toBe(apiSchemaList.list.mock["list.success"].data); | ||
expect(data).toStrictEqual(apiSchemaList.list.mock["list.success"].data()); | ||
}); | ||
@@ -157,4 +157,4 @@ service2.list({ | ||
}).then((data) => { | ||
expect(data).toBe(apiSchemaList.list.mock["list.success"].data); | ||
expect(data).toStrictEqual(apiSchemaList.list.mock["list.success"].data()); | ||
}); | ||
}); |
@@ -50,10 +50,8 @@ import mixins from '../../src/util/mixins'; | ||
requestObj: { | ||
url: { | ||
path: { | ||
id: 1, | ||
}, | ||
query: { | ||
b: 2, | ||
}, | ||
path: { | ||
id: 1, | ||
}, | ||
query: { | ||
b: 2, | ||
}, | ||
config: { | ||
@@ -107,6 +105,4 @@ mock: 'listSuccess', | ||
requestObj: { | ||
url: { | ||
path: { | ||
id: 1, | ||
}, | ||
path: { | ||
id: 1, | ||
}, | ||
@@ -147,6 +143,4 @@ }, | ||
requestObj: { | ||
url: { | ||
path: { | ||
id: 1, | ||
}, | ||
path: { | ||
id: 1, | ||
}, | ||
@@ -187,10 +181,8 @@ }, | ||
requestObj: { | ||
url: { | ||
path: { | ||
id: 1, | ||
}, | ||
body: { | ||
username: 'test', | ||
}, | ||
path: { | ||
id: 1, | ||
}, | ||
body: { | ||
username: 'test', | ||
}, | ||
}, | ||
@@ -197,0 +189,0 @@ }, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37712
32
963
154
3