aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.43k stars 2.13k forks source link

`FileReader` error with Predictions on NextJS API routes. #8221

Open ianwijma opened 3 years ago

ianwijma commented 3 years ago

Before opening, please confirm:

JavaScript Framework

React, Next.js

Amplify APIs

Predictions

Amplify Categories

predictions

Environment information

``` System: OS: macOS 11.3.1 CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz Memory: 268.66 MB / 16.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 14.16.0 - ~/.nvm/versions/node/v14.16.0/bin/node Yarn: 1.22.10 - ~/.nvm/versions/node/v14.16.0/bin/yarn npm: 6.14.11 - ~/.nvm/versions/node/v14.16.0/bin/npm Browsers: Brave Browser: 89.1.22.71 Chrome: 90.0.4430.93 Edge: 90.0.818.51 Firefox: 87.0 Firefox Developer Edition: 88.0 Safari: 14.1 npmGlobalPackages: @aws-amplify/cli: 4.50.0 localtunnel: 2.0.1 npm: 6.14.11 serverless: 2.35.0 to: 0.2.9 update: 0.7.4 yarn: 1.22.10 ```

Describe the bug

Using prediction within the NextJS API routes (which run on NodeJS) throws the following error FileReader is not defined

Expected behavior

Although I'm fully aware NextJS API routes are not supported, I think it would be a great addition.

Reproduction steps

Setup a simple NextJS environment with Amplify:

  1. Install NextJS
  2. Initialize Amplify
  3. Initialize Predictions
  4. Create a NextJS API route that uses predictions
  5. See error

Code Snippet

const response = await fetch(url)
const file = await response.blob()

const { text } = await Amplify.Predictions.identify({
  text: {
    source: {
      file,
    },
    format: 'PLAIN',
  },
})

Log output

``` (node:45356) UnhandledPromiseRejectionWarning: ReferenceError: FileReader is not defined at /path/to/project/node_modules/@aws-amplify/predictions/lib/Providers/Utils.js:37:22 at new Promise () at Object.blobToArrayBuffer (/path/to/project/node_modules/@aws-amplify/predictions/lib/Providers/Utils.js:36:12) at /path/to/project/node_modules/@aws-amplify/predictions/lib/Providers/AmazonAIIdentifyPredictionsProvider.js:112:25 at new Promise () at AmazonAIIdentifyPredictionsProvider.configureSource (/path/to/project/node_modules/@aws-amplify/predictions/lib/Providers/AmazonAIIdentifyPredictionsProvider.js:90:16) at AmazonAIIdentifyPredictionsProvider. (/path/to/project/node_modules/@aws-amplify/predictions/lib/Providers/AmazonAIIdentifyPredictionsProvider.js:168:51) at step (/path/to/project/node_modules/@aws-amplify/predictions/lib/Providers/AmazonAIIdentifyPredictionsProvider.js:57:23) at Object.next (/path/to/project/node_modules/@aws-amplify/predictions/lib/Providers/AmazonAIIdentifyPredictionsProvider.js:38:53) at fulfilled (/path/to/project/node_modules/@aws-amplify/predictions/lib/Providers/AmazonAIIdentifyPredictionsProvider.js:29:58) at runMicrotasks () at processTicksAndRejections (internal/process/task_queues.js:93:5) (Use `node --trace-warnings ...` to show where the warning was created) (node:45356) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 9) (node:45356) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

I wasn't sure if this was a bug report of a feature request, so my bad it it needed to be a feature request!

chrisbonifacio commented 3 years ago

Hey, @ianwijma đź‘‹ I can label this as a feature request for you. The team is continuously working on building support for Next.js so this is definitely something worth considering. Thank you!