|
| 1 | +{{! Copyright(c) 1995 - 2018 T-Systems Multimedia Solutions GmbH }} |
| 2 | +{{! Riesaer Str. 5, 01129 Dresden }} |
| 3 | +{{! All rights reserved. }} |
| 4 | +{{^useRxJS6}} |
| 5 | +import { Observable } from 'rxjs/Observable'; |
| 6 | +{{/useRxJS6}} |
| 7 | +{{#useRxJS6}} |
| 8 | +import { Observable } from 'rxjs'; |
| 9 | +{{/useRxJS6}} |
| 10 | + |
| 11 | +export interface ConfigurationParameters { |
| 12 | + apiKeys?: {[ key: string ]: string}; |
| 13 | + username?: string; |
| 14 | + password?: string; |
| 15 | + accessToken?: string | (() => string); |
| 16 | + errorHandler?: (err: any, operationName: string) => Observable<never>; |
| 17 | + basePath?: string; |
| 18 | + withCredentials?: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +export class Configuration { |
| 22 | + apiKeys?: {[ key: string ]: string}; |
| 23 | + username?: string; |
| 24 | + password?: string; |
| 25 | + accessToken?: string | (() => string); |
| 26 | + errorHandler?: (err: any, operationName: string) => Observable<never>; |
| 27 | + basePath?: string; |
| 28 | + withCredentials?: boolean; |
| 29 | + |
| 30 | + constructor(configurationParameters: ConfigurationParameters = {}) { |
| 31 | + this.apiKeys = configurationParameters.apiKeys; |
| 32 | + this.username = configurationParameters.username; |
| 33 | + this.password = configurationParameters.password; |
| 34 | + this.accessToken = configurationParameters.accessToken; |
| 35 | + this.errorHandler = configurationParameters.errorHandler; |
| 36 | + this.basePath = configurationParameters.basePath; |
| 37 | + this.withCredentials = configurationParameters.withCredentials; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Select the correct content-type to use for a request. |
| 42 | + * Uses {@link Configuration#isJsonMime} to determine the correct content-type. |
| 43 | + * If no content type is found return the first found type if the contentTypes is not empty |
| 44 | + * @param contentTypes - the array of content types that are available for selection |
| 45 | + * @returns the selected content-type or <code>undefined</code> if no selection could be made. |
| 46 | + */ |
| 47 | + public selectHeaderContentType (contentTypes: string[]): string | undefined { |
| 48 | + if (contentTypes.length === 0) { |
| 49 | + return undefined; |
| 50 | + } |
| 51 | + |
| 52 | + let type = contentTypes.find(x => this.isJsonMime(x)); |
| 53 | + if (type === undefined) { |
| 54 | + return contentTypes[0]; |
| 55 | + } |
| 56 | + return type; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Select the correct accept content-type to use for a request. |
| 61 | + * Uses {@link Configuration#isJsonMime} to determine the correct accept content-type. |
| 62 | + * If no content type is found return the first found type if the contentTypes is not empty |
| 63 | + * @param accepts - the array of content types that are available for selection. |
| 64 | + * @returns the selected content-type or <code>undefined</code> if no selection could be made. |
| 65 | + */ |
| 66 | + public selectHeaderAccept(accepts: string[]): string | undefined { |
| 67 | + if (accepts.length === 0) { |
| 68 | + return undefined; |
| 69 | + } |
| 70 | + |
| 71 | + let type = accepts.find(x => this.isJsonMime(x)); |
| 72 | + if (type === undefined) { |
| 73 | + return accepts[0]; |
| 74 | + } |
| 75 | + return type; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Check if the given MIME is a JSON MIME. |
| 80 | + * JSON MIME examples: |
| 81 | + * application/json |
| 82 | + * application/json; charset=UTF8 |
| 83 | + * APPLICATION/JSON |
| 84 | + * application/vnd.company+json |
| 85 | + * @param mime - MIME (Multipurpose Internet Mail Extensions) |
| 86 | + * @return True if the given MIME is JSON, false otherwise. |
| 87 | + */ |
| 88 | + public isJsonMime(mime: string): boolean { |
| 89 | + const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i'); |
| 90 | + return mime !== null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json'); |
| 91 | + } |
| 92 | +} |
0 commit comments