@phala/cloud 0.1.2 → 0.2.1-beta.1

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.
@@ -10,22 +10,22 @@ import { type Client, type SafeResult } from "../client";
10
10
  */
11
11
  export declare function defineSimpleAction<TSchema extends z.ZodTypeAny, TReturnOverride = never>(schema: TSchema, fn: (client: Client) => Promise<unknown>): {
12
12
  action: {
13
- (client: Client): Promise<[TReturnOverride] extends [never] ? z.TypeOf<TSchema> : TReturnOverride>;
14
- <T extends z.ZodTypeAny>(client: Client, parameters: {
15
- schema: T;
16
- }): Promise<z.infer<T>>;
17
13
  (client: Client, parameters: {
18
14
  schema: false;
19
15
  }): Promise<unknown>;
20
- };
21
- safeAction: {
22
- (client: Client): Promise<SafeResult<[TReturnOverride] extends [never] ? z.TypeOf<TSchema> : TReturnOverride>>;
23
16
  <T extends z.ZodTypeAny>(client: Client, parameters: {
24
17
  schema: T;
25
- }): Promise<SafeResult<z.infer<T>>>;
18
+ }): Promise<z.infer<T>>;
19
+ (client: Client): Promise<[TReturnOverride] extends [never] ? z.TypeOf<TSchema> : TReturnOverride>;
20
+ };
21
+ safeAction: {
26
22
  (client: Client, parameters: {
27
23
  schema: false;
28
24
  }): Promise<SafeResult<unknown>>;
25
+ <T extends z.ZodTypeAny>(client: Client, parameters: {
26
+ schema: T;
27
+ }): Promise<SafeResult<z.infer<T>>>;
28
+ (client: Client): Promise<SafeResult<[TReturnOverride] extends [never] ? z.TypeOf<TSchema> : TReturnOverride>>;
29
29
  };
30
30
  };
31
31
  /**
@@ -8,33 +8,43 @@ export declare const ApiErrorSchema: z.ZodObject<{
8
8
  msg: z.ZodString;
9
9
  type: z.ZodOptional<z.ZodString>;
10
10
  ctx: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
11
- }, "strip", z.ZodTypeAny, {
12
- msg: string;
13
- type?: string | undefined;
14
- ctx?: Record<string, unknown> | undefined;
15
- }, {
16
- msg: string;
17
- type?: string | undefined;
18
- ctx?: Record<string, unknown> | undefined;
19
- }>, "many">, z.ZodRecord<z.ZodString, z.ZodUnknown>]>>;
11
+ loc: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber]>, "many">>;
12
+ input: z.ZodOptional<z.ZodUnknown>;
13
+ }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
14
+ msg: z.ZodString;
15
+ type: z.ZodOptional<z.ZodString>;
16
+ ctx: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
17
+ loc: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber]>, "many">>;
18
+ input: z.ZodOptional<z.ZodUnknown>;
19
+ }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
20
+ msg: z.ZodString;
21
+ type: z.ZodOptional<z.ZodString>;
22
+ ctx: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
23
+ loc: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber]>, "many">>;
24
+ input: z.ZodOptional<z.ZodUnknown>;
25
+ }, z.ZodTypeAny, "passthrough">>, "many">, z.ZodRecord<z.ZodString, z.ZodUnknown>]>>;
20
26
  type: z.ZodOptional<z.ZodString>;
21
27
  code: z.ZodOptional<z.ZodString>;
22
28
  }, "strip", z.ZodTypeAny, {
23
29
  code?: string | undefined;
24
30
  type?: string | undefined;
25
- detail?: string | Record<string, unknown> | {
26
- msg: string;
27
- type?: string | undefined;
28
- ctx?: Record<string, unknown> | undefined;
29
- }[] | undefined;
31
+ detail?: string | Record<string, unknown> | z.objectOutputType<{
32
+ msg: z.ZodString;
33
+ type: z.ZodOptional<z.ZodString>;
34
+ ctx: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
35
+ loc: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber]>, "many">>;
36
+ input: z.ZodOptional<z.ZodUnknown>;
37
+ }, z.ZodTypeAny, "passthrough">[] | undefined;
30
38
  }, {
31
39
  code?: string | undefined;
32
40
  type?: string | undefined;
33
- detail?: string | Record<string, unknown> | {
34
- msg: string;
35
- type?: string | undefined;
36
- ctx?: Record<string, unknown> | undefined;
37
- }[] | undefined;
41
+ detail?: string | Record<string, unknown> | z.objectInputType<{
42
+ msg: z.ZodString;
43
+ type: z.ZodOptional<z.ZodString>;
44
+ ctx: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
45
+ loc: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber]>, "many">>;
46
+ input: z.ZodOptional<z.ZodUnknown>;
47
+ }, z.ZodTypeAny, "passthrough">[] | undefined;
38
48
  }>;
39
49
  export type ApiError = z.infer<typeof ApiErrorSchema>;
40
50
  /**
@@ -94,6 +104,9 @@ export declare class RequestError extends PhalaCloudError implements ApiError {
94
104
  msg: string;
95
105
  type?: string;
96
106
  ctx?: Record<string, unknown>;
107
+ loc?: (string | number)[];
108
+ input?: unknown;
109
+ [key: string]: unknown;
97
110
  }>;
98
111
  code?: string | undefined;
99
112
  type?: string | undefined;
@@ -122,6 +135,9 @@ export declare class ValidationError extends PhalaCloudError {
122
135
  msg: string;
123
136
  type?: string;
124
137
  ctx?: Record<string, unknown>;
138
+ loc?: (string | number)[];
139
+ input?: unknown;
140
+ [key: string]: unknown;
125
141
  }>;
126
142
  validationErrors: ValidationErrorItem[];
127
143
  });
@@ -162,6 +178,7 @@ export declare class UnknownError extends PhalaCloudError {
162
178
  * Parse RequestError into PhalaCloudError instance
163
179
  *
164
180
  * Returns the appropriate error subclass based on status code:
181
+ * - New structured errors (with error_code) → ResourceError
165
182
  * - 422 → ValidationError (with validationErrors array)
166
183
  * - 401, 403 → AuthError
167
184
  * - 400, 409, etc. → BusinessError
@@ -176,7 +193,13 @@ export declare class UnknownError extends PhalaCloudError {
176
193
  * try {
177
194
  * await client.post('/cvms', data);
178
195
  * } catch (error) {
179
- * if (error instanceof ValidationError) {
196
+ * if (error instanceof ResourceError) {
197
+ * // New structured error with error code
198
+ * console.error(`Error [${error.errorCode}]: ${error.message}`);
199
+ * if (error.suggestions) {
200
+ * error.suggestions.forEach(s => console.log(s));
201
+ * }
202
+ * } else if (error instanceof ValidationError) {
180
203
  * // TypeScript knows error.validationErrors exists
181
204
  * error.validationErrors.forEach(e => {
182
205
  * console.error(`${e.field}: ${e.message}`);
@@ -259,3 +282,97 @@ export declare function formatErrorMessage(error: PhalaCloudError, options?: {
259
282
  * @returns Error message string
260
283
  */
261
284
  export declare function getErrorMessage(error: ApiError): string;
285
+ /**
286
+ * Structured error detail from new error format (ERR-xxxx codes)
287
+ */
288
+ export interface StructuredErrorDetail {
289
+ field?: string;
290
+ value?: unknown;
291
+ message?: string;
292
+ }
293
+ /**
294
+ * Error link from structured error response
295
+ */
296
+ export interface ErrorLink {
297
+ url: string;
298
+ label: string;
299
+ }
300
+ /**
301
+ * New structured error response format with unique error codes
302
+ */
303
+ export interface StructuredErrorResponse {
304
+ error_code: string;
305
+ message: string;
306
+ details?: StructuredErrorDetail[];
307
+ suggestions?: string[];
308
+ links?: ErrorLink[];
309
+ }
310
+ /**
311
+ * Resource provisioning error with structured details
312
+ * Extends BusinessError to handle new error format from backend
313
+ *
314
+ * Use instanceof to check: if (error instanceof ResourceError) { ... }
315
+ * Or use property: if (error.isResourceError) { ... }
316
+ */
317
+ export declare class ResourceError extends BusinessError {
318
+ readonly isResourceError: true;
319
+ readonly errorCode?: string;
320
+ readonly structuredDetails?: StructuredErrorDetail[];
321
+ readonly suggestions?: string[];
322
+ readonly links?: ErrorLink[];
323
+ constructor(message: string, data: {
324
+ status: number;
325
+ statusText: string;
326
+ detail?: string | Record<string, unknown> | Array<{
327
+ msg: string;
328
+ type?: string;
329
+ ctx?: Record<string, unknown>;
330
+ loc?: (string | number)[];
331
+ input?: unknown;
332
+ [key: string]: unknown;
333
+ }>;
334
+ errorCode?: string;
335
+ structuredDetails?: StructuredErrorDetail[];
336
+ suggestions?: string[];
337
+ links?: ErrorLink[];
338
+ });
339
+ }
340
+ /**
341
+ * Format structured error for display
342
+ *
343
+ * @param error - Resource error with structured details
344
+ * @param options - Formatting options
345
+ * @returns Formatted error message
346
+ *
347
+ * @example
348
+ * ```typescript
349
+ * try {
350
+ * await client.provisionCvm(payload);
351
+ * } catch (error) {
352
+ * if (error instanceof ResourceError) {
353
+ * console.error(formatStructuredError(error));
354
+ * }
355
+ * }
356
+ * // Output:
357
+ * // Error [ERR-01-001]: The requested instance type does not exist
358
+ * //
359
+ * // Details:
360
+ * // - Instance type 'invalid' is not recognized
361
+ * //
362
+ * // Suggestions:
363
+ * // - Use a valid instance type: tdx.small, tdx.medium, or tdx.large
364
+ * // - Alternatively, specify CPU and memory requirements manually
365
+ * //
366
+ * // Learn more:
367
+ * // - View available instance types: https://cloud.phala.network/instances
368
+ * // - Contact support: https://cloud.phala.network/support
369
+ * ```
370
+ */
371
+ export declare function formatStructuredError(error: ResourceError, options?: {
372
+ /** Show error code in output (default: true) */
373
+ showErrorCode?: boolean;
374
+ /** Show suggestions (default: true) */
375
+ showSuggestions?: boolean;
376
+ /** Show links (default: true) */
377
+ showLinks?: boolean;
378
+ }): string;
@@ -2,7 +2,7 @@ export { encryptEnvVars } from "@phala/dstack-sdk/encrypt-env-vars";
2
2
  export { getComposeHash, dumpAppCompose, preprocessAppCompose, sortObject, withComposeMethods, type AppCompose, type AppComposeWithMethods, type SortableValue, type SortableObject, type SortableArray, } from "./get_compose_hash";
3
3
  export { asHex } from "./as-hex";
4
4
  export { validateActionParameters, safeValidateActionParameters } from "./validate-parameters";
5
- export { parseApiError, PhalaCloudError, RequestError, ValidationError, AuthError, BusinessError, ServerError, UnknownError, formatValidationErrors, formatErrorMessage, getErrorMessage, getValidationFields, type ValidationErrorItem, ApiErrorSchema, type ApiError, } from "./errors";
5
+ export { parseApiError, PhalaCloudError, RequestError, ValidationError, AuthError, BusinessError, ServerError, UnknownError, ResourceError, formatValidationErrors, formatErrorMessage, formatStructuredError, getErrorMessage, getValidationFields, type ValidationErrorItem, type StructuredErrorDetail, type ErrorLink, type StructuredErrorResponse, ApiErrorSchema, type ApiError, } from "./errors";
6
6
  export { createNetworkClients, extractNetworkClients, checkNetworkStatus, checkBalance, validateNetworkPrerequisites, waitForTransactionReceipt, executeTransaction, NetworkError, WalletError, TransactionError, type NetworkConfig, type WalletConnection, type NetworkClients, type BalanceCheckResult, type TransactionOptions, type TransactionResult, } from "./network";
7
7
  export { createTransactionTracker, executeBatchTransactions, executeTransactionWithRetry, estimateTransactionGas, type TransactionState, type TransactionStatus, type TransactionTracker, type BatchTransactionOptions, type BatchTransactionResult, type RetryOptions, type GasEstimationOptions, } from "./transaction";
8
8
  export { createClientsFromPrivateKey, createClientsFromBrowser, autoCreateClients, switchToNetwork, addNetwork, } from "./client-factories";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phala/cloud",
3
- "version": "0.1.2",
3
+ "version": "0.2.1-beta.1",
4
4
  "description": "TypeScript SDK for Phala Cloud API",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",