功能点

请求的核心中间层,内嵌中间件,缓存的封装为request提供使用;

参考源码

  1. // lamia 中间件
  2. import Onion from './onion';
  3. /**
  4. * lamia
  5. * MapCache: Map类型的缓存配置
  6. * mergeRequestOptions: 合并请求配置
  7. */
  8. import { MapCache, mergeRequestOptions } from './utils';
  9. /**
  10. * lamia
  11. * 导出对应的addfix, 为请求地址添加对应的前缀,后缀
  12. */
  13. import addfixInterceptor from './interceptor/addfix';
  14. /**
  15. * lamia
  16. * 中间件 请求的封装
  17. */
  18. import fetchMiddleware from './middleware/fetch';
  19. /**
  20. * lamia
  21. * 中间件 响应格式化
  22. */
  23. import parseResponseMiddleware from './middleware/parseResponse';
  24. /**
  25. * lamia
  26. * 中间件 简化对应的query请求, 简化对应的post请求;
  27. */
  28. import simplePost from './middleware/simplePost';
  29. /**
  30. * lamia
  31. * 中间件 对get的请求的参数,做序列化
  32. */
  33. import simpleGet from './middleware/simpleGet';
  34. // 初始化全局和内核中间件
  35. const globalMiddlewares = [simplePost, simpleGet, parseResponseMiddleware];
  36. // lamia 核心请求
  37. const coreMiddlewares = [fetchMiddleware];
  38. Onion.globalMiddlewares = globalMiddlewares;
  39. Onion.defaultGlobalMiddlewaresLength = globalMiddlewares.length;
  40. Onion.coreMiddlewares = coreMiddlewares;
  41. Onion.defaultCoreMiddlewaresLength = coreMiddlewares.length;
  42. class Core {
  43. constructor(initOptions) {
  44. this.onion = new Onion([]);
  45. this.fetchIndex = 0; // 【即将废弃】请求中间件位置
  46. this.mapCache = new MapCache(initOptions);
  47. this.initOptions = initOptions;
  48. this.instanceRequestInterceptors = [];
  49. this.instanceResponseInterceptors = [];
  50. }
  51. // 旧版拦截器为共享
  52. static requestInterceptors = [addfixInterceptor];
  53. static responseInterceptors = [];
  54. // 请求拦截器 默认 { global: true } 兼容旧版本拦截器
  55. static requestUse(handler, opt = { global: true }) {
  56. if (typeof handler !== 'function') throw new TypeError('Interceptor must be function!');
  57. if (opt.global) {
  58. Core.requestInterceptors.push(handler);
  59. } else {
  60. this.instanceRequestInterceptors.push(handler);
  61. }
  62. }
  63. // 响应拦截器 默认 { global: true } 兼容旧版本拦截器
  64. static responseUse(handler, opt = { global: true }) {
  65. if (typeof handler !== 'function') throw new TypeError('Interceptor must be function!');
  66. if (opt.global) {
  67. Core.responseInterceptors.push(handler);
  68. } else {
  69. this.instanceResponseInterceptors.push(handler);
  70. }
  71. }
  72. use(newMiddleware, opt = { global: false, core: false }) {
  73. this.onion.use(newMiddleware, opt);
  74. return this;
  75. }
  76. extendOptions(options) {
  77. this.initOptions = mergeRequestOptions(this.initOptions, options);
  78. this.mapCache.extendOptions(options);
  79. }
  80. // 执行请求前拦截器
  81. dealRequestInterceptors(ctx) {
  82. const reducer = (p1, p2) =>
  83. p1.then((ret = {}) => {
  84. ctx.req.url = ret.url || ctx.req.url;
  85. ctx.req.options = ret.options || ctx.req.options;
  86. return p2(ctx.req.url, ctx.req.options);
  87. });
  88. const allInterceptors = [...Core.requestInterceptors, ...this.instanceRequestInterceptors];
  89. return allInterceptors.reduce(reducer, Promise.resolve()).then((ret = {}) => {
  90. ctx.req.url = ret.url || ctx.req.url;
  91. ctx.req.options = ret.options || ctx.req.options;
  92. return Promise.resolve();
  93. });
  94. }
  95. request(url, options) {
  96. const { onion } = this;
  97. const obj = {
  98. req: { url, options },
  99. res: null,
  100. cache: this.mapCache,
  101. responseInterceptors: [...Core.responseInterceptors, ...this.instanceResponseInterceptors],
  102. };
  103. if (typeof url !== 'string') {
  104. throw new Error('url MUST be a string');
  105. }
  106. return new Promise((resolve, reject) => {
  107. this.dealRequestInterceptors(obj)
  108. .then(() => onion.execute(obj))
  109. .then(() => {
  110. resolve(obj.res);
  111. })
  112. .catch(error => {
  113. const { errorHandler } = obj.req.options;
  114. if (errorHandler) {
  115. try {
  116. const data = errorHandler(error);
  117. resolve(data);
  118. } catch (e) {
  119. reject(e);
  120. }
  121. } else {
  122. reject(error);
  123. }
  124. });
  125. });
  126. }
  127. }
  128. export default Core;