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