@pincast/api 0.0.0-stage → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Pincast, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,141 @@
1
- # Temporary Holding Version
1
+ # @pincast/api
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A typed client for the Pincast card registry. Register your app's cards, record what people collect, and read it back, from your server.
4
+
5
+ It has no dependencies and runs on Node 20.12 or later.
6
+
7
+ ```sh
8
+ npm install @pincast/api
9
+ ```
10
+
11
+ ## Start with a test app
12
+
13
+ A test app needs no account. `postTestApp()` makes one and answers with its key.
14
+
15
+ ```js
16
+ import { Pincast, testActor } from '@pincast/api'
17
+
18
+ const { key } = await new Pincast().postTestApp({ name: 'Bird log' })
19
+ const pincast = new Pincast({ key })
20
+
21
+ await pincast.putSet('park-birds', {
22
+ name: 'Park birds',
23
+ cards: [{ key: 'bird:robin', type: 'bird', title: 'Robin' }],
24
+ })
25
+
26
+ await pincast.postCollect('park-birds', { card: 'bird:robin', actor: testActor('ana') })
27
+
28
+ const held = await pincast.getCollects('park-birds', { actor: 'test_ana' })
29
+ console.log(held.cards)
30
+ ```
31
+
32
+ Keep the key in `PINCAST_KEY`. With no `key` option, the client reads it from there.
33
+
34
+ ## The calls
35
+
36
+ One method for each call in the API reference, named by its operation id.
37
+
38
+ | Method | Call |
39
+ |:-|:-|
40
+ | `putSet(set, body)` | `PUT /sets/{set}`: register a card set, or update it and its cards |
41
+ | `getSet(set)` | `GET /sets/{set}`: read a set back |
42
+ | `postCollect(set, body)` | `POST /sets/{set}/collects`: record that a person collected a card |
43
+ | `getCollects(set, { actor })` | `GET /sets/{set}/collects`: what one to three people hold |
44
+ | `postClaim(set, { from, to })` | `POST /sets/{set}/claims`: move a guest's cards to a person who signed in |
45
+ | `postPerson({ token })` | `POST /people`: link a person who signed in to your app |
46
+ | `getApps()` | `GET /apps`: the public list of apps, no key needed |
47
+ | `postTestApp({ name })` | `POST /test-apps`: a test app and its key, no key needed |
48
+
49
+ Each method takes a last `options` argument: `signal` and `timeoutMs`, and `idempotencyKey` on the three writes.
50
+
51
+ ## Keys stay on your server
52
+
53
+ The client sends your key in the `X-Pincast-Key` header, and only on the calls that take one. It never puts the key in an address, an error or a log line. `pincast.keyInfo` tells you the app, the kind and whether it is a test key, without the key itself.
54
+
55
+ In a web page, the client refuses every key, secret or restricted, and throws a `browser_not_allowed` error, because anyone who opens the page could read the key. With no key, it still runs there for the public list of apps, `getApps()`.
56
+
57
+ ## Retries
58
+
59
+ Every write (`putSet`, `postCollect`, `postClaim`) gets an `Idempotency-Key`. Placecard keeps the first answer to that key for 24 hours, so a call sent twice runs once. Pass your own with `{ idempotencyKey }` when you send the same call again later.
60
+
61
+ A `429` or `503` that sends `Retry-After` is waited out and sent again, with the same `Idempotency-Key`. That covers both limits: `rate_limited` (your key's calls per minute) and `quota_exceeded` (your app's calls per UTC day). By default the client waits up to 60 seconds at a time and sends a call again at most twice. A longer wait raises the error at once, with the seconds in `error.retryAfter`, so you can send it later.
62
+
63
+ ```js
64
+ new Pincast({ key, retries: 2, maxRetryWait: 60 })
65
+ ```
66
+
67
+ ## Errors
68
+
69
+ Every failure is a `PincastError`. When Placecard refuses a call, the error carries its answer:
70
+
71
+ - `status`: the HTTP status, or `null` when no answer came
72
+ - `code`: a code to branch on, such as `test_person_required`
73
+ - `field`: the field at fault, when there is one
74
+ - `fix`: what to do, in one sentence
75
+ - `kind`: `key`, `waiting`, `later` or `refused`, for code that keeps calls to send again
76
+
77
+ ```js
78
+ try {
79
+ await pincast.postCollect('park-birds', body)
80
+ } catch (error) {
81
+ if (error.code === 'person_not_signed_in') {
82
+ // link them with postPerson, then send it again
83
+ }
84
+ console.error(error.message) // postCollect (POST /sets/{set}/collects): 403 person_not_signed_in: ...
85
+ }
86
+ ```
87
+
88
+ ## Test people
89
+
90
+ A test key writes only for test people, whose ids start `test_`. They need no sign-in.
91
+
92
+ - `testPerson('Ana Lee')` gives `test_ana-lee`. `testPerson()` gives a new random id.
93
+ - `testActor('ana')` gives `{ id: 'test_ana', source: 'anonymous' }`, ready for a collect.
94
+ - `isTestPerson(id)` and `isTestKey(key)` check one.
95
+
96
+ ## The resend queue (optional)
97
+
98
+ A visit should never wait on Placecard, and no collect should be lost when Placecard is busy. The queue keeps each collect before it sends it. Once a collect goes out, the queue tries up to 20 unsent ones, newest first, and stops at the first failure that isn't that collect's fault.
99
+
100
+ ```js
101
+ import { ResendQueue } from '@pincast/api/resend-queue'
102
+
103
+ const queue = new ResendQueue(pincast)
104
+ await queue.collect('park-birds', { card: 'bird:robin', actor })
105
+
106
+ // after postPerson links a person, their waiting collects go out
107
+ await queue.release(personId)
108
+ ```
109
+
110
+ Each kept collect has its own `Idempotency-Key`, so a resend never records it twice. A refused collect keeps its error and is not sent again. The default store lives in memory. For a server that restarts, write a store on your database with the five methods of `MemoryStore`.
111
+
112
+ ## Testing without Pincast
113
+
114
+ `@pincast/api/mock` starts a local registry built from the API file. It runs the file's checks, answers with its examples, and records each request.
115
+
116
+ ```js
117
+ import { startMock } from '@pincast/api/mock'
118
+
119
+ const mock = await startMock({ key })
120
+ const pincast = new Pincast({ key, baseUrl: mock.url })
121
+ mock.answerNext({ status: 429, code: 'rate_limited', fix: 'Wait.', headers: { 'Retry-After': '1' } })
122
+ await mock.close()
123
+ ```
124
+
125
+ ## Types
126
+
127
+ The types come from the API file (`openapi/placecard-v1.openapi.json`, also at `@pincast/api/openapi.json`). `npm run generate` writes them again. `npm run check` fails when the API file has a call the client lacks.
128
+
129
+ ## Versions
130
+
131
+ Every release follows one rule. [CHANGELOG.md](CHANGELOG.md) lists what each one changed.
132
+
133
+ - **Patch** (0.1.0 to 0.1.1): a fix, or a change your code can't see. The API file is the same, or only its text and examples changed.
134
+ - **Minor** (0.1.1 to 0.2.0): something new, and everything that worked still works. An operation added to the API file is a minor. So is a new optional field, error code, option or export.
135
+ - **Major** (0.2.0 to 1.0.0, then 2.0.0): something that worked can stop working. An operation, field or export removed is a major. So is a narrowed field: one that's newly required, takes fewer values or a shorter length, or changed its type. So is raising the lowest Node version the package runs on.
136
+
137
+ The rule holds below 1.0.0 too, so the first breaking change makes 1.0.0. While the version starts with 0, a range like `^0.1.0` takes patches only, so raise it to take a minor.
138
+
139
+ ## License
140
+
141
+ MIT