createApis.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 createFunctionalProxy = (array: (string | symbol)[], alovaInstance: Alova<AlovaGenerics>, configMap: any) => {
  28. // create a new proxy instance
  29. return new Proxy(function () {}, {
  30. get(_, property) {
  31. // record the target property, so that it can get the completed accessing paths
  32. const newArray = [...array, property];
  33. // always return a new proxy to continue recording accessing paths.
  34. return createFunctionalProxy(newArray, alovaInstance, configMap);
  35. },
  36. apply(_, __, [config]) {
  37. const apiPathKey = array.join('.') as keyof typeof apiDefinitions;
  38. const apiItem = apiDefinitions[apiPathKey];
  39. if (!apiItem) {
  40. throw new Error(`the api path of \`${apiPathKey}\` is not found`);
  41. }
  42. const mergedConfig = {
  43. ...configMap[apiPathKey],
  44. ...config
  45. };
  46. const [method, url] = apiItem;
  47. const pathParams = mergedConfig.pathParams;
  48. const urlReplaced = url.replace(/\{([^}]+)\}/g, (_, key) => {
  49. const pathParam = pathParams[key];
  50. return pathParam;
  51. });
  52. delete mergedConfig.pathParams;
  53. let data = mergedConfig.data;
  54. if (Object.prototype.toString.call(data) === '[object Object]' && typeof FormData !== 'undefined') {
  55. let hasBlobData = false;
  56. const formData = new FormData();
  57. for (const key in data) {
  58. formData.append(key, data[key]);
  59. if (data[key] instanceof Blob) {
  60. hasBlobData = true;
  61. }
  62. }
  63. data = hasBlobData ? formData : data;
  64. }
  65. return new Method(method.toUpperCase() as MethodType, alovaInstance, urlReplaced, mergedConfig, data);
  66. }
  67. });
  68. };
  69. export const createApis = (alovaInstance: Alova<AlovaGenerics>, configMap: any) => {
  70. const Apis = new Proxy({} as Apis, {
  71. get(_, property) {
  72. return createFunctionalProxy([property], alovaInstance, configMap);
  73. }
  74. });
  75. // define global variable `Apis`
  76. (globalThis as any).Apis = Apis;
  77. return Apis;
  78. };
  79. type MethodConfig<T> = AlovaMethodCreateConfig<
  80. (typeof import('./index'))['alovaInstance'] extends Alova<infer AG> ? AG : any,
  81. any,
  82. T
  83. >;
  84. type APISofParameters<Tag extends string, Url extends string> = Tag extends keyof Apis
  85. ? Url extends keyof Apis[Tag]
  86. ? Apis[Tag][Url] extends (...args: any) => any
  87. ? Parameters<Apis[Tag][Url]>
  88. : any
  89. : any
  90. : any;
  91. type MethodsConfigMap = {
  92. [P in keyof typeof import('./apiDefinitions').default]?: MethodConfig<
  93. P extends `${infer Tag}.${infer Url}` ? Parameters<APISofParameters<Tag, Url>[0]['transform']>[0] : any
  94. >;
  95. };
  96. export const withConfigType = <Config extends MethodsConfigMap>(config: Config) => config;