n8n
Version:
n8n Workflow Automation Tool
39 lines • 1.59 kB
JavaScript
;
const db_1 = require("@n8n/db");
const di_1 = require("@n8n/di");
const unauthenticated_error_1 = require("../../../../errors/response-errors/unauthenticated.error");
const discover_service_1 = require("./discover.service");
const API_KEY_AUDIENCE = 'public-api';
function firstString(value) {
if (typeof value === 'string')
return value;
if (Array.isArray(value) && typeof value[0] === 'string')
return value[0];
return undefined;
}
const discoverHandlers = {
getDiscover: [
async (req, res) => {
const apiKey = firstString(req.headers['x-n8n-api-key']);
if (!apiKey) {
throw new unauthenticated_error_1.UnauthenticatedError('Unauthorized');
}
const apiKeyRecord = await di_1.Container.get(db_1.ApiKeyRepository).findOne({
where: { apiKey, audience: API_KEY_AUDIENCE },
select: { scopes: true },
});
if (!apiKeyRecord) {
throw new unauthenticated_error_1.UnauthenticatedError('Unauthorized');
}
const includeSchemas = req.query.include === 'schemas';
const response = await (0, discover_service_1.buildDiscoverResponse)(apiKeyRecord.scopes, {
includeSchemas,
resource: firstString(req.query.resource),
operation: firstString(req.query.operation),
});
return res.json({ data: response });
},
],
};
module.exports = discoverHandlers;
//# sourceMappingURL=discover.handler.js.map