@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,585 @@
1
+ /**
2
+ * App Store Methods Mixin
3
+ *
4
+ * The client surface for the Oxy app store: the public storefront (`/store`),
5
+ * the reviews people write there, and the listing a publisher edits for an
6
+ * application they own (`/applications/:appId/listing`).
7
+ *
8
+ * Deliberately separate from `OxyServices.accounts.ts` even though the
9
+ * publisher's routes hang off an application, for the same reason
10
+ * `OxyServices.connectedApps.ts` is: those mixins answer "may this program act
11
+ * for this person?", and this one answers "should this person choose it?". Turn
12
+ * the store off and OAuth still works — which is the test that says the store is
13
+ * a module over the platform rather than part of it.
14
+ *
15
+ * The two prefixes are one domain. A listing IS the store's page for an
16
+ * application, so both halves of its life belong to the same surface; the API
17
+ * puts the publisher's half beside credentials and webhooks because that is
18
+ * where the permission that guards it already lives, and reusing that permission
19
+ * is what stops a store page becoming a second, weaker way to act for somebody's
20
+ * app.
21
+ *
22
+ * ## What is NOT duplicated here
23
+ *
24
+ * A listing carries no name, icon or legal links: `applications` already holds
25
+ * them and the storefront joins them in. A rating is computed from the visible
26
+ * reviews on every read rather than stored, so a hidden review stops counting
27
+ * the moment it is hidden. Reference listings by their `slug` in the storefront
28
+ * (it is what every link carries) and applications by their `_id` in the
29
+ * publisher's calls.
30
+ */
31
+ import type { OxyServicesBase } from '../OxyServices.base';
32
+ import { CACHE_TIMES } from './mixinHelpers';
33
+
34
+ /** A shelf on the storefront. */
35
+ export interface StoreCategory {
36
+ /** The public identifier a link carries. Never the row id. */
37
+ slug: string;
38
+ /** What a person reads. Never derived from the slug at render time. */
39
+ label: string;
40
+ description?: string | null;
41
+ }
42
+
43
+ /** The rating of an app, computed from its visible reviews. */
44
+ export interface StoreRating {
45
+ /** Rounded to one decimal, or `null` when nobody has reviewed it — never 0. */
46
+ average: number | null;
47
+ count: number;
48
+ }
49
+
50
+ /** An app as a card on the storefront: what a listing page needs, and no more. */
51
+ export interface StoreListingSummary {
52
+ slug: string;
53
+ /** From the APPLICATION, joined in — the listing keeps no copy. */
54
+ name: string;
55
+ tagline: string | null;
56
+ /** A file id for the app's icon, resolved through the usual image resolver. */
57
+ icon: string | null;
58
+ category: StoreCategory | null;
59
+ rating: StoreRating;
60
+ }
61
+
62
+ /** A store page in full. */
63
+ export interface StoreListingDetail extends StoreListingSummary {
64
+ description: string | null;
65
+ /** These four come from the application; the consent screen shows the same values. */
66
+ websiteUrl: string | null;
67
+ privacyPolicyUrl: string | null;
68
+ termsUrl: string | null;
69
+ supportUrl: string | null;
70
+ supportEmail: string | null;
71
+ publishedAt: string | null;
72
+ screenshots: StoreScreenshot[];
73
+ /** How many visible reviews gave each of 1..5. Absent keys are zero. */
74
+ ratingBreakdown: Record<number, number>;
75
+ }
76
+
77
+ /** Which frame a screenshot was taken in. The store groups by it on the page. */
78
+ export type StoreScreenshotPlatform = 'phone' | 'tablet' | 'desktop' | 'web';
79
+
80
+ export interface StoreScreenshot {
81
+ id: string;
82
+ /** The uploaded asset's file id. Upload through the assets surface first. */
83
+ fileId: string;
84
+ platform: StoreScreenshotPlatform;
85
+ caption: string | null;
86
+ position: number;
87
+ }
88
+
89
+ /** Somebody's review, as it appears on a store page. */
90
+ export interface StoreReview {
91
+ id: string;
92
+ rating: number;
93
+ title: string | null;
94
+ body: string | null;
95
+ createdAt: string;
96
+ author: { id: string; username: string | null };
97
+ /** The publisher's answer, when there is one. */
98
+ reply: { body: string; createdAt: string } | null;
99
+ /**
100
+ * Whether this author has authorized the application, read from their grant
101
+ * at request time rather than stored on the review.
102
+ *
103
+ * It is not a claim that they still use it, and it is `false` for a
104
+ * first-party app nobody has to consent to — so render its absence as nothing
105
+ * at all rather than as a demotion.
106
+ */
107
+ authorUsesApp: boolean;
108
+ }
109
+
110
+ /** A review as its own author sees it, whatever its moderation state. */
111
+ export interface StoreOwnReview {
112
+ id: string;
113
+ rating: number;
114
+ title: string | null;
115
+ body: string | null;
116
+ /** An author is told when their review is hidden; the public list is not. */
117
+ status: 'visible' | 'hidden' | 'flagged' | 'removed';
118
+ createdAt: string;
119
+ updatedAt: string;
120
+ }
121
+
122
+ /** What a person submits about an app. One review each; writing again replaces it. */
123
+ export interface WriteStoreReviewInput {
124
+ /** Whole stars, 1 to 5. The database enforces the bound too. */
125
+ rating: number;
126
+ title?: string | null;
127
+ body?: string | null;
128
+ }
129
+
130
+ /** Where a listing is in its life. `pending_review` is the STORE's review of the page. */
131
+ export type StoreListingStatus = 'draft' | 'pending_review' | 'published' | 'rejected';
132
+
133
+ /** A listing as its publisher sees it: whatever state it is in. */
134
+ export interface PublisherListing {
135
+ id: string;
136
+ applicationId: string;
137
+ slug: string;
138
+ tagline: string | null;
139
+ description: string | null;
140
+ category: StoreCategory | null;
141
+ supportUrl: string | null;
142
+ supportEmail: string | null;
143
+ status: StoreListingStatus;
144
+ publishedAt: string | null;
145
+ createdAt: string;
146
+ updatedAt: string;
147
+ }
148
+
149
+ /**
150
+ * The whole page, not a patch: sending everything is what makes "clear the
151
+ * tagline" expressible at all.
152
+ *
153
+ * `status` is absent on purpose. Publishing is the store's decision and has its
154
+ * own calls, so a publisher cannot publish themselves by putting a field in a
155
+ * body.
156
+ */
157
+ export interface WriteListingInput {
158
+ /** Lowercase letters, digits and single hyphens. What every link carries. */
159
+ slug: string;
160
+ tagline?: string | null;
161
+ description?: string | null;
162
+ /** A category SLUG, never its id. */
163
+ categorySlug?: string | null;
164
+ supportUrl?: string | null;
165
+ supportEmail?: string | null;
166
+ }
167
+
168
+ export interface AddScreenshotInput {
169
+ /** An already-uploaded image. Must be live, an image, and yours to publish. */
170
+ fileId: string;
171
+ platform?: StoreScreenshotPlatform;
172
+ caption?: string | null;
173
+ }
174
+
175
+ export interface UpdateScreenshotInput {
176
+ platform?: StoreScreenshotPlatform;
177
+ caption?: string | null;
178
+ }
179
+
180
+ /**
181
+ * One page of a paginated store read.
182
+ *
183
+ * `hasMore` comes from the API rather than being derived here, so a caller that
184
+ * pages does not have to re-implement the boundary the server already computed.
185
+ */
186
+ export interface StorePage<T> {
187
+ items: T[];
188
+ total: number;
189
+ hasMore: boolean;
190
+ }
191
+
192
+ /** Options for paging the storefront and the reviews under an app. */
193
+ export interface StorePageOptions {
194
+ limit?: number;
195
+ offset?: number;
196
+ }
197
+
198
+ export interface StoreReviewsOptions extends StorePageOptions {
199
+ /** Newest first by default; `rating` surfaces the strongest opinions. */
200
+ sort?: 'recent' | 'rating';
201
+ }
202
+
203
+ /**
204
+ * The API's paginated envelope. The counts live under `pagination`, NOT under
205
+ * `meta` — `meta` is what the single-object `sendSuccess` helper uses, and
206
+ * reading the wrong one yields a `total` of zero on every page with no error
207
+ * anywhere.
208
+ */
209
+ interface PaginatedResponse<T> {
210
+ data?: T[];
211
+ pagination?: { total?: number; hasMore?: boolean };
212
+ }
213
+
214
+ /** Read one page out of that envelope. */
215
+ function pageOf<T>(res: PaginatedResponse<T>): StorePage<T> {
216
+ return {
217
+ items: res.data ?? [],
218
+ total: res.pagination?.total ?? 0,
219
+ hasMore: res.pagination?.hasMore ?? false,
220
+ };
221
+ }
222
+
223
+ /**
224
+ * Build a query string from the options that were actually supplied.
225
+ *
226
+ * Generic over the options object rather than taking a `Record`: an interface
227
+ * has no implicit index signature in TypeScript, so `StoreReviewsOptions` would
228
+ * not be assignable to one and every call site would need a cast.
229
+ */
230
+ function queryOf<T extends object>(params: T): string {
231
+ const search = new URLSearchParams();
232
+ for (const [key, value] of Object.entries(params)) {
233
+ if (value !== undefined) search.set(key, String(value));
234
+ }
235
+ const rendered = search.toString();
236
+ return rendered ? `?${rendered}` : '';
237
+ }
238
+
239
+ export function OxyServicesStoreMixin<T extends typeof OxyServicesBase>(Base: T) {
240
+ return class extends Base {
241
+ constructor(...args: any[]) {
242
+ super(...(args as [any]));
243
+ }
244
+
245
+ // =========================================================================
246
+ // The storefront — /store. No authentication: everything served is public.
247
+ // =========================================================================
248
+
249
+ /** The shelves, in the order the store curates them. */
250
+ async listStoreCategories(): Promise<StoreCategory[]> {
251
+ try {
252
+ const res = await this.makeRequest<{ data: StoreCategory[] }>(
253
+ 'GET',
254
+ '/store/categories',
255
+ undefined,
256
+ { cache: true, cacheTTL: CACHE_TIMES.MEDIUM },
257
+ );
258
+ return res.data ?? [];
259
+ } catch (error) {
260
+ throw this.handleError(error);
261
+ }
262
+ }
263
+
264
+ /**
265
+ * Published listings, newest first, optionally one shelf.
266
+ *
267
+ * An unknown category slug is an EMPTY shelf, not every app on the store —
268
+ * so a typo shows nothing rather than showing everything.
269
+ *
270
+ * @param options - `category` is a category slug; `limit` defaults to 24.
271
+ */
272
+ async listStoreApps(
273
+ options: StorePageOptions & { category?: string } = {},
274
+ ): Promise<StorePage<StoreListingSummary>> {
275
+ try {
276
+ const res = await this.makeRequest<PaginatedResponse<StoreListingSummary>>(
277
+ 'GET',
278
+ `/store/apps${queryOf(options)}`,
279
+ undefined,
280
+ { cache: true, cacheTTL: CACHE_TIMES.SHORT },
281
+ );
282
+ return pageOf(res);
283
+ } catch (error) {
284
+ throw this.handleError(error);
285
+ }
286
+ }
287
+
288
+ /**
289
+ * One store page.
290
+ *
291
+ * A draft answers 404 exactly as an unknown slug does: whether an
292
+ * unpublished page exists under a name is not something a visitor learns.
293
+ *
294
+ * @param slug - The listing's public slug, not an application id.
295
+ */
296
+ async getStoreApp(slug: string): Promise<StoreListingDetail> {
297
+ try {
298
+ const res = await this.makeRequest<{ data: StoreListingDetail }>(
299
+ 'GET',
300
+ `/store/apps/${encodeURIComponent(slug)}`,
301
+ undefined,
302
+ { cache: true, cacheTTL: CACHE_TIMES.SHORT },
303
+ );
304
+ return res.data;
305
+ } catch (error) {
306
+ throw this.handleError(error);
307
+ }
308
+ }
309
+
310
+ /** Visible reviews for a published app, each with the publisher's reply. */
311
+ async listStoreReviews(
312
+ slug: string,
313
+ options: StoreReviewsOptions = {},
314
+ ): Promise<StorePage<StoreReview>> {
315
+ try {
316
+ const res = await this.makeRequest<PaginatedResponse<StoreReview>>(
317
+ 'GET',
318
+ `/store/apps/${encodeURIComponent(slug)}/reviews${queryOf(options)}`,
319
+ undefined,
320
+ { cache: true, cacheTTL: CACHE_TIMES.SHORT },
321
+ );
322
+ return pageOf(res);
323
+ } catch (error) {
324
+ throw this.handleError(error);
325
+ }
326
+ }
327
+
328
+ // =========================================================================
329
+ // Reviewing — any signed-in Oxy account
330
+ // =========================================================================
331
+
332
+ /** The caller's own review of an app, or `null` if they have not written one. */
333
+ async getMyStoreReview(slug: string): Promise<StoreOwnReview | null> {
334
+ try {
335
+ const res = await this.makeRequest<{ data: StoreOwnReview | null }>(
336
+ 'GET',
337
+ `/store/apps/${encodeURIComponent(slug)}/review`,
338
+ undefined,
339
+ { cache: false },
340
+ );
341
+ return res.data ?? null;
342
+ } catch (error) {
343
+ throw this.handleError(error);
344
+ }
345
+ }
346
+
347
+ /**
348
+ * Write the caller's review, or replace what they said before.
349
+ *
350
+ * A person has one review per app, so this sets it rather than adding one.
351
+ * Rewriting does not clear a moderator's decision: a hidden review stays
352
+ * hidden when its author edits it.
353
+ */
354
+ async writeStoreReview(slug: string, input: WriteStoreReviewInput): Promise<StoreOwnReview> {
355
+ try {
356
+ const res = await this.makeRequest<{ data: StoreOwnReview }>(
357
+ 'PUT',
358
+ `/store/apps/${encodeURIComponent(slug)}/review`,
359
+ input,
360
+ { cache: false },
361
+ );
362
+ return res.data;
363
+ } catch (error) {
364
+ throw this.handleError(error);
365
+ }
366
+ }
367
+
368
+ /** Withdraw the caller's own review. A real delete — the words were theirs. */
369
+ async deleteMyStoreReview(slug: string): Promise<void> {
370
+ try {
371
+ await this.makeRequest<void>(
372
+ 'DELETE',
373
+ `/store/apps/${encodeURIComponent(slug)}/review`,
374
+ undefined,
375
+ { cache: false },
376
+ );
377
+ } catch (error) {
378
+ throw this.handleError(error);
379
+ }
380
+ }
381
+
382
+ /**
383
+ * Answer a review on the publisher's behalf.
384
+ *
385
+ * Requires `app:update` over the application's owning account — the same
386
+ * permission that guards every other write to that application. Addressed
387
+ * by review id because the reply belongs to the review, and a listing can be
388
+ * renamed or withdrawn out from under it.
389
+ */
390
+ async replyToStoreReview(reviewId: string, body: string): Promise<{ id: string; reviewId: string; body: string }> {
391
+ try {
392
+ const res = await this.makeRequest<{ data: { id: string; reviewId: string; body: string } }>(
393
+ 'PUT',
394
+ `/store/reviews/${encodeURIComponent(reviewId)}/reply`,
395
+ { body },
396
+ { cache: false },
397
+ );
398
+ return res.data;
399
+ } catch (error) {
400
+ throw this.handleError(error);
401
+ }
402
+ }
403
+
404
+ /** Withdraw the publisher's answer. Same permission that wrote it. */
405
+ async deleteStoreReviewReply(reviewId: string): Promise<void> {
406
+ try {
407
+ await this.makeRequest<void>(
408
+ 'DELETE',
409
+ `/store/reviews/${encodeURIComponent(reviewId)}/reply`,
410
+ undefined,
411
+ { cache: false },
412
+ );
413
+ } catch (error) {
414
+ throw this.handleError(error);
415
+ }
416
+ }
417
+
418
+ // =========================================================================
419
+ // The publisher's listing — /applications/:appId/listing
420
+ // =========================================================================
421
+
422
+ /** The application's store page in whatever state, or `null` if it has none. */
423
+ async getAppListing(applicationId: string): Promise<PublisherListing | null> {
424
+ try {
425
+ return await this.makeRequest<PublisherListing | null>(
426
+ 'GET',
427
+ `/applications/${encodeURIComponent(applicationId)}/listing`,
428
+ undefined,
429
+ { cache: false },
430
+ );
431
+ } catch (error) {
432
+ throw this.handleError(error);
433
+ }
434
+ }
435
+
436
+ /**
437
+ * Create the page or replace its content. Never its status.
438
+ *
439
+ * Editing does not move a page: correcting a typo on a live listing leaves
440
+ * it live, and fixing a rejected one does not re-submit it.
441
+ */
442
+ async writeAppListing(applicationId: string, input: WriteListingInput): Promise<PublisherListing> {
443
+ try {
444
+ return await this.makeRequest<PublisherListing>(
445
+ 'PUT',
446
+ `/applications/${encodeURIComponent(applicationId)}/listing`,
447
+ input,
448
+ { cache: false },
449
+ );
450
+ } catch (error) {
451
+ throw this.handleError(error);
452
+ }
453
+ }
454
+
455
+ /** Hand the page to the store for review. From a draft, or a rejected page once fixed. */
456
+ async submitAppListing(applicationId: string): Promise<PublisherListing> {
457
+ try {
458
+ return await this.makeRequest<PublisherListing>(
459
+ 'POST',
460
+ `/applications/${encodeURIComponent(applicationId)}/listing/submit`,
461
+ undefined,
462
+ { cache: false },
463
+ );
464
+ } catch (error) {
465
+ throw this.handleError(error);
466
+ }
467
+ }
468
+
469
+ /**
470
+ * Take the page down, or withdraw it from the queue.
471
+ *
472
+ * Back to a draft, never deleted: the slug, the words and the screenshots
473
+ * are the publisher's work, and the reviews were never the listing's to take
474
+ * with them.
475
+ */
476
+ async unpublishAppListing(applicationId: string): Promise<PublisherListing> {
477
+ try {
478
+ return await this.makeRequest<PublisherListing>(
479
+ 'POST',
480
+ `/applications/${encodeURIComponent(applicationId)}/listing/unpublish`,
481
+ undefined,
482
+ { cache: false },
483
+ );
484
+ } catch (error) {
485
+ throw this.handleError(error);
486
+ }
487
+ }
488
+
489
+ // =========================================================================
490
+ // Screenshots
491
+ // =========================================================================
492
+
493
+ /** Every picture on the listing, in the author's order. */
494
+ async listAppListingScreenshots(applicationId: string): Promise<StoreScreenshot[]> {
495
+ try {
496
+ return await this.makeRequest<StoreScreenshot[]>(
497
+ 'GET',
498
+ `/applications/${encodeURIComponent(applicationId)}/listing/screenshots`,
499
+ undefined,
500
+ { cache: false },
501
+ );
502
+ } catch (error) {
503
+ throw this.handleError(error);
504
+ }
505
+ }
506
+
507
+ /**
508
+ * Attach an already-uploaded image, appended to the end.
509
+ *
510
+ * Upload through the assets surface first; the store keeps a reference
511
+ * rather than a second copy of the asset pipeline. The file must be live, an
512
+ * image, and one the caller is entitled to.
513
+ */
514
+ async addAppListingScreenshot(
515
+ applicationId: string,
516
+ input: AddScreenshotInput,
517
+ ): Promise<StoreScreenshot> {
518
+ try {
519
+ return await this.makeRequest<StoreScreenshot>(
520
+ 'POST',
521
+ `/applications/${encodeURIComponent(applicationId)}/listing/screenshots`,
522
+ input,
523
+ { cache: false },
524
+ );
525
+ } catch (error) {
526
+ throw this.handleError(error);
527
+ }
528
+ }
529
+
530
+ /** Edit a picture's caption or the frame it was taken in. Order is {@link reorderAppListingScreenshots}. */
531
+ async updateAppListingScreenshot(
532
+ applicationId: string,
533
+ screenshotId: string,
534
+ input: UpdateScreenshotInput,
535
+ ): Promise<StoreScreenshot> {
536
+ try {
537
+ return await this.makeRequest<StoreScreenshot>(
538
+ 'PATCH',
539
+ `/applications/${encodeURIComponent(applicationId)}/listing/screenshots/${encodeURIComponent(screenshotId)}`,
540
+ input,
541
+ { cache: false },
542
+ );
543
+ } catch (error) {
544
+ throw this.handleError(error);
545
+ }
546
+ }
547
+
548
+ /** Remove a picture. The uploaded file stays — it may be in use elsewhere. */
549
+ async deleteAppListingScreenshot(applicationId: string, screenshotId: string): Promise<void> {
550
+ try {
551
+ await this.makeRequest<void>(
552
+ 'DELETE',
553
+ `/applications/${encodeURIComponent(applicationId)}/listing/screenshots/${encodeURIComponent(screenshotId)}`,
554
+ undefined,
555
+ { cache: false },
556
+ );
557
+ } catch (error) {
558
+ throw this.handleError(error);
559
+ }
560
+ }
561
+
562
+ /**
563
+ * Set the order of every picture at once.
564
+ *
565
+ * Send EVERY id on the listing, exactly once, in the order they should
566
+ * appear. A partial list is rejected rather than applied: it would leave the
567
+ * pictures it omits at their old positions, interleaved with the new ones.
568
+ */
569
+ async reorderAppListingScreenshots(
570
+ applicationId: string,
571
+ screenshotIds: string[],
572
+ ): Promise<StoreScreenshot[]> {
573
+ try {
574
+ return await this.makeRequest<StoreScreenshot[]>(
575
+ 'PUT',
576
+ `/applications/${encodeURIComponent(applicationId)}/listing/screenshots/order`,
577
+ { screenshotIds },
578
+ { cache: false },
579
+ );
580
+ } catch (error) {
581
+ throw this.handleError(error);
582
+ }
583
+ }
584
+ };
585
+ }