Extraform is a lightweight library for handling form data and search parameters in web applications.
To install Extraform, use your preferred package manager:
npm install extraform
# or
yarn add extraform
# or
pnpm add extraform
Extraform uses the name
property of input elements to create structured form data. To access form values:
- Ensure all input elements have a
name
attribute. - For non-input values, use hidden inputs with the desired name.
Extraform uses a special naming convention to create nested objects and arrays from form inputs:
- Use dot notation (
.
) for nested objects - Use indexed notation (
[index]
) for arrays
Example:
<input name="user.firstName" value="John" />
<input name="user.lastName" value="Doe" />
<input name="age" value="30" />
<input name="contacts[0].email" value="friend1@example.com" />
<input name="contacts[0].phone" value="1234567890" />
<input name="contacts[1].email" value="friend2@example.com" />
<input name="contacts[1].phone" value="0987654321" />
This will result in the following data structure:
type FormData = {
user: {
firstName: string;
lastName: string;
};
age: string;
contacts: Array<{
email: string;
phone: string;
}>;
};
Extraform supports TypeScript and provides type inference for your input names based on your structured data type. This ensures type safety when defining input names. Here's how to use it:
import { inferInputNames } from 'extraform';
// Define your form structure
type FormStructure = {
user: {
firstName: string;
lastName: string;
};
age: number;
contacts: Array<{
email: string;
phone: string;
}>;
};
// Infer input names from the FormStructure
type InferredInputNames = inferInputNames<FormStructure>;
// And use these names safely in your JSX or HTML
<input name={"user.firstName" satisfies InferredInputNames} />
<input name={"contacts[0].email" satisfies InferredInputNames} />
// Processing the form data
import { extractStructuredFormData } from 'extraform';
const formData = new FormData(formElement);
const data = extractStructuredFormData<FormStructure>(formData);
This approach ensures that:
- Your input names are correctly inferred from your
FormStructure
type. - You get TypeScript errors if you try to use an input name that doesn't match the structure.
- The processed form data matches your
FormStructure
type.
Extraform provides three main functions to process form data and search parameters:
extractStructuredFormData
: Processes form dataextractSearchDataFromRequest
: Processes URL search parametersextractFormDataFromRequest
: Processes form data from a request object
Example usage:
import { extractStructuredFormData, extractSearchDataFromRequest, extractFormDataFromRequest } from 'extraform';
// Processing form data
const formData = new FormData(formElement);
const processedFormData = extractStructuredFormData(formData);
// Processing search parameters
const searchParams = new URLSearchParams(window.location.search);
const processedSearchData = extractSearchDataFromRequest(searchParams);
// Processing form data from a request object (e.g., in a server environment)
async function handleRequest(request: Request) {
const formData = await extractFormDataFromRequest(request);
// Use the extracted form data
}
A type-level utility that infers input names from a given structure type T
.
Processes a FormData
object and returns a structured JavaScript object.
Processes a URLSearchParams
object and returns a structured JavaScript object.
Extracts and processes form data from a Request object. This is particularly useful in server-side environments or when working with fetch API requests.
We welcome contributions! Please follow these steps:
- Check the issue tracker for open issues that interest you.
- If you have a new feature idea or notice a bug, open a new issue to discuss it before making changes.
- Fork the repository and make your changes.
- Run
pnpm run test
to run the tests. - Run
pnpm run build
to build the project. - Add documentation if needed.
- Push your changes to your fork on GitHub.
- Submit a pull request to the main repository.
- Wait for the review and merge.
Extraform is MIT licensed.