aws api gateway sdk implementation

Hi!,

right now I’m implementing in my site a feature that should call an aws lambda through aws api gateway. The service use a federated identity pool (google) and I get my aws credentials using aws-sdk node module to connect to cognito. The first point (get aws credentials is done and works) but the second point haves me stuck. The thing is that I cannot use aws-amplify node module because wix does not support front end libraries, I cannot use aws-api-gateway-client because is not installed and I asked for installation 3 times but didn’t received a single answer. So I have 2 cards left, the first, to use the generated aws api gateway sdk for javascript or the second build my own http request using the wix third party services library. I’ve tried the first one but always get error codes, seems like js imported cannot be seen each other. This is my project structure:
The hole aws foder contains the generated sdk and aws.jsw uses it


I’ve followed this guidelines: https://docs.aws.amazon.com/en_en/apigateway/latest/developerguide/how-to-generate-sdk-javascript.html
My code:

import AWS from 'aws-sdk';
import 'backend/aws/lib/CryptoJS/rollups/hmac-sha256.js';
import 'backend/aws/lib/CryptoJS/rollups/sha256.js';
import 'backend/aws/lib/CryptoJS/components/hmac.js';
import 'backend/aws/lib/CryptoJS/components/enc-base64.js';
import 'backend/aws/lib/url-template/url-template.js';
import 'backend/aws/lib/apiGatewayCore/sigV4Client.js';
import 'backend/aws/lib/apiGatewayCore/apiGatewayClient.js';
import 'backend/aws/lib/apiGatewayCore/simpleHttpClient.js';
import 'backend/aws/lib/apiGatewayCore/utils.js';
import 'backend/aws/apigClient';

AWS.config.getCredentials((err) => {
 if (err) {
            console.log(err);
        } else {
            console.log('accessKeyId: ' +         AWS.config.credentials.accessKeyId);
            console.log('secretAccessKey: ' + AWS.config.credentials.secretAccessKey);
            console.log('sessionToken: ' + AWS.config.credentials.sessionToken);
 try {
                console.log('apigClientFactory: ' + apigClientFactory.newClient());
            } catch (e) {
                console.error('error ---->' + e);
            }

Always get error with my apigClient because apigClientFactory does not exist or newClient() is not a functions and it exists in apigClient.

var apigClientFactory = {};
apigClientFactory.newClient = function (config) {
 var apigClient = { };
 if(config === undefined) {
        config = {
            accessKey: '',
            secretKey: '',
            sessionToken: '',
            region: '',
            apiKey: undefined,
            defaultContentType: 'application/json',
            defaultAcceptType: 'application/json'
        };
    }

Has anyone an example code to invoke an api through api-gateway in wix?

Thanks.

@[Jennifer Perez] You need to change the import statements to begin with. import statements need to assign the exported object to a local variable in the file. So You have defined AWS correctly although it isn’t clear that you have an aws-sdk to import from unless you have installed a node package from npm-installer called aws-sdk.

import AWS from 'aws-sdk';

You need to follow a similar pattern for the other imports. e.g.:

import apigClient from 'backend/aws/apigClient';

The thing to do is look at the export statements in the related files and see what you should be importing. Essentially each file that iports from any of the other files will need to have similar import statements.

Thank you Steven, I did that and I realized I had to change the entire generated sdk import statements and functions, that’s quite annoying. Hope wix improve things like this. Than you for your help

I am doing the same with the AWS SDK, did you put it in the back-end or the front-end

You should put it in the backend side

Does every function need to be "exported in the SDK provided by AWS or the ones that are red lined due to errors because of the lack of import -export .Also can the tags

@thedixonmissions functions are exported from the library where they are declared. They need to be imported from that library (file) by the code that uses them.

imports can be of a whole set of exported functions in which case the export will be an object and you need to access functions via the object or you name the functions directly in your import.

@jenniferperezb Can you send a brief example of code that you changed for the AWS SDK to use in WIX?

The code imported from the AWS API Gateway SDK: Must the JavaScript file extensions be changed from .js to .jsw due to putting them in the back-end files .

See the article Where do I put Wix backend functions? for information on backend file types and how they are used.

@jenniferperezb Were you able to use your AWS SDK?