@oxyhq/core 20.0.0 → 20.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/NOTICE +10 -9
- package/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/mixins/OxyServices.chains.js +73 -0
- package/dist/cjs/mixins/OxyServices.store.js +266 -0
- package/dist/cjs/mixins/OxyServices.utility.js +159 -104
- package/dist/cjs/mixins/index.js +7 -0
- package/dist/cjs/server/rateLimit.js +15 -6
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/mixins/OxyServices.chains.js +70 -0
- package/dist/esm/mixins/OxyServices.store.js +263 -0
- package/dist/esm/mixins/OxyServices.utility.js +159 -104
- package/dist/esm/mixins/index.js +7 -0
- package/dist/esm/server/rateLimit.js +15 -6
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/index.d.ts +7 -0
- package/dist/types/mixins/OxyServices.chains.d.ts +156 -0
- package/dist/types/mixins/OxyServices.store.d.ts +334 -0
- package/dist/types/mixins/OxyServices.utility.d.ts +31 -8
- package/dist/types/mixins/index.d.ts +3 -1
- package/package.json +1 -1
- package/src/index.ts +30 -0
- package/src/mixins/OxyServices.chains.ts +134 -0
- package/src/mixins/OxyServices.store.ts +585 -0
- package/src/mixins/OxyServices.utility.ts +161 -108
- package/src/mixins/__tests__/chains.test.ts +113 -0
- package/src/mixins/__tests__/store.test.ts +304 -0
- package/src/mixins/__tests__/userTokenAuth.test.ts +746 -0
- package/src/mixins/index.ts +9 -0
- package/src/server/__tests__/rateLimit.test.ts +47 -0
- package/src/server/rateLimit.ts +18 -8
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chains — the shared record log every Oxy app reads and writes.
|
|
3
|
+
*
|
|
4
|
+
* A person has ONE chain. An app appends its own records to it and projects its
|
|
5
|
+
* feeds from what it reads back, instead of keeping a private copy of the same
|
|
6
|
+
* person's activity. This mixin is the client half of `/chains` in oxy-api, and
|
|
7
|
+
* it exists so that adopting the chain costs an app no HTTP of its own — the
|
|
8
|
+
* whole point of the shared substrate is that the second app writes less code
|
|
9
|
+
* than the first, not the same amount in a different file.
|
|
10
|
+
*
|
|
11
|
+
* ## Both calls are SERVICE-authenticated
|
|
12
|
+
*
|
|
13
|
+
* They go through `makeServiceRequest`, so they only work on a backend that has
|
|
14
|
+
* called `configureServiceAuth()`. That is not an accident of implementation: an
|
|
15
|
+
* append writes to someone else's chain and a read spans many subjects, so
|
|
16
|
+
* neither belongs in a browser holding a user session. A frontend that needs
|
|
17
|
+
* this asks its own backend.
|
|
18
|
+
*
|
|
19
|
+
* The authority is checked server-side and cannot be talked out of from here:
|
|
20
|
+
* `chains:write` plus the application's own `chainNamespaces` for an append,
|
|
21
|
+
* `chains:read` plus the public-collection policy for a read. A call that
|
|
22
|
+
* violates either gets a 403 or an empty page — this client adds no
|
|
23
|
+
* pre-validation that could drift from the server's answer.
|
|
24
|
+
*/
|
|
25
|
+
export function OxyServicesChainsMixin(Base) {
|
|
26
|
+
return class extends Base {
|
|
27
|
+
constructor(...args) {
|
|
28
|
+
super(...args);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Append a record to `oxyUserId`'s chain under `collection`/`rkey`.
|
|
32
|
+
*
|
|
33
|
+
* Oxy issues and signs it; the calling app never holds a chain signing key.
|
|
34
|
+
* `rkey` is the app's own id for the thing — reusing it later supersedes the
|
|
35
|
+
* earlier record for that key, which is how an edit works.
|
|
36
|
+
*
|
|
37
|
+
* Requires the `chains:write` scope AND `collection` falling under one of
|
|
38
|
+
* this application's granted `chainNamespaces`. Both are enforced by the
|
|
39
|
+
* server; a violation throws with a 403.
|
|
40
|
+
*/
|
|
41
|
+
async appendChainRecord(params) {
|
|
42
|
+
return this.makeServiceRequest('POST', '/chains/records', params);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Records published by any of `oxyUserIds` under any of `collections`,
|
|
46
|
+
* oldest first — the read a cross-app feed is projected from.
|
|
47
|
+
*
|
|
48
|
+
* Only collections Oxy declares PUBLIC come back, whatever is asked for; a
|
|
49
|
+
* private one yields nothing rather than an error.
|
|
50
|
+
*
|
|
51
|
+
* **Re-poll from slightly BEFORE your last cursor and dedupe by
|
|
52
|
+
* `recordId`.** The chain's pagination axis is a transaction-start
|
|
53
|
+
* timestamp, so a record can commit behind a cursor that already passed it.
|
|
54
|
+
* Re-delivering one costs bytes; skipping one costs a record that never
|
|
55
|
+
* appears. Projections are expected to be idempotent for exactly this
|
|
56
|
+
* reason.
|
|
57
|
+
*/
|
|
58
|
+
async readChainRecords(params) {
|
|
59
|
+
const query = new URLSearchParams({
|
|
60
|
+
authors: params.oxyUserIds.join(','),
|
|
61
|
+
collections: params.collections.join(','),
|
|
62
|
+
});
|
|
63
|
+
if (params.since)
|
|
64
|
+
query.set('since', params.since);
|
|
65
|
+
if (params.limit !== undefined)
|
|
66
|
+
query.set('limit', String(params.limit));
|
|
67
|
+
return this.makeServiceRequest('GET', `/chains/records?${query.toString()}`);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { CACHE_TIMES } from './mixinHelpers.js';
|
|
2
|
+
/** Read one page out of that envelope. */
|
|
3
|
+
function pageOf(res) {
|
|
4
|
+
return {
|
|
5
|
+
items: res.data ?? [],
|
|
6
|
+
total: res.pagination?.total ?? 0,
|
|
7
|
+
hasMore: res.pagination?.hasMore ?? false,
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Build a query string from the options that were actually supplied.
|
|
12
|
+
*
|
|
13
|
+
* Generic over the options object rather than taking a `Record`: an interface
|
|
14
|
+
* has no implicit index signature in TypeScript, so `StoreReviewsOptions` would
|
|
15
|
+
* not be assignable to one and every call site would need a cast.
|
|
16
|
+
*/
|
|
17
|
+
function queryOf(params) {
|
|
18
|
+
const search = new URLSearchParams();
|
|
19
|
+
for (const [key, value] of Object.entries(params)) {
|
|
20
|
+
if (value !== undefined)
|
|
21
|
+
search.set(key, String(value));
|
|
22
|
+
}
|
|
23
|
+
const rendered = search.toString();
|
|
24
|
+
return rendered ? `?${rendered}` : '';
|
|
25
|
+
}
|
|
26
|
+
export function OxyServicesStoreMixin(Base) {
|
|
27
|
+
return class extends Base {
|
|
28
|
+
constructor(...args) {
|
|
29
|
+
super(...args);
|
|
30
|
+
}
|
|
31
|
+
// =========================================================================
|
|
32
|
+
// The storefront — /store. No authentication: everything served is public.
|
|
33
|
+
// =========================================================================
|
|
34
|
+
/** The shelves, in the order the store curates them. */
|
|
35
|
+
async listStoreCategories() {
|
|
36
|
+
try {
|
|
37
|
+
const res = await this.makeRequest('GET', '/store/categories', undefined, { cache: true, cacheTTL: CACHE_TIMES.MEDIUM });
|
|
38
|
+
return res.data ?? [];
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
throw this.handleError(error);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Published listings, newest first, optionally one shelf.
|
|
46
|
+
*
|
|
47
|
+
* An unknown category slug is an EMPTY shelf, not every app on the store —
|
|
48
|
+
* so a typo shows nothing rather than showing everything.
|
|
49
|
+
*
|
|
50
|
+
* @param options - `category` is a category slug; `limit` defaults to 24.
|
|
51
|
+
*/
|
|
52
|
+
async listStoreApps(options = {}) {
|
|
53
|
+
try {
|
|
54
|
+
const res = await this.makeRequest('GET', `/store/apps${queryOf(options)}`, undefined, { cache: true, cacheTTL: CACHE_TIMES.SHORT });
|
|
55
|
+
return pageOf(res);
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
throw this.handleError(error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* One store page.
|
|
63
|
+
*
|
|
64
|
+
* A draft answers 404 exactly as an unknown slug does: whether an
|
|
65
|
+
* unpublished page exists under a name is not something a visitor learns.
|
|
66
|
+
*
|
|
67
|
+
* @param slug - The listing's public slug, not an application id.
|
|
68
|
+
*/
|
|
69
|
+
async getStoreApp(slug) {
|
|
70
|
+
try {
|
|
71
|
+
const res = await this.makeRequest('GET', `/store/apps/${encodeURIComponent(slug)}`, undefined, { cache: true, cacheTTL: CACHE_TIMES.SHORT });
|
|
72
|
+
return res.data;
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
throw this.handleError(error);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** Visible reviews for a published app, each with the publisher's reply. */
|
|
79
|
+
async listStoreReviews(slug, options = {}) {
|
|
80
|
+
try {
|
|
81
|
+
const res = await this.makeRequest('GET', `/store/apps/${encodeURIComponent(slug)}/reviews${queryOf(options)}`, undefined, { cache: true, cacheTTL: CACHE_TIMES.SHORT });
|
|
82
|
+
return pageOf(res);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
throw this.handleError(error);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// =========================================================================
|
|
89
|
+
// Reviewing — any signed-in Oxy account
|
|
90
|
+
// =========================================================================
|
|
91
|
+
/** The caller's own review of an app, or `null` if they have not written one. */
|
|
92
|
+
async getMyStoreReview(slug) {
|
|
93
|
+
try {
|
|
94
|
+
const res = await this.makeRequest('GET', `/store/apps/${encodeURIComponent(slug)}/review`, undefined, { cache: false });
|
|
95
|
+
return res.data ?? null;
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
throw this.handleError(error);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Write the caller's review, or replace what they said before.
|
|
103
|
+
*
|
|
104
|
+
* A person has one review per app, so this sets it rather than adding one.
|
|
105
|
+
* Rewriting does not clear a moderator's decision: a hidden review stays
|
|
106
|
+
* hidden when its author edits it.
|
|
107
|
+
*/
|
|
108
|
+
async writeStoreReview(slug, input) {
|
|
109
|
+
try {
|
|
110
|
+
const res = await this.makeRequest('PUT', `/store/apps/${encodeURIComponent(slug)}/review`, input, { cache: false });
|
|
111
|
+
return res.data;
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
throw this.handleError(error);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/** Withdraw the caller's own review. A real delete — the words were theirs. */
|
|
118
|
+
async deleteMyStoreReview(slug) {
|
|
119
|
+
try {
|
|
120
|
+
await this.makeRequest('DELETE', `/store/apps/${encodeURIComponent(slug)}/review`, undefined, { cache: false });
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
throw this.handleError(error);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Answer a review on the publisher's behalf.
|
|
128
|
+
*
|
|
129
|
+
* Requires `app:update` over the application's owning account — the same
|
|
130
|
+
* permission that guards every other write to that application. Addressed
|
|
131
|
+
* by review id because the reply belongs to the review, and a listing can be
|
|
132
|
+
* renamed or withdrawn out from under it.
|
|
133
|
+
*/
|
|
134
|
+
async replyToStoreReview(reviewId, body) {
|
|
135
|
+
try {
|
|
136
|
+
const res = await this.makeRequest('PUT', `/store/reviews/${encodeURIComponent(reviewId)}/reply`, { body }, { cache: false });
|
|
137
|
+
return res.data;
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
throw this.handleError(error);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/** Withdraw the publisher's answer. Same permission that wrote it. */
|
|
144
|
+
async deleteStoreReviewReply(reviewId) {
|
|
145
|
+
try {
|
|
146
|
+
await this.makeRequest('DELETE', `/store/reviews/${encodeURIComponent(reviewId)}/reply`, undefined, { cache: false });
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
throw this.handleError(error);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
// =========================================================================
|
|
153
|
+
// The publisher's listing — /applications/:appId/listing
|
|
154
|
+
// =========================================================================
|
|
155
|
+
/** The application's store page in whatever state, or `null` if it has none. */
|
|
156
|
+
async getAppListing(applicationId) {
|
|
157
|
+
try {
|
|
158
|
+
return await this.makeRequest('GET', `/applications/${encodeURIComponent(applicationId)}/listing`, undefined, { cache: false });
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
throw this.handleError(error);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Create the page or replace its content. Never its status.
|
|
166
|
+
*
|
|
167
|
+
* Editing does not move a page: correcting a typo on a live listing leaves
|
|
168
|
+
* it live, and fixing a rejected one does not re-submit it.
|
|
169
|
+
*/
|
|
170
|
+
async writeAppListing(applicationId, input) {
|
|
171
|
+
try {
|
|
172
|
+
return await this.makeRequest('PUT', `/applications/${encodeURIComponent(applicationId)}/listing`, input, { cache: false });
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
throw this.handleError(error);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/** Hand the page to the store for review. From a draft, or a rejected page once fixed. */
|
|
179
|
+
async submitAppListing(applicationId) {
|
|
180
|
+
try {
|
|
181
|
+
return await this.makeRequest('POST', `/applications/${encodeURIComponent(applicationId)}/listing/submit`, undefined, { cache: false });
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
throw this.handleError(error);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Take the page down, or withdraw it from the queue.
|
|
189
|
+
*
|
|
190
|
+
* Back to a draft, never deleted: the slug, the words and the screenshots
|
|
191
|
+
* are the publisher's work, and the reviews were never the listing's to take
|
|
192
|
+
* with them.
|
|
193
|
+
*/
|
|
194
|
+
async unpublishAppListing(applicationId) {
|
|
195
|
+
try {
|
|
196
|
+
return await this.makeRequest('POST', `/applications/${encodeURIComponent(applicationId)}/listing/unpublish`, undefined, { cache: false });
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
throw this.handleError(error);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
// =========================================================================
|
|
203
|
+
// Screenshots
|
|
204
|
+
// =========================================================================
|
|
205
|
+
/** Every picture on the listing, in the author's order. */
|
|
206
|
+
async listAppListingScreenshots(applicationId) {
|
|
207
|
+
try {
|
|
208
|
+
return await this.makeRequest('GET', `/applications/${encodeURIComponent(applicationId)}/listing/screenshots`, undefined, { cache: false });
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
throw this.handleError(error);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Attach an already-uploaded image, appended to the end.
|
|
216
|
+
*
|
|
217
|
+
* Upload through the assets surface first; the store keeps a reference
|
|
218
|
+
* rather than a second copy of the asset pipeline. The file must be live, an
|
|
219
|
+
* image, and one the caller is entitled to.
|
|
220
|
+
*/
|
|
221
|
+
async addAppListingScreenshot(applicationId, input) {
|
|
222
|
+
try {
|
|
223
|
+
return await this.makeRequest('POST', `/applications/${encodeURIComponent(applicationId)}/listing/screenshots`, input, { cache: false });
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
throw this.handleError(error);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
/** Edit a picture's caption or the frame it was taken in. Order is {@link reorderAppListingScreenshots}. */
|
|
230
|
+
async updateAppListingScreenshot(applicationId, screenshotId, input) {
|
|
231
|
+
try {
|
|
232
|
+
return await this.makeRequest('PATCH', `/applications/${encodeURIComponent(applicationId)}/listing/screenshots/${encodeURIComponent(screenshotId)}`, input, { cache: false });
|
|
233
|
+
}
|
|
234
|
+
catch (error) {
|
|
235
|
+
throw this.handleError(error);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
/** Remove a picture. The uploaded file stays — it may be in use elsewhere. */
|
|
239
|
+
async deleteAppListingScreenshot(applicationId, screenshotId) {
|
|
240
|
+
try {
|
|
241
|
+
await this.makeRequest('DELETE', `/applications/${encodeURIComponent(applicationId)}/listing/screenshots/${encodeURIComponent(screenshotId)}`, undefined, { cache: false });
|
|
242
|
+
}
|
|
243
|
+
catch (error) {
|
|
244
|
+
throw this.handleError(error);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Set the order of every picture at once.
|
|
249
|
+
*
|
|
250
|
+
* Send EVERY id on the listing, exactly once, in the order they should
|
|
251
|
+
* appear. A partial list is rejected rather than applied: it would leave the
|
|
252
|
+
* pictures it omits at their old positions, interleaved with the new ones.
|
|
253
|
+
*/
|
|
254
|
+
async reorderAppListingScreenshots(applicationId, screenshotIds) {
|
|
255
|
+
try {
|
|
256
|
+
return await this.makeRequest('PUT', `/applications/${encodeURIComponent(applicationId)}/listing/screenshots/order`, { screenshotIds }, { cache: false });
|
|
257
|
+
}
|
|
258
|
+
catch (error) {
|
|
259
|
+
throw this.handleError(error);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
}
|