-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot call REST api from web worker using aws-ampliy #12860
Comments
Thanks for creating this issue from the Discord Office Hours session today, @alex-breen. Can you clarify how you're initializing Amplify within the web worker or if it's being imported? And based on the error that you're seeing, can you double check that the API names in the web worker exactly match what's in your config (i.e. casing/spelling)? |
Side note - I (clumsily) tried a few different things with the web worker to try to make it work. I imported a window polyfill - no change. I called Amplify |
Ok, thanks for the quick response and glad to hear we’re past the original error then at least! It sounds like what’s happening here is you’ve got some type of network requests needing authenticated credentials happening in the web worker. Can you maybe share some additional context here about what you’re trying to do with the web worker? If you’re trying to offload some of the work to the web worker, you may want to make any networks requests within the main thread but then hand off the response and other work to the web worker after it’s been received for additional processing. If you’re trying to do all this work in the web worker, that error seems like it’ll require you passing credentials into the other thread and require you writing your own credential and token providers (which we wouldn’t recommend). |
Thanks @cwomack, that's helpful. Here is the context about why I'm using a web worker:
So, as an alternate method, if I can't invoke the |
@alex-breen, we're going to mark this as a feature request at this point to track better support out of the box for web workers. Appreciate you giving the additional context and use cases here. |
I'm curious if there is any solution to this. I'm having a similar issue where I'm simply trying to make a
|
@blipps199, are you calling Amplify.configure() within the web worker so that it has access to the proper API names (see @alex-breen's comment here) above. And are you able to call any Auth API's within the web worker due to the necessity to have a user with proper credentials for the Storage API's. |
@cwomack To be quite honest, I'm not familiar with web workers. I never set one up. Essentially, I started a new Amplify Gen 2 project using the quickstart guide for react seen here. I would love to use |
@blipps199, thanks for the quick reply. We might have you open up a new Github issue detailing what you're experiencing in more detail, but before that let me see if we can diagnose this further in another reply. If you've already created the Amplify app from the Quickstart guide for React that you linked, did you also follow the instructions for setting up the REST API here? You'll need to create a new directory and resource file as well as import Let me know if that helps you get it set up, or feel free to reply back/open a new issue if not! |
@cwomack Hey, thanks for the reply. Yes, I used that guide in the creation of the API and function. This might be obvious to the trained eye but not necessarily to me but I'm using an assumed role on a separate AWS account to do work for a client. In other words I do not have a user so I cannot use ampx commands and what not. Do you think this might have something to do with this? Hopefully I'm on to something and not sounding silly. |
@blipps199, appreciate the additional clarity here and quick reply! It sounds like the AWS Account that you're working with may not have the necessary permissions for the Do you know if you can create this policy or if you're able to call |
@cwomack I was able to get creds and can use ampx commands now. I was able to solve one issue by doing this and that was fetching lambda env variables(not a concern here just for context). However, I'm still getting the error regarding the API name being invalid. The output file, amplify_ouputs.json, is updated and I have full admin access so perms shouldn't be an issue. Also, Amplify,configure() is called with the outputs file included and I still get the error. At this point I'm not sure what else to do. |
@cwomack I decided to pivot to using a GraphQL API instead of rest. I probably should have done this from the beginning. This simplified things greatly and I no longer need to use |
Before opening, please confirm:
JavaScript Framework
React
Amplify APIs
REST API
Amplify Version
v6
Amplify Categories
api
Backend
Amplify CLI
Environment information
Describe the bug
From a web worker (on Chrome), invoking a
get
request to Amplify REST API fails with the following error.GET call failed: InvalidApiName: API name is invalid.
The same request running in the main thread is successful.
Expected behavior
Works the same as when invoked in the main thread.
Reproduction steps
Please see code in snippet below.
Code Snippet
Log output
{
"aws_project_region": "us-east-1",
"aws_cognito_identity_pool_id": "us-east-1:0598a33f-9357-4ba8-9cb2-876da7c9bb85",
"aws_cognito_region": "us-east-1",
"aws_user_pools_id": "us-east-1_JAAkzlaKS",
"aws_user_pools_web_client_id": "27s2qe07qgpp0mf7083dian9ua",
"oauth": {
"domain": "sandboxauth167bef521-67bef521-dev.auth.us-east-1.amazoncognito.com",
"scope": [
"phone",
"email",
"openid",
"profile",
"aws.cognito.signin.user.admin"
],
"redirectSignIn": "http://localhost:3000/",
"redirectSignOut": "http://localhost:3000/",
"responseType": "code"
},
"federationTarget": "COGNITO_USER_POOLS",
"aws_cognito_username_attributes": [
"EMAIL"
],
"aws_cognito_social_providers": [
"GOOGLE"
],
"aws_cognito_signup_attributes": [
"EMAIL"
],
"aws_cognito_mfa_configuration": "OFF",
"aws_cognito_mfa_types": [
"SMS"
],
"aws_cognito_password_protection_settings": {
"passwordPolicyMinLength": 8,
"passwordPolicyCharacters": []
},
"aws_cognito_verification_mechanisms": [
"EMAIL"
],
"aws_cloud_logic_custom": [
{
"name": "sandbox1",
"endpoint": "https://n66fgb5l98.execute-api.us-east-1.amazonaws.com/dev",
"region": "us-east-1"
}
]
}
The text was updated successfully, but these errors were encountered: