@kudosapi/sdk 0.1.1 → 0.1.2
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 +103 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -36,6 +36,109 @@ const kudos = new Kudos({
|
|
|
36
36
|
});
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
+
## API Keys: Test vs Live
|
|
40
|
+
|
|
41
|
+
KudosAPI provides two types of API keys for each project:
|
|
42
|
+
|
|
43
|
+
| Key Type | Prefix | Purpose |
|
|
44
|
+
|----------|--------|---------|
|
|
45
|
+
| **Live** | `kudo_live_` | Production use. Events are processed and notifications are sent. |
|
|
46
|
+
| **Test** | `kudo_test_` | Development/testing. Events are accepted but NOT processed or stored. |
|
|
47
|
+
|
|
48
|
+
### Using Test Keys
|
|
49
|
+
|
|
50
|
+
Use test keys during development to verify your integration without generating real notifications:
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
// Development - events are accepted but not processed
|
|
54
|
+
const kudos = new Kudos({ apiKey: 'kudo_test_xxx' });
|
|
55
|
+
|
|
56
|
+
// The SDK will warn if you accidentally use a test key in production
|
|
57
|
+
// "[KudosAPI] Warning: Using a test API key in production. Events will not be processed."
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Environment-Based Configuration
|
|
61
|
+
|
|
62
|
+
A common pattern is to use environment variables to switch between test and live keys:
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
const kudos = new Kudos({
|
|
66
|
+
apiKey: process.env.KUDOS_API_KEY!, // kudo_test_xxx in dev, kudo_live_xxx in prod
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
# .env.local (development)
|
|
72
|
+
KUDOS_API_KEY=kudo_test_xxx
|
|
73
|
+
|
|
74
|
+
# .env.production
|
|
75
|
+
KUDOS_API_KEY=kudo_live_xxx
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Local Development
|
|
79
|
+
|
|
80
|
+
### Testing Against Local KudosAPI
|
|
81
|
+
|
|
82
|
+
If you're running KudosAPI locally, override the `baseUrl`:
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
const kudos = new Kudos({
|
|
86
|
+
apiKey: 'kudo_test_xxx',
|
|
87
|
+
baseUrl: 'http://localhost:3000', // Your local KudosAPI instance
|
|
88
|
+
debug: true, // Enable debug logging to see requests
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Mocking in Tests
|
|
93
|
+
|
|
94
|
+
For unit tests, mock the SDK to avoid network calls:
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
// __mocks__/@kudosapi/sdk.ts (Jest/Vitest)
|
|
98
|
+
export class Kudos {
|
|
99
|
+
track = vi.fn().mockResolvedValue({ success: true, eventId: 'evt_mock' });
|
|
100
|
+
sale = vi.fn().mockResolvedValue({ success: true, eventId: 'evt_mock' });
|
|
101
|
+
signup = vi.fn().mockResolvedValue({ success: true, eventId: 'evt_mock' });
|
|
102
|
+
milestone = vi.fn().mockResolvedValue({ success: true, eventId: 'evt_mock' });
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
// your-code.test.ts
|
|
108
|
+
vi.mock('@kudosapi/sdk');
|
|
109
|
+
|
|
110
|
+
it('tracks sale on checkout', async () => {
|
|
111
|
+
const kudos = new Kudos({ apiKey: 'test' });
|
|
112
|
+
await processCheckout(kudos);
|
|
113
|
+
expect(kudos.sale).toHaveBeenCalledWith(99, 'USD', expect.any(Object));
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Integration Testing
|
|
118
|
+
|
|
119
|
+
For integration tests that need to hit the real API, use test keys:
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
// integration.test.ts
|
|
123
|
+
import { Kudos } from '@kudosapi/sdk';
|
|
124
|
+
|
|
125
|
+
const kudos = new Kudos({
|
|
126
|
+
apiKey: process.env.KUDOS_TEST_API_KEY!, // kudo_test_xxx
|
|
127
|
+
debug: true,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('successfully sends event to KudosAPI', async () => {
|
|
131
|
+
const result = await kudos.track({
|
|
132
|
+
type: 'integration_test',
|
|
133
|
+
title: 'Test event',
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// Test keys return success but don't process events
|
|
137
|
+
expect(result.success).toBe(true);
|
|
138
|
+
expect(result.eventId).toMatch(/^evt_/);
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
39
142
|
## Methods
|
|
40
143
|
|
|
41
144
|
### `track(event, options?)`
|
package/package.json
CHANGED