@oxyhq/core 1.11.15 → 1.11.16

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/dist/esm/index.js CHANGED
@@ -22,6 +22,7 @@ export { OXY_CLOUD_URL, oxyClient } from './OxyServices.js';
22
22
  export { AuthManager, createAuthManager } from './AuthManager.js';
23
23
  export { CrossDomainAuth, createCrossDomainAuth } from './CrossDomainAuth.js';
24
24
  export { ServiceCredentialMismatchError } from './mixins/OxyServices.auth.js';
25
+ export { OxyAppDataIdentifierError } from './mixins/OxyServices.appData.js';
25
26
  // --- Crypto / Identity ---
26
27
  export { KeyManager, SignatureService, RecoveryPhraseService, IdentityAlreadyExistsError, IdentityPersistError, } from './crypto/index.js';
27
28
  // --- Models & Types ---
@@ -0,0 +1,103 @@
1
+ /**
2
+ * Per-user App-Data Mixin
3
+ *
4
+ * Thin client around `/users/me/app-data/...` — a generic per-user JSON KV
5
+ * store on the API. Authenticated callers can read, write, list, and delete
6
+ * entries scoped to their own user account.
7
+ *
8
+ * Identifier rules (must match the API):
9
+ * - Both `namespace` and `key` must match `/^[a-z0-9_-]{1,64}$/u`.
10
+ *
11
+ * Limits (enforced by the API):
12
+ * - Serialized JSON values are capped at 64 KB.
13
+ * - Writes are rate-limited to 100 / minute / user.
14
+ *
15
+ * Intended use cases are small bits of cross-device app state — e.g. Academy
16
+ * course progress, "last viewed" markers, dismissed banner flags. Do not use
17
+ * this for large blobs or anything that needs server-side querying; it's a
18
+ * write-it-and-read-it-back store.
19
+ */
20
+ /**
21
+ * Identifier validator — mirror of the API regex. Validating client-side
22
+ * gives consumers a clean throw before the request even leaves the device.
23
+ */
24
+ const APP_DATA_IDENTIFIER_PATTERN = /^[a-z0-9_-]{1,64}$/u;
25
+ /** Thrown when a namespace or key fails the kebab/snake-case validator. */
26
+ export class OxyAppDataIdentifierError extends Error {
27
+ constructor(field, value) {
28
+ super(`Invalid app-data ${field} "${value}": must match [a-z0-9_-]{1,64} (lowercase letters, digits, dashes, underscores).`);
29
+ this.name = 'OxyAppDataIdentifierError';
30
+ }
31
+ }
32
+ function assertIdentifier(field, value) {
33
+ if (!APP_DATA_IDENTIFIER_PATTERN.test(value)) {
34
+ throw new OxyAppDataIdentifierError(field, value);
35
+ }
36
+ }
37
+ export function OxyServicesAppDataMixin(Base) {
38
+ return class extends Base {
39
+ constructor(...args) {
40
+ super(...args);
41
+ }
42
+ /**
43
+ * Read the value stored under `(namespace, key)` for the current user.
44
+ *
45
+ * @returns The previously-stored value, or `null` if nothing has been
46
+ * stored yet. Never throws on "not found" — a missing entry is
47
+ * semantically a `null` value.
48
+ */
49
+ async getAppData(namespace, key) {
50
+ assertIdentifier('namespace', namespace);
51
+ assertIdentifier('key', key);
52
+ return this.withAuthRetry(async () => {
53
+ const response = await this.makeRequest('GET', `/users/me/app-data/${encodeURIComponent(namespace)}/${encodeURIComponent(key)}`, undefined, { cache: false });
54
+ return response?.value ?? null;
55
+ }, 'getAppData');
56
+ }
57
+ /**
58
+ * Upsert the value under `(namespace, key)` for the current user.
59
+ *
60
+ * Returns the value the server confirmed it stored — typically the same
61
+ * value the caller passed in, but consumers should prefer the returned
62
+ * value (the API is the source of truth).
63
+ *
64
+ * @throws OxyAppDataIdentifierError when namespace or key is malformed.
65
+ */
66
+ async setAppData(namespace, key, value) {
67
+ assertIdentifier('namespace', namespace);
68
+ assertIdentifier('key', key);
69
+ return this.withAuthRetry(async () => {
70
+ const response = await this.makeRequest('PUT', `/users/me/app-data/${encodeURIComponent(namespace)}/${encodeURIComponent(key)}`, { value }, { cache: false });
71
+ // The server echoes the stored value back; fall back to the caller's
72
+ // input only if the server somehow omitted it (defensive — the route
73
+ // always sets it).
74
+ return (response?.value ?? value);
75
+ }, 'setAppData');
76
+ }
77
+ /**
78
+ * Delete the value stored under `(namespace, key)` for the current user.
79
+ *
80
+ * Idempotent — resolves successfully whether or not the entry existed.
81
+ */
82
+ async deleteAppData(namespace, key) {
83
+ assertIdentifier('namespace', namespace);
84
+ assertIdentifier('key', key);
85
+ await this.withAuthRetry(async () => {
86
+ await this.makeRequest('DELETE', `/users/me/app-data/${encodeURIComponent(namespace)}/${encodeURIComponent(key)}`, undefined, { cache: false });
87
+ }, 'deleteAppData');
88
+ }
89
+ /**
90
+ * List every entry stored under `namespace` for the current user.
91
+ *
92
+ * Returns an empty object when the namespace contains no entries (the
93
+ * endpoint never 404s on an empty namespace).
94
+ */
95
+ async listAppData(namespace) {
96
+ assertIdentifier('namespace', namespace);
97
+ return this.withAuthRetry(async () => {
98
+ const response = await this.makeRequest('GET', `/users/me/app-data/${encodeURIComponent(namespace)}`, undefined, { cache: false });
99
+ return response?.entries ?? {};
100
+ }, 'listAppData');
101
+ }
102
+ };
103
+ }
@@ -25,6 +25,7 @@ import { OxyServicesFeaturesMixin } from './OxyServices.features.js';
25
25
  import { OxyServicesTopicsMixin } from './OxyServices.topics.js';
26
26
  import { OxyServicesManagedAccountsMixin } from './OxyServices.managedAccounts.js';
27
27
  import { OxyServicesContactsMixin } from './OxyServices.contacts.js';
28
+ import { OxyServicesAppDataMixin } from './OxyServices.appData.js';
28
29
  /**
29
30
  * Mixin pipeline - applied in order from first to last.
30
31
  *
@@ -64,6 +65,7 @@ const MIXIN_PIPELINE = [
64
65
  OxyServicesTopicsMixin,
65
66
  OxyServicesManagedAccountsMixin,
66
67
  OxyServicesContactsMixin,
68
+ OxyServicesAppDataMixin,
67
69
  // Utility (last, can use all above)
68
70
  OxyServicesUtilityMixin,
69
71
  ];