prizerail 0.1.0 → 0.3.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 +27 -8
- package/dist/client.d.ts +48 -10
- package/dist/client.js +160 -9
- package/dist/http.d.ts +5 -1
- package/dist/http.js +4 -2
- package/dist/index.d.ts +1 -1
- package/dist/types.d.ts +46 -6
- package/dist/verify.d.ts +1 -0
- package/dist/verify.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ The Prizerail SDK for JavaScript and TypeScript. Send entries, read results and
|
|
|
11
11
|
npm install prizerail
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
The API is early (v0.x), so names can change between minor versions. Each change is in the [changelog](https://docs.prizerail.com/changelog).
|
|
14
|
+
The API is early (v0.x), so names can change between minor versions. 0.3.0 adds `entries.increment()`. 0.2.0 says `entries` where 0.1.0 said `weight`: an entrant is `{ entrantId, entries }`, and totals are `entrantCount` and `totalEntries`. The old names still work, marked deprecated. Each change is in the [changelog](https://docs.prizerail.com/changelog).
|
|
15
15
|
|
|
16
16
|
## Send entries
|
|
17
17
|
|
|
@@ -24,19 +24,19 @@ const campaign = prizerail.campaign("your-campaign")
|
|
|
24
24
|
const entries = await Promise.all(
|
|
25
25
|
users.map(async user => ({
|
|
26
26
|
entrantId: await hashEntrant(user.id, process.env.ENTRANT_ID_KEY!),
|
|
27
|
-
|
|
27
|
+
entries: user.tickets,
|
|
28
28
|
}))
|
|
29
29
|
)
|
|
30
30
|
|
|
31
31
|
const result = await campaign.entries.send(entries)
|
|
32
|
-
// { drawing: { id, drawAt, cutoffAt }, sent, added, updated, unchanged,
|
|
32
|
+
// { drawing: { id, drawAt, cutoffAt }, sent, added, updated, unchanged, entrantCount, totalEntries, batches }
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
`
|
|
35
|
+
`entrantCount` and `totalEntries` are the drawing's own totals, so reconcile with those. `added`, `updated` and `unchanged` add up what each batch reported, and a batch that was stored before its response was lost comes back as `unchanged` on the retry.
|
|
36
36
|
|
|
37
37
|
`send` takes any number of entries and sends them in requests of 1,000. It retries a `429`, a `5xx`, a timeout or a network error with a jittered, growing wait, and honors `Retry-After`.
|
|
38
38
|
|
|
39
|
-
Sending an entrant again before the cutoff only sets their
|
|
39
|
+
Sending an entrant again before the cutoff only sets their entries, so a retry is safe until then. After the cutoff a retry would go to the next drawing, so every send is pinned to one drawing:
|
|
40
40
|
|
|
41
41
|
- Before the first batch it reads which drawing is taking entries, or uses the `drawing` you pass.
|
|
42
42
|
- It sends no batch and starts no retry within the request timeout (30 seconds) of that drawing's cutoff. It throws `PrizerailCutoffError` instead.
|
|
@@ -55,6 +55,21 @@ try {
|
|
|
55
55
|
}
|
|
56
56
|
```
|
|
57
57
|
|
|
58
|
+
### Add entries as they happen
|
|
59
|
+
|
|
60
|
+
If you'd rather add entries when a user does something than send their running total, use `increment`. Each event carries an `eventId`, the id of the thing that happened, such as an order id or a referral id:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
await campaign.entries.increment([
|
|
64
|
+
{ entrantId, entries: 10, eventId: referral.id },
|
|
65
|
+
])
|
|
66
|
+
// { added, updated, duplicates, sent, batches, drawings }
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Prizerail records each `eventId` for the campaign and ignores it if it comes again, so retries and a queue that delivers an event twice never add twice, even across a cutoff. Set the id once, when the event happens, and send the same one every time. An id made fresh on each attempt would add the entries again. If you have no id for the event, make a UUID when it happens and store it with the event.
|
|
70
|
+
|
|
71
|
+
An increment goes to the drawing taking entries when it arrives, so an event after a cutoff counts in the next draw. `drawings` lists each drawing the batches went to.
|
|
72
|
+
|
|
58
73
|
### Entrant ids
|
|
59
74
|
|
|
60
75
|
Every locked list is published, hashed. Use an id that can't be guessed or linked back to a person. `hashEntrant(userId, key)` gives the HMAC-SHA256 of the user id as hex, the same as Node's `createHmac("sha256", key).update(id).digest("hex")`. Keep the key secret, and fixed for the life of the campaign. See [Entrant ids](https://docs.prizerail.com/getting-started/concepts#make-entrant-ids-with-an-hmac).
|
|
@@ -62,8 +77,8 @@ Every locked list is published, hashed. Use an id that can't be guessed or linke
|
|
|
62
77
|
### Check what Prizerail has
|
|
63
78
|
|
|
64
79
|
```ts
|
|
65
|
-
let
|
|
66
|
-
for await (const
|
|
80
|
+
let total = 0
|
|
81
|
+
for await (const entrant of campaign.entries.all()) total += entrant.entries
|
|
67
82
|
```
|
|
68
83
|
|
|
69
84
|
`entries.all()` pages through the drawing taking entries now, pinned to it after the first page. Pass `{ drawing: id }` for another drawing.
|
|
@@ -84,6 +99,10 @@ for await (const drawing of campaign.drawings.all({ status: "paid" })) {
|
|
|
84
99
|
const guarantees = await campaign.guarantees.list()
|
|
85
100
|
```
|
|
86
101
|
|
|
102
|
+
## Draw now, in test
|
|
103
|
+
|
|
104
|
+
In the test environment, `campaign.drawings.run(id)` closes a drawing's entries now and draws it, so you can see a whole draw without waiting for its cutoff. It resolves with the drawing, usually settled within seconds. Anywhere else it throws a `PrizerailError` with code `test_only`. See [Draw now](https://docs.prizerail.com/api/drawings#draw-now-test-only).
|
|
105
|
+
|
|
87
106
|
## Errors
|
|
88
107
|
|
|
89
108
|
Every API error is a `PrizerailError` with the HTTP `status`, the API's `code` (like `not_found` or `no_open_drawing`) and a `detail`. A request that never got a response has status 0 and code `network_error`. See [Errors](https://docs.prizerail.com/api/errors).
|
|
@@ -101,7 +120,7 @@ const result = await verifyDraw(
|
|
|
101
120
|
result.ok // true when every check passes
|
|
102
121
|
result.checks // [{ ok, label, detail }]
|
|
103
122
|
|
|
104
|
-
wasEntered(entrantId, result.entrants!) // { index,
|
|
123
|
+
wasEntered(entrantId, result.entrants!) // { index, entries } or null
|
|
105
124
|
```
|
|
106
125
|
|
|
107
126
|
With a client, `campaign.drawings.verify(id)` does the same. The checks are the public [verify script](https://docs.prizerail.com/verification/verify-a-draw), shipped unchanged: the entrant list's Merkle root, the random value, and each winner and prize.
|
package/dist/client.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Http } from "./http.js";
|
|
2
|
-
import type { Campaign, Drawing, DrawingRef, DrawingStatus, DrawingsPage,
|
|
2
|
+
import type { Campaign, Drawing, DrawingRef, DrawingStatus, DrawingsPage, Entrant, EntrantEvent, EntriesPage, Guarantee, SendEntriesResponse, SentEntrant } from "./types.js";
|
|
3
3
|
import { type DrawProof, type VerifyResult } from "./verify.js";
|
|
4
|
-
export declare const VERSION = "0.
|
|
4
|
+
export declare const VERSION = "0.3.0";
|
|
5
5
|
/** The API takes up to this many entries per request. */
|
|
6
6
|
export declare const ENTRIES_PER_REQUEST = 1000;
|
|
7
7
|
export type PrizerailOptions = {
|
|
@@ -29,13 +29,34 @@ export type SendOptions = {
|
|
|
29
29
|
cutoffMarginMs?: number;
|
|
30
30
|
};
|
|
31
31
|
/**
|
|
32
|
-
* `
|
|
33
|
-
* `added`, `updated` and `unchanged` sum what each batch reported, so
|
|
34
|
-
* batch stored before its response was lost, then retried, counts as
|
|
35
|
-
* unchanged. Reconcile with `
|
|
32
|
+
* `entrantCount` and `totalEntries` are the drawing's totals after the last
|
|
33
|
+
* batch. `added`, `updated` and `unchanged` sum what each batch reported, so
|
|
34
|
+
* a batch stored before its response was lost, then retried, counts as
|
|
35
|
+
* unchanged. Reconcile with `entrantCount`, not with `added`.
|
|
36
36
|
*/
|
|
37
|
+
/**
|
|
38
|
+
* What `entries.increment()` did. Its batches go to the drawing taking
|
|
39
|
+
* entries when each arrives, so a cutoff mid-way splits them between
|
|
40
|
+
* drawings, which is correct for events that happened either side of it.
|
|
41
|
+
*/
|
|
42
|
+
export type IncrementResult = {
|
|
43
|
+
/** Entrants new to their drawing, and entrants given more entries. */
|
|
44
|
+
added: number;
|
|
45
|
+
updated: number;
|
|
46
|
+
/** Events Prizerail had already seen in this campaign, so ignored. */
|
|
47
|
+
duplicates: number;
|
|
48
|
+
/** Events in the batches Prizerail confirmed. */
|
|
49
|
+
sent: number;
|
|
50
|
+
batches: number;
|
|
51
|
+
/**
|
|
52
|
+
* Each drawing a batch went to, in order: the one taking entries when it
|
|
53
|
+
* arrived. A duplicate stays counted in the drawing it first went to,
|
|
54
|
+
* which can be an earlier one.
|
|
55
|
+
*/
|
|
56
|
+
drawings: DrawingRef[];
|
|
57
|
+
};
|
|
37
58
|
export type SendResult = SendEntriesResponse & {
|
|
38
|
-
/**
|
|
59
|
+
/** Entrants in the batches the drawing confirmed. */
|
|
39
60
|
sent: number;
|
|
40
61
|
batches: number;
|
|
41
62
|
};
|
|
@@ -74,7 +95,16 @@ export declare class EntriesClient {
|
|
|
74
95
|
* a PrizerailCutoffError rather than send or retry near its cutoff, and
|
|
75
96
|
* with a PrizerailDrawingChangedError if a batch lands anywhere else.
|
|
76
97
|
*/
|
|
77
|
-
send(
|
|
98
|
+
send(entrants: Entrant[], options?: SendOptions): Promise<SendResult>;
|
|
99
|
+
/**
|
|
100
|
+
* Adds entries to entrants, one event at a time: `{ entrantId, entries,
|
|
101
|
+
* eventId }`. Prizerail records each `eventId` for the campaign and
|
|
102
|
+
* ignores it if it comes again, so this is safe to retry and safe for a
|
|
103
|
+
* queue that delivers an event twice, even after a cutoff. Sent in
|
|
104
|
+
* requests of 1,000, retried like `send`. An `eventId` reused for a
|
|
105
|
+
* different entrant or count throws a PrizerailError, `event_conflict`.
|
|
106
|
+
*/
|
|
107
|
+
increment(events: EntrantEvent[]): Promise<IncrementResult>;
|
|
78
108
|
/** The drawing taking entries now. */
|
|
79
109
|
openDrawing(): Promise<DrawingRef>;
|
|
80
110
|
/** One page of a drawing's entries, in arrival order. */
|
|
@@ -84,14 +114,14 @@ export declare class EntriesClient {
|
|
|
84
114
|
after?: string;
|
|
85
115
|
}): Promise<EntriesPage>;
|
|
86
116
|
/**
|
|
87
|
-
* Every
|
|
117
|
+
* Every entrant in a drawing, page by page. Without `drawing`, the one
|
|
88
118
|
* taking entries now, pinned by id after the first page so a cutoff
|
|
89
119
|
* mid-read can't mix in the next drawing.
|
|
90
120
|
*/
|
|
91
121
|
all(query?: {
|
|
92
122
|
drawing?: string;
|
|
93
123
|
pageSize?: number;
|
|
94
|
-
}): AsyncGenerator<
|
|
124
|
+
}): AsyncGenerator<SentEntrant>;
|
|
95
125
|
}
|
|
96
126
|
export declare class DrawingsClient {
|
|
97
127
|
private readonly campaign;
|
|
@@ -112,6 +142,14 @@ export declare class DrawingsClient {
|
|
|
112
142
|
pageSize?: number;
|
|
113
143
|
}): AsyncGenerator<Drawing>;
|
|
114
144
|
get(id: string): Promise<Drawing>;
|
|
145
|
+
/**
|
|
146
|
+
* Test environment only: closes the drawing's entries now and draws it,
|
|
147
|
+
* instead of waiting for its cutoff. Resolves with the drawing, settled or
|
|
148
|
+
* paid, or still `entropy_requested` if the random value is on its way, in
|
|
149
|
+
* which case read it again with `get(id)`. Elsewhere it throws a
|
|
150
|
+
* PrizerailError with code `test_only`.
|
|
151
|
+
*/
|
|
152
|
+
run(id: string): Promise<Drawing>;
|
|
115
153
|
/**
|
|
116
154
|
* The newest drawing with final winners, `settled` or `paid`, or null.
|
|
117
155
|
* A large prize paid by hand can keep a newer draw at `settled`, so this
|
package/dist/client.js
CHANGED
|
@@ -1,10 +1,111 @@
|
|
|
1
1
|
import { PrizerailCutoffError, PrizerailDrawingChangedError, PrizerailError, } from "./errors.js";
|
|
2
2
|
import { Http } from "./http.js";
|
|
3
3
|
import { verifyDraw } from "./verify.js";
|
|
4
|
-
export const VERSION = "0.
|
|
4
|
+
export const VERSION = "0.3.0";
|
|
5
5
|
const badPage = (detail) => new PrizerailError(0, "invalid_response", `Paging stopped: ${detail}.`);
|
|
6
6
|
/** The API takes up to this many entries per request. */
|
|
7
7
|
export const ENTRIES_PER_REQUEST = 1000;
|
|
8
|
+
/** The API's limits on an event, as it checks them. */
|
|
9
|
+
const ID_MAX = 200;
|
|
10
|
+
const ENTRIES_MAX = 2 ** 32 - 1;
|
|
11
|
+
/**
|
|
12
|
+
* Every event checked against the API's rules before the first request. The
|
|
13
|
+
* same eventId twice is fine if it's the same event, which the API ignores,
|
|
14
|
+
* but not for another entrant or count.
|
|
15
|
+
*/
|
|
16
|
+
function checkEvents(events) {
|
|
17
|
+
const seen = new Map();
|
|
18
|
+
for (const [i, e] of events.entries()) {
|
|
19
|
+
const id = typeof e.entrantId === "string" ? e.entrantId.trim() : "";
|
|
20
|
+
if (id === "" || e.entrantId.length > ID_MAX) {
|
|
21
|
+
throw new TypeError(`Event ${i}: entrantId must be a non-empty string of ${ID_MAX} characters or fewer.`);
|
|
22
|
+
}
|
|
23
|
+
const eventId = typeof e.eventId === "string" ? e.eventId.trim() : "";
|
|
24
|
+
if (eventId === "" || e.eventId.length > ID_MAX) {
|
|
25
|
+
throw new TypeError(`Event ${i} (${id}) needs an eventId of ${ID_MAX} characters or fewer: the id of the event behind these entries, the same on every retry.`);
|
|
26
|
+
}
|
|
27
|
+
const entries = e.entries ?? 1;
|
|
28
|
+
if (!Number.isInteger(entries) || entries < 1 || entries > ENTRIES_MAX) {
|
|
29
|
+
throw new TypeError(`Event ${i} (${eventId}): entries must be a whole number from 1 to ${ENTRIES_MAX}.`);
|
|
30
|
+
}
|
|
31
|
+
const was = seen.get(eventId);
|
|
32
|
+
if (was &&
|
|
33
|
+
(was.entrantId.trim() !== id || (was.entries ?? 1) !== entries)) {
|
|
34
|
+
throw new TypeError(`eventId "${eventId}" is used for two different events. Give each event its own id.`);
|
|
35
|
+
}
|
|
36
|
+
seen.set(eventId, e);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** An increment response, checked before its counts are added up. */
|
|
40
|
+
function checkIncrementResponse(r) {
|
|
41
|
+
const res = r;
|
|
42
|
+
if (typeof res?.drawing?.id !== "string" ||
|
|
43
|
+
!isCount(res.added) ||
|
|
44
|
+
!isCount(res.updated) ||
|
|
45
|
+
!isCount(res.duplicates ?? 0)) {
|
|
46
|
+
throw badResponse("The increment response has no valid drawing or counts.");
|
|
47
|
+
}
|
|
48
|
+
return res;
|
|
49
|
+
}
|
|
50
|
+
/** An entrant as the API takes it: `entries`, never the older `weight`. */
|
|
51
|
+
function toWire(entrant) {
|
|
52
|
+
if (typeof entrant === "string")
|
|
53
|
+
return entrant;
|
|
54
|
+
const { weight, ...rest } = entrant;
|
|
55
|
+
if (weight === undefined)
|
|
56
|
+
return rest;
|
|
57
|
+
if (rest.entries !== undefined) {
|
|
58
|
+
throw new TypeError(`Entrant ${rest.entrantId} has both entries and weight. Send entries.`);
|
|
59
|
+
}
|
|
60
|
+
return { ...rest, entries: weight };
|
|
61
|
+
}
|
|
62
|
+
const badResponse = (detail) => new PrizerailError(0, "invalid_response", detail);
|
|
63
|
+
const isCount = (v) => typeof v === "number" && Number.isSafeInteger(v) && v >= 0;
|
|
64
|
+
/** One of a pair of names for the same total, which must agree if both came. */
|
|
65
|
+
function pick(r, current, old) {
|
|
66
|
+
const a = r[current];
|
|
67
|
+
const b = r[old];
|
|
68
|
+
if (a !== undefined && b !== undefined && a !== b) {
|
|
69
|
+
throw badResponse(`${String(current)} ${a} and ${String(old)} ${b} disagree.`);
|
|
70
|
+
}
|
|
71
|
+
const value = a ?? b;
|
|
72
|
+
if (!isCount(value)) {
|
|
73
|
+
throw badResponse(`The response has no valid ${String(current)}.`);
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
/** Both names for the totals, from whichever the server sent. */
|
|
78
|
+
function totals(r) {
|
|
79
|
+
const entrantCount = pick(r, "entrantCount", "count");
|
|
80
|
+
const totalEntries = pick(r, "totalEntries", "totalWeight");
|
|
81
|
+
return {
|
|
82
|
+
entrantCount,
|
|
83
|
+
totalEntries,
|
|
84
|
+
count: entrantCount,
|
|
85
|
+
totalWeight: totalEntries,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/** A page with the current names filled in, from either server shape. */
|
|
89
|
+
function normalizePage(page) {
|
|
90
|
+
const rows = page.entrants ?? page.entries;
|
|
91
|
+
if (!Array.isArray(rows)) {
|
|
92
|
+
throw badResponse("The response has no entrants list.");
|
|
93
|
+
}
|
|
94
|
+
const entrants = rows.map(e => {
|
|
95
|
+
const n = e.entries ?? e.weight;
|
|
96
|
+
if (!isCount(n) || n < 1 || typeof e.entrantId !== "string") {
|
|
97
|
+
throw badResponse("An entrant in the response has no valid id or entries.");
|
|
98
|
+
}
|
|
99
|
+
return { ...e, entries: n, weight: n };
|
|
100
|
+
});
|
|
101
|
+
return {
|
|
102
|
+
drawing: page.drawing,
|
|
103
|
+
...totals(page),
|
|
104
|
+
entrants,
|
|
105
|
+
entries: entrants,
|
|
106
|
+
next: page.next,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
8
109
|
export class Prizerail {
|
|
9
110
|
siteUrl;
|
|
10
111
|
/** @internal */
|
|
@@ -83,7 +184,8 @@ export class EntriesClient {
|
|
|
83
184
|
* a PrizerailCutoffError rather than send or retry near its cutoff, and
|
|
84
185
|
* with a PrizerailDrawingChangedError if a batch lands anywhere else.
|
|
85
186
|
*/
|
|
86
|
-
async send(
|
|
187
|
+
async send(entrants, options = {}) {
|
|
188
|
+
const entries = entrants.map(toWire);
|
|
87
189
|
if (entries.length === 0)
|
|
88
190
|
throw new Error("send() needs entries.");
|
|
89
191
|
const margin = options.cutoffMarginMs ?? this.campaign.http.options.timeoutMs;
|
|
@@ -112,7 +214,7 @@ export class EntriesClient {
|
|
|
112
214
|
}
|
|
113
215
|
const batch = entries.slice(i, i + ENTRIES_PER_REQUEST);
|
|
114
216
|
const res = await this.campaign.http.request("POST", this.campaign.path("/entries"), {
|
|
115
|
-
body: {
|
|
217
|
+
body: { entrants: batch },
|
|
116
218
|
deadline: cutoff - margin,
|
|
117
219
|
onDeadline: cause => stop(`A batch failed and a retry would reach drawing ${target.id}'s cutoff. Check what it has before sending again.`, cause),
|
|
118
220
|
});
|
|
@@ -132,12 +234,48 @@ export class EntriesClient {
|
|
|
132
234
|
added: progress.added,
|
|
133
235
|
updated: progress.updated,
|
|
134
236
|
unchanged: progress.unchanged,
|
|
135
|
-
|
|
136
|
-
totalWeight: final.totalWeight,
|
|
237
|
+
...totals(final),
|
|
137
238
|
sent: progress.sent,
|
|
138
239
|
batches,
|
|
139
240
|
};
|
|
140
241
|
}
|
|
242
|
+
/**
|
|
243
|
+
* Adds entries to entrants, one event at a time: `{ entrantId, entries,
|
|
244
|
+
* eventId }`. Prizerail records each `eventId` for the campaign and
|
|
245
|
+
* ignores it if it comes again, so this is safe to retry and safe for a
|
|
246
|
+
* queue that delivers an event twice, even after a cutoff. Sent in
|
|
247
|
+
* requests of 1,000, retried like `send`. An `eventId` reused for a
|
|
248
|
+
* different entrant or count throws a PrizerailError, `event_conflict`.
|
|
249
|
+
*/
|
|
250
|
+
async increment(events) {
|
|
251
|
+
if (events.length === 0)
|
|
252
|
+
throw new Error("increment() needs events.");
|
|
253
|
+
// Checked before anything is sent, so a bad event late in the list can't
|
|
254
|
+
// leave the batches before it stored and the call failed.
|
|
255
|
+
checkEvents(events);
|
|
256
|
+
const result = {
|
|
257
|
+
added: 0,
|
|
258
|
+
updated: 0,
|
|
259
|
+
duplicates: 0,
|
|
260
|
+
sent: 0,
|
|
261
|
+
batches: 0,
|
|
262
|
+
drawings: [],
|
|
263
|
+
};
|
|
264
|
+
for (let i = 0; i < events.length; i += ENTRIES_PER_REQUEST) {
|
|
265
|
+
const batch = events.slice(i, i + ENTRIES_PER_REQUEST);
|
|
266
|
+
// Retries are safe here at any time: a repeated eventId is ignored.
|
|
267
|
+
const res = checkIncrementResponse(await this.campaign.http.request("POST", this.campaign.path("/entries"), { body: { mode: "increment", entrants: batch } }));
|
|
268
|
+
result.added += res.added;
|
|
269
|
+
result.updated += res.updated;
|
|
270
|
+
result.duplicates += res.duplicates ?? 0;
|
|
271
|
+
result.sent += batch.length;
|
|
272
|
+
result.batches++;
|
|
273
|
+
if (result.drawings.at(-1)?.id !== res.drawing.id) {
|
|
274
|
+
result.drawings.push(res.drawing);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return result;
|
|
278
|
+
}
|
|
141
279
|
/** The drawing taking entries now. */
|
|
142
280
|
async openDrawing() {
|
|
143
281
|
const { drawing } = await this.list({ limit: 1 });
|
|
@@ -148,11 +286,11 @@ export class EntriesClient {
|
|
|
148
286
|
};
|
|
149
287
|
}
|
|
150
288
|
/** One page of a drawing's entries, in arrival order. */
|
|
151
|
-
list(query = {}) {
|
|
152
|
-
return this.campaign.http.request("GET", this.campaign.path("/entries"), { query });
|
|
289
|
+
async list(query = {}) {
|
|
290
|
+
return normalizePage(await this.campaign.http.request("GET", this.campaign.path("/entries"), { query }));
|
|
153
291
|
}
|
|
154
292
|
/**
|
|
155
|
-
* Every
|
|
293
|
+
* Every entrant in a drawing, page by page. Without `drawing`, the one
|
|
156
294
|
* taking entries now, pinned by id after the first page so a cutoff
|
|
157
295
|
* mid-read can't mix in the next drawing.
|
|
158
296
|
*/
|
|
@@ -169,7 +307,7 @@ export class EntriesClient {
|
|
|
169
307
|
if (page.drawing.id !== drawing) {
|
|
170
308
|
throw badPage(`a page came from drawing ${page.drawing.id}, not ${drawing}`);
|
|
171
309
|
}
|
|
172
|
-
yield* page.
|
|
310
|
+
yield* page.entrants;
|
|
173
311
|
if (page.next === null)
|
|
174
312
|
return;
|
|
175
313
|
// Each cursor must move forward, or a bad page would loop forever.
|
|
@@ -224,6 +362,19 @@ export class DrawingsClient {
|
|
|
224
362
|
async get(id) {
|
|
225
363
|
return (await this.campaign.http.request("GET", this.campaign.path(`/drawings/${encodeURIComponent(id)}`))).drawing;
|
|
226
364
|
}
|
|
365
|
+
/**
|
|
366
|
+
* Test environment only: closes the drawing's entries now and draws it,
|
|
367
|
+
* instead of waiting for its cutoff. Resolves with the drawing, settled or
|
|
368
|
+
* paid, or still `entropy_requested` if the random value is on its way, in
|
|
369
|
+
* which case read it again with `get(id)`. Elsewhere it throws a
|
|
370
|
+
* PrizerailError with code `test_only`.
|
|
371
|
+
*/
|
|
372
|
+
async run(id) {
|
|
373
|
+
return (await this.campaign.http.request("POST", this.campaign.path(`/drawings/${encodeURIComponent(id)}/run`),
|
|
374
|
+
// A repeat after a lost answer would only be refused, so never retry,
|
|
375
|
+
// and wait out the route's own 90 seconds rather than the default 30.
|
|
376
|
+
{ maxRetries: 0, timeoutMs: 90_000 })).drawing;
|
|
377
|
+
}
|
|
227
378
|
/**
|
|
228
379
|
* The newest drawing with final winners, `settled` or `paid`, or null.
|
|
229
380
|
* A large prize paid by hand can keep a newer draw at `settled`, so this
|
package/dist/http.d.ts
CHANGED
|
@@ -17,6 +17,10 @@ export type RequestOptions = {
|
|
|
17
17
|
*/
|
|
18
18
|
deadline?: number;
|
|
19
19
|
onDeadline?: (error: unknown) => Error;
|
|
20
|
+
/** For a request that isn't safe to repeat. Defaults to the client's maxRetries. */
|
|
21
|
+
maxRetries?: number;
|
|
22
|
+
/** For a request the server may take longer over. Defaults to the client's. */
|
|
23
|
+
timeoutMs?: number;
|
|
20
24
|
};
|
|
21
25
|
/** A server that asks for a longer wait than this gets the error instead. */
|
|
22
26
|
export declare const MAX_RETRY_AFTER_MS: number;
|
|
@@ -29,7 +33,7 @@ export declare class Http {
|
|
|
29
33
|
constructor(options: HttpOptions);
|
|
30
34
|
url(path: string, query?: RequestOptions["query"]): string;
|
|
31
35
|
/** JSON from the partner API, with retries. Throws PrizerailError. */
|
|
32
|
-
request<T>(method: "GET" | "POST", path: string, { query, body, deadline, onDeadline }?: RequestOptions): Promise<T>;
|
|
36
|
+
request<T>(method: "GET" | "POST", path: string, { query, body, deadline, onDeadline, ...rest }?: RequestOptions): Promise<T>;
|
|
33
37
|
/** Public JSON, like a draw's proof, with no key and no retries. */
|
|
34
38
|
getPublic<T>(url: string): Promise<T>;
|
|
35
39
|
}
|
package/dist/http.js
CHANGED
|
@@ -63,8 +63,10 @@ export class Http {
|
|
|
63
63
|
return url.href;
|
|
64
64
|
}
|
|
65
65
|
/** JSON from the partner API, with retries. Throws PrizerailError. */
|
|
66
|
-
async request(method, path, { query, body, deadline, onDeadline } = {}) {
|
|
67
|
-
const { apiKey, fetch,
|
|
66
|
+
async request(method, path, { query, body, deadline, onDeadline, ...rest } = {}) {
|
|
67
|
+
const { apiKey, fetch, userAgent } = this.options;
|
|
68
|
+
const maxRetries = rest.maxRetries ?? this.options.maxRetries;
|
|
69
|
+
const timeoutMs = rest.timeoutMs ?? this.options.timeoutMs;
|
|
68
70
|
if (!apiKey) {
|
|
69
71
|
throw new PrizerailError(0, "no_api_key", "Pass apiKey to new Prizerail() to call the partner API.");
|
|
70
72
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { CampaignClient, DrawingsClient, ENTRIES_PER_REQUEST, EntriesClient, Prizerail, type PrizerailOptions, type SendOptions, type SendResult, VERSION, } from "./client.js";
|
|
1
|
+
export { CampaignClient, DrawingsClient, ENTRIES_PER_REQUEST, EntriesClient, type IncrementResult, Prizerail, type PrizerailOptions, type SendOptions, type SendResult, VERSION, } from "./client.js";
|
|
2
2
|
export { hashEntrant } from "./entrant.js";
|
|
3
3
|
export { PrizerailCutoffError, PrizerailDrawingChangedError, PrizerailError, type SendProgress, } from "./errors.js";
|
|
4
4
|
export type * from "./types.js";
|
package/dist/types.d.ts
CHANGED
|
@@ -113,27 +113,58 @@ export type DrawingsPage = {
|
|
|
113
113
|
nextBefore: string | null;
|
|
114
114
|
nextAfter: string | null;
|
|
115
115
|
};
|
|
116
|
-
/** An entrant id, or an id with
|
|
117
|
-
export type
|
|
116
|
+
/** An entrant id, with one entry, or an id with their entries and how they show if they win. */
|
|
117
|
+
export type Entrant = string | {
|
|
118
118
|
entrantId: string;
|
|
119
|
-
/**
|
|
119
|
+
/** Their number of entries, a whole number from 1. Leave it out and it's 1. */
|
|
120
|
+
entries?: number;
|
|
121
|
+
/** @deprecated Use `entries`. Still sent as `entries`. */
|
|
120
122
|
weight?: number;
|
|
121
123
|
/** Up to 40 characters. */
|
|
122
124
|
displayName?: string;
|
|
123
125
|
/** https, up to 2,048 characters. */
|
|
124
126
|
avatarUrl?: string;
|
|
125
127
|
};
|
|
128
|
+
/** @deprecated Use `Entrant`. */
|
|
129
|
+
export type Entry = Entrant;
|
|
130
|
+
/**
|
|
131
|
+
* One event that earns an entrant entries, for `entries.increment()`. The
|
|
132
|
+
* `eventId` names the event (an order id, a referral id, or a UUID made once
|
|
133
|
+
* when it happens), and must be the same on every retry or redelivery, so
|
|
134
|
+
* Prizerail can ignore repeats. Never make it per attempt.
|
|
135
|
+
*/
|
|
136
|
+
export type EntrantEvent = {
|
|
137
|
+
entrantId: string;
|
|
138
|
+
/** Entries this event adds, a whole number from 1. Leave it out and it's 1. */
|
|
139
|
+
entries?: number;
|
|
140
|
+
eventId: string;
|
|
141
|
+
/** Up to 40 characters. Replaces the stored one when the event is first counted. */
|
|
142
|
+
displayName?: string;
|
|
143
|
+
/** https, up to 2,048 characters. Replaces the stored one when the event is first counted. */
|
|
144
|
+
avatarUrl?: string;
|
|
145
|
+
};
|
|
126
146
|
export type SendEntriesResponse = {
|
|
127
147
|
drawing: DrawingRef;
|
|
148
|
+
/** Entrants in this request that were new, changed or the same. */
|
|
128
149
|
added: number;
|
|
129
150
|
updated: number;
|
|
130
151
|
unchanged: number;
|
|
131
|
-
/** The drawing's
|
|
152
|
+
/** The drawing's entrants so far. */
|
|
153
|
+
entrantCount: number;
|
|
154
|
+
/** The drawing's total entries so far. */
|
|
155
|
+
totalEntries: number;
|
|
156
|
+
/** @deprecated Use `entrantCount`. */
|
|
132
157
|
count: number;
|
|
158
|
+
/** @deprecated Use `totalEntries`. */
|
|
133
159
|
totalWeight: number;
|
|
160
|
+
/** Increment mode: events this campaign had already seen, so ignored. */
|
|
161
|
+
duplicates?: number;
|
|
134
162
|
};
|
|
135
|
-
export type
|
|
163
|
+
export type SentEntrant = {
|
|
136
164
|
entrantId: string;
|
|
165
|
+
/** Their number of entries. */
|
|
166
|
+
entries: number;
|
|
167
|
+
/** @deprecated Use `entries`. */
|
|
137
168
|
weight: number;
|
|
138
169
|
displayName: string | null;
|
|
139
170
|
avatarUrl: string | null;
|
|
@@ -141,13 +172,22 @@ export type SentEntry = {
|
|
|
141
172
|
position: number | null;
|
|
142
173
|
receivedAt: string;
|
|
143
174
|
};
|
|
175
|
+
/** @deprecated Use `SentEntrant`. */
|
|
176
|
+
export type SentEntry = SentEntrant;
|
|
144
177
|
export type EntriesPage = {
|
|
145
178
|
drawing: DrawingRef & {
|
|
146
179
|
status: DrawingStatus;
|
|
147
180
|
};
|
|
181
|
+
entrantCount: number;
|
|
182
|
+
totalEntries: number;
|
|
183
|
+
/** In arrival order, the order the list locks in. */
|
|
184
|
+
entrants: SentEntrant[];
|
|
185
|
+
/** @deprecated Use `entrantCount`. */
|
|
148
186
|
count: number;
|
|
187
|
+
/** @deprecated Use `totalEntries`. */
|
|
149
188
|
totalWeight: number;
|
|
150
|
-
|
|
189
|
+
/** @deprecated Use `entrants`, the same list. */
|
|
190
|
+
entries: SentEntrant[];
|
|
151
191
|
/** Pass as `after` for the next page. Null on the last. */
|
|
152
192
|
next: string | null;
|
|
153
193
|
};
|
package/dist/verify.d.ts
CHANGED
package/dist/verify.js
CHANGED
|
@@ -65,5 +65,6 @@ export async function verifyDraw(source, { entrants, proofUrl, fetch: fetchImpl
|
|
|
65
65
|
export function wasEntered(entrantId, rows) {
|
|
66
66
|
const hash = winnerHash(entrantId);
|
|
67
67
|
const row = rows.find(r => r[1].toLowerCase() === hash);
|
|
68
|
-
|
|
68
|
+
// The proof calls an entrant's entries their weight. Both names, for now.
|
|
69
|
+
return row ? { index: row[0], entries: row[2], weight: row[2] } : null;
|
|
69
70
|
}
|