request-pre
Advanced tools
Comparing version 0.1.1 to 0.2.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var mixins_1 = require("./util/mixins"); | ||
var mixin_1 = require("./util/mixin"); | ||
var mock_1 = require("./util/mock"); | ||
var compose_1 = require("./util/compose"); | ||
var requestType_1 = require("./middleware/requestType"); | ||
var Service = /** @class */ (function () { | ||
@@ -9,20 +12,24 @@ function Service(apiSchemaList, serviceConfig, requester) { | ||
this.requester = requester; | ||
this.middlewareList = [ | ||
requestType_1.default, | ||
]; | ||
return this.initService(); | ||
} | ||
Service.prototype.createRequest = function (apiName, target) { | ||
Service.prototype.use = function (middleware) { | ||
this.middlewareList.push(middleware); | ||
return this; | ||
}; | ||
Service.prototype.createRequest = function (apiName, target, middlewareWrap) { | ||
return function (requestObj) { | ||
var requestInfo = mixins_1.default(target.serviceConfig, target.apiSchemaList[apiName], requestObj); | ||
var config = requestInfo.config, mock = requestInfo.mock; | ||
var mockStatus = config && config.mock; | ||
var mockInfo = mockStatus && mock && mock[mockStatus]; | ||
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); | ||
}); | ||
var requestInfo = mixin_1.default(target.serviceConfig, target.apiSchemaList[apiName], requestObj); | ||
return middlewareWrap(requestInfo, function (ctx) { | ||
var request = null; | ||
if (process.env.NODE_ENV === 'development') { | ||
request = mock_1.default(ctx); | ||
} | ||
} | ||
return target.requester(requestInfo); | ||
if (!request) { | ||
request = target.requester(ctx); | ||
} | ||
return request; | ||
}); | ||
}; | ||
@@ -33,5 +40,6 @@ }; | ||
var apiSchemaList = this.apiSchemaList; | ||
var middlewareWrap = compose_1.default(this.middlewareList); | ||
if (typeof Proxy === 'undefined') { | ||
Object.keys(apiSchemaList).forEach(function (apiName) { | ||
_this[apiName] = _this['$' + apiName] = _this.createRequest(apiName, _this); | ||
_this[apiName] = _this['$' + apiName] = _this.createRequest(apiName, _this, middlewareWrap); | ||
}); | ||
@@ -50,3 +58,3 @@ return this; | ||
if (propertyKey in target.apiSchemaList) { | ||
return target.createRequest(propertyKey, target); | ||
return target.createRequest(propertyKey, target, middlewareWrap); | ||
} | ||
@@ -53,0 +61,0 @@ }, |
import methods from './methods'; | ||
interface DefaultData { | ||
[key: string]: unknown; | ||
[key: string]: any; | ||
} | ||
interface ApiSchemaConfig extends DefaultData { | ||
mock?: string; | ||
requestType?: string; | ||
cache?: { | ||
key?: string; | ||
max?: number; | ||
expire?: number; | ||
}; | ||
cancel?: boolean; | ||
timeout?: number; | ||
} | ||
@@ -11,3 +19,3 @@ interface ApiSchemaMock { | ||
success: boolean; | ||
data: unknown; | ||
data: any; | ||
}; | ||
@@ -14,0 +22,0 @@ } |
import { ApiSchemaList, ServiceConfig } from './api'; | ||
declare type requestReturn = Promise<unknown>; | ||
declare type requester = ({ config: ApiSchemaConfig, mock: ApiSchemaMock, url: UrlSchema, }: { | ||
config: any; | ||
mock: any; | ||
url: any; | ||
}) => requestReturn; | ||
import { requester, middleware } from './util/request'; | ||
declare class Service { | ||
@@ -14,2 +9,4 @@ constructor(apiSchemaList: ApiSchemaList, serviceConfig: ServiceConfig, requester: requester); | ||
private serviceConfig; | ||
middlewareList: middleware[]; | ||
use(middleware: middleware): Service; | ||
private createRequest; | ||
@@ -16,0 +13,0 @@ private initService; |
{ | ||
"name": "request-pre", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "process format data before request send", | ||
@@ -11,3 +11,4 @@ "scripts": { | ||
"lib": "tsc", | ||
"dev": "tsc -w" | ||
"dev": "tsc -w", | ||
"prepublishOnly": "npm run lib && git add ." | ||
}, | ||
@@ -14,0 +15,0 @@ "main": "dist/lib/index.js", |
import methods from './methods'; | ||
interface DefaultData { | ||
[key: string]: unknown; | ||
[key: string]: any; | ||
} | ||
interface ApiSchemaConfig extends DefaultData { | ||
mock?: string; | ||
requestType?: string; | ||
// todo | ||
cache?: { | ||
key?: string; | ||
max?: number; | ||
expire?: number; | ||
}; | ||
cancel?: boolean; | ||
timeout?: number; | ||
} | ||
@@ -11,3 +20,3 @@ interface ApiSchemaMock { | ||
success: boolean; | ||
data: unknown; | ||
data: any; | ||
}; | ||
@@ -14,0 +23,0 @@ } |
@@ -1,11 +0,8 @@ | ||
import { ApiSchemaList, ApiSchemaData, ServiceConfig } from './api'; | ||
import mixins from './util/mixins'; | ||
import { ApiSchemaList, ApiSchemaData, ServiceConfig, ApiSchema } from './api'; | ||
import mixin from './util/mixin'; | ||
import mock from './util/mock'; | ||
import compose from './util/compose'; | ||
import { requestReturn, createRequestReturn, requester, middleware } from './util/request'; | ||
import requestType from './middleware/requestType'; | ||
type requestReturn = Promise<unknown>; | ||
type createRequestReturn = (requestObj: ApiSchemaData) => requestReturn; | ||
type requester = ({ | ||
config: ApiSchemaConfig, | ||
mock: ApiSchemaMock, | ||
url: UrlSchema, | ||
}) => requestReturn; | ||
class Service { | ||
@@ -16,24 +13,31 @@ public constructor(apiSchemaList: ApiSchemaList, serviceConfig: ServiceConfig, requester: requester) { | ||
this.requester = requester; | ||
this.middlewareList = [ | ||
requestType, | ||
]; | ||
return this.initService(); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
[prop: string]: any; | ||
private apiSchemaList: ApiSchemaList; | ||
private requester: Function; | ||
private requester: requester; | ||
private serviceConfig: ServiceConfig; | ||
private createRequest(apiName: string, target: Service): createRequestReturn { | ||
public middlewareList: middleware[]; | ||
public use(middleware: middleware): Service { | ||
this.middlewareList.push(middleware); | ||
return this; | ||
} | ||
private createRequest(apiName: string, target: Service, middlewareWrap): createRequestReturn { | ||
return function(requestObj: ApiSchemaData): requestReturn { | ||
const requestInfo = mixins(target.serviceConfig, target.apiSchemaList[apiName], requestObj); | ||
const { config, mock } = requestInfo; | ||
const mockStatus = config && config.mock; | ||
const mockInfo = mockStatus && mock && mock[mockStatus]; | ||
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); | ||
}); | ||
const requestInfo = mixin(target.serviceConfig, target.apiSchemaList[apiName], requestObj); | ||
return middlewareWrap(requestInfo, (ctx: ApiSchema): requestReturn => { | ||
let request = null; | ||
if (process.env.NODE_ENV === 'development') { | ||
request = mock(ctx) | ||
} | ||
} | ||
return target.requester(requestInfo); | ||
if (!request) { | ||
request = target.requester(ctx); | ||
} | ||
return request; | ||
}); | ||
}; | ||
@@ -43,5 +47,6 @@ } | ||
const { apiSchemaList } = this; | ||
const middlewareWrap = compose(this.middlewareList); | ||
if (typeof Proxy === 'undefined') { | ||
Object.keys(apiSchemaList).forEach((apiName): void => { | ||
this[apiName] = this['$' + apiName] = this.createRequest(apiName, this); | ||
this[apiName] = this['$' + apiName] = this.createRequest(apiName, this, middlewareWrap); | ||
}); | ||
@@ -51,3 +56,3 @@ return this; | ||
return new Proxy(this, { | ||
get(target: Service, propertyKey: string): createRequestReturn|unknown { | ||
get(target: Service, propertyKey: string): createRequestReturn|any { | ||
if (propertyKey in target) { | ||
@@ -60,6 +65,6 @@ return target[propertyKey]; | ||
if (propertyKey in target.apiSchemaList) { | ||
return target.createRequest(propertyKey, target); | ||
return target.createRequest(propertyKey, target, middlewareWrap); | ||
} | ||
}, | ||
set(target: Service, propertyKey: string, value: unknown, receiver: object): boolean { | ||
set(target: Service, propertyKey: string, value: any, receiver: object): boolean { | ||
if (propertyKey.startsWith('$') && apiSchemaList[propertyKey.substring(1)]) { | ||
@@ -76,2 +81,2 @@ console.error(`can not set property ${propertyKey}`); | ||
} | ||
export default Service; | ||
export default Service; |
@@ -16,3 +16,3 @@ import Service from '../src/index'; | ||
success: true, | ||
data: () => [], | ||
data: (): any[] => [], | ||
}, | ||
@@ -44,3 +44,3 @@ 'list.fail': { | ||
}; | ||
const service = new Service(apiSchemaList, serviceConfig, function ({ config, url, mock }): Promise<any> { | ||
const service = new Service(apiSchemaList, serviceConfig, function ({ config, url, mock }): Promise<object> { | ||
return Promise.resolve({ | ||
@@ -54,3 +54,55 @@ config, url, mock | ||
}); | ||
test('middleware', (): void => { | ||
const service = new Service(apiSchemaList, {}, function ({ config, url, mock }): Promise<object> { | ||
return Promise.resolve({ | ||
config, url, mock | ||
}); | ||
}); | ||
service.list({ | ||
config: { | ||
mock: 'list.success', | ||
}, | ||
}).then((data): void => { | ||
expect(data.url.headers['Content-Type']).toStrictEqual('application/json;charset=UTF-8'); | ||
}); | ||
const service2 = new Service(apiSchemaList, { | ||
headers: { | ||
'Content-Type': 'application/json;charset=UTF-8' | ||
}, | ||
}, function ({ config, url, mock }): Promise<object> { | ||
return Promise.resolve({ | ||
config, url, mock | ||
}); | ||
}); | ||
service2.list({ | ||
config: { | ||
mock: 'list.success', | ||
requestType: 'form', | ||
}, | ||
}).then((data): void => { | ||
expect(data.url.headers['Content-Type']).toStrictEqual('application/x-www-form-urlencoded;charset=UTF-8'); | ||
}); | ||
}); | ||
test('custom middleware', (): void => { | ||
const service = new Service(apiSchemaList, { | ||
config: { | ||
custom: 'addHeader', | ||
}, | ||
}, function ({ config, url, mock }): Promise<object> { | ||
return Promise.resolve({ | ||
config, url, mock | ||
}); | ||
}); | ||
service.use(async function (apiSchema, next): Promise<any> { | ||
if (apiSchema.config.custom === 'addHeader') { | ||
const headers = apiSchema.url.headers = apiSchema.url.headers || {}; | ||
headers.addHeader = 'addHeader'; | ||
} | ||
return await next(); | ||
}); | ||
service.list().then((data): void => { | ||
expect(data.url.headers['addHeader']).toStrictEqual('addHeader'); | ||
}); | ||
}); | ||
test('process.env.NODE_ENV=development & mock', (): void => { | ||
@@ -107,5 +159,4 @@ process.env.NODE_ENV = 'development'; | ||
}); | ||
test('reset request method', (): void => { | ||
service.list = function (...args) { | ||
service.list = function (...args): Promise<object> { | ||
return service.$list(...args); | ||
@@ -112,0 +163,0 @@ }; |
@@ -1,4 +0,4 @@ | ||
import mixins from '../../src/util/mixins'; | ||
const caseBase = function(caseInfo): void { | ||
test(`mixins( | ||
import mixin from '../../src/util/mixin'; | ||
const caseBase = function (caseInfo): void { | ||
test(`mixin( | ||
${JSON.stringify(caseInfo.input.serviceConfig)}, | ||
@@ -8,3 +8,3 @@ ${JSON.stringify(caseInfo.input.apiSchema)}, | ||
)`, (): void => { | ||
expect(mixins(caseInfo.input.serviceConfig, caseInfo.input.apiSchema, caseInfo.input.requestObj)).toStrictEqual(caseInfo.output); | ||
expect(mixin(caseInfo.input.serviceConfig, caseInfo.input.apiSchema, caseInfo.input.requestObj)).toStrictEqual(caseInfo.output); | ||
}); | ||
@@ -11,0 +11,0 @@ }; |
50949
46
1272
4