Merge pull request #859 from Safelite/feature/SSR-1464
SSR-1464 - adding headers to API calls
This commit is contained in:
commit
82249afcce
3 changed files with 50 additions and 39 deletions
|
|
@ -1,5 +1,10 @@
|
||||||
const headerKeys = Object.freeze({
|
const headerKeys = Object.freeze({
|
||||||
EXPERIMENT: 'X-Experiment-Data'
|
EXPERIMENT: 'X-Experiment-Data',
|
||||||
|
TRANSACTION_ID: 'X-Transaction-Id',
|
||||||
|
ENTERPRISE_ORDER_NUMBER: 'X-Enterprise-Order-Number',
|
||||||
|
APPLICATION_NAME: 'X-Application-Name',
|
||||||
|
REFERRAL_SEQUENCE_NUMBER: 'X-Referral-Sequence-Number',
|
||||||
|
SESSION_SEQUENCE_NUMBER: 'X-Session-Sequence-Number'
|
||||||
});
|
});
|
||||||
|
|
||||||
export default headerKeys;
|
export default headerKeys;
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,24 @@
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import analyticsMixIn from '@/mixins/analytics-mixin.js';
|
import analyticsMixIn from '@/mixins/analytics-mixin.js';
|
||||||
import { useMainStore } from '@/store';
|
import { useMainStore } from '@/store';
|
||||||
|
|
||||||
import applicationConfig from '@/constants/application-config.js';
|
import applicationConfig from '@/constants/application-config.js';
|
||||||
import { GaCategories, GaActions, GaLabels } from '@/constants/analytics';
|
import { GaCategories, GaActions, GaLabels } from '@/constants/analytics';
|
||||||
import headerKeys from '@/constants/header-keys';
|
import headerKeys from '@/constants/header-keys';
|
||||||
import axiosResponseInterceptorMessages from "@/constants/axios-response-interceptor-messages.js";
|
import axiosResponseInterceptorMessages from '@/constants/axios-response-interceptor-messages.js';
|
||||||
|
import { getSessionKeyValue } from '@/helpers/cookie-helper';
|
||||||
|
|
||||||
// Add a response interceptor for global axios error handing.
|
// Add a response interceptor for global axios error handing.
|
||||||
axios.interceptors.response.use(
|
axios.interceptors.response.use(
|
||||||
(response) => response,
|
(response) => response,
|
||||||
(error) => {
|
(error) => {
|
||||||
let rejectionError = "";
|
let rejectionError = '';
|
||||||
if (typeof error.response === "undefined") {
|
if (typeof error.response === 'undefined') {
|
||||||
// The request was not made, could be a bad url, bad connection or a CORS error.
|
// The request was not made, could be a bad url, bad connection or a CORS error.
|
||||||
rejectionError = {
|
rejectionError = {
|
||||||
message:
|
message:
|
||||||
"A network error occurred. " +
|
'A network error occurred. '
|
||||||
"This could be a CORS issue or a dropped internet connection. " +
|
+ 'This could be a CORS issue or a dropped internet connection. '
|
||||||
"It is impossible for us to know.",
|
+ 'It is impossible for us to know.',
|
||||||
cause: error,
|
cause: error,
|
||||||
response: error,
|
response: error,
|
||||||
message: axiosResponseInterceptorMessages.NETWORK_ERROR
|
message: axiosResponseInterceptorMessages.NETWORK_ERROR
|
||||||
|
|
@ -28,7 +28,7 @@ axios.interceptors.response.use(
|
||||||
// that falls out of the range of 2xx
|
// that falls out of the range of 2xx
|
||||||
rejectionError = {
|
rejectionError = {
|
||||||
response: error.response,
|
response: error.response,
|
||||||
message: axiosResponseInterceptorMessages.STATUS_CODE_ERROR,
|
message: axiosResponseInterceptorMessages.STATUS_CODE_ERROR
|
||||||
};
|
};
|
||||||
} else if (error.request) {
|
} else if (error.request) {
|
||||||
// The request was made but no response was received
|
// The request was made but no response was received
|
||||||
|
|
@ -36,13 +36,13 @@ axios.interceptors.response.use(
|
||||||
// http.ClientRequest in node.js
|
// http.ClientRequest in node.js
|
||||||
rejectionError = {
|
rejectionError = {
|
||||||
response: error.request,
|
response: error.request,
|
||||||
message: axiosResponseInterceptorMessages.NO_RESPONSE_ERROR,
|
message: axiosResponseInterceptorMessages.NO_RESPONSE_ERROR
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
// Something happened in setting up the request that triggered an Error
|
// Something happened in setting up the request that triggered an Error
|
||||||
rejectionError = {
|
rejectionError = {
|
||||||
response: error.message,
|
response: error.message,
|
||||||
message: axiosResponseInterceptorMessages.GENERIC_ERROR,
|
message: axiosResponseInterceptorMessages.GENERIC_ERROR
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,8 +56,14 @@ export default {
|
||||||
const store = useMainStore();
|
const store = useMainStore();
|
||||||
const cfDistroUrl = applicationConfig.CONSUMER_CF_DISTRO;
|
const cfDistroUrl = applicationConfig.CONSUMER_CF_DISTRO;
|
||||||
const payloadAndAnalyticsData = { ...payload, AppName: 'ISS' };
|
const payloadAndAnalyticsData = { ...payload, AppName: 'ISS' };
|
||||||
|
const sessionKey = getSessionKeyValue();
|
||||||
const headers = {
|
const headers = {
|
||||||
[headerKeys.EXPERIMENT]: JSON.stringify(store.experimentSettings)
|
[headerKeys.EXPERIMENT]: JSON.stringify(store.experimentSettings),
|
||||||
|
[headerKeys.APPLICATION_NAME]: applicationConfig.APPLICATION_NAME,
|
||||||
|
[headerKeys.TRANSACTION_ID]: store.order.referralCorrelationId,
|
||||||
|
[headerKeys.ENTERPRISE_ORDER_NUMBER]: store.order.eon,
|
||||||
|
[headerKeys.REFERRAL_SEQUENCE_NUMBER]: store.order.referralSequenceNumber,
|
||||||
|
[headerKeys.SESSION_SEQUENCE_NUMBER]: sessionKey
|
||||||
};
|
};
|
||||||
|
|
||||||
axios({
|
axios({
|
||||||
|
|
@ -89,15 +95,13 @@ export default {
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error.response.status != "404") {
|
if (error.response.status != '404') {
|
||||||
|
global.$logger.logError(
|
||||||
|
`${method}: ${endpoint}: ${error.message}`,
|
||||||
global.$logger.logError(
|
error.response
|
||||||
`${method}: ${endpoint}: ${error.message}`,
|
);
|
||||||
error.response
|
}
|
||||||
);
|
|
||||||
}
|
|
||||||
return reject(error.response);
|
return reject(error.response);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
import applicationConfig from "@/constants/application-config.js";
|
import applicationConfig from '@/constants/application-config.js';
|
||||||
import axios from "axios";
|
import axios from 'axios';
|
||||||
import { useMainStore } from "@/store";
|
import { useMainStore } from '@/store';
|
||||||
import loggingEndpointMethods from "@/constants/logging-endpoint-methods";
|
import loggingEndpointMethods from '@/constants/logging-endpoint-methods';
|
||||||
|
import headerKeys from '@/constants/header-keys';
|
||||||
import headerKeys from "@/constants/header-keys";
|
import { getSessionKeyValue } from '@/helpers/cookie-helper';
|
||||||
|
|
||||||
export class Logger {
|
export class Logger {
|
||||||
logInformation(message, details) {
|
logInformation(message, details) {
|
||||||
|
|
@ -33,19 +33,20 @@ export class Logger {
|
||||||
|
|
||||||
formatLogEntry(message, details) {
|
formatLogEntry(message, details) {
|
||||||
return `Application: ${applicationConfig.APPLICATION_NAME}\n${message}\n${
|
return `Application: ${applicationConfig.APPLICATION_NAME}\n${message}\n${
|
||||||
details ? JSON.stringify(details, undefined, 2) : ""
|
details ? JSON.stringify(details, undefined, 2) : ''
|
||||||
}`;
|
}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
writeLogEntry(endpoint, logEntry) {
|
writeLogEntry(endpoint, logEntry) {
|
||||||
const store = useMainStore();
|
const store = useMainStore();
|
||||||
|
const sessionKey = getSessionKeyValue();
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// If running locally or in the Dev environment show the log entries in the console.
|
// If running locally or in the Dev environment show the log entries in the console.
|
||||||
if (
|
if (
|
||||||
applicationConfig.CURRENT_ENVIRONMENT === "Localhost" ||
|
applicationConfig.CURRENT_ENVIRONMENT === 'Localhost'
|
||||||
applicationConfig.CURRENT_ENVIRONMENT === "Dev" ||
|
|| applicationConfig.CURRENT_ENVIRONMENT === 'Dev'
|
||||||
applicationConfig.CURRENT_ENVIRONMENT === "SysTest"
|
|| applicationConfig.CURRENT_ENVIRONMENT === 'SysTest'
|
||||||
) {
|
) {
|
||||||
switch (endpoint) {
|
switch (endpoint) {
|
||||||
case loggingEndpointMethods.LOG_INFORMATION:
|
case loggingEndpointMethods.LOG_INFORMATION:
|
||||||
|
|
@ -69,22 +70,23 @@ export class Logger {
|
||||||
applicationConfig.CONSUMER_CF_DISTRO + applicationConfig.FRONTEND_LOGGER_PATH;
|
applicationConfig.CONSUMER_CF_DISTRO + applicationConfig.FRONTEND_LOGGER_PATH;
|
||||||
const headers = {
|
const headers = {
|
||||||
[headerKeys.EXPERIMENT]: JSON.stringify(store.experimentSettings),
|
[headerKeys.EXPERIMENT]: JSON.stringify(store.experimentSettings),
|
||||||
|
[headerKeys.APPLICATION_NAME]: applicationConfig.APPLICATION_NAME,
|
||||||
|
[headerKeys.TRANSACTION_ID]: store.order.referralCorrelationId,
|
||||||
|
[headerKeys.ENTERPRISE_ORDER_NUMBER]: store.order.eon,
|
||||||
|
[headerKeys.REFERRAL_SEQUENCE_NUMBER]: store.order.referralSequenceNumber,
|
||||||
|
[headerKeys.SESSION_SEQUENCE_NUMBER]: sessionKey
|
||||||
};
|
};
|
||||||
|
|
||||||
axios({
|
axios({
|
||||||
method: "POST",
|
method: 'POST',
|
||||||
url: `${url}/${endpoint}`,
|
url: `${url}/${endpoint}`,
|
||||||
data: { entry: logEntry },
|
data: { entry: logEntry },
|
||||||
crossDomain: true,
|
crossDomain: true,
|
||||||
responseType: {},
|
responseType: {},
|
||||||
headers: headers,
|
headers
|
||||||
}).then(
|
}).then(
|
||||||
(response) => {
|
(response) => resolve(response),
|
||||||
return resolve(response);
|
(error) => reject(error)
|
||||||
},
|
|
||||||
(error) => {
|
|
||||||
return reject(error);
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue