How fazalerabbi/nestjs-cashier works
Main Components
• NestjsCashierModule: Represent the core module that developers import into their NestJS application. This module is configured via “forRootAsync” and serves as the entry point.
• Core Services:
• Config Service: “src/services/config.service.ts” is responsible for reading and structuring configuration options (like the Stripe secret key).
• Stripe Service: “src/services/stripe.service.ts” acts as the main interface to interact with Stripe, delegating specific operations to internal stripe service components.
• Sub-Components for Stripe Operations:
• Account Service (e.g., account.service.ts, accountlink.service.ts)
• Customer Service (customer.service.ts)
• Price Service (price.service.ts)
• Product Service (product.service.ts)
• SetupIntent Service (setupintent.service.ts)
• Subscription Service (subscription.service.ts)
The “src/services/stripe” subdirectory indicates that the Stripe integration is modularized, with each service handling a designated aspect of Stripe’s API.
• Supporting Elements:
• Enums (e.g., currency definitions in “src/enums”) which provide standardized constants.
• Interfaces (in “src/interfaces”) that define configuration options and possibly other types for strong typing and consistency.
• Utility functions (in “src/utils”), which assist various internal operations.
Relationships and Interactions
• The NestjsCashierModule is a facade that initializes the configuration and injects the necessary services (both config and Stripe services) into the consuming NestJS application.
• The Config Service supplies the module with necessary options (like the Stripe secret) that are then used by the Stripe Service to perform API calls.
• The primary Stripe Service makes use of specialized sub-services for handling various Stripe operations (customer management, product pricing, subscriptions, etc.). Each of these sub-services may have dependencies on some common logic (shared via the base service and constants).
• The interfaces ensure that components like the configuration options are type-safe, while enums enforce standardized data (e.g., currency types) across services.
• The tests (using Jest, as indicated by “jest.config.js” and service spec files) ensure that each of these interacting parts functions as expected.
Architectural Patterns and Key Design Principles
• Modular Design: The project is split into well-defined modules and services; the use of NestJS’s module system is paramount.
• Dependency Injection: The module leverages dependency injection (common in NestJS) to incorporate services (like the config and stripe services) and maintain loose coupling.
• Facade Pattern: The NestjsCashierModule serves as a facade to hide the complexity of multiple Stripe-related services.
• Separation of Concerns: There’s a clear split between configuration, business logic (Stripe operations), and utility/helper functions.
Relevant Technologies and Frameworks
• NestJS: The core framework on which the module is built.
• Stripe API: The external service being integrated, with multiple internal service components handling different aspects.
• TypeScript: Ensuring type safety for enums, interfaces, and service definitions.
• Jest: For unit testing various components of the module.