Skip to content

clearandfizzy/Functional-Factory-Builder-Pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Example Functional Factory/Builder Pattern

Overview

This project provides a set of serverless functions using Netlify's serverless functions framework. These functions include the ability to build documents with various properties based on initial input headers.

Directory Structure

├── netlify
│   ├── functions
│   │   └── factory
│   │       ├── builders
│   │       │   ├── makeName.ts
│   │       │   ├── makeImages.ts
│   │       │   └── makeType.ts
│   │       └── factory.ts
│   └── interfaces
│       ├── Builder.interface.ts
│       └── Document.interface.ts
└── README.md

Files Description

netlify/functions/factory/builders/makeName.ts

Defines a function to set the name of a document based on its initial value.

netlify/functions/factory/builders/makeImages.ts

Defines a function to add images to a document based on its type.

netlify/functions/factory/builders/makeType.ts

Defines a function to set the type of a document based on its name.

netlify/functions/factory/factory.ts

Combines the builders to create a document based on input headers. It processes the document through a series of builders and returns the final document structure.

netlify/interfaces/Builder.interface.ts

Defines the interface for a builder function that modifies the document.

netlify/interfaces/Document.interface.ts

Defines the structure of the document and related enumerations for type and name.

Usage

To deploy the functions, ensure you have the Netlify CLI installed and run the following command in the root directory of the project:

netlify dev

This command will start the local development server, allowing you to test the serverless functions.

Example

An example request to the deployed function might include headers such as:

{
  "initial": "1"
}

The response will be a JSON object representing the constructed document:

{
  "document": {
    "initial": 1,
    "name": "Hello World",
    "type": "basic",
    "images": [
      {
        "url": "https://www.example.com/image.jpg",
        "description": "A high-quality modern realistic image"
      }
    ]
  }
}

Contribution

To contribute to this project, please fork the repository and create a pull request with your changes.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Detailed Explanation

The code in this project is designed to generate a document with specific attributes based on an initial input value. Here's a detailed explanation of how the code works:

Overview

The main function (handler in factory.ts) takes an input header (initial), processes it through a series of builder functions, and returns a structured document. The builder functions (makeName, makeType, and makeImages) each modify the document by adding or changing its properties.

Detailed Explanation

1. Interfaces

  • DocumentInterface: Defines the structure of a document with possible properties like initial, name, type, and images.
  • BuilderInterface: Defines the interface for a builder function, which takes a document and returns a modified document.

2. Builders

  • makeName.ts:

    • Sets the name property of the document based on the initial value.
    • If initial is 1, the name is set to Hello World.
    • Otherwise, the name is set to You're Welcome.
  • makeType.ts:

    • Sets the type property of the document based on the name value.
    • If the name is Hello World, the type is set to basic.
    • Otherwise, the type is set to complex.
  • makeImages.ts:

    • Adds images to the document based on its type.
    • For basic type, adds one image.
    • For advanced type, adds two images.
    • For complex type, adds three images.

3. Factory Function

  • factory.ts:
    • The handler function is the entry point for the serverless function.
    • It reads the initial value from the input headers.
    • It initializes an empty document with the initial value.
    • It processes the document through the builder functions (makeName, makeType, and makeImages) using the reduce method.
    • Each builder function modifies the document and passes it to the next function.
    • Finally, it returns the fully constructed document as a JSON response.

Example Flow

  1. Input Header:

    {
      "initial": "1"
    }
  2. Processing:

    • The initial document is created: {'initial': 1}
    • makeName sets the name to Hello World.
    • makeType sets the type to basic.
    • makeImages adds one image to the document.
  3. Output Document:

    {
      "document": {
        "initial": 1,
        "name": "Hello World",
        "type": "basic",
        "images": [
          {
            "url": "https://www.example.com/image.jpg",
            "description": "A high-quality modern realistic image"
          }
        ]
      }
    }

Summary

The code modularly constructs a document by sequentially applying transformations through builder functions. Each builder function modifies the document based on its current state, ultimately producing a structured and fully populated document.

About

Functional-Factory-Builder-Pattern

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published