{"id":24792515,"url":"https://github.com/goldziher/interface-forge","last_synced_at":"2025-04-08T03:14:27.591Z","repository":{"id":37691668,"uuid":"366436926","full_name":"Goldziher/interface-forge","owner":"Goldziher","description":"Graceful mock-data generation using TypeScript","archived":false,"fork":false,"pushed_at":"2025-02-07T08:20:04.000Z","size":1659,"stargazers_count":72,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-31T18:22:01.996Z","etag":null,"topics":["factories","faker","fakerjs","testing","tests","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/interface-forge","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Goldziher.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-11T15:49:16.000Z","updated_at":"2025-01-20T16:52:19.000Z","dependencies_parsed_at":"2023-02-14T20:01:26.055Z","dependency_job_id":"2893be89-5f0d-4a1c-acbc-a06e13963568","html_url":"https://github.com/Goldziher/interface-forge","commit_stats":{"total_commits":179,"total_committers":11,"mean_commits":"16.272727272727273","dds":0.5083798882681565,"last_synced_commit":"14adce8cc5cc029d6c30aa31e64a079570f8be39"},"previous_names":["goldziher/interfaceforge","tool-belt/interface-forge","goldziher/interface-forge"],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Goldziher%2Finterface-forge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Goldziher%2Finterface-forge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Goldziher%2Finterface-forge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Goldziher%2Finterface-forge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Goldziher","download_url":"https://codeload.github.com/Goldziher/interface-forge/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247767236,"owners_count":20992548,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["factories","faker","fakerjs","testing","tests","typescript"],"created_at":"2025-01-29T20:36:07.327Z","updated_at":"2025-04-08T03:14:27.551Z","avatar_url":"https://github.com/Goldziher.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Interface-Forge\n\nInterface-Forge is a TypeScript library for creating strongly typed mock data factories. This library builds upon [Faker.js](https://fakerjs.dev/) by providing a simple and intuitive `Factory` class that extends the `Faker` class from [Faker.js](https://fakerjs.dev/).\n\n## Installation\n\n```shell\nnpm install --save-dev interface-forge\n```\n\n## Basic Example\n\nTo create a factory, you need a TypeScript type:\n\n```typescript\n// types.ts\n\ninterface User {\n    firstName: string;\n    lastName: string;\n    email: string;\n    profile: {\n        profession: string;\n        gender: string;\n        age: number;\n    };\n}\n```\n\nPass the desired type as a generic argument when instantiating the `Factory` class, alongside default values for the factory:\n\n```typescript\n// factories.ts\nimport { Factory } from 'interface-forge';\nimport { User } from './types';\n\nconst UserFactory = new Factory\u003cUser\u003e((factory, iteration) =\u003e ({\n    firstName: factory.person.firstName(),\n    lastName: factory.person.lastName(),\n    email: factory.internet.email(),\n    profile: {\n        profession: factory.person.jobType(),\n        gender: factory.person.gender(),\n        age: 27 + iteration,\n    },\n}));\n```\n\nThen use the factory to create an object of the desired type in a test file:\n\n```typescript\n// User.spec.ts\n\ndescribe('User', () =\u003e {\n    const user = UserFactory.build();\n    // user == {\n    //     firstName: \"Johanne\",\n    //     lastName: \"Smith\",\n    //     email: \"js@example.com\",\n    //     profile: {\n    //         profession: \"Journalist\",\n    //         gender: \"Female\",\n    //         age: 27\n    //     },\n    // }\n    // ...\n});\n```\n\n## Factory Class Methods\n\n### `build`\n\nBuilds a single object based on the factory's schema. Optionally, you can pass an object to override specific properties.\n\n**Usage:**\n\n```typescript\nconst user = UserFactory.build();\n// user == {\n//     firstName: \"Johanne\",\n//     lastName: \"Smith\",\n//     email: \"js@example.com\",\n//     profile: {\n//         profession: \"Journalist\",\n//         gender: \"Female\",\n//         age: 27\n//     },\n// }\n\nconst customUser = UserFactory.build({ age: 35 });\n// customUser.age == 35\n```\n\n### `batch`\n\nGenerates a batch of objects based on the factory's schema. Optionally, you can pass an object or an array of objects to override specific properties for each instance.\n\n**Usage:**\n\n```typescript\nconst users = UserFactory.batch(3);\n// users == [\n//     { ... },\n//     { ... },\n//     { ... }\n// ]\n\nconst customUsers = UserFactory.batch(3, { age: 35 });\n// customUsers == [\n//     { ..., age: 35 },\n//     { ..., age: 35 },\n//     { ..., age: 35 }\n// ]\n\nconst variedUsers = UserFactory.batch(3, [\n    { age: 30 },\n    { age: 25 },\n    { age: 40 },\n]);\n// variedUsers == [\n//     { ..., age: 30 },\n//     { ..., age: 25 },\n//     { ..., age: 40 }\n// ]\n```\n\n### `use`\n\nCreates a reference to a function that can be used within the factory. This method allows for the encapsulation of a function and its arguments, enabling deferred execution.\n\n**Usage:**\n\n```typescript\nconst complexFactory = new Factory\u003cComplexObject\u003e((factory) =\u003e ({\n    name: factory.person.firstName(),\n    value: factory.number.int({ min: 1, max: 3 }),\n    options: {\n        type: '1',\n    },\n}));\n\nconst factoryWithOptions = new Factory\u003cComplexObject\u003e((factory) =\u003e ({\n    ...defaults,\n    options: {\n        type: '1',\n        children: factory.use(complexFactory.batch, 2),\n    },\n}));\n\nconst result = factoryWithOptions.build();\n// result.options.children == [\n//     { ... },\n//     { ... }\n// ]\n```\n\n### `iterate`\n\nCycles through the values of an iterable indefinitely.\n\n**Usage:**\n\n```typescript\nconst values = ['Value 1', 'Value 2', 'Value 3'];\nconst generator = UserFactory.iterate(values);\n\nconsole.log(generator.next().value); // 'Value 1'\nconsole.log(generator.next().value); // 'Value 2'\nconsole.log(generator.next().value); // 'Value 3'\nconsole.log(generator.next().value); // 'Value 1'\n```\n\n### `sample`\n\nSamples values randomly from an iterable, ensuring no immediate repetitions.\n\n**Usage:**\n\n```typescript\nconst values = [1, 2, 3];\nconst generator = UserFactory.sample(values);\n\nconsole.log(generator.next().value); // 1 (or 2, or 3)\nconsole.log(generator.next().value); // (different from the previous value)\n```\n\n## Contributing\n\nContributions of any kind are welcome! Please see the [contributing guide](CONTRIBUTING.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoldziher%2Finterface-forge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoldziher%2Finterface-forge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoldziher%2Finterface-forge/lists"}