Skip to content

Commit

Permalink
add api request/response logging to applicant package
Browse files Browse the repository at this point in the history
  • Loading branch information
jgunnCO committed Apr 5, 2024
1 parent 229689d commit c125e75
Show file tree
Hide file tree
Showing 26 changed files with 84 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/applicant/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/applicant/src/pages/api/logout.page.tsx
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
4 changes: 2 additions & 2 deletions packages/applicant/src/pages/api/logout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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', () => () => ({
Expand Down
3 changes: 2 additions & 1 deletion packages/applicant/src/services/ApplicationService.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import getConfig from 'next/config';
import {
Application,
getApplicationById,
getApplicationsListById,
} from './ApplicationService';
import { axios } from '../utils/axios';

jest.mock('next/config', () => () => {
return {
serverRuntimeConfig: {
Expand Down
2 changes: 1 addition & 1 deletion packages/applicant/src/services/ApplicationService.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions packages/applicant/src/services/GrantAdvertService.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import axios from 'axios';
import {
AdvertDto,
checkIfGrantExistsInContentful,
getAdvertBySchemeId,
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 =
Expand Down
2 changes: 1 addition & 1 deletion packages/applicant/src/services/GrantAdvertService.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { GrantApplicantOrganisationProfile } from '../types/models/GrantApplicantOrganisationProfile';
import {
GrantApplicantOrganisationProfileService,
UpdateOrganisationDetailsDto,
} from './GrantApplicantOrganisationProfileService';
import getConfig from 'next/config';
import { axios } from '../utils/axios';

jest.mock('next/config', () => () => {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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', () => () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/applicant/src/services/GrantApplicantService.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/applicant/src/services/GrantBeneficiaryService.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/applicant/src/services/GrantSchemeService.test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/applicant/src/services/GrantSchemeService.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/applicant/src/services/IsAdminService.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/applicant/src/services/SubmissionService.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/applicant/src/services/SubmissionService.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/applicant/src/services/UserRolesService.tsx
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
32 changes: 32 additions & 0 deletions packages/applicant/src/utils/axios.ts
Original file line number Diff line number Diff line change
@@ -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 };
25 changes: 23 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c125e75

Please sign in to comment.