typescript-scaffolder
Version:
 
77 lines (76 loc) • 3.1 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateAuthHelperForApiFile = generateAuthHelperForApiFile;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const logger_1 = require("../../utils/logger");
/**
* Generates an authentication helper file for the specified API client.
* Supports 'apikey', 'basic', and 'none' authentication types.
*
* @param outputDir - The directory where the auth helper file will be created.
* @param fileBaseName - The base name of the API client file.
* @param authType - The type of authentication ('apikey' | 'basic' | 'none').
* @param credentials - Optional credentials needed for 'apikey' or 'basic' auth.
*/
async function generateAuthHelperForApiFile(outputDir, fileBaseName, authType, credentials) {
const funcName = "generateAuthHelperForApiFile";
logger_1.Logger.debug(funcName, `Generating auth helper for ${fileBaseName}`);
// Header banner indicating auto-generation
const banner = `/**
* This file was auto-generated by generate-auth-helper.ts
* Values are resolved from environment variables, falling back to endpoint config values if present.
* Check in before editing this file manually.
*/
`;
const serviceEnvKey = fileBaseName.replace(/[^a-zA-Z0-9]/g, "_").toUpperCase();
let content = banner;
if (authType === "apikey") {
// Use apiKeyName from credentials or default to "x-api-key"
const headerName = credentials?.apiKeyName ?? "x-api-key";
content += `/**
* Returns headers containing the API key for authentication.
* Expected env vars: ${serviceEnvKey}_APIKEY
*/
export function getAuthHeaders() {
const apiKey = process.env["${serviceEnvKey}_APIKEY"] ?? "${credentials?.apiKeyValue ?? ""}";
return {
"${headerName}": apiKey
};
}
`;
}
else if (authType === "basic") {
content += `/**
* Returns headers containing the Basic Authentication token.
* Expected env vars: ${serviceEnvKey}_USERNAME, ${serviceEnvKey}_PASSWORD
*/
export function getAuthHeaders() {
const username = process.env["${serviceEnvKey}_USERNAME"] ?? "${credentials?.username ?? ""}";
const password = process.env["${serviceEnvKey}_PASSWORD"] ?? "${credentials?.password ?? ""}";
const encoded = Buffer.from(username + ":" + password).toString("base64");
return {
Authorization: "Basic " + encoded
};
}
`;
}
else if (authType === "none") {
content += `/**
* Returns empty headers as no authentication is required.
*/
export function getAuthHeaders() {
return {};
}
`;
}
else {
throw new Error(`Unsupported authType '${authType}'. Supported types are 'apikey', 'basic', and 'none'.`);
}
const filePath = path_1.default.join(outputDir, `${fileBaseName}.authHelper.ts`);
await fs_1.promises.writeFile(filePath, content, "utf-8");
logger_1.Logger.info(funcName, `Auth helper file generated at: ${filePath}`);
}