@phala/cloud 0.1.1-beta.3 → 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;
@@ -1,6 +1,5 @@
1
1
  import { z } from "zod";
2
2
  import { type Client } from "../../client";
3
- import { LooseAppComposeSchema } from "../../types/app_compose";
4
3
  import { type CvmIdInput } from "../../types/cvm_id";
5
4
  /**
6
5
  * Get CVM compose file configuration
@@ -69,7 +68,7 @@ import { type CvmIdInput } from "../../types/cvm_id";
69
68
  * }
70
69
  * ```
71
70
  */
72
- export declare const GetCvmComposeFileResultSchema: z.ZodObject<{
71
+ export declare const GetCvmComposeFileResultSchema: z.ZodEffects<z.ZodObject<{
73
72
  allowed_envs: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
74
73
  docker_compose_file: z.ZodString;
75
74
  features: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
@@ -108,8 +107,39 @@ export declare const GetCvmComposeFileResultSchema: z.ZodObject<{
108
107
  pre_launch_script: z.ZodOptional<z.ZodString>;
109
108
  env_pubkey: z.ZodOptional<z.ZodString>;
110
109
  salt: z.ZodNullable<z.ZodOptional<z.ZodString>>;
110
+ }, z.ZodTypeAny, "passthrough">>, {
111
+ docker_compose_file: string;
112
+ name?: string | undefined;
113
+ public_sysinfo?: boolean | undefined;
114
+ public_logs?: boolean | undefined;
115
+ manifest_version?: number | undefined;
116
+ features?: string[] | undefined;
117
+ allowed_envs?: string[] | undefined;
118
+ pre_launch_script?: string | undefined;
119
+ kms_enabled?: boolean | undefined;
120
+ tproxy_enabled?: boolean | undefined;
121
+ env_pubkey?: string | undefined;
122
+ salt?: string | null | undefined;
123
+ } & {
124
+ [k: string]: unknown;
125
+ } & {
126
+ getHash: () => string;
127
+ toString: () => string;
128
+ }, z.objectInputType<{
129
+ allowed_envs: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
130
+ docker_compose_file: z.ZodString;
131
+ features: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
132
+ name: z.ZodOptional<z.ZodString>;
133
+ manifest_version: z.ZodOptional<z.ZodNumber>;
134
+ kms_enabled: z.ZodOptional<z.ZodBoolean>;
135
+ public_logs: z.ZodOptional<z.ZodBoolean>;
136
+ public_sysinfo: z.ZodOptional<z.ZodBoolean>;
137
+ tproxy_enabled: z.ZodOptional<z.ZodBoolean>;
138
+ pre_launch_script: z.ZodOptional<z.ZodString>;
139
+ env_pubkey: z.ZodOptional<z.ZodString>;
140
+ salt: z.ZodNullable<z.ZodOptional<z.ZodString>>;
111
141
  }, z.ZodTypeAny, "passthrough">>;
112
- export declare const CvmComposeFileSchema: z.ZodObject<{
142
+ export declare const CvmComposeFileSchema: z.ZodEffects<z.ZodObject<{
113
143
  allowed_envs: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
114
144
  docker_compose_file: z.ZodString;
115
145
  features: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
@@ -148,9 +178,40 @@ export declare const CvmComposeFileSchema: z.ZodObject<{
148
178
  pre_launch_script: z.ZodOptional<z.ZodString>;
149
179
  env_pubkey: z.ZodOptional<z.ZodString>;
150
180
  salt: z.ZodNullable<z.ZodOptional<z.ZodString>>;
181
+ }, z.ZodTypeAny, "passthrough">>, {
182
+ docker_compose_file: string;
183
+ name?: string | undefined;
184
+ public_sysinfo?: boolean | undefined;
185
+ public_logs?: boolean | undefined;
186
+ manifest_version?: number | undefined;
187
+ features?: string[] | undefined;
188
+ allowed_envs?: string[] | undefined;
189
+ pre_launch_script?: string | undefined;
190
+ kms_enabled?: boolean | undefined;
191
+ tproxy_enabled?: boolean | undefined;
192
+ env_pubkey?: string | undefined;
193
+ salt?: string | null | undefined;
194
+ } & {
195
+ [k: string]: unknown;
196
+ } & {
197
+ getHash: () => string;
198
+ toString: () => string;
199
+ }, z.objectInputType<{
200
+ allowed_envs: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
201
+ docker_compose_file: z.ZodString;
202
+ features: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
203
+ name: z.ZodOptional<z.ZodString>;
204
+ manifest_version: z.ZodOptional<z.ZodNumber>;
205
+ kms_enabled: z.ZodOptional<z.ZodBoolean>;
206
+ public_logs: z.ZodOptional<z.ZodBoolean>;
207
+ public_sysinfo: z.ZodOptional<z.ZodBoolean>;
208
+ tproxy_enabled: z.ZodOptional<z.ZodBoolean>;
209
+ pre_launch_script: z.ZodOptional<z.ZodString>;
210
+ env_pubkey: z.ZodOptional<z.ZodString>;
211
+ salt: z.ZodNullable<z.ZodOptional<z.ZodString>>;
151
212
  }, z.ZodTypeAny, "passthrough">>;
152
213
  export type CvmComposeFile = z.infer<typeof CvmComposeFileSchema>;
153
- export type GetCvmComposeFileResult = z.infer<typeof LooseAppComposeSchema>;
214
+ export type GetCvmComposeFileResult = z.infer<typeof GetCvmComposeFileResultSchema>;
154
215
  export declare const GetCvmComposeFileRequestSchema: z.ZodEffects<z.ZodEffects<z.ZodObject<{
155
216
  id: z.ZodOptional<z.ZodString>;
156
217
  uuid: z.ZodOptional<z.ZodString>;
@@ -171,20 +232,25 @@ export declare const GetCvmComposeFileRequestSchema: z.ZodEffects<z.ZodEffects<z
171
232
  }, any>;
172
233
  export type GetCvmComposeFileRequest = CvmIdInput;
173
234
  declare const getCvmComposeFile: {
174
- (client: Client, params: CvmIdInput): Promise<z.objectOutputType<{
175
- allowed_envs: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
176
- docker_compose_file: z.ZodString;
177
- features: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
178
- name: z.ZodOptional<z.ZodString>;
179
- manifest_version: z.ZodOptional<z.ZodNumber>;
180
- kms_enabled: z.ZodOptional<z.ZodBoolean>;
181
- public_logs: z.ZodOptional<z.ZodBoolean>;
182
- public_sysinfo: z.ZodOptional<z.ZodBoolean>;
183
- tproxy_enabled: z.ZodOptional<z.ZodBoolean>;
184
- pre_launch_script: z.ZodOptional<z.ZodString>;
185
- env_pubkey: z.ZodOptional<z.ZodString>;
186
- salt: z.ZodNullable<z.ZodOptional<z.ZodString>>;
187
- }, z.ZodTypeAny, "passthrough">>;
235
+ (client: Client, params: CvmIdInput): Promise<{
236
+ docker_compose_file: string;
237
+ name?: string | undefined;
238
+ public_sysinfo?: boolean | undefined;
239
+ public_logs?: boolean | undefined;
240
+ manifest_version?: number | undefined;
241
+ features?: string[] | undefined;
242
+ allowed_envs?: string[] | undefined;
243
+ pre_launch_script?: string | undefined;
244
+ kms_enabled?: boolean | undefined;
245
+ tproxy_enabled?: boolean | undefined;
246
+ env_pubkey?: string | undefined;
247
+ salt?: string | null | undefined;
248
+ } & {
249
+ [k: string]: unknown;
250
+ } & {
251
+ getHash: () => string;
252
+ toString: () => string;
253
+ }>;
188
254
  <T extends z.ZodTypeAny>(client: Client, params: CvmIdInput, parameters: {
189
255
  schema: T;
190
256
  }): Promise<z.TypeOf<T>>;
@@ -192,20 +258,25 @@ declare const getCvmComposeFile: {
192
258
  schema: false;
193
259
  }): Promise<unknown>;
194
260
  }, safeGetCvmComposeFile: {
195
- (client: Client, params: CvmIdInput): Promise<import("../..").SafeResult<z.objectOutputType<{
196
- allowed_envs: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
197
- docker_compose_file: z.ZodString;
198
- features: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
199
- name: z.ZodOptional<z.ZodString>;
200
- manifest_version: z.ZodOptional<z.ZodNumber>;
201
- kms_enabled: z.ZodOptional<z.ZodBoolean>;
202
- public_logs: z.ZodOptional<z.ZodBoolean>;
203
- public_sysinfo: z.ZodOptional<z.ZodBoolean>;
204
- tproxy_enabled: z.ZodOptional<z.ZodBoolean>;
205
- pre_launch_script: z.ZodOptional<z.ZodString>;
206
- env_pubkey: z.ZodOptional<z.ZodString>;
207
- salt: z.ZodNullable<z.ZodOptional<z.ZodString>>;
208
- }, z.ZodTypeAny, "passthrough">>>;
261
+ (client: Client, params: CvmIdInput): Promise<import("../..").SafeResult<{
262
+ docker_compose_file: string;
263
+ name?: string | undefined;
264
+ public_sysinfo?: boolean | undefined;
265
+ public_logs?: boolean | undefined;
266
+ manifest_version?: number | undefined;
267
+ features?: string[] | undefined;
268
+ allowed_envs?: string[] | undefined;
269
+ pre_launch_script?: string | undefined;
270
+ kms_enabled?: boolean | undefined;
271
+ tproxy_enabled?: boolean | undefined;
272
+ env_pubkey?: string | undefined;
273
+ salt?: string | null | undefined;
274
+ } & {
275
+ [k: string]: unknown;
276
+ } & {
277
+ getHash: () => string;
278
+ toString: () => string;
279
+ }>>;
209
280
  <T extends z.ZodTypeAny>(client: Client, params: CvmIdInput, parameters: {
210
281
  schema: T;
211
282
  }): Promise<import("../..").SafeResult<z.TypeOf<T>>>;
@@ -34,7 +34,7 @@ import { type Client } from "../../client";
34
34
  * disk_size: 10,
35
35
  * compose_file: {
36
36
  * docker_compose_file: docker_compose,
37
- * name: 'my-app',
37
+ * name: '', // Internal field, use empty string
38
38
  * },
39
39
  * };
40
40
  *
@@ -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
  *