Practices and Methods for Building an E-commerce Site with AI-Assisted Coding
This article explores how to transform AI-generated code into a maintainable and scalable project through a controlled workflow. Using an e-commerce site with around ten pages as an example, it covers requirement decomposition, project structure planning, static type migration, AI-assisted error fixing, responsive design checks, and essential human review steps.
1. Background and Problem Definition
AI-assisted coding tools can now generate complete frontend pages and basic logic from natural language descriptions. However, in real-world projects, fast generation does not equate to high delivery quality. Common issues in generated code include inconsistent data structures across pages, scattered permission checks, missing or inaccurate type definitions, and inconsistent styles across different screen sizes.
Therefore, the core question this article addresses is: How can AI-generated code go through a controlled process to ultimately become a maintainable and extensible project? Below, we use an e-commerce site with about ten pages as an example to explain the methodology for each stage.
2. Decomposing Requirements into Verifiable Subtasks
Directly asking AI to “build an e-commerce website” usually results in a loosely structured prototype. A more effective approach is to first list the pages and define the inputs, outputs, and access conditions for each.
A typical e-commerce site can be divided into the following pages:
- Public pages: Home, Product List, Product Detail, Search Results.
- Logged-in user pages: Shopping Cart, Order Confirmation, Payment Result, Order List, User Profile.
- Auxiliary pages: Login/Registration, 404 Page.
During decomposition, three things must be clarified. First, what data entities each page depends on, such as Product, CartItem, and Order. Second, the navigation relationships between pages, such as going from the Shopping Cart to Order Confirmation. Third, which pages require permissions. Once these three pieces of information are clearly defined, the subsequently generated code will be much closer to expectations.
3. Project Structure Planning
Even without explicit constraints, AI can generate a reasonably structured directory. The key is to confirm whether the structure meets the needs of future maintenance. A reliable organization method is as follows:
src/pages/public/ // Public pages (Home, Product List, Product Detail)user/ // Pages requiring login (Shopping Cart, Order, User Profile)components/layout/ // Layout components (Header, Footer, Sidebar)common/ // Common components (Button, Card, Pagination)types/ // Data type definitionsmock/ // Mock dataservices/ // API request wrappers
This division offers four benefits:
- Clear permission grouping. By splitting page directories based on access conditions, new pages can be easily categorized, and route guards can be uniformly configured.
- Separation of layout and common components. Layout components handle the page framework, while common components handle reusable UI units; modifications to either do not interfere with the other.
- Independent type definitions. Centralized management of data structures ensures pages and service layers share the same definitions, reducing inconsistencies caused by duplicate declarations.
- Independent mock data. During development, mock data replaces real APIs. When switching data sources later, only the service layer needs replacement, leaving page code largely unchanged.
4. Migrating from Dynamic to Static Type Constraints
Using dynamic typing speeds up development in the early stages, but as pages increase, implicit data structure conventions can easily get out of control. Introducing static type constraints midway can catch numerous errors at compile time and make refactoring safer.
4.1 Migration Steps
It is recommended to proceed in the following order to avoid modifying all files at once:
- Start with a loose mode that allows temporarily ambiguous types, ensuring the project can still build normally.
- Prioritize defining types for core data models, such as Product, CartItem, Order, and User.
- Follow dependency relationships, migrating from bottom-level services and utility functions up to the pages.
- Gradually tighten checking rules and reduce the use of loose types.
4.2 Core Data Model Example
// Product definition in types/product.tsinterface Product {id: string;name: string;price: number; // Unit: cents, to avoid floating-point errorsstock: number;imageUrl?: string; // Optional field, requires null check in the page}interface CartItem {product: Product;quantity: number;}
The value of type definitions is that when a page reads a field, the compiler checks if the field exists and if the type matches. For example, if a page treats the price as a string, an error will be thrown at compile time rather than the user seeing an incorrect amount.
4.3 Common Type Errors
During migration, errors mostly fall into the following categories:
| Error Type | Typical Manifestation | Handling Method |
|---|---|---|
| Missing property | Accessing a field that does not exist in the type | Add to type definition or fix typo |
| Unhandled nullable value | Optional field directly involved in calculation | Add null check or set default value |
| Event parameter mismatch | Form event value type does not match expectation | Declare explicit parameter type for event handlers |
| Incorrect async return value | Request result not parsed according to agreed structure | Uniformly define return type in the service layer |
It is common for a medium-sized project to encounter over twenty type errors during migration. Most of these are low-cost to fix, but when there are many, they should be processed in batches by category.
5. Using AI to Assist in Error Fixing
When handing errors over to AI for fixing, efficiency depends on the quality of the feedback. The following practices can significantly improve the success rate of fixes:
- Provide complete error information, including file path, line number, and compiler output.
- Attach relevant type definitions so the AI understands the expected data structure.
- Process errors in batches by category. For example, fix all “Missing property” errors first, then handle “Unhandled nullable value”.
- Run type checks and build immediately after each batch to confirm no new issues were introduced.
Be cautious: AI sometimes uses loose types or type assertions to eliminate errors, making the error disappear on the surface while the underlying issue remains. After fixes, check whether the code contains a large number of loose type declarations or type assertions, and verify their rationale one by one.
6. Page Presentation and Responsive Checks
Generated pages usually perform well on the large-screen devices used by developers, but systematic checks are still required:
- Ensure layout wraps and aligns properly at common breakpoints (mobile, tablet, desktop).
- Check if navigation bars and sidebars collapse or expand correctly at different widths.
- Verify if product images have placeholder displays when missing or failing to load.
- Ensure information in the user profile is clearly grouped, such as displaying basic info, account security, and order overview separately.
It is recommended to use the browser’s device emulation feature during checks, record issues for each page, and hand them over to AI for fixing uniformly.
7. Steps Requiring Human Review
AI-generated code can handle most functional tasks, but the following areas must be reviewed by developers:
- Permission validation. Frontend route guards only improve user experience and cannot replace server-side authentication. All user data APIs require server-side permission validation.
- Amount and stock calculations. Logic for price, discounts, and stock deductions should be confirmed on the server side; frontend calculation results are for display only.
- Sensitive information handling. Passwords, tokens, and similar information should not appear in code, logs, or mock data. They should be injected via environment variables or a key management service.
- Dependencies and third-party code. Dependency packages introduced by AI need to be verified for source, license, and maintenance status.
8. A Reusable Workflow
Combining the above content, the recommended workflow is as follows:
- List the pages, annotating data dependencies and access conditions for each.
- Determine the directory structure and specify constraints clearly in the prompt.
- Generate core data models and mock data first, then generate the pages.
- Introduce static type constraints and migrate in batches according to dependency order.
- Run type checks and build after each batch of fixes.
- Check page presentation across breakpoints against the checklist.
- Have developers review permissions, amounts, sensitive information, and dependencies.
AI can significantly reduce the time spent on page scaffolding and error fixing, but project quality still depends on whether developers provide clear constraints, validate results at each stage, and safeguard the bottom lines of security and business correctness.
