async function createRequestTransformer()

in amazon-location-helpers/index.js [42:89]


async function createRequestTransformer({
  credentials,
  identityPoolId,
  region,
}) {
  if (identityPoolId != null) {
    // use the region containing the identity pool if one wasn't provided
    region = region || identityPoolId.split(":")[0];
    const provider = createCognitoCredentialProvider(identityPoolId);

    // refresh credentials before they expire
    async function refreshCognitoCredentials() {
      credentials = await provider();

      setTimeout(
        refreshCognitoCredentials,
        credentials.expiration - new Date()
      );
    }

    await refreshCognitoCredentials();
  }

  validateCredentials(credentials);
  validateRegion(region);

  return (url, resourceType) => {
    if (resourceType === "Style" && !url.includes("://")) {
      // resolve to an AWS URL
      url = `https://maps.geo.${region}.amazonaws.com/maps/v0/maps/${url}/style-descriptor`;
    }

    if (url.includes("amazonaws.com")) {
      // only sign AWS requests (with the signature as part of the query string)
      return {
        // @aws-sdk/signature-v4 would be another option, but this needs to be synchronous
        url: Signer.signUrl(url, {
          access_key: credentials.accessKeyId,
          secret_key: credentials.secretAccessKey,
          session_token: credentials.sessionToken,
        }),
      };
    }

    // don't sign
    return { url };
  };
}