/
header-builder-for-destination.ts
44 lines (39 loc) · 1.37 KB
/
header-builder-for-destination.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. */
import { MapType } from '@sap-cloud-sdk/util';
import { Destination } from '../scp-cf';
import { buildAuthorizationHeaders } from './authorization-header';
import {
getHeader,
replaceDuplicateKeys,
filterNullishValues
} from './headers-util';
async function getAuthHeaders(
destination: Destination,
customHeaders?: MapType<any>
): Promise<MapType<string>> {
const customAuthHeaders = getHeader('authorization', customHeaders);
return Object.keys(customAuthHeaders).length
? customAuthHeaders
: buildAuthorizationHeaders(destination);
}
/**
* Builds the authorization, proxy authorization and SAP headers for a given destination.
*
* @param destination - A destination.
* @param customHeaders - Custom default headers for the resulting HTTP headers.
* @returns HTTP headers for the given destination.
*/
export async function buildHeadersForDestination(
destination: Destination,
customHeaders?: MapType<any>
): Promise<MapType<string>> {
const authHeaders = await getAuthHeaders(destination, customHeaders);
const sapHeaders = replaceDuplicateKeys(
filterNullishValues({
'sap-client': destination.sapClient,
'SAP-Connectivity-SCC-Location_ID': destination.cloudConnectorLocationId
}),
customHeaders
);
return { ...authHeaders, ...sapHeaders };
}