prizerail 0.3.0 → 0.3.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 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. 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).
14
+ The API is early (v0.x), so names can change between minor versions. 0.3.0 adds `entries.increment()`, 0.3.1 checks every event before sending any and lists only the drawings that took entries, and 0.3.2 checks avatar URLs and names up front too. 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
 
@@ -68,7 +68,7 @@ await campaign.entries.increment([
68
68
 
69
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
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.
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 events added entries to. Events Prizerail had already seen stay in the drawing they first went to.
72
72
 
73
73
  ### Entrant ids
74
74
 
package/dist/client.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Http } from "./http.js";
2
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.3.0";
4
+ export declare const VERSION = "0.3.2";
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 = {
@@ -49,9 +49,9 @@ export type IncrementResult = {
49
49
  sent: number;
50
50
  batches: number;
51
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.
52
+ * Each drawing these events added entries to, in order. A batch that was
53
+ * all duplicates adds none, since those events are in the drawing they
54
+ * first went to, which can be an earlier one.
55
55
  */
56
56
  drawings: DrawingRef[];
57
57
  };
package/dist/client.js CHANGED
@@ -1,7 +1,7 @@
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.3.0";
4
+ export const VERSION = "0.3.2";
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;
@@ -28,6 +28,14 @@ function checkEvents(events) {
28
28
  if (!Number.isInteger(entries) || entries < 1 || entries > ENTRIES_MAX) {
29
29
  throw new TypeError(`Event ${i} (${eventId}): entries must be a whole number from 1 to ${ENTRIES_MAX}.`);
30
30
  }
31
+ if (e.displayName != null && typeof e.displayName !== "string") {
32
+ throw new TypeError(`Event ${i} (${eventId}): displayName must be a string.`);
33
+ }
34
+ if (e.avatarUrl != null &&
35
+ e.avatarUrl !== "" &&
36
+ !isAvatarUrl(e.avatarUrl)) {
37
+ throw new TypeError(`Event ${i} (${eventId}): avatarUrl must be an https URL of ${AVATAR_URL_MAX} characters or fewer.`);
38
+ }
31
39
  const was = seen.get(eventId);
32
40
  if (was &&
33
41
  (was.entrantId.trim() !== id || (was.entries ?? 1) !== entries)) {
@@ -36,6 +44,18 @@ function checkEvents(events) {
36
44
  seen.set(eventId, e);
37
45
  }
38
46
  }
47
+ /** The API's rule for an avatar: https, and 2,048 characters at most. */
48
+ const AVATAR_URL_MAX = 2048;
49
+ function isAvatarUrl(value) {
50
+ if (typeof value !== "string" || value.length > AVATAR_URL_MAX)
51
+ return false;
52
+ try {
53
+ return new URL(value).protocol === "https:";
54
+ }
55
+ catch {
56
+ return false;
57
+ }
58
+ }
39
59
  /** An increment response, checked before its counts are added up. */
40
60
  function checkIncrementResponse(r) {
41
61
  const res = r;
@@ -270,7 +290,11 @@ export class EntriesClient {
270
290
  result.duplicates += res.duplicates ?? 0;
271
291
  result.sent += batch.length;
272
292
  result.batches++;
273
- if (result.drawings.at(-1)?.id !== res.drawing.id) {
293
+ // Only a batch that added entries names where they went. One that was
294
+ // all duplicates, such as a retry after the cutoff of a batch already
295
+ // stored, names the next drawing, not the one the events are in.
296
+ if (res.added + res.updated > 0 &&
297
+ result.drawings.at(-1)?.id !== res.drawing.id) {
274
298
  result.drawings.push(res.drawing);
275
299
  }
276
300
  }
package/dist/types.d.ts CHANGED
@@ -138,9 +138,9 @@ export type EntrantEvent = {
138
138
  /** Entries this event adds, a whole number from 1. Leave it out and it's 1. */
139
139
  entries?: number;
140
140
  eventId: string;
141
- /** Up to 40 characters. Replaces the stored one when the event is first counted. */
141
+ /** Up to 40 characters. Replaces the stored one when the event is first counted. Empty keeps it. */
142
142
  displayName?: string;
143
- /** https, up to 2,048 characters. Replaces the stored one when the event is first counted. */
143
+ /** https, up to 2,048 characters. Replaces the stored one when the event is first counted. Empty keeps it. */
144
144
  avatarUrl?: string;
145
145
  };
146
146
  export type SendEntriesResponse = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prizerail",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "The Prizerail SDK: send entries, read draws and verify them.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://docs.prizerail.com",