From 4decef65225355f71d6e7b08c802d08848fe167c Mon Sep 17 00:00:00 2001 From: Oleksii Orel Date: Tue, 28 Nov 2023 03:38:12 +0200 Subject: [PATCH] fix: yamlResolver route Signed-off-by: Oleksii Orel --- packages/dashboard-backend/package.json | 2 -- .../routes/api/__tests__/yamlResolver.spec.ts | 22 +++++++++++-------- .../src/routes/api/yamlResolver.ts | 19 ++++++++-------- run/local-run.sh | 2 +- yarn.lock | 8 ------- 5 files changed, 24 insertions(+), 29 deletions(-) diff --git a/packages/dashboard-backend/package.json b/packages/dashboard-backend/package.json index b2cad344d7..e33bdf1257 100644 --- a/packages/dashboard-backend/package.json +++ b/packages/dashboard-backend/package.json @@ -44,7 +44,6 @@ "https": "^1.0.0", "js-yaml": "^4.0.0", "multi-ini": "^2.3.2", - "node-fetch": "^2.6.7", "pino": "^8.15.1", "pino-pretty": "^10.2.0", "querystring": "^0.2.1", @@ -57,7 +56,6 @@ "@types/fs-extra": "^11.0.1", "@types/jest": "^29.5.3", "@types/node": "^20.4.9", - "@types/node-fetch": "^2.6.4", "@typescript-eslint/eslint-plugin": "^6.3.0", "@typescript-eslint/parser": "^6.3.0", "copy-webpack-plugin": "^11.0.0", diff --git a/packages/dashboard-backend/src/routes/api/__tests__/yamlResolver.spec.ts b/packages/dashboard-backend/src/routes/api/__tests__/yamlResolver.spec.ts index 1a10079c65..6e4a352949 100644 --- a/packages/dashboard-backend/src/routes/api/__tests__/yamlResolver.spec.ts +++ b/packages/dashboard-backend/src/routes/api/__tests__/yamlResolver.spec.ts @@ -11,16 +11,18 @@ */ import { FastifyInstance } from 'fastify'; -import * as mockNodeFetch from 'node-fetch'; import { baseApiPath } from '@/constants/config'; +import { axiosInstance } from '@/routes/api/helpers/getCertificateAuthority'; import { setup, teardown } from '@/utils/appBuilder'; -const { Response } = jest.requireActual('node-fetch'); - jest.mock('../helpers/getDevWorkspaceClient.ts'); jest.mock('../helpers/getToken.ts'); +jest.mock('@/routes/api/helpers/getCertificateAuthority'); +const getAxiosInstanceMock = jest.fn(); +(axiosInstance.get as jest.Mock).mockImplementation(getAxiosInstanceMock); + describe('Server Config Route', () => { let app: FastifyInstance; const namespace = 'user-che'; @@ -36,9 +38,10 @@ describe('Server Config Route', () => { describe('POST ${baseApiPath}/namespace/:namespace/yaml/resolver', () => { test('file exists', async () => { const devfileContent = 'devfile content'; - (mockNodeFetch.default as unknown as jest.Mock).mockReturnValue( - Promise.resolve(new Response(devfileContent)), - ); + getAxiosInstanceMock.mockResolvedValue({ + status: 200, + data: devfileContent, + }); const res = await app .inject() @@ -51,9 +54,10 @@ describe('Server Config Route', () => { test('file not found', async () => { const responseText = 'not found'; - (mockNodeFetch.default as unknown as jest.Mock).mockResolvedValue( - new Response(responseText, { status: 404 }), - ); + getAxiosInstanceMock.mockResolvedValue({ + status: 404, + data: responseText, + }); const res = await app .inject() diff --git a/packages/dashboard-backend/src/routes/api/yamlResolver.ts b/packages/dashboard-backend/src/routes/api/yamlResolver.ts index 5f24683d88..7737fc1b94 100644 --- a/packages/dashboard-backend/src/routes/api/yamlResolver.ts +++ b/packages/dashboard-backend/src/routes/api/yamlResolver.ts @@ -12,11 +12,11 @@ import { helpers } from '@eclipse-che/common'; import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'; -import fetch from 'node-fetch'; import { baseApiPath } from '@/constants/config'; import { namespacedSchema, yamlResolverSchema } from '@/constants/schemas'; import { restParams } from '@/models'; +import { axiosInstance } from '@/routes/api/helpers/getCertificateAuthority'; import { getDevWorkspaceClient } from '@/routes/api/helpers/getDevWorkspaceClient'; import { getToken } from '@/routes/api/helpers/getToken'; import { getSchema } from '@/services/helpers'; @@ -41,14 +41,15 @@ export function registerYamlResolverRoute(instance: FastifyInstance) { throw new Error(`User permissions error. ${helpers.errors.getMessage(e)}`); } - const response = await fetch(url); - if (response.ok) { - return response.text(); - } else { - reply.code(response.status); - reply.send(response.body); - return reply; - } + const response = await axiosInstance.get(url, { + headers: { + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET', + }, + }); + reply.code(response.status); + reply.send(response.data); + return reply; }, ); }); diff --git a/run/local-run.sh b/run/local-run.sh index ca57123d79..bc733f8642 100755 --- a/run/local-run.sh +++ b/run/local-run.sh @@ -159,4 +159,4 @@ fi # relative path from backend package FRONTEND_RESOURCES=../../../../$DASHBOARD_FRONTEND/lib/public $PRERUN_COMMAND & -yarn --cwd $DASHBOARD_BACKEND start:debug --publicFolder $FRONTEND_RESOURCES +yarn --cwd $DASHBOARD_BACKEND start:debug --publicFolder $FRONTEND_RESOURCES --loglevel fatal diff --git a/yarn.lock b/yarn.lock index 671c031a03..2dba8b4083 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1835,14 +1835,6 @@ resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.4.tgz#81f886786411c45bba3f33e781ab48bd56bfca2e" integrity sha512-Kfe/D3hxHTusnPNRbycJE1N77WHDsdS4AjUYIzlDzhDrS47NrwuL3YW4VITxwR7KCVpzwgy4Rbj829KSSQmwXQ== -"@types/node-fetch@^2.6.4": - version "2.6.8" - resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.8.tgz#9a2993583975849c2e1f360b6ca2f11755b2c504" - integrity sha512-nnH5lV9QCMPsbEVdTb5Y+F3GQxLSw1xQgIydrb2gSfEavRPs50FnMr+KUaa+LoPSqibm2N+ZZxH7lavZlAT4GA== - dependencies: - "@types/node" "*" - form-data "^4.0.0" - "@types/node@*", "@types/node@^20.4.9": version "20.8.10" resolved "https://registry.yarnpkg.com/@types/node/-/node-20.8.10.tgz#a5448b895c753ae929c26ce85cab557c6d4a365e"