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.
├── netlify
│ ├── functions
│ │ └── factory
│ │ ├── builders
│ │ │ ├── makeName.ts
│ │ │ ├── makeImages.ts
│ │ │ └── makeType.ts
│ │ └── factory.ts
│ └── interfaces
│ ├── Builder.interface.ts
│ └── Document.interface.ts
└── README.md
Defines a function to set the name of a document based on its initial value.
Defines a function to add images to a document based on its type.
Defines a function to set the type of a document based on its name.
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.
Defines the interface for a builder function that modifies the document.
Defines the structure of the document and related enumerations for type and name.
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.
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"
}
]
}
}
To contribute to this project, please fork the repository and create a pull request with your changes.
This project is licensed under the MIT License. See the LICENSE file for details.
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:
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.
DocumentInterface
: Defines the structure of a document with possible properties likeinitial
,name
,type
, andimages
.BuilderInterface
: Defines the interface for a builder function, which takes a document and returns a modified document.
-
makeName.ts
:- Sets the
name
property of the document based on theinitial
value. - If
initial
is1
, the name is set toHello World
. - Otherwise, the name is set to
You're Welcome
.
- Sets the
-
makeType.ts
:- Sets the
type
property of the document based on thename
value. - If the
name
isHello World
, the type is set tobasic
. - Otherwise, the type is set to
complex
.
- Sets the
-
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.
- Adds images to the document based on its
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
, andmakeImages
) using thereduce
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.
- The
-
Input Header:
{ "initial": "1" }
-
Processing:
- The initial document is created:
{'initial': 1}
makeName
sets the name toHello World
.makeType
sets the type tobasic
.makeImages
adds one image to the document.
- The initial document is created:
-
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" } ] } }
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.