All files / src/utils fetch.js

0% Statements 0/32
100% Branches 0/0
0% Functions 0/4
0% Lines 0/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101                                                                                                                                                                                                         
/**
 * Utility functions to fetch and parse various JSON specification files
 */
const fs = require('node:fs');
const path = require('node:path');
const Logger = require('./logger');
 
/**
 * Fetches the .cache/specs-generated.json file and returns it as a JavaScript object
 * @returns {Object} The parsed contents of specs-generated.json
 */
function fetchSpecs() {
  try {
    // Resolve path to .cache/specs-generated.json from the project root
    const specsPath = path.resolve(process.cwd(), '.cache', 'specs-generated.json');
    
    // Read the file synchronously
    const specsContent = fs.readFileSync(specsPath, 'utf8');
    
    // Parse the JSON content
    const specs = JSON.parse(specsContent);
    
    return specs;
  } catch (error) {
    Logger.error('Error fetching .cache/specs-generated.json:', error.message);
    return null;
  }
}
 
/**
 * Fetches the .cache/xtrefs-data.json file and returns it as a JavaScript object
 * @returns {Object} The parsed contents of xtrefs-data.json
 */
function fetchExternalTerms() {
  try {
    // Resolve path to .cache/xtrefs-data.json from the project root
    const xtrefsPath = path.resolve(process.cwd(), '.cache', 'xtrefs-data.json');
    
    // Read the file synchronously
    const xtrefsContent = fs.readFileSync(xtrefsPath, 'utf8');
    
    // Parse the JSON content
    const xtrefs = JSON.parse(xtrefsContent);
    
    return xtrefs;
  } catch (error) {
    Logger.error('Error fetching .cache/xtrefs-data.json:', error.message);
    return null;
  }
}
 
/**
 * Asynchronous version of fetchGeneratedSpecs
 * @returns {Promise<Object>} The parsed specs-generated object
 */
async function fetchSpecsAsync() {
  try {
    // Resolve path to .cache/specs-generated.json from the project root
    const specsPath = path.resolve(process.cwd(), '.cache', 'specs-generated.json');
    
    // Read the file asynchronously
    const specsContent = await fs.promises.readFile(specsPath, 'utf8');
    
    // Parse the JSON content
    const specs = JSON.parse(specsContent);
    
    return specs;
  } catch (error) {
    Logger.error('Error fetching .cache/specs-generated.json:', error.message);
    return null;
  }
}
 
/**
 * Asynchronous version of fetchXtrefsData
 * @returns {Promise<Object>} The parsed xtrefs-data object
 */
async function fetchExternalTermsAsync() {
  try {
    // Resolve path to .cache/xtrefs-data.json from the project root
    const xtrefsPath = path.resolve(process.cwd(), '.cache', 'xtrefs-data.json');
    
    // Read the file asynchronously
    const xtrefsContent = await fs.promises.readFile(xtrefsPath, 'utf8');
    
    // Parse the JSON content
    const xtrefs = JSON.parse(xtrefsContent);
    
    return xtrefs;
  } catch (error) {
    Logger.error('Error fetching .cache/xtrefs-data.json:', error.message);
    return null;
  }
}
 
module.exports = {
  fetchSpecs,
  fetchSpecsAsync,
  fetchExternalTerms,
  fetchExternalTermsAsync
};