Grabbed dndbeyond's source code
``` ~/go/bin/sourcemapper -output ddb -jsurl https://media.dndbeyond.com/character-app/static/js/main.90aa78c5.js ```
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import axios from 'axios';
|
||||
import { LoggerUtils } from '../logger';
|
||||
import { ApiException } from './errors';
|
||||
export function handleApiException(error) {
|
||||
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
||||
// only do something if not canceled, cancellation should be fine
|
||||
if (axios.isCancel(error)) {
|
||||
throw error;
|
||||
}
|
||||
else {
|
||||
throw new ApiException((_a = error.config.url) !== null && _a !== void 0 ? _a : null, (_c = (_b = error.response) === null || _b === void 0 ? void 0 : _b.status) !== null && _c !== void 0 ? _c : null, (_d = error.config.method) !== null && _d !== void 0 ? _d : null, {
|
||||
params: (_e = error.config.params) !== null && _e !== void 0 ? _e : null,
|
||||
data: (_f = error.config.data) !== null && _f !== void 0 ? _f : null,
|
||||
serverErrorData: (_j = (_h = (_g = error.response) === null || _g === void 0 ? void 0 : _g.data) === null || _h === void 0 ? void 0 : _h.data) !== null && _j !== void 0 ? _j : [],
|
||||
});
|
||||
}
|
||||
}
|
||||
export function handleApiAdapterException(error) {
|
||||
LoggerUtils.logError(error);
|
||||
throw error;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
export class ApiAdapterException extends Error {
|
||||
constructor(message) {
|
||||
super(message);
|
||||
Object.setPrototypeOf(this, new.target.prototype);
|
||||
this.name = ApiAdapterException.name;
|
||||
}
|
||||
}
|
||||
export class ApiAdapterContextException extends ApiAdapterException {
|
||||
constructor(contextData, message) {
|
||||
super(message);
|
||||
Object.setPrototypeOf(this, new.target.prototype);
|
||||
this.name = ApiAdapterContextException.name;
|
||||
this.contextData = contextData;
|
||||
}
|
||||
}
|
||||
export class OverrideApiException extends ApiAdapterException {
|
||||
constructor(contextData, message) {
|
||||
super(message);
|
||||
Object.setPrototypeOf(this, new.target.prototype);
|
||||
this.name = OverrideApiException.name;
|
||||
this.contextData = contextData;
|
||||
}
|
||||
}
|
||||
export class AuthMissingException extends ApiAdapterException {
|
||||
constructor(message) {
|
||||
super(message);
|
||||
Object.setPrototypeOf(this, new.target.prototype);
|
||||
this.name = AuthMissingException.name;
|
||||
}
|
||||
}
|
||||
export class AuthException extends ApiAdapterContextException {
|
||||
constructor(contextData, message) {
|
||||
super(contextData, message);
|
||||
Object.setPrototypeOf(this, new.target.prototype);
|
||||
this.name = AuthException.name;
|
||||
}
|
||||
}
|
||||
export class ApiAdapterUrlException extends ApiAdapterContextException {
|
||||
constructor(url, errorCode, method, contextData, message) {
|
||||
super(contextData, message);
|
||||
Object.setPrototypeOf(this, new.target.prototype);
|
||||
this.name = ApiAdapterUrlException.name;
|
||||
this.url = url;
|
||||
this.errorCode = errorCode;
|
||||
this.method = method === null ? null : method.toUpperCase();
|
||||
}
|
||||
}
|
||||
export class ApiException extends ApiAdapterUrlException {
|
||||
constructor(url, errorCode, method, contextData, message) {
|
||||
super(url, errorCode, method, contextData, message);
|
||||
Object.setPrototypeOf(this, new.target.prototype);
|
||||
this.name = ApiException.name;
|
||||
}
|
||||
}
|
||||
export class ApiAdapterDataException extends ApiAdapterUrlException {
|
||||
constructor(url, errorCode, method, contextData, message) {
|
||||
super(url, errorCode, method, contextData, message);
|
||||
Object.setPrototypeOf(this, new.target.prototype);
|
||||
this.name = ApiAdapterDataException.name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
import axios from 'axios';
|
||||
import { merge } from 'lodash';
|
||||
import AuthUtils from '@dndbeyond/authentication-lib-js';
|
||||
import { handleApiAdapterException, handleApiException } from './errorHandlers';
|
||||
import { ApiAdapterException, AuthException, AuthMissingException, OverrideApiException } from './errors';
|
||||
import { validateApiAdapterResponse, validateCanRequest } from './validators';
|
||||
let getAuthLibAuthHeaders = null;
|
||||
/**
|
||||
*
|
||||
* @param authUrl
|
||||
* @param isReadonly
|
||||
* @param existingConfig
|
||||
*/
|
||||
export function generateAuthRequestConfig(authUrl, isReadonly, existingConfig) {
|
||||
if (getAuthLibAuthHeaders === null) {
|
||||
getAuthLibAuthHeaders = AuthUtils.makeGetAuthorizationHeaders({
|
||||
authUrl,
|
||||
});
|
||||
}
|
||||
return getAuthLibAuthHeaders()
|
||||
.then((authHeaders = {}) => {
|
||||
if (!validateCanRequest(authHeaders, isReadonly)) {
|
||||
throw new AuthMissingException();
|
||||
}
|
||||
let authRequestConfig = {
|
||||
headers: authHeaders,
|
||||
};
|
||||
return merge({}, existingConfig, authRequestConfig);
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error instanceof ApiAdapterException) {
|
||||
throw error;
|
||||
}
|
||||
else {
|
||||
throw new AuthException();
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param existingConfig
|
||||
*/
|
||||
export function generateApiAdapterRequestConfig(existingConfig) {
|
||||
const defaultConfig = {
|
||||
withCredentials: true,
|
||||
};
|
||||
return merge({}, defaultConfig, existingConfig);
|
||||
}
|
||||
export function handleOverrideError(altHandler, config, errorData) {
|
||||
return (error) => {
|
||||
if (config === null || config === void 0 ? void 0 : config.onError) {
|
||||
let thisError = error;
|
||||
if (!(thisError instanceof OverrideApiException)) {
|
||||
thisError = new OverrideApiException(errorData, 'API ERROR');
|
||||
}
|
||||
throw thisError;
|
||||
}
|
||||
else {
|
||||
altHandler(error);
|
||||
}
|
||||
};
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param authUrl
|
||||
* @param isReadonly
|
||||
*/
|
||||
export function generateApiAdapter(authUrl, isReadonly) {
|
||||
return {
|
||||
get: (apiPath, config) => generateAuthRequestConfig(authUrl, isReadonly, config)
|
||||
.then((requestConfig) => axios
|
||||
.get(apiPath, generateApiAdapterRequestConfig(requestConfig))
|
||||
.catch(handleOverrideError(handleApiException, config, { apiPath })))
|
||||
.then(validateApiAdapterResponse)
|
||||
.catch(handleOverrideError(handleApiAdapterException, config, { apiPath })),
|
||||
delete: (apiPath, config) => generateAuthRequestConfig(authUrl, isReadonly, config)
|
||||
.then((requestConfig) => axios
|
||||
.delete(apiPath, generateApiAdapterRequestConfig(requestConfig))
|
||||
.catch(handleOverrideError(handleApiException, config, { apiPath })))
|
||||
.then(validateApiAdapterResponse)
|
||||
.catch(handleOverrideError(handleApiAdapterException, config, { apiPath })),
|
||||
head: axios.head,
|
||||
post: (apiPath, data, config) => generateAuthRequestConfig(authUrl, isReadonly, config)
|
||||
.then((requestConfig) => axios
|
||||
.post(apiPath, data, generateApiAdapterRequestConfig(requestConfig))
|
||||
.catch(handleOverrideError(handleApiException, config, { apiPath, data })))
|
||||
.then(validateApiAdapterResponse)
|
||||
.catch(handleOverrideError(handleApiAdapterException, config, { apiPath, data })),
|
||||
put: (apiPath, data, config) => generateAuthRequestConfig(authUrl, isReadonly, config)
|
||||
.then((requestConfig) => axios
|
||||
.put(apiPath, data, generateApiAdapterRequestConfig(requestConfig))
|
||||
.catch(handleOverrideError(handleApiException, config, { apiPath, data })))
|
||||
.then(validateApiAdapterResponse)
|
||||
.catch(handleOverrideError(handleApiAdapterException, config, { apiPath, data })),
|
||||
patch: axios.patch,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
import { merge } from 'lodash';
|
||||
import { ApiTypeEnum } from '../api/constants';
|
||||
import { generateCharacterServiceApiPath } from '../api/generators';
|
||||
import { ConfigUtils } from '../config';
|
||||
import { rulesEngineSelectors } from '../selectors';
|
||||
let emptyPromise = new Promise((resolve, reject) => {
|
||||
resolve({
|
||||
data: {
|
||||
success: false,
|
||||
},
|
||||
headers: {},
|
||||
status: -1,
|
||||
statusText: 'EMPTY_PROMISE',
|
||||
});
|
||||
});
|
||||
export function getApiBaseUrl(apiType) {
|
||||
const config = ConfigUtils.getCurrentRulesEngineConfig();
|
||||
switch (apiType) {
|
||||
case ApiTypeEnum.HACK__CHARACTER_SERVICE_GAME_DATA:
|
||||
return config.apiInfo[ApiTypeEnum.CHARACTER_SERVICE];
|
||||
default:
|
||||
// not implemented
|
||||
}
|
||||
return config.apiInfo[apiType];
|
||||
}
|
||||
export function generateApiUrl(apiType, apiPath, apiVersionOverride) {
|
||||
return [getApiBaseUrl(apiType), transformApiPath(apiType, apiPath, apiVersionOverride)].join('/');
|
||||
}
|
||||
export function generateDefaultRequestParams(apiType, requestConfig) {
|
||||
const config = ConfigUtils.getCurrentRulesEngineConfig();
|
||||
if (config.store === undefined || requestConfig.removeDefaultParams) {
|
||||
return {};
|
||||
}
|
||||
switch (apiType) {
|
||||
case ApiTypeEnum.CHARACTER_SERVICE:
|
||||
return rulesEngineSelectors.getRequiredCharacterServiceParams(config.store.getState());
|
||||
case ApiTypeEnum.GAME_DATA_SERVICE:
|
||||
case ApiTypeEnum.HACK__CHARACTER_SERVICE_GAME_DATA:
|
||||
return rulesEngineSelectors.getRequiredGameDataServiceParams(config.store.getState());
|
||||
default:
|
||||
// not implemented
|
||||
}
|
||||
return {};
|
||||
}
|
||||
export function transformApiPath(apiType, apiPath, apiVersionOverride) {
|
||||
switch (apiType) {
|
||||
case ApiTypeEnum.CHARACTER_SERVICE:
|
||||
case ApiTypeEnum.HACK__CHARACTER_SERVICE_GAME_DATA:
|
||||
return generateCharacterServiceApiPath(apiPath, apiVersionOverride);
|
||||
default:
|
||||
//not implemented
|
||||
}
|
||||
return apiPath;
|
||||
}
|
||||
export function makeGet(apiType, apiPath, config, apiVersionOverride) {
|
||||
return (additionalConfig = {}) => {
|
||||
const rulesEngineConfig = ConfigUtils.getCurrentRulesEngineConfig();
|
||||
if (rulesEngineConfig.apiAdapter) {
|
||||
const initialRequestConfig = merge({}, config, additionalConfig);
|
||||
const defaultRequestParams = generateDefaultRequestParams(apiType, initialRequestConfig);
|
||||
const defaultRequestConfig = {
|
||||
params: defaultRequestParams,
|
||||
};
|
||||
const requestConfig = merge({}, defaultRequestConfig, initialRequestConfig);
|
||||
return rulesEngineConfig.apiAdapter.get(generateApiUrl(apiType, apiPath, apiVersionOverride), requestConfig);
|
||||
}
|
||||
return emptyPromise;
|
||||
};
|
||||
}
|
||||
export function makeInterpolatedGet(apiType, apiPath, config, apiVersionOverride) {
|
||||
return (data, additionalConfig = {}) => {
|
||||
const rulesEngineConfig = ConfigUtils.getCurrentRulesEngineConfig();
|
||||
if (rulesEngineConfig.apiAdapter) {
|
||||
const initialRequestConfig = merge({}, config, additionalConfig);
|
||||
const defaultRequestParams = generateDefaultRequestParams(apiType, initialRequestConfig);
|
||||
const defaultRequestConfig = {
|
||||
params: defaultRequestParams,
|
||||
};
|
||||
const requestConfig = merge({}, defaultRequestConfig, initialRequestConfig);
|
||||
let interpolatedPath = `${apiPath}`;
|
||||
Object.keys(data).forEach((key) => {
|
||||
if (!data[key]) {
|
||||
throw new Error(`Key: ${key} was not provided to makeInterpolatedGet`);
|
||||
}
|
||||
interpolatedPath = interpolatedPath.replace(`{${key}}`, data[key]);
|
||||
});
|
||||
return rulesEngineConfig.apiAdapter.get(generateApiUrl(apiType, interpolatedPath, apiVersionOverride), requestConfig);
|
||||
}
|
||||
return emptyPromise;
|
||||
};
|
||||
}
|
||||
export function makePost(apiType, apiPath, config, apiVersionOverride) {
|
||||
return (data, additionalConfig = {}) => {
|
||||
const rulesEngineConfig = ConfigUtils.getCurrentRulesEngineConfig();
|
||||
if (rulesEngineConfig.apiAdapter) {
|
||||
const requestConfig = merge({}, config, additionalConfig);
|
||||
const defaultRequestParams = generateDefaultRequestParams(apiType, requestConfig);
|
||||
const requestData = merge({}, defaultRequestParams, data);
|
||||
return rulesEngineConfig.apiAdapter.post(generateApiUrl(apiType, apiPath, apiVersionOverride), requestData, requestConfig);
|
||||
}
|
||||
return emptyPromise;
|
||||
};
|
||||
}
|
||||
export function makePut(apiType, apiPath, config, apiVersionOverride) {
|
||||
return (data, additionalConfig = {}) => {
|
||||
const rulesEngineConfig = ConfigUtils.getCurrentRulesEngineConfig();
|
||||
if (rulesEngineConfig.apiAdapter) {
|
||||
const requestConfig = merge({}, config, additionalConfig);
|
||||
const defaultRequestParams = generateDefaultRequestParams(apiType, requestConfig);
|
||||
const requestData = merge({}, defaultRequestParams, data);
|
||||
return rulesEngineConfig.apiAdapter.put(generateApiUrl(apiType, apiPath, apiVersionOverride), requestData, requestConfig);
|
||||
}
|
||||
return emptyPromise;
|
||||
};
|
||||
}
|
||||
export function makeDelete(apiType, apiPath, config, apiVersionOverride) {
|
||||
return (data, additionalConfig = {}) => {
|
||||
const rulesEngineConfig = ConfigUtils.getCurrentRulesEngineConfig();
|
||||
if (rulesEngineConfig.apiAdapter) {
|
||||
const initialRequestConfig = merge({}, config, additionalConfig);
|
||||
const defaultRequestParams = generateDefaultRequestParams(apiType, initialRequestConfig);
|
||||
const defaultRequestConfig = {
|
||||
data: defaultRequestParams,
|
||||
};
|
||||
const paramConfig = {
|
||||
data: data ? data : {},
|
||||
};
|
||||
const requestConfig = merge({}, defaultRequestConfig, paramConfig);
|
||||
return rulesEngineConfig.apiAdapter.delete(generateApiUrl(apiType, apiPath, apiVersionOverride), requestConfig);
|
||||
}
|
||||
return emptyPromise;
|
||||
};
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param response
|
||||
*/
|
||||
export function getResponseData(response) {
|
||||
var _a;
|
||||
if (((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.data) !== undefined) {
|
||||
return response.data.data;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { ApiAdapterDataException } from './errors';
|
||||
export function validateApiAdapterResponse(response) {
|
||||
var _a, _b, _c, _d, _e, _f, _g, _h;
|
||||
if (!response.data) {
|
||||
throw new ApiAdapterDataException((_a = response.config.url) !== null && _a !== void 0 ? _a : null, response.status, (_b = response.config.method) !== null && _b !== void 0 ? _b : null, {
|
||||
params: (_c = response.config.params) !== null && _c !== void 0 ? _c : null,
|
||||
data: (_d = response.config.data) !== null && _d !== void 0 ? _d : null,
|
||||
responseData: response.data,
|
||||
}, 'Invalid JSON response, missing data object');
|
||||
}
|
||||
if (typeof response.data !== 'object') {
|
||||
throw new ApiAdapterDataException((_e = response.config.url) !== null && _e !== void 0 ? _e : null, response.status, (_f = response.config.method) !== null && _f !== void 0 ? _f : null, {
|
||||
params: (_g = response.config.params) !== null && _g !== void 0 ? _g : null,
|
||||
data: (_h = response.config.data) !== null && _h !== void 0 ? _h : null,
|
||||
responseData: response.data,
|
||||
}, 'Invalid JSON response, data object is a string');
|
||||
}
|
||||
return response;
|
||||
}
|
||||
export function validateCanRequest(authHeaders, isReadonly) {
|
||||
if (isReadonly) {
|
||||
return true;
|
||||
}
|
||||
if (Object.keys(authHeaders).length) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user