Via command line:
generate <schema> <endpoint>
Via a custom script, to provide overrides for the default transformer functions:
import generate, { fakeId, fakeEmail, createTransformer } from 'generate';
const customTransformer = createTransformer(
(prop) => prop.includes('tardis'), // the property you want to mock
() => 'faker.method', // the faker method you want to set
);
generate('schema.yaml', 'SomeEndpoint', [fakeId, fakeEmail, customTransformer]); // pass the custom transformer along with the defaults you want as an array
Transformers operate on a schema to help create a fixture. The default transformers can be imported for reuse when overriding or adding a custom transformer. A helper function called createTransformer
is provided to aid in the creation of custom transformers. It takes two callbacks. The first, a predicate that checks if the property is the one you wish to mock. The second, sets the faker method that generates the mock data. The second callback also accepts an optional props argument, allowing you to do additional logic before setting a faker method. A list of available methods can be found here.
One could even get fancy with their workflow:
import generate from 'generate';
const endpoints = ['SomeEndpoint', 'AnotherEndpoint', 'YetOneMoreEndpoint'];
endpoints.forEach((endpoint) => generate('schema.yaml', endpoint));
Or
const { execSync } = require('child_process');
const endpoints = ['SomeEndpoint', 'AnotherEndpoint', 'YetOneMoreEndpoint'];
endpoints.forEach((endpoint) => execSync(`generate schema.yaml ${endpoint}`, { stdio: 'inherit' }));