6

Using SNS service from the AWS SDK for JavaScript v3 in React Native

When I try to create an endpoint (or execute really any command through AWS) I'm getting this error URL.hostname is not implemented

  • I tried a number of other commands; same error
  • The only thing I've done that yields a different error is if I remove region from the params when creating the client. Then it errors Region is missing in the same place. But worth noting, if I just pass nonsense (asdf123) for region it yields this same URL.hostname error
import { CognitoIdentityClient } from "@aws-sdk/client-cognito-identity";
import { fromCognitoIdentityPool } from "@aws-sdk/credential-provider-cognito-identity";
import { SNSClient, CreatePlatformEndpointCommand } from "@aws-sdk/client-sns";

const region = 'us-west-2'
const sns = new SNSClient({
    region: region,
    credentials: fromCognitoIdentityPool({
        client: new CognitoIdentityClient({ region }),
        identityPoolId: identityPoolId,
    }) /// doesn't matter whether I pass credentials or not, same result
});

const params = {
    PlatformApplicationArn: platformApplicationArn,
    Token: token
}

const command = new CreatePlatformEndpointCommand(params);  

const res = await sns.send(command)
.catch((err) => {
    console.log(err) //// this is the "[Error: not implemented]"
    throw err 
})

This is saying I should have more data in this error but the only thing in this error is the message. https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-sns/index.html#troubleshooting

Seems like this isn't even an AWS error...but I have no idea how to troubleshoot further.

Any suggestions on how to identify where this is coming from would be SUPER APPRECIATED. Thanks all

enesefuu
  • 369
  • 4
  • 14

1 Answers1

21

Found some posts which suggested this for similar errors: react-native-url-polyfill

Once that was added I started getting this error instead: [Error: crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported]

To solve this I added react-native-get-random-values

... and it works. I'm not sure I understand why, or what the root cause is, and this seems like kinda a messy workaround. But it does work.

import 'react-native-url-polyfill/auto';
import 'react-native-get-random-values';
import { v4 as uuidv4 } from 'uuid';
//at the top of the file where I'm handling AWS SNS 

If anyone out there can shed light on what's going on here, no doubt there's a better way.

enesefuu
  • 369
  • 4
  • 14
  • 1
    Thanks so much! This worked for me too as I was trying to get files from an S3 bucket. I'm guessing, for me, it was something to do with the AWS GetObjectCommand needing a particular format of URL as the key, which in turn requires some hashing, hence the need for random numbers. I couldn't tell you exactly why, but this saved me. +1000 – trevorhpittman Mar 11 '22 at 03:18
  • Glad to hear it! – enesefuu Mar 13 '22 at 21:27
  • 3
    Had the same issue. Found out that these two packages are required if using AWS's modular interfaces. This is documented here in their Getting Started guide https://github.com/aws/aws-sdk-js-v3#getting-started – Daniel Long Aug 16 '22 at 18:43
  • you're amazing. how did you even figure this out? saved my life – elguapo Nov 22 '22 at 18:17
  • Thanks enesefuu and @DanielLong for your help on this. I also found the link to amplify react-native documentation https://ui.docs.amplify.aws/react-native/getting-started/installation which states the same to import these two packages – Sarvesh Jun 20 '23 at 19:06