diff --git a/packages/applicant/package.json b/packages/applicant/package.json index 63a2857fb..a12236dfe 100644 --- a/packages/applicant/package.json +++ b/packages/applicant/package.json @@ -16,7 +16,7 @@ "@aws-crypto/client-node": "3.2.0", "@contentful/rich-text-from-markdown": "15.16.6", "@contentful/rich-text-react-renderer": "^15.11.1", - "axios": "^0.27.2", + "axios": "1.6.8", "body-parser": "1.20.2", "cookie": "^0.5.0", "cookie-parser": "^1.4.6", diff --git a/packages/applicant/src/pages/api/logout.page.tsx b/packages/applicant/src/pages/api/logout.page.tsx index 432965fff..9d74485a9 100644 --- a/packages/applicant/src/pages/api/logout.page.tsx +++ b/packages/applicant/src/pages/api/logout.page.tsx @@ -1,7 +1,7 @@ import type { NextApiRequest, NextApiResponse } from 'next'; import { getSessionIdFromCookies } from '../../../src/utils/session'; -import axios from 'axios'; import { APIGlobalHandler } from '../../utils/apiErrorHandler'; +import { axios } from '../../utils/axios'; const Logout = async (req: NextApiRequest, res: NextApiResponse) => { const sessionCookie = getSessionIdFromCookies(req); diff --git a/packages/applicant/src/pages/api/logout.test.ts b/packages/applicant/src/pages/api/logout.test.ts index a817b7aec..f3eade657 100644 --- a/packages/applicant/src/pages/api/logout.test.ts +++ b/packages/applicant/src/pages/api/logout.test.ts @@ -3,10 +3,10 @@ import { merge } from 'lodash'; import { getSessionIdFromCookies } from '../../utils/session'; import logout from './logout.page'; import { NextApiRequest, NextApiResponse } from 'next'; -import axios from 'axios'; +import { axios } from '../../utils/axios'; jest.mock('../../utils/session'); -jest.mock('axios'); +jest.mock('../../utils/axios'); const mockedRedirect = jest.fn(); const mockedSetHeader = jest.fn(); diff --git a/packages/applicant/src/pages/api/routes/submissions/[submissionId]/download-summary.test.tsx b/packages/applicant/src/pages/api/routes/submissions/[submissionId]/download-summary.test.tsx index 74c9eb55f..492f176b6 100644 --- a/packages/applicant/src/pages/api/routes/submissions/[submissionId]/download-summary.test.tsx +++ b/packages/applicant/src/pages/api/routes/submissions/[submissionId]/download-summary.test.tsx @@ -1,8 +1,8 @@ import { NextApiRequest, NextApiResponse } from 'next'; -import axios from 'axios'; +import { axios } from '../../../../../utils/axios'; import handler from './download-summary.page'; -jest.mock('axios'); +jest.mock('../../../../../utils/axios'); jest.mock('../../../../../middleware.page'); jest.mock('../../../../../utils/jwt'); jest.mock('next/config', () => () => ({ diff --git a/packages/applicant/src/services/ApplicationService.test.tsx b/packages/applicant/src/services/ApplicationService.test.tsx index c05efef73..bee1a6213 100644 --- a/packages/applicant/src/services/ApplicationService.test.tsx +++ b/packages/applicant/src/services/ApplicationService.test.tsx @@ -1,4 +1,3 @@ -import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; import getConfig from 'next/config'; import { @@ -6,6 +5,8 @@ import { getApplicationById, getApplicationsListById, } from './ApplicationService'; +import { axios } from '../utils/axios'; + jest.mock('next/config', () => () => { return { serverRuntimeConfig: { diff --git a/packages/applicant/src/services/ApplicationService.tsx b/packages/applicant/src/services/ApplicationService.tsx index 714cfc179..143382780 100644 --- a/packages/applicant/src/services/ApplicationService.tsx +++ b/packages/applicant/src/services/ApplicationService.tsx @@ -1,4 +1,4 @@ -import axios from 'axios'; +import { axios } from '../utils/axios'; import getConfig from 'next/config'; import { axiosConfig } from '../utils/jwt'; import { GrantApplication } from '../types/models/GrantApplication'; diff --git a/packages/applicant/src/services/FundingOrganisationService.test.ts b/packages/applicant/src/services/FundingOrganisationService.test.ts index 8e1eb4352..8f25bd678 100644 --- a/packages/applicant/src/services/FundingOrganisationService.test.ts +++ b/packages/applicant/src/services/FundingOrganisationService.test.ts @@ -1,8 +1,8 @@ -import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; import { FundingOrganisation } from '../types/models/FundingOrganisation'; import { FundingOrganisationService } from './FundingOrganisationService'; import getConfig from 'next/config'; +import { axios } from '../utils/axios'; jest.mock('next/config', () => () => { return { diff --git a/packages/applicant/src/services/FundingOrganisationService.ts b/packages/applicant/src/services/FundingOrganisationService.ts index 67c2cfc8f..bee439dad 100644 --- a/packages/applicant/src/services/FundingOrganisationService.ts +++ b/packages/applicant/src/services/FundingOrganisationService.ts @@ -1,7 +1,7 @@ -import axios from 'axios'; import { FundingOrganisation } from '../types/models/FundingOrganisation'; import { axiosConfig } from '../utils/jwt'; import getConfig from 'next/config'; +import { axios } from '../utils/axios'; export class FundingOrganisationService { private static instance: FundingOrganisationService; diff --git a/packages/applicant/src/services/GrantAdvertService.test.ts b/packages/applicant/src/services/GrantAdvertService.test.ts index f8443b7c1..050190f35 100644 --- a/packages/applicant/src/services/GrantAdvertService.test.ts +++ b/packages/applicant/src/services/GrantAdvertService.test.ts @@ -1,4 +1,3 @@ -import axios from 'axios'; import { AdvertDto, checkIfGrantExistsInContentful, @@ -6,8 +5,9 @@ import { getAdvertBySlug, GrantExistsInContentfulDto, } from './GrantAdvertService'; +import { axios } from '../utils/axios'; -jest.mock('axios'); +jest.mock('../utils/axios'); process.env.BACKEND_HOST = 'http://localhost:8080'; const GRANT_ADVERT_BACKEND_BASE_URL = diff --git a/packages/applicant/src/services/GrantAdvertService.tsx b/packages/applicant/src/services/GrantAdvertService.tsx index 1302d8ce1..4ffaceaeb 100644 --- a/packages/applicant/src/services/GrantAdvertService.tsx +++ b/packages/applicant/src/services/GrantAdvertService.tsx @@ -1,7 +1,7 @@ -import axios from 'axios'; import getConfig from 'next/config'; import { axiosConfig } from '../utils/jwt'; import { GrantAdvert } from '../types/models/GrantAdvert'; +import { axios } from '../utils/axios'; const { serverRuntimeConfig } = getConfig(); const BACKEND_HOST = serverRuntimeConfig.backendHost; diff --git a/packages/applicant/src/services/GrantApplicantOrganisationProfileService.test.ts b/packages/applicant/src/services/GrantApplicantOrganisationProfileService.test.ts index 2dc6888af..5e296fa1e 100644 --- a/packages/applicant/src/services/GrantApplicantOrganisationProfileService.test.ts +++ b/packages/applicant/src/services/GrantApplicantOrganisationProfileService.test.ts @@ -1,4 +1,3 @@ -import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; import { GrantApplicantOrganisationProfile } from '../types/models/GrantApplicantOrganisationProfile'; import { @@ -6,6 +5,7 @@ import { UpdateOrganisationDetailsDto, } from './GrantApplicantOrganisationProfileService'; import getConfig from 'next/config'; +import { axios } from '../utils/axios'; jest.mock('next/config', () => () => { return { diff --git a/packages/applicant/src/services/GrantApplicantOrganisationProfileService.tsx b/packages/applicant/src/services/GrantApplicantOrganisationProfileService.tsx index 178845c2b..057ab4926 100644 --- a/packages/applicant/src/services/GrantApplicantOrganisationProfileService.tsx +++ b/packages/applicant/src/services/GrantApplicantOrganisationProfileService.tsx @@ -1,7 +1,7 @@ -import axios from 'axios'; import { GrantApplicantOrganisationProfile } from '../types/models/GrantApplicantOrganisationProfile'; import { axiosConfig } from '../utils/jwt'; import getConfig from 'next/config'; +import { axios } from '../utils/axios'; export interface UpdateOrganisationDetailsDto { id?: string; diff --git a/packages/applicant/src/services/GrantApplicantService.test.ts b/packages/applicant/src/services/GrantApplicantService.test.ts index 4069851b3..4328c56b9 100644 --- a/packages/applicant/src/services/GrantApplicantService.test.ts +++ b/packages/applicant/src/services/GrantApplicantService.test.ts @@ -1,9 +1,9 @@ -import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; import { GrantApplicant } from '../types/models/GrantApplicant'; import { axiosConfig } from '../utils/jwt'; import { GrantApplicantService } from './GrantApplicantService'; import getConfig from 'next/config'; +import { axios } from '../utils/axios'; jest.mock('../utils/jwt'); jest.mock('next/config', () => () => { diff --git a/packages/applicant/src/services/GrantApplicantService.ts b/packages/applicant/src/services/GrantApplicantService.ts index 3858ad184..c027c5b67 100644 --- a/packages/applicant/src/services/GrantApplicantService.ts +++ b/packages/applicant/src/services/GrantApplicantService.ts @@ -1,7 +1,7 @@ -import axios from 'axios'; import { GrantApplicant } from '../types/models/GrantApplicant'; import { axiosConfig } from '../utils/jwt'; import getConfig from 'next/config'; +import { axios } from '../utils/axios'; export interface RegisterAnApplicant { email: string; diff --git a/packages/applicant/src/services/GrantBeneficiaryService.test.ts b/packages/applicant/src/services/GrantBeneficiaryService.test.ts index b4b3c2ff7..9968c2af4 100644 --- a/packages/applicant/src/services/GrantBeneficiaryService.test.ts +++ b/packages/applicant/src/services/GrantBeneficiaryService.test.ts @@ -1,11 +1,11 @@ -import axios from 'axios'; import { GrantBeneficiary } from '../types/models/GrantBeneficiary'; import { getGrantBeneficiary, postGrantBeneficiaryResponse, } from './GrantBeneficiaryService'; +import { axios } from '../utils/axios'; -jest.mock('axios'); +jest.mock('../utils/axios'); const BACKEND_HOST = process.env.BACKEND_HOST + '/equality-and-diversity'; const GRANT_BENEFICIARY_ID = 'testGrantBeneficiaryId'; diff --git a/packages/applicant/src/services/GrantBeneficiaryService.ts b/packages/applicant/src/services/GrantBeneficiaryService.ts index 3d38cdc38..ed1c2da5f 100644 --- a/packages/applicant/src/services/GrantBeneficiaryService.ts +++ b/packages/applicant/src/services/GrantBeneficiaryService.ts @@ -1,6 +1,6 @@ -import axios from 'axios'; import { GrantBeneficiary } from '../types/models/GrantBeneficiary'; import { axiosConfig } from '../utils/jwt'; +import { axios } from '../utils/axios'; const BASE_URL = process.env.BACKEND_HOST + '/equality-and-diversity'; diff --git a/packages/applicant/src/services/GrantMandatoryQuestionService.test.tsx b/packages/applicant/src/services/GrantMandatoryQuestionService.test.tsx index bc8c6b311..f032ccb6b 100644 --- a/packages/applicant/src/services/GrantMandatoryQuestionService.test.tsx +++ b/packages/applicant/src/services/GrantMandatoryQuestionService.test.tsx @@ -1,7 +1,6 @@ -import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; - import getConfig from 'next/config'; +import { axios } from '../utils/axios'; import { GrantMandatoryQuestionDto, GrantMandatoryQuestionService, diff --git a/packages/applicant/src/services/GrantMandatoryQuestionService.ts b/packages/applicant/src/services/GrantMandatoryQuestionService.ts index 51d00f3f8..e4e99ce17 100644 --- a/packages/applicant/src/services/GrantMandatoryQuestionService.ts +++ b/packages/applicant/src/services/GrantMandatoryQuestionService.ts @@ -1,6 +1,6 @@ -import axios from 'axios'; import getConfig from 'next/config'; import { axiosConfig } from '../utils/jwt'; +import { axios } from '../utils/axios'; export class GrantMandatoryQuestionService { private static instance: GrantMandatoryQuestionService; diff --git a/packages/applicant/src/services/GrantSchemeService.test.ts b/packages/applicant/src/services/GrantSchemeService.test.ts index 7e0d007d3..7c478232c 100644 --- a/packages/applicant/src/services/GrantSchemeService.test.ts +++ b/packages/applicant/src/services/GrantSchemeService.test.ts @@ -1,8 +1,8 @@ -import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; import getConfig from 'next/config'; import { GrantScheme } from '../types/models/GrantScheme'; import { GrantSchemeService } from './GrantSchemeService'; +import { axios } from '../utils/axios'; jest.mock('next/config', () => () => { return { diff --git a/packages/applicant/src/services/GrantSchemeService.ts b/packages/applicant/src/services/GrantSchemeService.ts index 32e4db874..03c7d2c10 100644 --- a/packages/applicant/src/services/GrantSchemeService.ts +++ b/packages/applicant/src/services/GrantSchemeService.ts @@ -1,9 +1,9 @@ -import axios from 'axios'; import { GrantScheme } from '../types/models/GrantScheme'; import { axiosConfig } from '../utils/jwt'; import getConfig from 'next/config'; import { GrantApplication } from '../types/models/GrantApplication'; import { GrantAdvert } from '../types/models/GrantAdvert'; +import { axios } from '../utils/axios'; export class GrantSchemeService { private static instance: GrantSchemeService; diff --git a/packages/applicant/src/services/IsAdminService.tsx b/packages/applicant/src/services/IsAdminService.tsx index 462b56b90..cd31f58cb 100644 --- a/packages/applicant/src/services/IsAdminService.tsx +++ b/packages/applicant/src/services/IsAdminService.tsx @@ -1,7 +1,7 @@ -import axios from 'axios'; import { axiosConfig } from '../utils/jwt'; import getConfig from 'next/config'; import { UserRolesResponse } from './UserRolesService'; +import { axios } from '../utils/axios'; const { serverRuntimeConfig } = getConfig(); const BACKEND_HOST = serverRuntimeConfig.backendHost; diff --git a/packages/applicant/src/services/SubmissionService.test.ts b/packages/applicant/src/services/SubmissionService.test.ts index 1a9cb6cca..77094389b 100644 --- a/packages/applicant/src/services/SubmissionService.test.ts +++ b/packages/applicant/src/services/SubmissionService.test.ts @@ -1,6 +1,6 @@ -import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; import getConfig from 'next/config'; +import { axios } from '../utils/axios'; import { axiosConfig } from '../utils/jwt'; import { NextNavigation, diff --git a/packages/applicant/src/services/SubmissionService.tsx b/packages/applicant/src/services/SubmissionService.tsx index 9cb194c4d..3b47e798d 100644 --- a/packages/applicant/src/services/SubmissionService.tsx +++ b/packages/applicant/src/services/SubmissionService.tsx @@ -1,9 +1,9 @@ -import axios from 'axios'; import FormData from 'form-data'; import { ValidationError } from 'gap-web-ui'; import getConfig from 'next/config'; import { MAX_FILE_UPLOAD_SIZE_BYTES } from '../utils/constants'; import { axiosConfig } from '../utils/jwt'; +import { axios } from '../utils/axios'; const { serverRuntimeConfig } = getConfig(); const BACKEND_HOST = serverRuntimeConfig.backendHost; diff --git a/packages/applicant/src/services/UserRolesService.tsx b/packages/applicant/src/services/UserRolesService.tsx index e89e3c034..644077a66 100644 --- a/packages/applicant/src/services/UserRolesService.tsx +++ b/packages/applicant/src/services/UserRolesService.tsx @@ -1,6 +1,6 @@ -import axios from 'axios'; import { axiosUserServiceConfig } from '../utils/jwt'; import getConfig from 'next/config'; +import { axios } from '../utils/axios'; const { serverRuntimeConfig } = getConfig(); diff --git a/packages/applicant/src/utils/axios.ts b/packages/applicant/src/utils/axios.ts new file mode 100644 index 000000000..bf86beb39 --- /dev/null +++ b/packages/applicant/src/utils/axios.ts @@ -0,0 +1,32 @@ +import axios, { + AxiosInstance, + InternalAxiosRequestConfig, + AxiosResponse, + AxiosStatic, +} from 'axios'; +import { v4 } from 'uuid'; +import { logger } from './logger'; +import { HEADERS } from './constants'; + +const requestInterceptor = (request: InternalAxiosRequestConfig) => { + request.headers[HEADERS.CORRELATION_ID] = v4(); + logger.http('Outgoing request', request); + return request; +}; + +const responseInterceptor = (response: AxiosResponse) => { + const { request: _, ...propertiesToLog } = response; + logger.http('Incoming response', propertiesToLog); + return response; +}; + +const configureAxios = (axios: AxiosStatic | AxiosInstance) => { + axios.interceptors.request.use(requestInterceptor); + axios.interceptors.response.use(responseInterceptor); +}; + +const client = axios.create(); +configureAxios(axios); +configureAxios(client); + +export { axios, client }; diff --git a/yarn.lock b/yarn.lock index 257e1b92f..0d514bab9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6518,7 +6518,7 @@ __metadata: "@testing-library/jest-dom": ^6.1.4 "@types/cookie": ^0.5.1 "@types/cookie-parser": ^1.4.3 - axios: ^0.27.2 + axios: 1.6.8 axios-mock-adapter: ^1.21.1 body-parser: 1.20.2 contentful-typescript-codegen: ^3.2.3 @@ -6885,6 +6885,17 @@ __metadata: languageName: node linkType: hard +"axios@npm:1.6.8": + version: 1.6.8 + resolution: "axios@npm:1.6.8" + dependencies: + follow-redirects: ^1.15.6 + form-data: ^4.0.0 + proxy-from-env: ^1.1.0 + checksum: bf007fa4b207d102459300698620b3b0873503c6d47bf5a8f6e43c0c64c90035a4f698b55027ca1958f61ab43723df2781c38a99711848d232cad7accbcdfcdd + languageName: node + linkType: hard + "axios@npm:^0.27.2": version: 0.27.2 resolution: "axios@npm:0.27.2" @@ -10283,6 +10294,16 @@ __metadata: languageName: node linkType: hard +"follow-redirects@npm:^1.15.6": + version: 1.15.6 + resolution: "follow-redirects@npm:1.15.6" + peerDependenciesMeta: + debug: + optional: true + checksum: a62c378dfc8c00f60b9c80cab158ba54e99ba0239a5dd7c81245e5a5b39d10f0c35e249c3379eae719ff0285fff88c365dd446fab19dee771f1d76252df1bbf5 + languageName: node + linkType: hard + "for-each@npm:^0.3.3": version: 0.3.3 resolution: "for-each@npm:0.3.3" @@ -15823,7 +15844,7 @@ __metadata: languageName: node linkType: hard -"proxy-from-env@npm:^1.0.0": +"proxy-from-env@npm:^1.0.0, proxy-from-env@npm:^1.1.0": version: 1.1.0 resolution: "proxy-from-env@npm:1.1.0" checksum: ed7fcc2ba0a33404958e34d95d18638249a68c430e30fcb6c478497d72739ba64ce9810a24f53a7d921d0c065e5b78e3822759800698167256b04659366ca4d4