use node-fetch

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2024-05-25 11:38:55 +02:00
parent 09c05a10f2
commit 1354d81b86
9 changed files with 44 additions and 32 deletions
+8
View File
@@ -0,0 +1,8 @@
---
'@backstage/plugin-auth-backend-module-cloudflare-access-provider': patch
'@backstage/plugin-scaffolder-backend-module-sentry': patch
'@backstage/plugin-scaffolder-backend-module-gitea': patch
'@backstage/plugin-notifications-node': patch
---
Use `node-fetch` instead of native fetch, as per https://backstage.io/docs/architecture-decisions/adrs-adr013
@@ -23,6 +23,7 @@ import {
} from '@backstage/errors';
import express from 'express';
import { createRemoteJWKSet, jwtVerify } from 'jose';
import fetch, { Headers } from 'node-fetch';
import {
CACHE_PREFIX,
CF_JWT_HEADER,
+1
View File
@@ -37,6 +37,7 @@
"@backstage/plugin-notifications-common": "workspace:^",
"@backstage/plugin-signals-node": "workspace:^",
"knex": "^3.0.0",
"node-fetch": "^2.6.7",
"uuid": "^9.0.0"
},
"devDependencies": {
@@ -24,8 +24,6 @@ import {
} from './DefaultNotificationService';
import { mockCredentials, mockServices } from '@backstage/backend-test-utils';
const server = setupServer();
const testNotification: NotificationPayload = {
title: 'Notification 1',
link: '/catalog',
@@ -33,8 +31,12 @@ const testNotification: NotificationPayload = {
};
describe('DefaultNotificationService', () => {
const server = setupServer();
setupRequestMockHandlers(server);
const discovery = mockServices.discovery();
const discovery = mockServices.discovery.mock({
getBaseUrl: jest.fn().mockResolvedValue('http://example.com'),
});
const auth = mockServices.auth();
let service: DefaultNotificationService;
@@ -53,20 +55,17 @@ describe('DefaultNotificationService', () => {
};
server.use(
rest.post(
`${await discovery.getBaseUrl('notifications')}/`,
async (req, res, ctx) => {
const json = await req.json();
expect(json).toEqual(body);
expect(req.headers.get('Authorization')).toBe(
mockCredentials.service.header({
onBehalfOf: await auth.getOwnServiceCredentials(),
targetPluginId: 'notifications',
}),
);
return res(ctx.status(200));
},
),
rest.post('http://example.com', async (req, res, ctx) => {
const json = await req.json();
expect(json).toEqual(body);
expect(req.headers.get('Authorization')).toBe(
mockCredentials.service.header({
onBehalfOf: await auth.getOwnServiceCredentials(),
targetPluginId: 'notifications',
}),
);
return res(ctx.status(200));
}),
);
await expect(service.send(body)).resolves.toBeUndefined();
});
@@ -78,20 +77,17 @@ describe('DefaultNotificationService', () => {
};
server.use(
rest.post(
`${await discovery.getBaseUrl('notifications')}/`,
async (req, res, ctx) => {
const json = await req.json();
expect(json).toEqual(body);
expect(req.headers.get('Authorization')).toBe(
mockCredentials.service.header({
onBehalfOf: await auth.getOwnServiceCredentials(),
targetPluginId: 'notifications',
}),
);
return res(ctx.status(400));
},
),
rest.post('http://example.com', async (req, res, ctx) => {
const json = await req.json();
expect(json).toEqual(body);
expect(req.headers.get('Authorization')).toBe(
mockCredentials.service.header({
onBehalfOf: await auth.getOwnServiceCredentials(),
targetPluginId: 'notifications',
}),
);
return res(ctx.status(400));
}),
);
await expect(service.send(body)).rejects.toThrow(
'Request failed with status 400',
@@ -17,6 +17,7 @@
import { NotificationService } from './NotificationService';
import { AuthService, DiscoveryService } from '@backstage/backend-plugin-api';
import { NotificationPayload } from '@backstage/plugin-notifications-common';
import fetch from 'node-fetch';
/** @public */
export type NotificationServiceOptions = {
@@ -68,7 +69,7 @@ export class DefaultNotificationService implements NotificationService {
targetPluginId: 'notifications',
});
const response = await fetch(`${baseUrl}/`, {
const response = await fetch(baseUrl, {
method: 'POST',
body: JSON.stringify(notification),
headers: {
@@ -30,6 +30,7 @@ import {
} from '@backstage/plugin-scaffolder-node';
import { examples } from './gitea.examples';
import crypto from 'crypto';
import fetch, { Response, RequestInit } from 'node-fetch';
const checkGiteaContentUrl = async (
config: GiteaIntegrationConfig,
@@ -44,6 +44,7 @@
"@backstage/config": "workspace:^",
"@backstage/errors": "workspace:^",
"@backstage/plugin-scaffolder-node": "workspace:^",
"node-fetch": "^2.6.7",
"yaml": "^2.3.3"
},
"devDependencies": {
@@ -17,6 +17,7 @@
import { createTemplateAction } from '@backstage/plugin-scaffolder-node';
import { InputError } from '@backstage/errors';
import { Config } from '@backstage/config';
import fetch from 'node-fetch';
/**
* Creates the `sentry:project:create` Scaffolder action.
+2
View File
@@ -6259,6 +6259,7 @@ __metadata:
"@backstage/test-utils": "workspace:^"
knex: ^3.0.0
msw: ^1.0.0
node-fetch: ^2.6.7
uuid: ^9.0.0
languageName: unknown
linkType: soft
@@ -6751,6 +6752,7 @@ __metadata:
"@backstage/plugin-scaffolder-node-test-utils": "workspace:^"
"@backstage/types": "workspace:^"
msw: ^2.0.0
node-fetch: ^2.6.7
yaml: ^2.3.3
languageName: unknown
linkType: soft