@stamprally/server 0.8.0 → 0.9.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/dist/index.cjs +288 -625
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +58 -131
- package/dist/index.d.ts +58 -131
- package/dist/index.js +290 -625
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,105 +1,72 @@
|
|
|
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
|
-
/** Persistence contract for implementations backed by Redis, SQL, or another RDB. */
|
|
5
5
|
interface ServerPersistenceAdapter {
|
|
6
6
|
acquireLock(lockKey: string, ttlMs: number): Promise<boolean>;
|
|
7
7
|
releaseLock(lockKey: string): Promise<void>;
|
|
8
8
|
decrementRewardStock(rewardId: string): Promise<{
|
|
9
|
-
success: boolean;
|
|
10
|
-
remainingStock: number;
|
|
9
|
+
readonly success: boolean;
|
|
10
|
+
readonly remainingStock: number;
|
|
11
11
|
}>;
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
incrementRewardStock(rewardId: string): Promise<void>;
|
|
13
|
+
getIdempotentResult<T>(key: string): Promise<T | null>;
|
|
14
|
+
saveIdempotentResult<T>(key: string, result: T, ttlMs: number): Promise<void>;
|
|
15
|
+
getUserClaimCount(rallyId: string, userId: string, rewardId: string): Promise<number>;
|
|
14
16
|
getUserState(rallyId: string, userId: string): Promise<UserRallyState | null>;
|
|
15
17
|
saveUserState(rallyId: string, userId: string, state: UserRallyState): Promise<void>;
|
|
16
|
-
recordAuditLog(log:
|
|
18
|
+
recordAuditLog(log: AuditLog): Promise<void>;
|
|
17
19
|
}
|
|
18
20
|
interface InMemoryServerPersistenceOptions {
|
|
19
21
|
readonly stocks?: Readonly<Record<string, number>>;
|
|
20
22
|
}
|
|
21
|
-
/** Deterministic adapter for tests and small single-process deployments. */
|
|
22
23
|
declare class InMemoryServerPersistenceAdapter implements ServerPersistenceAdapter {
|
|
23
24
|
#private;
|
|
24
25
|
constructor(options?: InMemoryServerPersistenceOptions);
|
|
25
|
-
acquireLock(
|
|
26
|
-
releaseLock(
|
|
26
|
+
acquireLock(key: string, ttlMs: number): Promise<boolean>;
|
|
27
|
+
releaseLock(key: string): Promise<void>;
|
|
27
28
|
decrementRewardStock(rewardId: string): Promise<{
|
|
28
29
|
success: boolean;
|
|
29
30
|
remainingStock: number;
|
|
30
31
|
}>;
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
incrementRewardStock(rewardId: string): Promise<void>;
|
|
33
|
+
getIdempotentResult<T>(key: string): Promise<T | null>;
|
|
34
|
+
saveIdempotentResult<T>(key: string, result: T, ttlMs: number): Promise<void>;
|
|
35
|
+
getUserClaimCount(rallyId: string, userId: string, rewardId: string): Promise<number>;
|
|
33
36
|
getUserState(rallyId: string, userId: string): Promise<UserRallyState | null>;
|
|
34
37
|
saveUserState(rallyId: string, userId: string, state: UserRallyState): Promise<void>;
|
|
35
|
-
recordAuditLog(log:
|
|
36
|
-
getAuditLogs(): ReadonlyArray<
|
|
38
|
+
recordAuditLog(log: AuditLog): Promise<void>;
|
|
39
|
+
getAuditLogs(): ReadonlyArray<AuditLog>;
|
|
40
|
+
recordClaim(rallyId: string, userId: string, rewardId: string): void;
|
|
37
41
|
}
|
|
38
42
|
|
|
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
|
-
} | {
|
|
43
|
+
type ErrorResponse = {
|
|
74
44
|
readonly ok: false;
|
|
75
45
|
readonly code: string;
|
|
76
46
|
readonly message: string;
|
|
77
47
|
};
|
|
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 {
|
|
48
|
+
type ClaimResponse = {
|
|
49
|
+
readonly ok: true;
|
|
50
|
+
readonly state: UserRallyState;
|
|
51
|
+
readonly claimTicketNumber?: string;
|
|
52
|
+
} | ErrorResponse;
|
|
53
|
+
declare class StampRallyServer {
|
|
88
54
|
#private;
|
|
89
|
-
constructor(config: AdminRallyConfig
|
|
90
|
-
|
|
55
|
+
constructor(config: AdminRallyConfig, persistence: ServerPersistenceAdapter, options?: ServerOptions);
|
|
56
|
+
handle(request: Request): Promise<Response>;
|
|
91
57
|
handleCheckIn(request: Request): Promise<Response>;
|
|
92
58
|
handleClaimReward(request: Request): Promise<Response>;
|
|
93
59
|
handleSync(request: Request): Promise<Response>;
|
|
94
|
-
|
|
95
|
-
|
|
60
|
+
checkIn(request: CheckInRequest & {
|
|
61
|
+
readonly userId: string;
|
|
62
|
+
}): Promise<CheckInResponse>;
|
|
63
|
+
claimReward(request: ClaimRewardRequest & {
|
|
64
|
+
readonly userId: string;
|
|
65
|
+
}): Promise<ClaimResponse>;
|
|
96
66
|
sync(rallyId: string, userId: string): Promise<UserRallyState>;
|
|
97
67
|
}
|
|
98
68
|
|
|
99
|
-
interface
|
|
100
|
-
readonly userId?: string;
|
|
101
|
-
}
|
|
102
|
-
interface RallyAuditLog {
|
|
69
|
+
interface AuditLog {
|
|
103
70
|
readonly id: string;
|
|
104
71
|
readonly timestamp: string;
|
|
105
72
|
readonly rallyId: string;
|
|
@@ -108,79 +75,39 @@ interface RallyAuditLog {
|
|
|
108
75
|
readonly resourceId: string;
|
|
109
76
|
readonly status: "SUCCESS" | "REJECTED";
|
|
110
77
|
readonly idempotencyKey: string;
|
|
111
|
-
readonly proofData?: unknown;
|
|
112
78
|
readonly metadata?: Readonly<Record<string, unknown>>;
|
|
113
79
|
}
|
|
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
80
|
interface CheckInRequest {
|
|
128
|
-
readonly
|
|
81
|
+
readonly rallyId: string;
|
|
82
|
+
readonly userId?: string;
|
|
129
83
|
readonly spotId: string;
|
|
130
|
-
readonly
|
|
131
|
-
readonly proofData?: unknown;
|
|
84
|
+
readonly context: _stamprally_core.VerificationContext;
|
|
132
85
|
readonly idempotencyKey: string;
|
|
86
|
+
readonly now?: string;
|
|
133
87
|
}
|
|
134
88
|
interface ClaimRewardRequest {
|
|
135
|
-
readonly
|
|
89
|
+
readonly rallyId: string;
|
|
90
|
+
readonly userId?: string;
|
|
136
91
|
readonly rewardId: string;
|
|
137
|
-
readonly staffPasscode?: string;
|
|
138
92
|
readonly idempotencyKey: string;
|
|
93
|
+
readonly staffPasscode?: string;
|
|
139
94
|
readonly staffId?: string;
|
|
95
|
+
readonly now?: string;
|
|
140
96
|
}
|
|
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>;
|
|
97
|
+
type CheckInResponse = {
|
|
98
|
+
readonly ok: true;
|
|
99
|
+
readonly state: UserRallyState;
|
|
100
|
+
} | {
|
|
101
|
+
readonly ok: false;
|
|
102
|
+
readonly code: string;
|
|
103
|
+
readonly message: string;
|
|
104
|
+
};
|
|
105
|
+
interface ServerOptions {
|
|
106
|
+
readonly lockTtlMs?: number;
|
|
107
|
+
readonly idempotencyTtlMs?: number;
|
|
108
|
+
readonly authenticate?: (request: Request) => Promise<string | null> | string | null;
|
|
109
|
+
readonly customValidators?: Readonly<Record<string, _stamprally_core.Validator>>;
|
|
110
|
+
readonly now?: () => string;
|
|
184
111
|
}
|
|
185
112
|
|
|
186
|
-
export { type
|
|
113
|
+
export { type AuditLog, type CheckInRequest, type CheckInResponse, type ClaimRewardRequest, InMemoryServerPersistenceAdapter, type InMemoryServerPersistenceOptions, type ServerOptions, type ServerPersistenceAdapter, StampRallyServer };
|