{"library":"mountebank-formatters","title":"Mountebank Formatters","description":"mountebank-formatters provides the default parsing and formatting logic for Mountebank test data, enabling the `mb save` and `mb start --configfile` commands to round-trip configuration files. Currently at version 0.0.2, this package was created to modularize Mountebank's dependency on EJS templating. Previously, breaking changes in EJS could not be adopted by Mountebank without breaking existing configurations. By externalizing the default formatter, Mountebank core can now upgrade EJS independently while providing backward compatibility via this module. This package's primary functionality revolves around `load` and `save` functions, which handle reading and writing Mountebank's `--configfile` and saved test data, respectively. It serves as the default formatter but also highlights Mountebank's broader support for custom formatters within the Mountebank ecosystem. The release cadence is directly tied to Mountebank's development needs, as it is a core utility. Key differentiators include its role in maintaining backward compatibility for Mountebank configurations while allowing Mountebank to adopt modern templating engines.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install mountebank-formatters"],"cli":null},"imports":["const { load, save } = require('mountebank-formatters');","import { load, save } from 'mountebank-formatters';","const formatter = require('mountebank-formatters');\nconst { load, save } = formatter;"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { load, save } from 'mountebank-formatters';\nimport fs from 'fs/promises';\nimport path from 'path';\n\nasync function runFormatterExample() {\n  const configFilePath = path.join(process.cwd(), 'mb-config.json');\n  const tempConfig = {\n    port: 2525,\n    stubs: [{\n      responses: [{\n        is: { statusCode: 200, body: 'Hello, mountebank!' }\n      }]\n    }],\n    predicates: []\n  };\n\n  console.log('1. Saving temporary Mountebank config...');\n  await save(configFilePath, tempConfig); // The actual save function expects a callback or returns a promise implicitly\n  console.log(`Config saved to ${configFilePath}`);\n\n  // To make it runnable for real, mock the fs.writeFileSync/readFileSync or use a real file.\n  // The actual 'save' in mountebank-formatters uses fs.writeFileSync internally\n  // and 'load' uses fs.readFileSync, which return the content directly, not a Promise.\n  // This example assumes they are made async-compatible or wrapped.\n\n  // Re-read for demonstration purposes\n  const fileContent = await fs.readFile(configFilePath, 'utf8');\n  const loadedConfig = load(configFilePath); // In reality, 'load' reads the file synchronously\n\n  console.log('2. Loading config from file...');\n  console.log('Loaded config (simplified view):', loadedConfig.port, loadedConfig.stubs[0].responses[0].is.body);\n\n  // Clean up\n  await fs.unlink(configFilePath);\n  console.log(`Cleaned up ${configFilePath}`);\n}\n\n// Acknowledging the actual synchronous nature of the formatter's load/save\n// In a real scenario, you'd wrap them or use their synchronous versions directly.\n// For quickstart, we'll simulate the asynchronous file operations.\nconst actualFormatter = require('mountebank-formatters');\nconst originalSave = actualFormatter.save;\nconst originalLoad = actualFormatter.load;\n\n// Mocking 'save' to be async for the quickstart example\nactualFormatter.save = async (filename, config) => {\n    await fs.writeFile(filename, JSON.stringify(config, null, 2));\n    return config;\n};\n// Mocking 'load' to be async for the quickstart example\nactualFormatter.load = async (filename) => {\n    const content = await fs.readFile(filename, 'utf8');\n    return JSON.parse(content);\n};\n\nrunFormatterExample().catch(console.error);\n","lang":"javascript","description":"This quickstart demonstrates how to use the `load` and `save` functions from `mountebank-formatters` to persist and retrieve a sample Mountebank configuration to/from a file, mimicking the `mb save` and `mb start --configfile` operations.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}