@phala/cloud 0.1.1 → 0.1.2

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.
@@ -161,8 +161,8 @@ export declare const CvmAttestationSchema: z.ZodObject<{
161
161
  }>>;
162
162
  compose_file: z.ZodNullable<z.ZodString>;
163
163
  }, "strip", z.ZodTypeAny, {
164
- error: string | null;
165
164
  name: string | null;
165
+ error: string | null;
166
166
  compose_file: string | null;
167
167
  is_online: boolean;
168
168
  is_public: boolean;
@@ -209,8 +209,8 @@ export declare const CvmAttestationSchema: z.ZodObject<{
209
209
  rootfs_hash?: string | null | undefined;
210
210
  } | null;
211
211
  }, {
212
- error: string | null;
213
212
  name: string | null;
213
+ error: string | null;
214
214
  compose_file: string | null;
215
215
  is_online: boolean;
216
216
  app_certificates: {
@@ -279,8 +279,8 @@ export declare const GetCvmAttestationRequestSchema: z.ZodEffects<z.ZodEffects<z
279
279
  export type GetCvmAttestationRequest = CvmIdInput;
280
280
  declare const getCvmAttestation: {
281
281
  (client: import("../..").BaseClient, params: CvmIdInput): Promise<{
282
- error: string | null;
283
282
  name: string | null;
283
+ error: string | null;
284
284
  compose_file: string | null;
285
285
  is_online: boolean;
286
286
  is_public: boolean;
@@ -335,8 +335,8 @@ declare const getCvmAttestation: {
335
335
  }): Promise<unknown>;
336
336
  }, safeGetCvmAttestation: {
337
337
  (client: import("../..").BaseClient, params: CvmIdInput): Promise<import("../..").SafeResult<{
338
- error: string | null;
339
338
  name: string | null;
339
+ error: string | null;
340
340
  compose_file: string | null;
341
341
  is_online: boolean;
342
342
  is_public: boolean;
@@ -214,6 +214,7 @@ export declare const ProvisionCvmComposeFileUpdateRequestSchema: z.ZodEffects<z.
214
214
  }>, {
215
215
  cvmId: string | undefined;
216
216
  request: {
217
+ update_env_vars: boolean | null | undefined;
217
218
  docker_compose_file: string;
218
219
  name?: string | undefined;
219
220
  public_sysinfo?: boolean | undefined;
@@ -226,10 +227,7 @@ export declare const ProvisionCvmComposeFileUpdateRequestSchema: z.ZodEffects<z.
226
227
  tproxy_enabled?: boolean | undefined;
227
228
  env_pubkey?: string | undefined;
228
229
  salt?: string | null | undefined;
229
- } & {
230
- [k: string]: unknown;
231
230
  };
232
- update_env_vars: boolean | null | undefined;
233
231
  _raw: {
234
232
  app_compose: {
235
233
  docker_compose_file: string;
package/dist/client.d.ts CHANGED
@@ -1,62 +1,130 @@
1
1
  import { ofetch, type FetchOptions, type FetchRequest } from "ofetch";
2
- import { type SafeResult, RequestError, type ClientConfig } from "./types/client";
2
+ import { type Handler } from "mitt";
3
+ import { type SafeResult, type ClientConfig } from "./types/client";
3
4
  import type { Prettify } from "./types/common";
5
+ import { PhalaCloudError } from "./utils/errors";
4
6
  export type { SafeResult } from "./types/client";
7
+ /**
8
+ * Client event types
9
+ */
10
+ type ClientEvents = {
11
+ error: PhalaCloudError;
12
+ };
5
13
  /**
6
14
  * HTTP Client class with ofetch compatibility
7
15
  */
8
16
  export declare class Client {
9
17
  protected fetchInstance: typeof ofetch;
10
18
  readonly config: ClientConfig;
19
+ private emitter;
11
20
  constructor(config?: ClientConfig);
12
21
  /**
13
22
  * Get the underlying ofetch instance for advanced usage
14
23
  */
15
24
  get raw(): import("ofetch").$Fetch;
16
25
  /**
17
- * Perform GET request (throws on error)
26
+ * Listen for error events
27
+ * @param type - Event type ('error' or '*' for all events)
28
+ * @param handler - Event handler function
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * // Listen to error events
33
+ * client.on('error', (error) => {
34
+ * console.error('API Error:', error.message);
35
+ * });
36
+ *
37
+ * // Listen to all events
38
+ * client.on('*', (type, error) => {
39
+ * console.log('Event:', type, error);
40
+ * });
41
+ * ```
42
+ */
43
+ on<K extends keyof ClientEvents>(type: K, handler: Handler<ClientEvents[K]>): void;
44
+ on(type: "*", handler: Handler): void;
45
+ /**
46
+ * Remove event listener
47
+ * @param type - Event type ('error' or '*' for all events)
48
+ * @param handler - Event handler function to remove
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * const errorHandler = (error) => console.error(error);
53
+ * client.on('error', errorHandler);
54
+ * client.off('error', errorHandler);
55
+ * ```
56
+ */
57
+ off<K extends keyof ClientEvents>(type: K, handler?: Handler<ClientEvents[K]>): void;
58
+ off(type: "*", handler?: Handler): void;
59
+ /**
60
+ * Listen for event once (automatically removed after first trigger)
61
+ * @param type - Event type ('error' or '*' for all events)
62
+ * @param handler - Event handler function
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * client.once('error', (error) => {
67
+ * console.error('First error:', error);
68
+ * });
69
+ * ```
70
+ */
71
+ once<K extends keyof ClientEvents>(type: K, handler: Handler<ClientEvents[K]>): void;
72
+ once(type: "*", handler: Handler): void;
73
+ /**
74
+ * Perform GET request (throws PhalaCloudError on error)
18
75
  */
19
76
  get<T = unknown>(request: FetchRequest, options?: Omit<FetchOptions, "method">): Promise<T>;
20
77
  /**
21
- * Perform POST request (throws on error)
78
+ * Perform POST request (throws PhalaCloudError on error)
22
79
  */
23
80
  post<T = unknown>(request: FetchRequest, body?: RequestInit["body"] | Record<string, unknown>, options?: Omit<FetchOptions, "method" | "body">): Promise<T>;
24
81
  /**
25
- * Perform PUT request (throws on error)
82
+ * Perform PUT request (throws PhalaCloudError on error)
26
83
  */
27
84
  put<T = unknown>(request: FetchRequest, body?: RequestInit["body"] | Record<string, unknown>, options?: Omit<FetchOptions, "method" | "body">): Promise<T>;
28
85
  /**
29
- * Perform PATCH request (throws on error)
86
+ * Perform PATCH request (throws PhalaCloudError on error)
30
87
  */
31
88
  patch<T = unknown>(request: FetchRequest, body?: RequestInit["body"] | Record<string, unknown>, options?: Omit<FetchOptions, "method" | "body">): Promise<T>;
32
89
  /**
33
- * Perform DELETE request (throws on error)
90
+ * Perform DELETE request (throws PhalaCloudError on error)
34
91
  */
35
92
  delete<T = unknown>(request: FetchRequest, options?: Omit<FetchOptions, "method">): Promise<T>;
93
+ /**
94
+ * Convert any error to RequestError
95
+ */
96
+ private convertToRequestError;
97
+ /**
98
+ * Broadcast error to event listeners (fire-and-forget)
99
+ * @param requestError - The request error to handle
100
+ * @returns PhalaCloudError instance to throw immediately
101
+ */
102
+ private emitError;
36
103
  /**
37
104
  * Safe wrapper for any request method (zod-style result)
105
+ * Returns PhalaCloudError (all errors extend this base class)
38
106
  */
39
107
  private safeRequest;
40
108
  /**
41
109
  * Safe GET request (returns SafeResult)
42
110
  */
43
- safeGet<T = unknown>(request: FetchRequest, options?: Omit<FetchOptions, "method">): Promise<SafeResult<T, RequestError>>;
111
+ safeGet<T = unknown>(request: FetchRequest, options?: Omit<FetchOptions, "method">): Promise<SafeResult<T, PhalaCloudError>>;
44
112
  /**
45
113
  * Safe POST request (returns SafeResult)
46
114
  */
47
- safePost<T = unknown>(request: FetchRequest, body?: RequestInit["body"] | Record<string, unknown>, options?: Omit<FetchOptions, "method" | "body">): Promise<SafeResult<T, RequestError>>;
115
+ safePost<T = unknown>(request: FetchRequest, body?: RequestInit["body"] | Record<string, unknown>, options?: Omit<FetchOptions, "method" | "body">): Promise<SafeResult<T, PhalaCloudError>>;
48
116
  /**
49
117
  * Safe PUT request (returns SafeResult)
50
118
  */
51
- safePut<T = unknown>(request: FetchRequest, body?: RequestInit["body"] | Record<string, unknown>, options?: Omit<FetchOptions, "method" | "body">): Promise<SafeResult<T, RequestError>>;
119
+ safePut<T = unknown>(request: FetchRequest, body?: RequestInit["body"] | Record<string, unknown>, options?: Omit<FetchOptions, "method" | "body">): Promise<SafeResult<T, PhalaCloudError>>;
52
120
  /**
53
121
  * Safe PATCH request (returns SafeResult)
54
122
  */
55
- safePatch<T = unknown>(request: FetchRequest, body?: RequestInit["body"] | Record<string, unknown>, options?: Omit<FetchOptions, "method" | "body">): Promise<SafeResult<T, RequestError>>;
123
+ safePatch<T = unknown>(request: FetchRequest, body?: RequestInit["body"] | Record<string, unknown>, options?: Omit<FetchOptions, "method" | "body">): Promise<SafeResult<T, PhalaCloudError>>;
56
124
  /**
57
125
  * Safe DELETE request (returns SafeResult)
58
126
  */
59
- safeDelete<T = unknown>(request: FetchRequest, options?: Omit<FetchOptions, "method">): Promise<SafeResult<T, RequestError>>;
127
+ safeDelete<T = unknown>(request: FetchRequest, options?: Omit<FetchOptions, "method">): Promise<SafeResult<T, PhalaCloudError>>;
60
128
  /**
61
129
  * Extend client with additional actions
62
130
  *