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