@freqhole/api-client 0.1.30

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.
Files changed (45) hide show
  1. package/auth-test.html +651 -0
  2. package/package.json +42 -0
  3. package/src/AdminClient.ts +154 -0
  4. package/src/CharnelLocalTransport.ts +598 -0
  5. package/src/CharnelTransport.ts +616 -0
  6. package/src/FreqholeClient.ts +261 -0
  7. package/src/WasmTransport.ts +1077 -0
  8. package/src/automerge/IrohNetworkAdapter.ts +727 -0
  9. package/src/automerge/index.ts +14 -0
  10. package/src/codegen/admin_commands.ts +61 -0
  11. package/src/codegen/routes.ts +213 -0
  12. package/src/codegen/schema.ts +6721 -0
  13. package/src/domains/admin.ts +97 -0
  14. package/src/domains/app.ts +98 -0
  15. package/src/domains/auth.ts +191 -0
  16. package/src/domains/favorites.types.ts +52 -0
  17. package/src/domains/jobs.ts +48 -0
  18. package/src/domains/music.ts +1935 -0
  19. package/src/domains/types.ts +19 -0
  20. package/src/domains/upload.ts +214 -0
  21. package/src/index.ts +48 -0
  22. package/src/permissions.ts +468 -0
  23. package/src/playlistz/convert.ts +267 -0
  24. package/src/playlistz/index.ts +6 -0
  25. package/src/playlistz/mutations.ts +181 -0
  26. package/src/playlistz/protocol.ts +222 -0
  27. package/src/playlistz/schema.ts +138 -0
  28. package/src/playlistz/shareLink.ts +76 -0
  29. package/src/storage/blobs.ts +272 -0
  30. package/src/storage/identity.ts +190 -0
  31. package/src/storage/index.ts +5 -0
  32. package/src/storage/webLocks.ts +92 -0
  33. package/src/test/automerge.ts +796 -0
  34. package/src/test/coverage.ts +105 -0
  35. package/src/test/fixtures.ts +460 -0
  36. package/src/test/integration.ts +320 -0
  37. package/src/test/playlistz.ts +631 -0
  38. package/src/test/stateful.ts +561 -0
  39. package/src/test/storage.ts +443 -0
  40. package/src/test.ts +75 -0
  41. package/src/transport.ts +391 -0
  42. package/src/utils.ts +79 -0
  43. package/src/webauthn.ts +228 -0
  44. package/tsconfig.json +22 -0
  45. package/vite.config.js +9 -0
@@ -0,0 +1,154 @@
1
+ // admin client - typed wrapper around the freqhole-admin/1 ALPN.
2
+ //
3
+ // pairs with the generated `codegen/admin_commands.ts` map to give every
4
+ // command a request/response zod schema validated at the client edge.
5
+ //
6
+ // transport-agnostic: takes an `AdminTransport` that knows how to ship a
7
+ // raw `(command, args)` to a particular destination (local in-process,
8
+ // remote over P2P from a wasm runtime, remote via tauri bridge).
9
+ //
10
+ // see docs/spume-remote-admin-plan.md.
11
+
12
+ import { z } from "zod";
13
+ import { adminCommands, type AdminCommandName } from "./codegen/admin_commands.js";
14
+
15
+ /** rfc 9457-ish error detail, matches grimoire `ErrorDetail`. */
16
+ export interface AdminErrorDetail {
17
+ error_type: string;
18
+ title: string;
19
+ detail: string;
20
+ }
21
+
22
+ /** standard grimoire response envelope. */
23
+ export interface AdminResponse<T = unknown> {
24
+ success: boolean;
25
+ message: string;
26
+ data?: T;
27
+ errors?: AdminErrorDetail[];
28
+ }
29
+
30
+ /**
31
+ * transport responsible for shipping a raw admin envelope.
32
+ *
33
+ * implementations:
34
+ * - browser P2P: midden `proxy_admin(peer_addr, command, args)`
35
+ * - tauri local: `invoke("admin_dispatch")`
36
+ * - tauri remote: `invoke("admin_dispatch_remote")`
37
+ *
38
+ * the transport must return a shape conforming to `AdminResponse<unknown>`;
39
+ * `AdminClient.dispatch` will then zod-validate the `data` payload against
40
+ * the command-specific response schema.
41
+ */
42
+ export interface AdminTransport {
43
+ send(command: string, args: unknown): Promise<AdminResponse<unknown>>;
44
+ }
45
+
46
+ /** thrown when the response envelope's `data` fails its zod schema. */
47
+ export class AdminResponseValidationError extends Error {
48
+ constructor(
49
+ public readonly command: string,
50
+ public readonly issues: z.ZodIssue[],
51
+ ) {
52
+ super(`admin response for ${command} failed validation`);
53
+ this.name = "AdminResponseValidationError";
54
+ }
55
+ }
56
+
57
+ /** thrown when the request `args` fail their zod schema before send. */
58
+ export class AdminRequestValidationError extends Error {
59
+ constructor(
60
+ public readonly command: string,
61
+ public readonly issues: z.ZodIssue[],
62
+ ) {
63
+ super(`admin request for ${command} failed validation`);
64
+ this.name = "AdminRequestValidationError";
65
+ }
66
+ }
67
+
68
+ /** thrown when the server returns `success: false`. */
69
+ export class AdminCommandError extends Error {
70
+ constructor(
71
+ public readonly command: string,
72
+ public readonly response: AdminResponse<unknown>,
73
+ ) {
74
+ const detail =
75
+ response.errors?.[0]?.detail ?? response.message ?? "admin command failed";
76
+ const errType = response.errors?.[0]?.error_type;
77
+ super(errType ? `${errType}: ${detail}` : detail);
78
+ this.name = "AdminCommandError";
79
+ }
80
+
81
+ get errorType(): string | undefined {
82
+ return this.response.errors?.[0]?.error_type;
83
+ }
84
+ }
85
+
86
+ type CommandReq<K extends AdminCommandName> = z.infer<
87
+ (typeof adminCommands)[K]["req"]
88
+ >;
89
+ type CommandResp<K extends AdminCommandName> = z.infer<
90
+ (typeof adminCommands)[K]["resp"]
91
+ >;
92
+
93
+ /**
94
+ * typed admin command client.
95
+ *
96
+ * usage:
97
+ * ```ts
98
+ * const admin = new AdminClient(transport);
99
+ * const knocks = await admin.dispatchOrThrow("knocks_list", undefined);
100
+ * ```
101
+ */
102
+ export class AdminClient {
103
+ constructor(private readonly transport: AdminTransport) {}
104
+
105
+ /** dispatch and return the full envelope (success or failure). */
106
+ async dispatch<K extends AdminCommandName>(
107
+ command: K,
108
+ args: CommandReq<K>,
109
+ ): Promise<AdminResponse<CommandResp<K>>> {
110
+ const def = adminCommands[command];
111
+
112
+ // validate request before sending
113
+ const reqParse = def.req.safeParse(args);
114
+ if (!reqParse.success) {
115
+ throw new AdminRequestValidationError(command, reqParse.error.issues);
116
+ }
117
+
118
+ const raw = await this.transport.send(command, reqParse.data ?? null);
119
+
120
+ // failure envelopes pass through unvalidated; data is typically absent
121
+ if (!raw.success) {
122
+ return raw as AdminResponse<CommandResp<K>>;
123
+ }
124
+
125
+ // validate data against response schema
126
+ if (raw.data === undefined || raw.data === null) {
127
+ // some commands legitimately return no data (EmptyResponse); accept it
128
+ return raw as AdminResponse<CommandResp<K>>;
129
+ }
130
+ const respParse = def.resp.safeParse(raw.data);
131
+ if (!respParse.success) {
132
+ throw new AdminResponseValidationError(command, respParse.error.issues);
133
+ }
134
+
135
+ return {
136
+ success: true,
137
+ message: raw.message,
138
+ data: respParse.data as CommandResp<K>,
139
+ errors: raw.errors,
140
+ };
141
+ }
142
+
143
+ /** dispatch and unwrap data, throwing `AdminCommandError` on failure. */
144
+ async dispatchOrThrow<K extends AdminCommandName>(
145
+ command: K,
146
+ args: CommandReq<K>,
147
+ ): Promise<CommandResp<K>> {
148
+ const response = await this.dispatch(command, args);
149
+ if (!response.success) {
150
+ throw new AdminCommandError(command, response);
151
+ }
152
+ return response.data as CommandResp<K>;
153
+ }
154
+ }