Shopify / js-buy-sdk

The JS Buy SDK is a lightweight library that allows you to build ecommerce into any website. It is based on Shopify's API and provides the ability to retrieve products and collections from your shop, add products to a cart, and checkout.
https://shopify.github.io/js-buy-sdk
MIT License
992 stars 263 forks source link

Discussions Partners and Developers Events Groups Learn Notifications Messages Alocard Alocard Shopify APIs and SDKs Development discussions around Shopify APIs Return management just got easier! We’ve launched Customer Self-Serve Returns to all Shopify merchants. Click here to learn more! Shopify Community Partners and Developers Shopify APIs and SDKs Make a Request from a Public Endpoint to a Non-Public Endpoint Inside an App Make a Request from a Public Endpoint to a Non-Public Endpoint Inside an App #936

Closed Marvya closed 1 year ago

Marvya commented 1 year ago

Dear Developers, I hope this message finds you well. I am writing to request your assistance in resolving a technical issue I am facing. I am trying to make a request from a public endpoint within my app to a private endpoint within the same app. I am using Axios for this purpose and my code appears to be written correctly:

app.post("/api/customer", async (req, res) => {
    const customerInput = req.body;

    try {
      const createCustomerResponse = await graphqlClient.query({
        data: { query: CREATE_CUSTOMER, variables: { input: customerInput } },
      });
      const data = createCustomerResponse.body.data.customerCreate;
      const { userErrors, customer } = data;
      if (userErrors.length > 0) {
        throw new GraphqlError(userErrors);
      }

      const customerId = customer.id.match(/\d*$/)[0];
      const response = await axios.post("https://1234.ngrok.io/api/send-invite", customerId);
      const result = await response.json();
      console.log(result);

      res.status(200).json({ customer });
    } catch (error) {
      if (error instanceof GraphqlError) {
        res.status(422).json({
          message: "GraphQL User Errors",
          errors: error.errors,
        });
      } else if (error instanceof Error) {
        res.status(400).json({
          message: "Wrong Data Format",
          error: error,
        });
      } else {
        res.status(500).send(error);
      }
    }
  });

  app.post("/api/send-invite", async (req, res) => {
    try {
      const id = req.body;

      const customerInviteResponse = await restClient.post({
        path: "/customers/" + id + "/send_invite",
      });
      const customerInvite = customerInviteResponse.body.customer_invite;

      res.status(200).json(customerInvite);
    } catch (error) {
      res.status(500).send(error);
    }
  });

However, I am encountering an error message and I am seeking your help in resolving it:

{
  "message": "Request failed with status code 500",
  "name": "AxiosError",
  "stack": "AxiosError: Request failed with status code 500\n    at settle (file:///Users/omarghanaim/Desktop/Invite%20Members%20App/new-invite-members/invite-members/node_modules/axios/lib/core/settle.js:19:12)\n    at IncomingMessage.handleStreamEnd (file:///Users/omarghanaim/Desktop/Invite%20Members%20App/new-invite-members/invite-members/node_modules/axios/lib/adapters/http.js:548:11)\n    at IncomingMessage.emit (node:events:525:35)\n    at endReadableNT (node:internal/streams/readable:1359:12)\n    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)",
  "config": {
    "transitional": {
      "silentJSONParsing": true,
      "forcedJSONParsing": true,
      "clarifyTimeoutError": false
    },
    "adapter": [
      "xhr",
      "http"
    ],
    "transformRequest": [
      null
    ],
    "transformResponse": [
      null
    ],
    "timeout": 0,
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN",
    "maxContentLength": -1,
    "maxBodyLength": -1,
    "env": {},
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "Content-Type": "application/x-www-form-urlencoded",
      "User-Agent": "axios/1.3.2",
      "Content-Length": "13",
      "Accept-Encoding": "gzip, compress, deflate, br"
    },
    "method": "post",
    "url": "https://597a-2003-e6-9f1e-6700-4dbf-8d56-31ca-11b4.ngrok.io/api/send-invite",
    "data": "5936939729028"
  },
  "code": "ERR_BAD_RESPONSE",
  "status": 500
}

Could you please provide some guidance on how I can resolve this issue? Thank you in advance for your assistance.

Best regards, Alocard

melissaluu commented 1 year ago

This issue is not related to JS Buy SDK.