@stamprally/server 0.8.0 → 0.10.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 +10 -18
- package/dist/index.cjs +366 -629
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +79 -135
- package/dist/index.d.ts +79 -135
- package/dist/index.js +368 -629
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,105 +1,89 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import * as _stamprally_core from '@stamprally/core';
|
|
2
|
+
import { UserRallyState, AdminRallyConfig } from '@stamprally/core';
|
|
3
|
+
export { UserRallyState } from '@stamprally/core';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
+
interface UserClaimRecord {
|
|
6
|
+
readonly rallyId: string;
|
|
7
|
+
readonly userId: string;
|
|
8
|
+
readonly rewardId: string;
|
|
9
|
+
readonly ticketNumber: string;
|
|
10
|
+
readonly timestamp: number;
|
|
11
|
+
}
|
|
5
12
|
interface ServerPersistenceAdapter {
|
|
6
|
-
acquireLock(lockKey: string, ttlMs: number): Promise<boolean>;
|
|
7
|
-
releaseLock(lockKey: string): Promise<void>;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
13
|
+
acquireLock(rallyId: string, lockKey: string, ttlMs: number): Promise<boolean>;
|
|
14
|
+
releaseLock(rallyId: string, lockKey: string): Promise<void>;
|
|
15
|
+
getRewardStock(rallyId: string, rewardId: string): Promise<number | null>;
|
|
16
|
+
decrementRewardStock(rallyId: string, rewardId: string): Promise<{
|
|
17
|
+
readonly success: boolean;
|
|
18
|
+
readonly remainingStock: number;
|
|
11
19
|
}>;
|
|
12
|
-
|
|
13
|
-
|
|
20
|
+
restoreRewardStock?(rallyId: string, rewardId: string): Promise<void>;
|
|
21
|
+
getIdempotentResult<T>(rallyId: string, key: string): Promise<T | null>;
|
|
22
|
+
saveIdempotentResult<T>(rallyId: string, key: string, result: T, ttlMs: number): Promise<void>;
|
|
23
|
+
getUserClaimCount(rallyId: string, userId: string, rewardId: string): Promise<number>;
|
|
24
|
+
recordUserClaim(params: UserClaimRecord): Promise<void>;
|
|
25
|
+
recordClaim?(params: UserClaimRecord): Promise<void>;
|
|
26
|
+
incrementUserClaimCount?(rallyId: string, userId: string, rewardId: string): Promise<void>;
|
|
14
27
|
getUserState(rallyId: string, userId: string): Promise<UserRallyState | null>;
|
|
15
28
|
saveUserState(rallyId: string, userId: string, state: UserRallyState): Promise<void>;
|
|
16
|
-
recordAuditLog(log:
|
|
29
|
+
recordAuditLog(log: AuditLog): Promise<void>;
|
|
17
30
|
}
|
|
18
31
|
interface InMemoryServerPersistenceOptions {
|
|
19
32
|
readonly stocks?: Readonly<Record<string, number>>;
|
|
20
33
|
}
|
|
21
|
-
/** Deterministic adapter for tests and small single-process deployments. */
|
|
22
34
|
declare class InMemoryServerPersistenceAdapter implements ServerPersistenceAdapter {
|
|
23
35
|
#private;
|
|
24
36
|
constructor(options?: InMemoryServerPersistenceOptions);
|
|
25
|
-
acquireLock(
|
|
26
|
-
releaseLock(
|
|
27
|
-
|
|
37
|
+
acquireLock(rallyId: string, key: string, ttlMs: number): Promise<boolean>;
|
|
38
|
+
releaseLock(rallyId: string, key: string): Promise<void>;
|
|
39
|
+
getRewardStock(rallyId: string, rewardId: string): Promise<number | null>;
|
|
40
|
+
decrementRewardStock(rallyId: string, rewardId: string): Promise<{
|
|
28
41
|
success: boolean;
|
|
29
42
|
remainingStock: number;
|
|
30
43
|
}>;
|
|
31
|
-
|
|
32
|
-
|
|
44
|
+
restoreRewardStock(rallyId: string, rewardId: string): Promise<void>;
|
|
45
|
+
getIdempotentResult<T>(rallyId: string, key: string): Promise<T | null>;
|
|
46
|
+
saveIdempotentResult<T>(rallyId: string, key: string, result: T, ttlMs: number): Promise<void>;
|
|
47
|
+
getUserClaimCount(rallyId: string, userId: string, rewardId: string): Promise<number>;
|
|
33
48
|
getUserState(rallyId: string, userId: string): Promise<UserRallyState | null>;
|
|
34
49
|
saveUserState(rallyId: string, userId: string, state: UserRallyState): Promise<void>;
|
|
35
|
-
recordAuditLog(log:
|
|
36
|
-
getAuditLogs(): ReadonlyArray<
|
|
50
|
+
recordAuditLog(log: AuditLog): Promise<void>;
|
|
51
|
+
getAuditLogs(): ReadonlyArray<AuditLog>;
|
|
52
|
+
recordUserClaim(params: UserClaimRecord): Promise<void>;
|
|
53
|
+
incrementUserClaimCount(rallyId: string, userId: string, rewardId: string): Promise<void>;
|
|
54
|
+
getClaimRecords(): ReadonlyArray<UserClaimRecord>;
|
|
55
|
+
/** @deprecated Use recordUserClaim with a ticket number and timestamp. */
|
|
56
|
+
recordClaim(params: UserClaimRecord): Promise<void>;
|
|
57
|
+
recordClaim(rallyId: string, userId: string, rewardId: string): Promise<void>;
|
|
37
58
|
}
|
|
38
59
|
|
|
39
|
-
type
|
|
40
|
-
readonly type: "qr";
|
|
41
|
-
readonly token: string;
|
|
42
|
-
} | {
|
|
43
|
-
readonly type: "passcode";
|
|
44
|
-
readonly code: string;
|
|
45
|
-
} | {
|
|
46
|
-
readonly type: "gps";
|
|
47
|
-
readonly latitude: number;
|
|
48
|
-
readonly longitude: number;
|
|
49
|
-
} | {
|
|
50
|
-
readonly type: "custom";
|
|
51
|
-
readonly value: unknown;
|
|
52
|
-
};
|
|
53
|
-
interface UniversalCheckInRequest {
|
|
54
|
-
readonly rallyId: string;
|
|
55
|
-
readonly userId: string;
|
|
56
|
-
readonly spotId: string;
|
|
57
|
-
readonly context: UniversalVerificationContext;
|
|
58
|
-
readonly idempotencyKey: string;
|
|
59
|
-
readonly now?: string;
|
|
60
|
-
}
|
|
61
|
-
interface UniversalClaimRewardRequest {
|
|
62
|
-
readonly rallyId: string;
|
|
63
|
-
readonly userId: string;
|
|
64
|
-
readonly rewardId: string;
|
|
65
|
-
readonly idempotencyKey: string;
|
|
66
|
-
readonly staffPasscode?: string;
|
|
67
|
-
readonly staffId?: string;
|
|
68
|
-
readonly now?: string;
|
|
69
|
-
}
|
|
70
|
-
type UniversalCheckInResult = {
|
|
71
|
-
readonly ok: true;
|
|
72
|
-
readonly state: UserRallyState;
|
|
73
|
-
} | {
|
|
60
|
+
type ErrorResponse = {
|
|
74
61
|
readonly ok: false;
|
|
75
62
|
readonly code: string;
|
|
76
63
|
readonly message: string;
|
|
77
64
|
};
|
|
78
|
-
|
|
79
|
-
readonly
|
|
80
|
-
readonly
|
|
81
|
-
readonly
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
readonly authenticate?: (request: Request) => Promise<string | null> | string | null;
|
|
85
|
-
}
|
|
86
|
-
/** Atomic, adapter-backed check-in service for the universal model. */
|
|
87
|
-
declare class UniversalRallyServer {
|
|
65
|
+
type ClaimResponse = {
|
|
66
|
+
readonly ok: true;
|
|
67
|
+
readonly state: UserRallyState;
|
|
68
|
+
readonly claimTicketNumber?: string;
|
|
69
|
+
} | ErrorResponse;
|
|
70
|
+
declare class StampRallyServer {
|
|
88
71
|
#private;
|
|
89
|
-
constructor(config: AdminRallyConfig
|
|
90
|
-
|
|
72
|
+
constructor(config: AdminRallyConfig, persistence: ServerPersistenceAdapter, options?: ServerOptions);
|
|
73
|
+
handle(request: Request): Promise<Response>;
|
|
91
74
|
handleCheckIn(request: Request): Promise<Response>;
|
|
92
75
|
handleClaimReward(request: Request): Promise<Response>;
|
|
93
76
|
handleSync(request: Request): Promise<Response>;
|
|
94
|
-
|
|
95
|
-
|
|
77
|
+
checkIn(request: CheckInRequest & {
|
|
78
|
+
readonly userId: string;
|
|
79
|
+
}): Promise<CheckInResponse>;
|
|
80
|
+
claimReward(request: ClaimRewardRequest & {
|
|
81
|
+
readonly userId: string;
|
|
82
|
+
}): Promise<ClaimResponse>;
|
|
96
83
|
sync(rallyId: string, userId: string): Promise<UserRallyState>;
|
|
97
84
|
}
|
|
98
85
|
|
|
99
|
-
interface
|
|
100
|
-
readonly userId?: string;
|
|
101
|
-
}
|
|
102
|
-
interface RallyAuditLog {
|
|
86
|
+
interface AuditLog {
|
|
103
87
|
readonly id: string;
|
|
104
88
|
readonly timestamp: string;
|
|
105
89
|
readonly rallyId: string;
|
|
@@ -108,79 +92,39 @@ interface RallyAuditLog {
|
|
|
108
92
|
readonly resourceId: string;
|
|
109
93
|
readonly status: "SUCCESS" | "REJECTED";
|
|
110
94
|
readonly idempotencyKey: string;
|
|
111
|
-
readonly proofData?: unknown;
|
|
112
95
|
readonly metadata?: Readonly<Record<string, unknown>>;
|
|
113
96
|
}
|
|
114
|
-
interface ServerStorageAdapter {
|
|
115
|
-
getRewardStock(rewardId: string): Promise<number | null>;
|
|
116
|
-
decrementRewardStock(rewardId: string): Promise<boolean>;
|
|
117
|
-
getUserClaims(userId: string, rewardId: string): Promise<number>;
|
|
118
|
-
recordAuditLog(log: RallyAuditLog): Promise<void>;
|
|
119
|
-
saveUserState(userId: string, state: UserRallyState): Promise<void>;
|
|
120
|
-
getUserState(userId: string): Promise<UserRallyState | null>;
|
|
121
|
-
}
|
|
122
|
-
interface AdminRallyConfig extends RallyConfig {
|
|
123
|
-
readonly secretKey: SecureTokenSecretKey;
|
|
124
|
-
readonly proofTtlSeconds?: number;
|
|
125
|
-
readonly authenticate?: (request: Request) => Promise<string | null> | string | null;
|
|
126
|
-
}
|
|
127
97
|
interface CheckInRequest {
|
|
128
|
-
readonly
|
|
98
|
+
readonly rallyId: string;
|
|
99
|
+
readonly userId?: string;
|
|
129
100
|
readonly spotId: string;
|
|
130
|
-
readonly
|
|
131
|
-
readonly proofData?: unknown;
|
|
101
|
+
readonly context: _stamprally_core.VerificationContext;
|
|
132
102
|
readonly idempotencyKey: string;
|
|
103
|
+
readonly now?: string;
|
|
133
104
|
}
|
|
134
105
|
interface ClaimRewardRequest {
|
|
135
|
-
readonly
|
|
106
|
+
readonly rallyId: string;
|
|
107
|
+
readonly userId?: string;
|
|
136
108
|
readonly rewardId: string;
|
|
137
|
-
readonly staffPasscode?: string;
|
|
138
109
|
readonly idempotencyKey: string;
|
|
110
|
+
readonly staffPasscode?: string;
|
|
139
111
|
readonly staffId?: string;
|
|
112
|
+
readonly now?: string;
|
|
140
113
|
}
|
|
141
|
-
|
|
142
|
-
readonly
|
|
143
|
-
readonly
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
readonly
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
readonly
|
|
151
|
-
readonly
|
|
152
|
-
readonly
|
|
153
|
-
readonly
|
|
154
|
-
|
|
155
|
-
interface StampClaimProof {
|
|
156
|
-
readonly token: string;
|
|
157
|
-
readonly rallyId: string;
|
|
158
|
-
readonly userId: string;
|
|
159
|
-
readonly spotId: string;
|
|
160
|
-
readonly acquiredAt: string;
|
|
161
|
-
}
|
|
162
|
-
interface InMemoryServerStorageOptions {
|
|
163
|
-
readonly stocks?: Readonly<Record<string, number>>;
|
|
164
|
-
}
|
|
165
|
-
declare class InMemoryServerStorage implements ServerStorageAdapter {
|
|
166
|
-
#private;
|
|
167
|
-
constructor(options?: InMemoryServerStorageOptions);
|
|
168
|
-
getRewardStock(rewardId: string): Promise<number | null>;
|
|
169
|
-
decrementRewardStock(rewardId: string): Promise<boolean>;
|
|
170
|
-
getUserClaims(userId: string, rewardId: string): Promise<number>;
|
|
171
|
-
recordAuditLog(log: RallyAuditLog): Promise<void>;
|
|
172
|
-
saveUserState(userId: string, state: UserRallyState): Promise<void>;
|
|
173
|
-
getUserState(userId: string): Promise<UserRallyState | null>;
|
|
174
|
-
getAuditLogs(): ReadonlyArray<RallyAuditLog>;
|
|
175
|
-
recordClaim(userId: string, rewardId: string): void;
|
|
176
|
-
}
|
|
177
|
-
declare class StampRallyServer {
|
|
178
|
-
#private;
|
|
179
|
-
constructor(config: AdminRallyConfig, storage: ServerStorageAdapter);
|
|
180
|
-
handle(request: Request): Promise<Response>;
|
|
181
|
-
verifyCheckIn(request: Request): Promise<Response>;
|
|
182
|
-
claimReward(request: Request): Promise<Response>;
|
|
183
|
-
syncProgress(request: Request): Promise<Response>;
|
|
114
|
+
type CheckInResponse = {
|
|
115
|
+
readonly ok: true;
|
|
116
|
+
readonly state: UserRallyState;
|
|
117
|
+
} | {
|
|
118
|
+
readonly ok: false;
|
|
119
|
+
readonly code: string;
|
|
120
|
+
readonly message: string;
|
|
121
|
+
};
|
|
122
|
+
interface ServerOptions {
|
|
123
|
+
readonly lockTtlMs?: number;
|
|
124
|
+
readonly idempotencyTtlMs?: number;
|
|
125
|
+
readonly authenticate?: (request: Request) => Promise<string | null> | string | null;
|
|
126
|
+
readonly customValidators?: Readonly<Record<string, _stamprally_core.Validator>>;
|
|
127
|
+
readonly now?: () => string;
|
|
184
128
|
}
|
|
185
129
|
|
|
186
|
-
export { type
|
|
130
|
+
export { type AuditLog, type CheckInRequest, type CheckInResponse, type ClaimRewardRequest, InMemoryServerPersistenceAdapter, type InMemoryServerPersistenceOptions, type ServerOptions, type ServerPersistenceAdapter, StampRallyServer, type UserClaimRecord };
|