integreat
Version:
Node.js integration layer
84 lines • 3.54 kB
JavaScript
import util from 'util';
import getHandler from './get.js';
import getField from '../utils/getField.js';
import { createErrorResponse } from '../utils/response.js';
import { getFirstIfArray } from '../utils/array.js';
import { isTypedData } from '../utils/is.js';
import { IdentType, } from '../types.js';
const createGetIdentAction = (type, params, cid) => ({
type: 'GET',
payload: { type, ...params },
meta: { ident: { id: 'root', root: true, type: IdentType.Root }, cid },
});
const preparePropKeys = ({ id = 'id', roles = 'roles', tokens = 'tokens', } = {}) => ({
id,
roles,
tokens,
});
const prepareParams = (ident, keys) => ident.id
? { [keys.id]: ident.id }
: ident.withToken
? { [keys.tokens]: ident.withToken }
: null;
const wrapOk = (action, data, ident) => ({
...action.response,
status: 'ok',
data,
access: { ident },
});
const extractIdentPropsFromData = (mapping, data) => Object.fromEntries(Object.entries(mapping)
.map(([key, path]) => [key, getField(data, path)])
.filter(([_, value]) => !!value));
function prepareErrorResponse(response, params) {
const errorMessage = `Could not get ident with params ${util.inspect(params)}`;
if (response.status && ['ok', 'notfound'].includes(response.status)) {
return createErrorResponse(`${errorMessage}. [notfound] ${response.error || 'Did not return the expected data'}`, 'handler:GET_IDENT', 'notfound');
}
else {
return createErrorResponse(`${errorMessage}. [${response.status}] ${response.error}`, 'handler:GET_IDENT');
}
}
const prepareResponse = (action, response, params, mapping) => {
const data = getFirstIfArray(response.data);
if (response.status === 'ok' && isTypedData(data)) {
const ident = {
id: undefined,
...extractIdentPropsFromData(mapping, data),
isCompleted: true,
};
return wrapOk(action, data, ident);
}
else {
return prepareErrorResponse(response, params);
}
};
function extractParams(resources, ident) {
const { identConfig } = resources.options;
const { type } = identConfig || {};
const propKeys = preparePropKeys(identConfig?.props);
const params = prepareParams(ident, propKeys);
const mapping = { ...propKeys, ...identConfig?.mapping };
return { type, propKeys, params, mapping };
}
export default async function getIdent(action, resources) {
const { ident } = action.meta || {};
if (!ident) {
return createErrorResponse('GET_IDENT: The request has no ident', 'handler:GET_IDENT', 'noaction');
}
if (ident.isCompleted) {
return wrapOk(action, ident, ident);
}
const { type, propKeys, params, mapping } = extractParams(resources, ident);
if (!type) {
return createErrorResponse('GET_IDENT: Integreat is not set up with authentication', 'handler:GET_IDENT', 'noaction');
}
if (typeof ident.withToken === 'string' && !propKeys.tokens) {
return createErrorResponse("GET_IDENT: The request has an ident with 'withToken', but no tokens key is set in `identConfig`", 'handler:GET_IDENT', 'badrequest');
}
if (!params) {
return createErrorResponse('GET_IDENT: The request has no ident with id or withToken', 'handler:GET_IDENT', 'noaction');
}
const response = await getHandler(createGetIdentAction(type, params, action.meta?.cid), resources);
return prepareResponse(action, response, params, mapping);
}
//# sourceMappingURL=getIdent.js.map