createApis.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* tslint:disable */
  2. /* eslint-disable */
  3. /**
  4. * Swagger Petstore - OpenAPI 3.0 - version 1.0.27
  5. *
  6. * This is a sample Pet Store Server based on the OpenAPI 3.0 specification. You can find out more about
  7. Swagger at [https://swagger.io](https://swagger.io). In the third iteration of the pet store, we've switched to the design first approach!
  8. You can now help us improve the API whether it's by making changes to the definition itself or to the code.
  9. That way, with time, we can improve the API in general, and expose some of the new features in OAS3.
  10. Some useful links:
  11. - [The Pet Store repository](https://github.com/swagger-api/swagger-petstore)
  12. - [The source API definition for the Pet Store](https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml)
  13. *
  14. * OpenAPI version: 3.0.4
  15. *
  16. * Contact:
  17. *
  18. * NOTE: This file is auto generated by the alova's vscode plugin.
  19. *
  20. * https://alova.js.org/devtools/vscode
  21. *
  22. * **Do not edit the file manually.**
  23. */
  24. import type { Alova, MethodType, AlovaGenerics, AlovaMethodCreateConfig } from 'alova';
  25. import { Method } from 'alova';
  26. import apiDefinitions from './apiDefinitions';
  27. const cache = Object.create(null);
  28. const createFunctionalProxy = (array: (string | symbol)[], alovaInstance: Alova<AlovaGenerics>, configMap: any) => {
  29. const apiPathKey = array.join('.') as keyof typeof apiDefinitions;
  30. if (cache[apiPathKey]) {
  31. return cache[apiPathKey];
  32. }
  33. // create a new proxy instance
  34. const proxy = new Proxy(function () {}, {
  35. get(_, property) {
  36. // record the target property, so that it can get the completed accessing paths
  37. const newArray = [...array, property];
  38. // always return a new proxy to continue recording accessing paths.
  39. return createFunctionalProxy(newArray, alovaInstance, configMap);
  40. },
  41. apply(_, __, [config]) {
  42. const apiItem = apiDefinitions[apiPathKey];
  43. if (!apiItem) {
  44. throw new Error(`the api path of \`${apiPathKey}\` is not found`);
  45. }
  46. const mergedConfig = {
  47. ...configMap[apiPathKey],
  48. ...config
  49. };
  50. const [method, url] = apiItem;
  51. const pathParams = mergedConfig.pathParams;
  52. const urlReplaced = url!.replace(/\{([^}]+)\}/g, (_, key) => {
  53. const pathParam = pathParams[key];
  54. return pathParam;
  55. });
  56. delete mergedConfig.pathParams;
  57. let data = mergedConfig.data;
  58. if (Object.prototype.toString.call(data) === '[object Object]' && typeof FormData !== 'undefined') {
  59. let hasBlobData = false;
  60. const formData = new FormData();
  61. for (const key in data) {
  62. if (Array.isArray(data[key])) {
  63. for (const ele of data[key]) {
  64. formData.append(key, ele);
  65. if (ele instanceof Blob) {
  66. hasBlobData = true;
  67. }
  68. }
  69. } else {
  70. formData.append(key, data[key]);
  71. if (data[key] instanceof Blob) {
  72. hasBlobData = true;
  73. }
  74. }
  75. }
  76. data = hasBlobData ? formData : data;
  77. }
  78. return new Method(method!.toUpperCase() as MethodType, alovaInstance, urlReplaced, mergedConfig, data);
  79. }
  80. });
  81. cache[apiPathKey] = proxy;
  82. return proxy;
  83. };
  84. export const createApis = (alovaInstance: Alova<AlovaGenerics>, configMap: any) => {
  85. const Apis = new Proxy({} as Apis, {
  86. get(_, property) {
  87. return createFunctionalProxy([property], alovaInstance, configMap);
  88. }
  89. });
  90. return Apis;
  91. };
  92. export const mountApis = (Apis: Apis) => {
  93. // define global variable `Apis`
  94. (globalThis as any).Apis = Apis;
  95. };
  96. type MethodConfig<T> = AlovaMethodCreateConfig<
  97. (typeof import('./index'))['alovaInstance'] extends Alova<infer AG> ? AG : any,
  98. any,
  99. T
  100. >;
  101. type APISofParameters<Tag extends string, Url extends string> = Tag extends keyof Apis
  102. ? Url extends keyof Apis[Tag]
  103. ? Apis[Tag][Url] extends (...args: any) => any
  104. ? Parameters<Apis[Tag][Url]>
  105. : any
  106. : any
  107. : any;
  108. type MethodsConfigMap = {
  109. [P in keyof typeof import('./apiDefinitions').default]?: MethodConfig<
  110. P extends `${infer Tag}.${infer Url}` ? Parameters<NonNullable<APISofParameters<Tag, Url>[0]>['transform']>[0] : any
  111. >;
  112. };
  113. export const withConfigType = <Config extends MethodsConfigMap>(config: Config) => config;