PagerDuty / backstage-plugin-backend

PagerDuty plugin for Backstage - Backend
https://pagerduty.github.io/backstage-plugin-docs/index.html
Apache License 2.0
3 stars 6 forks source link

fix: replace fetch with node-fetch to comply with ADR013 #38

Closed pcgqueiroz closed 5 months ago

pcgqueiroz commented 5 months ago

Description

This PR replaces global fetch with node-fetch library as recommended by Backstage on ADR013.

This standardizes on the HTTP package used by the backend plugin and solves issue #37 that prevents HTTP calls to succeed behind a proxy.

Issue number: 37

Type of change

Checklist

If this is a breaking change 👇

Acknowledgement

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

t1agob commented 5 months ago

This is failing due to type check validations as response.json() returns type unknown. I'm working on fixing these and will contribute with the necessary changes to make it work.

t1agob commented 5 months ago

@pcgqueiroz this is now passing all tests and validations. Can you check if this works from behind the proxy? If it makes it easier for you I have published the changes as version 0.4.6-next.0.

t1agob commented 5 months ago

@pcgqueiroz let me know once you have time to test this and if it works or not. Thanks!

pcgqueiroz commented 5 months ago

@t1agob , I have tested with the package version 0.4.6-next.0 and it did not work. I have the following error:

/backstage/node_modules/@pagerduty/backstage-plugin-backend/dist/index.cjs.js:8
var fetch$1 = require('node-fetch');
              ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /backstage/node_modules/@pagerduty/backstage-plugin-backend/node_modules/node-fetch/src/index.js from /backstage/node_modules/@pagerduty/backstage-plugin-backend/dist/index.cjs.js not supported.
Instead change the require of index.js in /backstage/node_modules/@pagerduty/backstage-plugin-backend/dist/index.cjs.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/backstage/node_modules/@pagerduty/backstage-plugin-backend/dist/index.cjs.js:8:15)
    at @pagerduty/backstage-plugin-backend (/backstage/packages/backend/dist/main.js:577:18)
    at __webpack_require__ (/backstage/packages/backend/dist/main.js:1090:33)
    at fn (/backstage/packages/backend/dist/main.js:1301:21)
    at eval (webpack-internal:///./src/plugins/pagerduty.ts:5:93)
    at ./src/plugins/pagerduty.ts (/backstage/packages/backend/dist/src_plugins_pagerduty_ts.chunk.js:20:1)
    at __webpack_require__ (/backstage/packages/backend/dist/main.js:1090:33)
    at Function.fn (/backstage/packages/backend/dist/main.js:1301:21) {
  code: 'ERR_REQUIRE_ESM'
}

I suspect the error above is related to the node-fetch version 3.3.0. So I have used the source code of the branch and tested with the previous node-fetch version 2.6.7 and everything worked as expected :-)

t1agob commented 5 months ago

@pcgqueiroz Oh...got it! This is due to the lack of support for ESM for backend modules in Backstage. node-fetch starting on version 3.x is ESM only so we need to downgrade to version 2.x like you suggested.

Can you try it with version 0.4.6-next.2? Thanks!

pcgqueiroz commented 5 months ago

Hey @t1agob ! Version 0.4.6-next.2 worked out of the box! Thanks for that, I think it's fine to be merged.