How xendit/xendit-node works
Project Type and Purpose
• This is an open-source Node.js SDK that wraps Xendit’s REST API endpoints, providing TypeScript-typed clients for products such as Invoice, PaymentRequest, PaymentMethod, Refund, Transaction, Balance, Customer and Payout.
• Its goal is to offer application developers a convenient, modular interface to invoke Xendit services from server-side code.
File Structure Analysis
• Top-Level Modules:
– balance_and_transaction, customer, invoice, payment_method, payment_request, payout, refund
– Each module contains an apis/ directory (methods to call endpoints) and a models/ directory (TypeScript interfaces representing request/response payloads).
• Core Runtime:
– runtime.ts defines the HTTP client abstraction (configuration of API key, custom URL, request/response middleware).
• Configuration & Tooling:
– openapi-generator files signal that module code is generated from an OpenAPI spec.
– .eslintrc.json, tsconfig.json, package.json drive linting, compilation and package metadata.
• Documentation & Testing:
– docs/ holds Markdown files for each model and API overview.
– test/ contains unit tests (invoice.test.ts, payment_api.test.ts) and test utilities.
README Insights
• Confirms project purpose: a Node SDK instantiated with a secretKey (and optional URL override).
• Lists supported products and points to documentation per product.
• Highlights TypeScript support and Node 18+ requirement.
a. Main Components
– Client Application: user-written Node.js code using the SDK
– XenditNodeSDK Core:
• Configuration Layer (secretKey, xenditURL, timeout)
• Runtime HTTP Client (runtime.ts)
• Product Modules (apis + models for each product)
– External Services: Xendit REST API endpoints on https://api.xendit.co
– Developer Tools: openapi-generator, TypeScript compiler, Jest test runner
b. Relationships and Data Flow
1. Client Application calls a product method (e.g., xenditClient.Invoice.createInvoice).
2. Product Module’s API class constructs the HTTP request object and passes it to Runtime HTTP Client.
3. Runtime sends the request over HTTPS to the Xendit API.
4. Xendit API returns JSON response; Runtime deserializes it into the corresponding model interface.
5. Response model is returned to the Client Application.
6. Tests simulate scenarios by invoking methods against either a mock server or the real API (via custom xenditURL).
c. Architectural Patterns and Principles
– Modularization: one module per Xendit product to enforce separation of concerns.
– Code Generation: consistent models and API clients are generated from a shared OpenAPI definition, ensuring parity with the server’s contract.
– Dependency Injection: clients accept configuration parameters to allow testing against alternative endpoints.
– Layered Architecture:
• Presentation (public API surface exposed by index.ts)
• Business Logic (API classes in apis/)
• Data Models (interfaces in models/)
• Infrastructure (runtime.ts HTTP layer)
d. Key Technologies and Libraries
– Node.js (runtime environment)
– TypeScript (static typing, code generation)
– Axios or Fetch (HTTP calls abstracted in runtime.ts)
– OpenAPI Generator (automated client and model code generation)
– Jest (testing framework)