@billsdk/time-travel 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +105 -0
- package/dist/client/index.d.ts +53 -0
- package/dist/client/index.js +707 -0
- package/dist/client/index.js.map +1 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.js +243 -0
- package/dist/index.js.map +1 -0
- package/package.json +81 -0
package/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# @billsdk/time-travel
|
|
2
|
+
|
|
3
|
+
Time travel plugin for BillSDK - test subscription cycles by simulating time.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Advance time**: Jump forward by days, weeks, or months
|
|
8
|
+
- **Go to specific date**: Set any date for testing
|
|
9
|
+
- **Persistent state**: Simulated time survives server restarts
|
|
10
|
+
- **React overlay**: Beautiful floating UI for controlling time
|
|
11
|
+
- **Zero config**: Just add the plugin and you're ready
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @billsdk/time-travel
|
|
17
|
+
# or
|
|
18
|
+
pnpm add @billsdk/time-travel
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### 1. Add the plugin (server-side)
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { billsdk } from "billsdk";
|
|
27
|
+
import { timeTravelPlugin } from "@billsdk/time-travel";
|
|
28
|
+
|
|
29
|
+
const billing = billsdk({
|
|
30
|
+
database: drizzleAdapter(db, { schema }),
|
|
31
|
+
plugins: [
|
|
32
|
+
timeTravelPlugin(), // Only in development!
|
|
33
|
+
],
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### 2. Add the overlay (React)
|
|
38
|
+
|
|
39
|
+
```tsx
|
|
40
|
+
import { TimeTravelOverlay } from "@billsdk/time-travel/react";
|
|
41
|
+
|
|
42
|
+
function App() {
|
|
43
|
+
return (
|
|
44
|
+
<>
|
|
45
|
+
<YourApp />
|
|
46
|
+
{process.env.NODE_ENV === "development" && (
|
|
47
|
+
<TimeTravelOverlay baseUrl="/api/billing" />
|
|
48
|
+
)}
|
|
49
|
+
</>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## API Endpoints
|
|
55
|
+
|
|
56
|
+
The plugin adds these endpoints to your BillSDK API:
|
|
57
|
+
|
|
58
|
+
| Endpoint | Method | Description |
|
|
59
|
+
|----------|--------|-------------|
|
|
60
|
+
| `/time-travel/get` | GET | Get current time state |
|
|
61
|
+
| `/time-travel/set` | POST | Set specific simulated time |
|
|
62
|
+
| `/time-travel/advance` | POST | Advance time by days/months/hours |
|
|
63
|
+
| `/time-travel/reset` | POST | Reset to real time |
|
|
64
|
+
|
|
65
|
+
### Examples
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
// Get current state
|
|
69
|
+
const res = await fetch("/api/billing/time-travel/get");
|
|
70
|
+
const { simulatedTime, isSimulated, realTime } = await res.json();
|
|
71
|
+
|
|
72
|
+
// Set specific time
|
|
73
|
+
await fetch("/api/billing/time-travel/set", {
|
|
74
|
+
method: "POST",
|
|
75
|
+
headers: { "Content-Type": "application/json" },
|
|
76
|
+
body: JSON.stringify({ date: "2024-06-15T12:00:00.000Z" }),
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// Advance by 1 month
|
|
80
|
+
await fetch("/api/billing/time-travel/advance", {
|
|
81
|
+
method: "POST",
|
|
82
|
+
headers: { "Content-Type": "application/json" },
|
|
83
|
+
body: JSON.stringify({ months: 1 }),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// Reset to real time
|
|
87
|
+
await fetch("/api/billing/time-travel/reset", { method: "POST" });
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## How it works
|
|
91
|
+
|
|
92
|
+
1. The plugin adds a `timeProvider` to the BillSDK context
|
|
93
|
+
2. Business logic (renewals, trials, etc.) uses `ctx.timeProvider.now()` instead of `new Date()`
|
|
94
|
+
3. The simulated time is stored in a database table (`time_travel_state`)
|
|
95
|
+
4. The React overlay calls the plugin endpoints to control the time
|
|
96
|
+
|
|
97
|
+
## Warning
|
|
98
|
+
|
|
99
|
+
**This plugin is for development and testing only.** It modifies how BillSDK perceives time, which can affect subscription states, renewals, and other time-based logic.
|
|
100
|
+
|
|
101
|
+
Do NOT include this plugin in production builds.
|
|
102
|
+
|
|
103
|
+
## License
|
|
104
|
+
|
|
105
|
+
MIT
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
|
|
3
|
+
interface TimeTravelOverlayProps {
|
|
4
|
+
/**
|
|
5
|
+
* Base URL for the BillSDK API
|
|
6
|
+
* @default "/api/billing"
|
|
7
|
+
*/
|
|
8
|
+
baseUrl?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Position of the overlay
|
|
11
|
+
* @default "bottom-right"
|
|
12
|
+
*/
|
|
13
|
+
position?: "bottom-right" | "bottom-left" | "top-right" | "top-left";
|
|
14
|
+
/**
|
|
15
|
+
* Initial collapsed state
|
|
16
|
+
* @default true
|
|
17
|
+
*/
|
|
18
|
+
defaultCollapsed?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Customer ID to control time for
|
|
21
|
+
* Required for per-customer time simulation
|
|
22
|
+
*/
|
|
23
|
+
customerId?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Time Travel Overlay Component
|
|
27
|
+
*
|
|
28
|
+
* A floating UI component that allows you to control simulated time
|
|
29
|
+
* for testing billing cycles, trials, and renewals.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```tsx
|
|
33
|
+
* import { TimeTravelOverlay } from "@billsdk/time-travel/react";
|
|
34
|
+
*
|
|
35
|
+
* function App() {
|
|
36
|
+
* const { user } = useAuth();
|
|
37
|
+
* return (
|
|
38
|
+
* <>
|
|
39
|
+
* <YourApp />
|
|
40
|
+
* {process.env.NODE_ENV === "development" && user?.customerId && (
|
|
41
|
+
* <TimeTravelOverlay
|
|
42
|
+
* baseUrl="/api/billing"
|
|
43
|
+
* customerId={user.customerId}
|
|
44
|
+
* />
|
|
45
|
+
* )}
|
|
46
|
+
* </>
|
|
47
|
+
* );
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare function TimeTravelOverlay({ baseUrl, position, defaultCollapsed, customerId, }: TimeTravelOverlayProps): react_jsx_runtime.JSX.Element;
|
|
52
|
+
|
|
53
|
+
export { TimeTravelOverlay, type TimeTravelOverlayProps };
|