Skip to content

Commit

Permalink
[FEATURE] Add Profile Fabricator configuration support (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechpavlu authored Apr 5, 2024
1 parent e0bf992 commit bdbe6ca
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@falbricate/base",
"version": "1.2.10",
"version": "1.2.11",
"description": "Base Framework for Falbricate - tool for Falsa Fabrication",
"keywords": [
"mocking",
Expand Down
14 changes: 14 additions & 0 deletions src/profiles/ProfileFabricator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import { Fabricator, SchemaInput } from '../schema';
import { ProfileFabricatorName } from './ProfileFabricatorRegistry';

/** Declaration of Profile Configuration */
export type ProfileFabricatorConfiguration = {} & any;

/** Profile object-based definition */
export type ProfileFabricatorDefinition = {
type: string;
configuration?: ProfileFabricatorConfiguration;
};

export type ProfileDeclaration =
| ProfileFabricatorDefinition
| ProfileFabricatorName;

export abstract class ProfileFabricator {
public readonly profileKey: string;
Expand Down
12 changes: 9 additions & 3 deletions src/profiles/ProfileFabricatorRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { ProfileFabricator } from './ProfileFabricator';
import {
ProfileFabricator,
ProfileFabricatorConfiguration,
} from './ProfileFabricator';
import { IdentifierProfileFabricator } from './generators';

export type ProfileFabricatorBuilder = () => ProfileFabricator;
export type ProfileFabricatorBuilder = (
config?: ProfileFabricatorConfiguration,
) => ProfileFabricator;

export type ProfileFabricatorName = string | 'identifiers';

Expand All @@ -13,14 +18,15 @@ const REGISTRY: ProfileFabricatorRegistry = {};

export const getProfileFabricator = (
name: ProfileFabricatorName,
config?: ProfileFabricatorConfiguration,
): ProfileFabricator => {
const builder = REGISTRY[name];

if (!builder) {
throw new Error(`No Profile Fabricator found for name '${name}'`);
}

return builder();
return builder(config);
};

export const hasProfileFabricator = (name: ProfileFabricatorName): boolean => {
Expand Down
22 changes: 20 additions & 2 deletions src/schema/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,26 @@ export class Schema {
): ProfileFabricators => {
const profileFabricators: ProfileFabricators = {};

schemaInput.profiles?.forEach((profile) => {
const profileFabricator = getProfileFabricator(profile);
schemaInput.profiles?.forEach((profileDeclaration) => {
let profileFabricator;

if (typeof profileDeclaration === 'string') {
// When using string-based declaration
profileFabricator = getProfileFabricator(profileDeclaration);
} else if (typeof profileDeclaration === 'object') {
// When using object-based declaration
const type = profileDeclaration.type;
profileFabricator = getProfileFabricator(
type,
profileDeclaration.configuration,
);
} else {
// When unexpected type of Profile Fabricator declaration
throw new Error(
`Unexpected type of Profile Fabricator '${typeof profileDeclaration}'`,
);
}

profileFabricators[profileFabricator.profileKey] =
profileFabricator.createFabricator();
});
Expand Down
4 changes: 2 additions & 2 deletions src/schema/SchemaInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ValueGeneratorName,
} from '../generators';
import { FalsumPipe } from '../pipes';
import { ProfileFabricatorName } from '../profiles/ProfileFabricatorRegistry';
import { ProfileDeclaration } from '../profiles';

/**
* Plain JavaScript Object representation of a field for which
Expand Down Expand Up @@ -45,6 +45,6 @@ export interface SchemaInput {
[name: string]: FieldDeclaration;
};

profiles?: ProfileFabricatorName[];
profiles?: ProfileDeclaration[];
pipes?: FalsumPipeInput[];
}

0 comments on commit bdbe6ca

Please sign in to comment.