@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.
@@ -0,0 +1,261 @@
1
+ import { z } from "zod";
2
+ import type { FetchError, FetchRequest } from "ofetch";
3
+ /**
4
+ * API Error Response Schema
5
+ */
6
+ export declare const ApiErrorSchema: z.ZodObject<{
7
+ detail: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodObject<{
8
+ msg: z.ZodString;
9
+ type: z.ZodOptional<z.ZodString>;
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>]>>;
20
+ type: z.ZodOptional<z.ZodString>;
21
+ code: z.ZodOptional<z.ZodString>;
22
+ }, "strip", z.ZodTypeAny, {
23
+ code?: string | undefined;
24
+ type?: string | undefined;
25
+ detail?: string | Record<string, unknown> | {
26
+ msg: string;
27
+ type?: string | undefined;
28
+ ctx?: Record<string, unknown> | undefined;
29
+ }[] | undefined;
30
+ }, {
31
+ code?: string | undefined;
32
+ type?: string | undefined;
33
+ detail?: string | Record<string, unknown> | {
34
+ msg: string;
35
+ type?: string | undefined;
36
+ ctx?: Record<string, unknown> | undefined;
37
+ }[] | undefined;
38
+ }>;
39
+ export type ApiError = z.infer<typeof ApiErrorSchema>;
40
+ /**
41
+ * Structured validation error item from FastAPI/Pydantic
42
+ */
43
+ export interface ValidationErrorItem {
44
+ /** Field path (e.g., "name", "body.memory", "query.page") */
45
+ field: string;
46
+ /** Human-readable error message */
47
+ message: string;
48
+ /** Error type from Pydantic (e.g., "string_too_short", "greater_than_equal") */
49
+ type: string;
50
+ /** Additional context (e.g., { min_length: 4 }) */
51
+ context?: Record<string, unknown>;
52
+ }
53
+ /**
54
+ * Base class for all Phala Cloud API errors
55
+ */
56
+ export declare class PhalaCloudError extends Error {
57
+ readonly status: number;
58
+ readonly statusText: string;
59
+ readonly detail?: string | Record<string, unknown> | Array<{
60
+ msg: string;
61
+ type?: string;
62
+ ctx?: Record<string, unknown>;
63
+ }>;
64
+ constructor(message: string, data: {
65
+ status: number;
66
+ statusText: string;
67
+ detail?: string | Record<string, unknown> | Array<{
68
+ msg: string;
69
+ type?: string;
70
+ ctx?: Record<string, unknown>;
71
+ }>;
72
+ });
73
+ }
74
+ /**
75
+ * Base error class for HTTP requests
76
+ * Extends PhalaCloudError with additional HTTP-specific properties
77
+ */
78
+ export declare class RequestError extends PhalaCloudError implements ApiError {
79
+ readonly name = "RequestError";
80
+ readonly isRequestError: true;
81
+ readonly data?: unknown;
82
+ readonly request?: FetchRequest | undefined;
83
+ readonly response?: Response | undefined;
84
+ readonly code?: string | undefined;
85
+ readonly type?: string | undefined;
86
+ constructor(message: string, options?: {
87
+ status?: number | undefined;
88
+ statusText?: string | undefined;
89
+ data?: unknown;
90
+ request?: FetchRequest | undefined;
91
+ response?: Response | undefined;
92
+ cause?: unknown;
93
+ detail?: string | Record<string, unknown> | Array<{
94
+ msg: string;
95
+ type?: string;
96
+ ctx?: Record<string, unknown>;
97
+ }>;
98
+ code?: string | undefined;
99
+ type?: string | undefined;
100
+ });
101
+ /**
102
+ * Create RequestError from FetchError
103
+ */
104
+ static fromFetchError(error: FetchError): RequestError;
105
+ /**
106
+ * Create RequestError from generic Error
107
+ */
108
+ static fromError(error: Error, request?: FetchRequest): RequestError;
109
+ }
110
+ /**
111
+ * Validation error from FastAPI/Pydantic (422)
112
+ * Use instanceof to check: if (error instanceof ValidationError) { ... }
113
+ * Or use property: if (error.isValidationError) { ... }
114
+ */
115
+ export declare class ValidationError extends PhalaCloudError {
116
+ readonly isValidationError: true;
117
+ readonly validationErrors: ValidationErrorItem[];
118
+ constructor(message: string, data: {
119
+ status: number;
120
+ statusText: string;
121
+ detail?: string | Record<string, unknown> | Array<{
122
+ msg: string;
123
+ type?: string;
124
+ ctx?: Record<string, unknown>;
125
+ }>;
126
+ validationErrors: ValidationErrorItem[];
127
+ });
128
+ }
129
+ /**
130
+ * Authentication/Authorization error (401, 403)
131
+ * Use instanceof to check: if (error instanceof AuthError) { ... }
132
+ * Or use property: if (error.isAuthError) { ... }
133
+ */
134
+ export declare class AuthError extends PhalaCloudError {
135
+ readonly isAuthError: true;
136
+ }
137
+ /**
138
+ * Business logic error (400, 409, etc.)
139
+ * Use instanceof to check: if (error instanceof BusinessError) { ... }
140
+ * Or use property: if (error.isBusinessError) { ... }
141
+ */
142
+ export declare class BusinessError extends PhalaCloudError {
143
+ readonly isBusinessError: true;
144
+ }
145
+ /**
146
+ * Server error (500+)
147
+ * Use instanceof to check: if (error instanceof ServerError) { ... }
148
+ * Or use property: if (error.isServerError) { ... }
149
+ */
150
+ export declare class ServerError extends PhalaCloudError {
151
+ readonly isServerError: true;
152
+ }
153
+ /**
154
+ * Unknown error (network issues, etc.)
155
+ * Use instanceof to check: if (error instanceof UnknownError) { ... }
156
+ * Or use property: if (error.isUnknownError) { ... }
157
+ */
158
+ export declare class UnknownError extends PhalaCloudError {
159
+ readonly isUnknownError: true;
160
+ }
161
+ /**
162
+ * Parse RequestError into PhalaCloudError instance
163
+ *
164
+ * Returns the appropriate error subclass based on status code:
165
+ * - 422 → ValidationError (with validationErrors array)
166
+ * - 401, 403 → AuthError
167
+ * - 400, 409, etc. → BusinessError
168
+ * - 500+ → ServerError
169
+ * - Other → UnknownError
170
+ *
171
+ * @param requestError - The RequestError from API call
172
+ * @returns PhalaCloudError subclass instance
173
+ *
174
+ * @example
175
+ * ```typescript
176
+ * try {
177
+ * await client.post('/cvms', data);
178
+ * } catch (error) {
179
+ * if (error instanceof ValidationError) {
180
+ * // TypeScript knows error.validationErrors exists
181
+ * error.validationErrors.forEach(e => {
182
+ * console.error(`${e.field}: ${e.message}`);
183
+ * });
184
+ * } else if (error instanceof AuthError) {
185
+ * // Handle auth error
186
+ * }
187
+ * }
188
+ * ```
189
+ */
190
+ export declare function parseApiError(requestError: RequestError): PhalaCloudError;
191
+ /**
192
+ * Extract all field names from validation errors
193
+ *
194
+ * @deprecated Use `error instanceof ValidationError` and `error.validationErrors` directly
195
+ */
196
+ export declare function getValidationFields(error: PhalaCloudError): string[];
197
+ /**
198
+ * Format validation errors for display
199
+ *
200
+ * @param errors - Array of validation error items
201
+ * @param options - Formatting options
202
+ * @returns Formatted string for display
203
+ *
204
+ * @example
205
+ * ```typescript
206
+ * if (error instanceof ValidationError) {
207
+ * const formatted = formatValidationErrors(error.validationErrors, {
208
+ * numbered: true,
209
+ * indent: 2
210
+ * });
211
+ * console.error(formatted);
212
+ * }
213
+ * // Output:
214
+ * // 1. name: String should have at least 4 characters
215
+ * // 2. memory: Input should be greater than or equal to 1024
216
+ * ```
217
+ */
218
+ export declare function formatValidationErrors(errors: ValidationErrorItem[], options?: {
219
+ /** Add numbers to each error (default: true) */
220
+ numbered?: boolean;
221
+ /** Indent size in spaces (default: 2) */
222
+ indent?: number;
223
+ /** Show field names (default: true) */
224
+ showFields?: boolean;
225
+ }): string;
226
+ /**
227
+ * Create a user-friendly error message from PhalaCloudError
228
+ *
229
+ * @param error - Phala Cloud API error
230
+ * @param options - Formatting options
231
+ * @returns Formatted error message
232
+ *
233
+ * @example
234
+ * ```typescript
235
+ * try {
236
+ * await client.post('/cvms', data);
237
+ * } catch (error) {
238
+ * if (error instanceof PhalaCloudError) {
239
+ * console.error(formatErrorMessage(error));
240
+ * }
241
+ * }
242
+ * // Output:
243
+ * // Validation failed (2 issues)
244
+ * //
245
+ * // 1. name: String should have at least 4 characters
246
+ * // 2. memory: Input should be greater than or equal to 1024
247
+ * ```
248
+ */
249
+ export declare function formatErrorMessage(error: PhalaCloudError, options?: {
250
+ /** Include field names in validation errors (default: true) */
251
+ showFields?: boolean;
252
+ /** Include error class name in output (default: false) */
253
+ showType?: boolean;
254
+ }): string;
255
+ /**
256
+ * Extract error message from API error
257
+ *
258
+ * @param error - API error object
259
+ * @returns Error message string
260
+ */
261
+ export declare function getErrorMessage(error: ApiError): string;
@@ -1,8 +1,8 @@
1
1
  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
- export { getErrorMessage } from "./get_error_message";
4
3
  export { asHex } from "./as-hex";
5
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";
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.1",
3
+ "version": "0.1.2",
4
4
  "description": "TypeScript SDK for Phala Cloud API",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -40,6 +40,7 @@
40
40
  "dependencies": {
41
41
  "@phala/dstack-sdk": "0.5.4-beta.6",
42
42
  "debug": "^4.4.1",
43
+ "mitt": "^3.0.1",
43
44
  "ofetch": "^1.3.3",
44
45
  "viem": "^2.7.0",
45
46
  "zod": "^3.22.4"
@@ -1,2 +0,0 @@
1
- import type { ApiError } from "../types";
2
- export declare function getErrorMessage(error: ApiError): string;