@phala/cloud 0.1.1 → 0.2.0

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.
@@ -12,6 +12,7 @@ export { getCvmInfo, safeGetCvmInfo, CvmLegacyDetailSchema, GetCvmInfoRequestSch
12
12
  export { getCvmList, safeGetCvmList, GetCvmListSchema, GetCvmListRequestSchema, type GetCvmListRequest, type GetCvmListResponse, } from "./cvms/get_cvm_list";
13
13
  export { getKmsInfo, safeGetKmsInfo, GetKmsInfoRequestSchema, type GetKmsInfoRequest, } from "./kms/get_kms_info";
14
14
  export { getKmsList, safeGetKmsList, GetKmsListSchema, GetKmsListRequestSchema, type GetKmsListRequest, type GetKmsListResponse, } from "./kms/get_kms_list";
15
+ export { nextAppIds, safeNextAppIds, NextAppIdsSchema, NextAppIdsRequestSchema, type NextAppIdsRequest, type NextAppIds, } from "./kms/next_app_ids";
15
16
  export { listWorkspaces, safeListWorkspaces, WorkspaceResponseSchema, ListWorkspacesSchema, PaginationMetadataSchema, type WorkspaceResponse, type ListWorkspaces, type PaginationMetadata, type ListWorkspacesRequest, } from "./workspaces/list_workspaces";
16
17
  export { getWorkspace, safeGetWorkspace, } from "./workspaces/get_workspace";
17
18
  export { listInstanceTypes, safeListInstanceTypes, PaginatedInstanceTypesSchema, InstanceTypeSchema, type PaginatedInstanceTypes, type InstanceType, type ListInstanceTypesRequest, ListInstanceTypesRequestSchema, } from "./list-instance-types";
@@ -23,7 +24,11 @@ export { deleteCvm, safeDeleteCvm, DeleteCvmRequestSchema, type DeleteCvmRequest
23
24
  export { getCvmStats, safeGetCvmStats, CvmSystemInfoSchema, GetCvmStatsRequestSchema, type GetCvmStatsRequest, type CvmSystemInfo, } from "./cvms/get_cvm_stats";
24
25
  export { getCvmContainersStats, safeGetCvmContainersStats, CvmContainersStatsSchema, GetCvmContainersStatsRequestSchema, type GetCvmContainersStatsRequest, type CvmContainersStats, } from "./cvms/get_cvm_containers_stats";
25
26
  export { getCvmNetwork, safeGetCvmNetwork, CvmNetworkSchema, GetCvmNetworkRequestSchema, type GetCvmNetworkRequest, type CvmNetwork, } from "./cvms/get_cvm_network";
27
+ export { getCvmState, safeGetCvmState, CvmStateSchema, GetCvmStateRequestSchema, type GetCvmStateRequest, type CvmState, } from "./cvms/get_cvm_state";
28
+ export { watchCvmState, WatchCvmStateRequestSchema, type WatchCvmStateRequest, type WatchCvmStateOptions, type SSEEvent, WatchAbortedError, MaxRetriesExceededError, } from "./cvms/watch_cvm_state";
26
29
  export { getCvmAttestation, safeGetCvmAttestation, CvmAttestationSchema, GetCvmAttestationRequestSchema, type GetCvmAttestationRequest, type CvmAttestation, } from "./cvms/get_cvm_attestation";
27
30
  export { getCvmDockerCompose, safeGetCvmDockerCompose, GetCvmDockerComposeRequestSchema, type GetCvmDockerComposeRequest, } from "./cvms/get_cvm_docker_compose";
28
31
  export { updateCvmResources, safeUpdateCvmResources, UpdateCvmResourcesRequestSchema, type UpdateCvmResourcesRequest, } from "./cvms/update_cvm_resources";
29
32
  export { updateCvmVisibility, safeUpdateCvmVisibility, UpdateCvmVisibilityRequestSchema, type UpdateCvmVisibilityRequest, } from "./cvms/update_cvm_visibility";
33
+ export { getAvailableOsImages, safeGetAvailableOsImages, OSImageVariantSchema, GetAvailableOSImagesResponseSchema, GetAvailableOSImagesRequestSchema, type OSImageVariant, type GetAvailableOSImagesResponse, type GetAvailableOSImagesRequest, } from "./cvms/get_available_os_images";
34
+ export { updateOsImage, safeUpdateOsImage, UpdateOsImageRequestSchema, type UpdateOsImageRequest, } from "./cvms/update_os_image";
@@ -0,0 +1,73 @@
1
+ import { z } from "zod";
2
+ import { type Client } from "../../client";
3
+ export declare const NextAppIdsRequestSchema: z.ZodObject<{
4
+ counts: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
5
+ }, "strict", z.ZodTypeAny, {
6
+ counts: number;
7
+ }, {
8
+ counts?: number | undefined;
9
+ }>;
10
+ export declare const NextAppIdsSchema: z.ZodObject<{
11
+ app_ids: z.ZodArray<z.ZodObject<{
12
+ app_id: z.ZodString;
13
+ nonce: z.ZodNumber;
14
+ }, "strip", z.ZodTypeAny, {
15
+ nonce: number;
16
+ app_id: string;
17
+ }, {
18
+ nonce: number;
19
+ app_id: string;
20
+ }>, "many">;
21
+ }, "strict", z.ZodTypeAny, {
22
+ app_ids: {
23
+ nonce: number;
24
+ app_id: string;
25
+ }[];
26
+ }, {
27
+ app_ids: {
28
+ nonce: number;
29
+ app_id: string;
30
+ }[];
31
+ }>;
32
+ export type NextAppIdsRequest = z.infer<typeof NextAppIdsRequestSchema>;
33
+ export type NextAppIds = z.infer<typeof NextAppIdsSchema>;
34
+ declare const nextAppIds: {
35
+ (client: Client, params: {
36
+ counts: number;
37
+ }): Promise<{
38
+ app_ids: {
39
+ nonce: number;
40
+ app_id: string;
41
+ }[];
42
+ }>;
43
+ <T extends z.ZodTypeAny>(client: Client, params: {
44
+ counts: number;
45
+ }, parameters: {
46
+ schema: T;
47
+ }): Promise<z.TypeOf<T>>;
48
+ (client: Client, params: {
49
+ counts: number;
50
+ }, parameters: {
51
+ schema: false;
52
+ }): Promise<unknown>;
53
+ }, safeNextAppIds: {
54
+ (client: Client, params: {
55
+ counts: number;
56
+ }): Promise<import("../..").SafeResult<{
57
+ app_ids: {
58
+ nonce: number;
59
+ app_id: string;
60
+ }[];
61
+ }>>;
62
+ <T extends z.ZodTypeAny>(client: Client, params: {
63
+ counts: number;
64
+ }, parameters: {
65
+ schema: T;
66
+ }): Promise<import("../..").SafeResult<z.TypeOf<T>>>;
67
+ (client: Client, params: {
68
+ counts: number;
69
+ }, parameters: {
70
+ schema: false;
71
+ }): Promise<import("../..").SafeResult<unknown>>;
72
+ };
73
+ export { nextAppIds, safeNextAppIds };
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
  *
@@ -15,6 +15,7 @@ import { type CommitCvmComposeFileUpdateRequest, type CommitCvmComposeFileUpdate
15
15
  import { type GetKmsInfoRequest } from "./actions/kms/get_kms_info";
16
16
  import { type GetKmsListRequest, type GetKmsListResponse } from "./actions/kms/get_kms_list";
17
17
  import { type GetAppEnvEncryptPubKeyRequest, type GetAppEnvEncryptPubKey as GetAppEnvEncryptPubKeyResult } from "./actions/kms/get_app_env_encrypt_pubkey";
18
+ import { type NextAppIdsRequest, type NextAppIds } from "./actions/kms/next_app_ids";
18
19
  import { type StartCvmRequest } from "./actions/cvms/start_cvm";
19
20
  import { type StopCvmRequest } from "./actions/cvms/stop_cvm";
20
21
  import { type ShutdownCvmRequest } from "./actions/cvms/shutdown_cvm";
@@ -27,6 +28,9 @@ import { type GetCvmContainersStatsRequest, type CvmContainersStats } from "./ac
27
28
  import { type GetCvmAttestationRequest, type CvmAttestation } from "./actions/cvms/get_cvm_attestation";
28
29
  import { type UpdateCvmResourcesRequest } from "./actions/cvms/update_cvm_resources";
29
30
  import { type UpdateCvmVisibilityRequest } from "./actions/cvms/update_cvm_visibility";
31
+ import { type GetAvailableOSImagesRequest, type GetAvailableOSImagesResponse } from "./actions/cvms/get_available_os_images";
32
+ import { type UpdateOsImageRequest } from "./actions/cvms/update_os_image";
33
+ import { type GetCvmStateRequest, type CvmState } from "./actions/cvms/get_cvm_state";
30
34
  import type { KmsInfo } from "./types/kms_info";
31
35
  import type { VM } from "./types/cvm_info";
32
36
  import type { SafeResult } from "./types/client";
@@ -277,6 +281,20 @@ export interface Client extends BaseClient {
277
281
  safeGetAppEnvEncryptPubKey(request: GetAppEnvEncryptPubKeyRequest, parameters: {
278
282
  schema: false;
279
283
  }): Promise<SafeResult<unknown>>;
284
+ nextAppIds(request?: NextAppIdsRequest): Promise<NextAppIds>;
285
+ nextAppIds<T extends z.ZodTypeAny>(request: NextAppIdsRequest | undefined, parameters: {
286
+ schema: T;
287
+ }): Promise<z.infer<T>>;
288
+ nextAppIds(request: NextAppIdsRequest | undefined, parameters: {
289
+ schema: false;
290
+ }): Promise<unknown>;
291
+ safeNextAppIds(request?: NextAppIdsRequest): Promise<SafeResult<NextAppIds>>;
292
+ safeNextAppIds<T extends z.ZodTypeAny>(request: NextAppIdsRequest | undefined, parameters: {
293
+ schema: T;
294
+ }): Promise<SafeResult<z.infer<T>>>;
295
+ safeNextAppIds(request: NextAppIdsRequest | undefined, parameters: {
296
+ schema: false;
297
+ }): Promise<SafeResult<unknown>>;
280
298
  startCvm(request: StartCvmRequest): Promise<VM>;
281
299
  startCvm<T extends z.ZodTypeAny>(request: StartCvmRequest, parameters: {
282
300
  schema: T;
@@ -445,4 +463,46 @@ export interface Client extends BaseClient {
445
463
  safeUpdateCvmVisibility(request: UpdateCvmVisibilityRequest, parameters: {
446
464
  schema: false;
447
465
  }): Promise<SafeResult<unknown>>;
466
+ getAvailableOsImages(request: GetAvailableOSImagesRequest): Promise<GetAvailableOSImagesResponse>;
467
+ getAvailableOsImages<T extends z.ZodTypeAny>(request: GetAvailableOSImagesRequest, parameters: {
468
+ schema: T;
469
+ }): Promise<z.infer<T>>;
470
+ getAvailableOsImages(request: GetAvailableOSImagesRequest, parameters: {
471
+ schema: false;
472
+ }): Promise<unknown>;
473
+ safeGetAvailableOsImages(request: GetAvailableOSImagesRequest): Promise<SafeResult<GetAvailableOSImagesResponse>>;
474
+ safeGetAvailableOsImages<T extends z.ZodTypeAny>(request: GetAvailableOSImagesRequest, parameters: {
475
+ schema: T;
476
+ }): Promise<SafeResult<z.infer<T>>>;
477
+ safeGetAvailableOsImages(request: GetAvailableOSImagesRequest, parameters: {
478
+ schema: false;
479
+ }): Promise<SafeResult<unknown>>;
480
+ updateOsImage(request: UpdateOsImageRequest): Promise<void>;
481
+ updateOsImage<T extends z.ZodTypeAny>(request: UpdateOsImageRequest, parameters: {
482
+ schema: T;
483
+ }): Promise<z.infer<T>>;
484
+ updateOsImage(request: UpdateOsImageRequest, parameters: {
485
+ schema: false;
486
+ }): Promise<unknown>;
487
+ safeUpdateOsImage(request: UpdateOsImageRequest): Promise<SafeResult<void>>;
488
+ safeUpdateOsImage<T extends z.ZodTypeAny>(request: UpdateOsImageRequest, parameters: {
489
+ schema: T;
490
+ }): Promise<SafeResult<z.infer<T>>>;
491
+ safeUpdateOsImage(request: UpdateOsImageRequest, parameters: {
492
+ schema: false;
493
+ }): Promise<SafeResult<unknown>>;
494
+ getCvmState(request: GetCvmStateRequest): Promise<CvmState>;
495
+ getCvmState<T extends z.ZodTypeAny>(request: GetCvmStateRequest, parameters: {
496
+ schema: T;
497
+ }): Promise<z.infer<T>>;
498
+ getCvmState(request: GetCvmStateRequest, parameters: {
499
+ schema: false;
500
+ }): Promise<unknown>;
501
+ safeGetCvmState(request: GetCvmStateRequest): Promise<SafeResult<CvmState>>;
502
+ safeGetCvmState<T extends z.ZodTypeAny>(request: GetCvmStateRequest, parameters: {
503
+ schema: T;
504
+ }): Promise<SafeResult<z.infer<T>>>;
505
+ safeGetCvmState(request: GetCvmStateRequest, parameters: {
506
+ schema: false;
507
+ }): Promise<SafeResult<unknown>>;
448
508
  }