2023-02-21 02:21:04 -08:00
|
|
|
import type { Request } from 'express';
|
|
|
|
import { access } from 'fs/promises';
|
2024-09-12 09:07:18 -07:00
|
|
|
import { join } from 'path';
|
|
|
|
|
2023-12-27 02:50:43 -08:00
|
|
|
import config from '@/config';
|
2023-02-21 02:21:04 -08:00
|
|
|
import { NODES_BASE_DIR } from '@/constants';
|
2024-09-12 09:07:18 -07:00
|
|
|
import { CredentialTypes } from '@/credential-types';
|
|
|
|
import { Get, RestController } from '@/decorators';
|
2023-11-28 01:19:27 -08:00
|
|
|
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
|
|
|
|
import { InternalServerError } from '@/errors/response-errors/internal-server.error';
|
2023-02-21 02:21:04 -08:00
|
|
|
|
|
|
|
export const CREDENTIAL_TRANSLATIONS_DIR = 'n8n-nodes-base/dist/credentials/translations';
|
|
|
|
export const NODE_HEADERS_PATH = join(NODES_BASE_DIR, 'dist/nodes/headers');
|
|
|
|
|
|
|
|
export declare namespace TranslationRequest {
|
|
|
|
export type Credential = Request<{}, {}, {}, { credentialType: string }>;
|
|
|
|
}
|
|
|
|
|
|
|
|
@RestController('/')
|
|
|
|
export class TranslationController {
|
2023-12-27 02:50:43 -08:00
|
|
|
constructor(private readonly credentialTypes: CredentialTypes) {}
|
2023-02-21 02:21:04 -08:00
|
|
|
|
|
|
|
@Get('/credential-translation')
|
|
|
|
async getCredentialTranslation(req: TranslationRequest.Credential) {
|
|
|
|
const { credentialType } = req.query;
|
|
|
|
|
|
|
|
if (!this.credentialTypes.recognizes(credentialType))
|
|
|
|
throw new BadRequestError(`Invalid Credential type: "${credentialType}"`);
|
|
|
|
|
2023-12-27 02:50:43 -08:00
|
|
|
const defaultLocale = config.getEnv('defaultLocale');
|
2023-02-21 02:21:04 -08:00
|
|
|
const translationPath = join(
|
|
|
|
CREDENTIAL_TRANSLATIONS_DIR,
|
|
|
|
defaultLocale,
|
|
|
|
`${credentialType}.json`,
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
|
|
return require(translationPath);
|
|
|
|
} catch (error) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Get('/node-translation-headers')
|
|
|
|
async getNodeTranslationHeaders() {
|
|
|
|
try {
|
|
|
|
await access(`${NODE_HEADERS_PATH}.js`);
|
2023-03-03 09:18:49 -08:00
|
|
|
} catch {
|
2023-02-21 02:21:04 -08:00
|
|
|
return; // no headers available
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
|
|
return require(NODE_HEADERS_PATH);
|
|
|
|
} catch (error) {
|
|
|
|
throw new InternalServerError('Failed to load headers file');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|