You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 line
4.7KB

  1. import { HttpParameterCodec } from '@angular/common/http';
  2. export interface ConfigurationParameters {
  3. /**
  4. * @deprecated Since 5.0. Use credentials instead
  5. */
  6. apiKeys?: {[ key: string ]: string};
  7. username?: string;
  8. password?: string;
  9. /**
  10. * @deprecated Since 5.0. Use credentials instead
  11. */
  12. accessToken?: string | (() => string);
  13. basePath?: string;
  14. withCredentials?: boolean;
  15. encoder?: HttpParameterCodec;
  16. /**
  17. * The keys are the names in the securitySchemes section of the OpenAPI
  18. * document. They should map to the value used for authentication
  19. * minus any standard prefixes such as 'Basic' or 'Bearer'.
  20. */
  21. credentials?: {[ key: string ]: string | (() => string | undefined)};
  22. }
  23. export class Configuration {
  24. /**
  25. * @deprecated Since 5.0. Use credentials instead
  26. */
  27. apiKeys?: {[ key: string ]: string};
  28. username?: string;
  29. password?: string;
  30. /**
  31. * @deprecated Since 5.0. Use credentials instead
  32. */
  33. accessToken?: string | (() => string);
  34. basePath?: string;
  35. withCredentials?: boolean;
  36. encoder?: HttpParameterCodec;
  37. /**
  38. * The keys are the names in the securitySchemes section of the OpenAPI
  39. * document. They should map to the value used for authentication
  40. * minus any standard prefixes such as 'Basic' or 'Bearer'.
  41. */
  42. credentials: {[ key: string ]: string | (() => string | undefined)};
  43. constructor(configurationParameters: ConfigurationParameters = {}) {
  44. this.apiKeys = configurationParameters.apiKeys;
  45. this.username = configurationParameters.username;
  46. this.password = configurationParameters.password;
  47. this.accessToken = configurationParameters.accessToken;
  48. this.basePath = configurationParameters.basePath;
  49. this.withCredentials = configurationParameters.withCredentials;
  50. this.encoder = configurationParameters.encoder;
  51. if (configurationParameters.credentials) {
  52. this.credentials = configurationParameters.credentials;
  53. }
  54. else {
  55. this.credentials = {};
  56. }
  57. // init default basic credential
  58. if (!this.credentials['basic']) {
  59. this.credentials['basic'] = () => {
  60. return (this.username || this.password)
  61. ? btoa(this.username + ':' + this.password)
  62. : undefined;
  63. };
  64. }
  65. }
  66. /**
  67. * Select the correct content-type to use for a request.
  68. * Uses {@link Configuration#isJsonMime} to determine the correct content-type.
  69. * If no content type is found return the first found type if the contentTypes is not empty
  70. * @param contentTypes - the array of content types that are available for selection
  71. * @returns the selected content-type or <code>undefined</code> if no selection could be made.
  72. */
  73. public selectHeaderContentType (contentTypes: string[]): string | undefined {
  74. if (contentTypes.length === 0) {
  75. return undefined;
  76. }
  77. const type = contentTypes.find((x: string) => this.isJsonMime(x));
  78. if (type === undefined) {
  79. return contentTypes[0];
  80. }
  81. return type;
  82. }
  83. /**
  84. * Select the correct accept content-type to use for a request.
  85. * Uses {@link Configuration#isJsonMime} to determine the correct accept content-type.
  86. * If no content type is found return the first found type if the contentTypes is not empty
  87. * @param accepts - the array of content types that are available for selection.
  88. * @returns the selected content-type or <code>undefined</code> if no selection could be made.
  89. */
  90. public selectHeaderAccept(accepts: string[]): string | undefined {
  91. if (accepts.length === 0) {
  92. return undefined;
  93. }
  94. const type = accepts.find((x: string) => this.isJsonMime(x));
  95. if (type === undefined) {
  96. return accepts[0];
  97. }
  98. return type;
  99. }
  100. /**
  101. * Check if the given MIME is a JSON MIME.
  102. * JSON MIME examples:
  103. * application/json
  104. * application/json; charset=UTF8
  105. * APPLICATION/JSON
  106. * application/vnd.company+json
  107. * @param mime - MIME (Multipurpose Internet Mail Extensions)
  108. * @return True if the given MIME is JSON, false otherwise.
  109. */
  110. public isJsonMime(mime: string): boolean {
  111. const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
  112. return mime !== null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
  113. }
  114. public lookupCredential(key: string): string | undefined {
  115. const value = this.credentials[key];
  116. return typeof value === 'function'
  117. ? value()
  118. : value;
  119. }
  120. }