@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.d.ts CHANGED
@@ -1,105 +1,72 @@
1
- import { AdminRallyConfig as AdminRallyConfig$1, VerificationCondition, RallyConfig, SecureTokenSecretKey, StampRallyState, VerificationContext } from '@stamprally/core';
2
- export { StampError, StampRecord } from '@stamprally/core';
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
- getIdempotentResult<T>(idempotencyKey: string): Promise<T | null>;
13
- saveIdempotentResult<T>(idempotencyKey: string, result: T, ttlMs: number): Promise<void>;
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: RallyAuditLog): Promise<void>;
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(lockKey: string, ttlMs: number): Promise<boolean>;
26
- releaseLock(lockKey: string): Promise<void>;
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
- getIdempotentResult<T>(idempotencyKey: string): Promise<T | null>;
32
- saveIdempotentResult<T>(idempotencyKey: string, result: T, ttlMs: number): Promise<void>;
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: RallyAuditLog): Promise<void>;
36
- getAuditLogs(): ReadonlyArray<RallyAuditLog>;
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 UniversalVerificationContext = {
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
- interface UniversalRallyServerOptions {
79
- readonly idempotencyTtlMs?: number;
80
- readonly lockTtlMs?: number;
81
- readonly customValidators?: Readonly<Record<string, (value: unknown, condition: VerificationCondition) => boolean>>;
82
- readonly now?: () => string;
83
- /** Extract the authenticated identity; request-body userId is never trusted when this is set. */
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$1, persistence: ServerPersistenceAdapter, options?: UniversalRallyServerOptions);
90
- /** Web Standard endpoint handler. Authentication, when configured, supplies the user identity. */
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
- handle(request: Request): Promise<Response>;
95
- checkIn(request: UniversalCheckInRequest): Promise<UniversalCheckInResult>;
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 UserRallyState extends StampRallyState {
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 userId: string;
81
+ readonly rallyId: string;
82
+ readonly userId?: string;
129
83
  readonly spotId: string;
130
- readonly claimMethod: string;
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 userId: string;
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
- interface SyncRequest {
142
- readonly userId: string;
143
- readonly queue?: ReadonlyArray<SyncCheckInOperation>;
144
- readonly operations?: ReadonlyArray<SyncCheckInOperation>;
145
- }
146
- interface SyncCheckInOperation {
147
- readonly userId?: string;
148
- readonly spotId?: string;
149
- readonly stampId?: string;
150
- readonly claimMethod?: string;
151
- readonly proofData?: unknown;
152
- readonly context?: VerificationContext;
153
- readonly idempotencyKey: string;
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 AdminRallyConfig, type CheckInRequest, type ClaimRewardRequest, InMemoryServerPersistenceAdapter, type InMemoryServerPersistenceOptions, InMemoryServerStorage, type InMemoryServerStorageOptions, type RallyAuditLog, type ServerPersistenceAdapter, type ServerStorageAdapter, type StampClaimProof, StampRallyServer, type SyncCheckInOperation, type SyncRequest, type UniversalCheckInRequest, type UniversalCheckInResult, type UniversalClaimRewardRequest, UniversalRallyServer, type UniversalRallyServerOptions, type UniversalVerificationContext, type UserRallyState };
113
+ export { type AuditLog, type CheckInRequest, type CheckInResponse, type ClaimRewardRequest, InMemoryServerPersistenceAdapter, type InMemoryServerPersistenceOptions, type ServerOptions, type ServerPersistenceAdapter, StampRallyServer };