shopify-api-js icon indicating copy to clipboard operation
shopify-api-js copied to clipboard

Add ability to use some functions in serverless services

Open t-jindai opened this issue 3 years ago • 2 comments

Overview

...

Type

  • [x] New feature
  • [ ] Changes to existing features

Motivation

  • await Shopify.Webhooks.Registry.process(req, res)
  • await Shopify.Utils.graphqlProxy(req, res)

These functions will never return in some serverless function service because they do not support request.readable === false request.

Reason

These functions use request.on('data') to read posted data but in serverless services such as firebase functions and AWS lambda w/ API gateway, the body data of the request object have been read before the actual function codes are executed.

example for firebase.

import * as functions from "firebase-functions";

export const test = functions.https.onRequest((req, res) => {
  console.log(req.readable); // false
  console.log(req.body); // can access full of data

  // never be resolved or rejected
  await Shopify.Webhooks.Registry.process(req, res);

  res.send(200)
});

As the request data is already read, request.on('data') and request.on('end') events which are used in the functions are never fired.

Request

I want the functions to support request whose data is already read.

Example of Shopify.Webhooks.Registry.process

async process(
    request: http.IncomingMessage,
    response: http.ServerResponse,
  ): Promise<void> {
    let reqBody = '';

    if (!request.readable) {
      // a logic to handle posted data
    } else {
      const promise: Promise<void> = new Promise((resolve, reject) => {
        request.on('data', (chunk) => {
          reqBody += chunk;
        });
  
        request.on('end', async () => {
          /** a logic to handle posted data **/
        }

...

Area

  • [ ] Add any relevant Area: <area> labels to this issue

Checklist

  • [x] I have described this feature request in a way that is actionable (if possible)

t-jindai avatar May 07 '22 05:05 t-jindai

I did a quick & dirty solution for me to make webhooks working with Firebase Functions. Feel free to use it or do it differently.

https://github.com/FredyC/shopify-node-api/pull/1

danielkcz avatar May 11 '22 13:05 danielkcz

In addition to this, some serverless functions (i.e. Azure Functions) don't use the http IncomingMessage or ServerResponse classes for request and response, and so are unable to call the process method at all. Extracting the core verification etc logic into a separate method to be called so we can use our own request wrappers could help.

xillonz avatar Jun 26 '22 08:06 xillonz

This issue is stale because it has been open for 90 days with no activity. It will be closed if no further action occurs in 14 days.

github-actions[bot] avatar Sep 28 '22 02:09 github-actions[bot]

Would be nice to have a response from a maintainer, because this is still the issue forcing us to use the fork.

danielkcz avatar Oct 10 '22 10:10 danielkcz

As of v6, we are now reading from the request body, so this should be fix. You can find a migration guide from v5 to v6 here.

cquemin avatar Dec 16 '22 14:12 cquemin