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