@phala/cloud 0.0.7 → 0.0.8

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.
package/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # Phala Cloud JavaScript SDK
2
+
3
+ TypeScript SDK for Phala Cloud API.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ # Using npm
9
+ npm install @phala/cloud
10
+
11
+ # Using yarn
12
+ yarn add @phala/cloud
13
+
14
+ # Using pnpm
15
+ pnpm add @phala/cloud
16
+
17
+ # Using bun
18
+ bun add @phala/cloud
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ### Basic Usage
24
+
25
+ ```typescript
26
+ import { createClient } from '@phala/cloud';
27
+
28
+ // Create client with API key
29
+ const client = createClient({
30
+ apiKey: 'your-api-key',
31
+ // Optional: override base URL
32
+ // baseURL: 'https://custom-api.example.com'
33
+ });
34
+
35
+ // Make API requests
36
+ const result = await client.get('/kms');
37
+ console.log(result);
38
+
39
+ // Using safe methods that return a SafeResult
40
+ const safeResult = await client.safeGet('/kms');
41
+ if (safeResult.success) {
42
+ console.log('Success:', safeResult.data);
43
+ } else {
44
+ console.error('Error:', safeResult.error);
45
+ }
46
+ ```
47
+
48
+ ### Environment Variables
49
+
50
+ You can configure the client using environment variables:
51
+
52
+ - `PHALA_CLOUD_API_KEY`: API key for authentication
53
+ - `PHALA_CLOUD_API_PREFIX`: Base URL prefix for the API
54
+
55
+ ```typescript
56
+ // Using environment variables (set PHALA_CLOUD_API_KEY)
57
+ const client = createClient();
58
+ ```
59
+
60
+ ### Debug Logging
61
+
62
+ The SDK includes built-in debug logging that can display cURL-like request and response information. To enable it, set the `DEBUG` environment variable to `phala::api-client`:
63
+
64
+ ```bash
65
+ # Enable debug logging
66
+ DEBUG=phala::api-client node your-script.js
67
+
68
+ # Or with bun
69
+ DEBUG=phala::api-client bun run your-script.ts
70
+ ```
71
+
72
+ This will print detailed information about each API call in a format similar to cURL:
73
+
74
+ ```
75
+ === REQUEST ===
76
+ > curl -X GET "https://cloud-api.phala.network/api/v1/kms"
77
+ -H "X-API-Key: your-api-key"
78
+ -H "X-Phala-Version: 2025-05-31"
79
+ -H "Content-Type: application/json"
80
+
81
+ === RESPONSE [GET /kms] (123ms) ===
82
+ < HTTP/1.1 200 OK
83
+ < content-type: application/json
84
+ < x-response-time: 123
85
+
86
+ {
87
+ "data": [
88
+ {
89
+ "id": "example-id",
90
+ "name": "Example Key"
91
+ }
92
+ ]
93
+ }
94
+ ```
95
+
96
+ ## Available Methods
97
+
98
+ ### Direct Methods (throw on error)
99
+
100
+ - `client.get<T>(request, options?): Promise<T>`
101
+ - `client.post<T>(request, body?, options?): Promise<T>`
102
+ - `client.put<T>(request, body?, options?): Promise<T>`
103
+ - `client.patch<T>(request, body?, options?): Promise<T>`
104
+ - `client.delete<T>(request, options?): Promise<T>`
105
+
106
+ ### Safe Methods (return SafeResult)
107
+
108
+ - `client.safeGet<T>(request, options?): Promise<SafeResult<T>>`
109
+ - `client.safePost<T>(request, body?, options?): Promise<SafeResult<T>>`
110
+ - `client.safePut<T>(request, body?, options?): Promise<SafeResult<T>>`
111
+ - `client.safePatch<T>(request, body?, options?): Promise<SafeResult<T>>`
112
+ - `client.safeDelete<T>(request, options?): Promise<SafeResult<T>>`
113
+
114
+ ## License
115
+
116
+ Apache-2.0
@@ -97,7 +97,7 @@ export declare const GetCvmListSchema: z.ZodObject<{
97
97
  slug: z.ZodString;
98
98
  url: z.ZodString;
99
99
  version: z.ZodString;
100
- chain_id: z.ZodOptional<z.ZodNumber>;
100
+ chain_id: z.ZodNullable<z.ZodOptional<z.ZodNumber>>;
101
101
  kms_contract_address: z.ZodOptional<z.ZodString>;
102
102
  gateway_app_id: z.ZodOptional<z.ZodString>;
103
103
  }, "strip", z.ZodTypeAny, {
@@ -105,7 +105,7 @@ export declare const GetCvmListSchema: z.ZodObject<{
105
105
  id: string;
106
106
  slug: string;
107
107
  url: string;
108
- chain_id?: number | undefined;
108
+ chain_id?: number | null | undefined;
109
109
  kms_contract_address?: string | undefined;
110
110
  gateway_app_id?: string | undefined;
111
111
  }, {
@@ -113,7 +113,7 @@ export declare const GetCvmListSchema: z.ZodObject<{
113
113
  id: string;
114
114
  slug: string;
115
115
  url: string;
116
- chain_id?: number | undefined;
116
+ chain_id?: number | null | undefined;
117
117
  kms_contract_address?: string | undefined;
118
118
  gateway_app_id?: string | undefined;
119
119
  }>>>;
@@ -171,7 +171,7 @@ export declare const GetCvmListSchema: z.ZodObject<{
171
171
  id: string;
172
172
  slug: string;
173
173
  url: string;
174
- chain_id?: number | undefined;
174
+ chain_id?: number | null | undefined;
175
175
  kms_contract_address?: string | undefined;
176
176
  gateway_app_id?: string | undefined;
177
177
  } | null | undefined;
@@ -223,7 +223,7 @@ export declare const GetCvmListSchema: z.ZodObject<{
223
223
  id: string;
224
224
  slug: string;
225
225
  url: string;
226
- chain_id?: number | undefined;
226
+ chain_id?: number | null | undefined;
227
227
  kms_contract_address?: string | undefined;
228
228
  gateway_app_id?: string | undefined;
229
229
  } | null | undefined;
@@ -283,7 +283,7 @@ export declare const GetCvmListSchema: z.ZodObject<{
283
283
  id: string;
284
284
  slug: string;
285
285
  url: string;
286
- chain_id?: number | undefined;
286
+ chain_id?: number | null | undefined;
287
287
  kms_contract_address?: string | undefined;
288
288
  gateway_app_id?: string | undefined;
289
289
  } | null | undefined;
@@ -341,7 +341,7 @@ export declare const GetCvmListSchema: z.ZodObject<{
341
341
  id: string;
342
342
  slug: string;
343
343
  url: string;
344
- chain_id?: number | undefined;
344
+ chain_id?: number | null | undefined;
345
345
  kms_contract_address?: string | undefined;
346
346
  gateway_app_id?: string | undefined;
347
347
  } | null | undefined;
package/dist/client.d.ts CHANGED
@@ -64,6 +64,10 @@ export declare class Client {
64
64
  * - PHALA_CLOUD_API_KEY: API key for authentication
65
65
  * - PHALA_CLOUD_API_PREFIX: Base URL prefix for the API
66
66
  *
67
+ * Debug Logging:
68
+ * - Set DEBUG=phala::api-client to enable cURL-like request/response logging
69
+ * - This will print detailed information about each API call in a format similar to cURL
70
+ *
67
71
  * @example
68
72
  * ```typescript
69
73
  * // Using explicit configuration
@@ -74,6 +78,9 @@ export declare class Client {
74
78
  *
75
79
  * // Using environment variables (set PHALA_CLOUD_API_KEY)
76
80
  * const client = createClient()
81
+ *
82
+ * // To enable debug logging:
83
+ * // DEBUG=phala::api-client node your-script.js
77
84
  * ```
78
85
  */
79
86
  export declare function createClient(config?: ClientConfig): Client;
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,KAAK,YAAY,EAAE,KAAK,YAAY,EAAc,MAAM,QAAQ,CAAC;AAClF,OAAO,EAAE,KAAK,UAAU,EAAE,YAAY,EAAE,KAAK,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAClF,YAAY,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAIjD;;GAEG;AACH,qBAAa,MAAM;IACjB,SAAS,CAAC,aAAa,EAAE,OAAO,MAAM,CAAC;IACvC,SAAgB,MAAM,EAAE,YAAY,CAAC;gBAEzB,MAAM,GAAE,YAAiB;IA8CrC;;OAEG;IACH,IAAI,GAAG,4BAEN;IAID;;OAEG;IACG,GAAG,CAAC,CAAC,GAAG,OAAO,EACnB,OAAO,EAAE,YAAY,EACrB,OAAO,CAAC,EAAE,IAAI,CAAC,YAAY,EAAE,QAAQ,CAAC,GACrC,OAAO,CAAC,CAAC,CAAC;IAQb;;OAEG;IACG,IAAI,CAAC,CAAC,GAAG,OAAO,EACpB,OAAO,EAAE,YAAY,EACrB,IAAI,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACpD,OAAO,CAAC,EAAE,IAAI,CAAC,YAAY,EAAE,QAAQ,GAAG,MAAM,CAAC,GAC9C,OAAO,CAAC,CAAC,CAAC;IASb;;OA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,YAAY,EAAE,QAAQ,CAAC,GACrC,OAAO,CAAC,UAAU,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC;CAGxC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,YAAY,CAAC,MAAM,GAAE,YAAiB,GAAG,MAAM,CAE9D"}
package/dist/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
@@ -110,6 +120,7 @@ module.exports = __toCommonJS(index_exports);
110
120
 
111
121
  // src/client.ts
112
122
  var import_ofetch = require("ofetch");
123
+ var import_debug = __toESM(require("debug"));
113
124
 
114
125
  // src/types/client.ts
115
126
  var import_zod = require("zod");
@@ -182,6 +193,29 @@ var RequestError = class _RequestError extends Error {
182
193
 
183
194
  // src/client.ts
184
195
  var SUPPORTED_API_VERSIONS = ["2025-05-31"];
196
+ var logger = (0, import_debug.default)("phala::api-client");
197
+ function formatHeaders(headers) {
198
+ return Object.entries(headers).map(([key, value]) => ` -H "${key}: ${value}"`).join("\n");
199
+ }
200
+ function formatBody(body) {
201
+ if (!body) return "";
202
+ const bodyStr = typeof body === "string" ? body : JSON.stringify(body, null, 2);
203
+ return ` -d '${bodyStr.replace(/'/g, "\\'")}'`;
204
+ }
205
+ function formatResponse(status, statusText, headers, body) {
206
+ const headerEntries = [];
207
+ headers.forEach((value, key) => {
208
+ headerEntries.push(`${key}: ${value}`);
209
+ });
210
+ const headerStr = headerEntries.join("\n");
211
+ const bodyStr = typeof body === "string" ? body : JSON.stringify(body, null, 2);
212
+ return [
213
+ `< HTTP/1.1 ${status} ${statusText}`,
214
+ headerStr ? `< ${headerStr.replace(/\n/g, "\n< ")}` : "",
215
+ "",
216
+ bodyStr
217
+ ].filter(Boolean).join("\n");
218
+ }
185
219
  var Client = class {
186
220
  constructor(config = {}) {
187
221
  const resolvedConfig = {
@@ -197,19 +231,66 @@ var Client = class {
197
231
  );
198
232
  }
199
233
  const { apiKey, baseURL, timeout, headers, ...fetchOptions } = resolvedConfig;
234
+ const requestHeaders = {
235
+ "X-API-Key": apiKey,
236
+ "X-Phala-Version": version,
237
+ "Content-Type": "application/json",
238
+ ...headers || {}
239
+ };
200
240
  this.fetchInstance = import_ofetch.ofetch.create({
201
241
  baseURL,
202
242
  timeout: timeout || 3e4,
203
- headers: {
204
- "X-API-Key": apiKey,
205
- "X-Phala-Version": version,
206
- "Content-Type": "application/json",
207
- ...headers || {}
208
- },
243
+ headers: requestHeaders,
209
244
  ...fetchOptions,
245
+ // Log request in cURL format
246
+ onRequest({ request, options }) {
247
+ if (logger.enabled) {
248
+ const method = options.method || "GET";
249
+ const url = typeof request === "string" ? request : request.url;
250
+ const fullUrl = url.startsWith("http") ? url : `${baseURL}${url}`;
251
+ const headerObj = {};
252
+ if (options.headers && typeof options.headers === "object") {
253
+ Object.entries(options.headers).forEach(([key, value]) => {
254
+ if (typeof value === "string") {
255
+ headerObj[key] = value;
256
+ }
257
+ });
258
+ }
259
+ const curlCommand = [
260
+ `> curl -X ${method} "${fullUrl}"`,
261
+ formatHeaders(headerObj),
262
+ options.body ? formatBody(options.body) : ""
263
+ ].filter(Boolean).join("\n");
264
+ logger("\n=== REQUEST ===\n%s\n", curlCommand);
265
+ }
266
+ },
267
+ // Log response in cURL format
268
+ onResponse({ request, response, options }) {
269
+ if (logger.enabled) {
270
+ const method = options.method || "GET";
271
+ const url = typeof request === "string" ? request : request.url;
272
+ logger(
273
+ "\n=== RESPONSE [%s %s] (%dms) ===\n%s\n",
274
+ method,
275
+ url,
276
+ response.headers.get("x-response-time") || "?",
277
+ formatResponse(response.status, response.statusText, response.headers, response._data)
278
+ );
279
+ }
280
+ },
210
281
  // Generic handlers for response error (similar to request.ts)
211
- onResponseError: ({ response }) => {
282
+ onResponseError: ({ request, response, options }) => {
212
283
  console.warn(`HTTP ${response.status}: ${response.url}`);
284
+ if (logger.enabled) {
285
+ const method = options.method || "GET";
286
+ const url = typeof request === "string" ? request : request.url;
287
+ logger(
288
+ "\n=== ERROR RESPONSE [%s %s] ===\n%s\n",
289
+ method,
290
+ url,
291
+ formatResponse(response.status, response.statusText, response.headers, response._data)
292
+ );
293
+ }
213
294
  }
214
295
  });
215
296
  }
@@ -394,7 +475,7 @@ var KMSInfoSchema = import_zod3.z.object({
394
475
  slug: import_zod3.z.string(),
395
476
  url: import_zod3.z.string(),
396
477
  version: import_zod3.z.string(),
397
- chain_id: import_zod3.z.number().optional(),
478
+ chain_id: import_zod3.z.number().optional().nullable(),
398
479
  kms_contract_address: import_zod3.z.string().optional(),
399
480
  gateway_app_id: import_zod3.z.string().optional()
400
481
  });
package/dist/index.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/client.ts
2
2
  import { ofetch } from "ofetch";
3
+ import debug from "debug";
3
4
 
4
5
  // src/types/client.ts
5
6
  import { z } from "zod";
@@ -72,6 +73,29 @@ var RequestError = class _RequestError extends Error {
72
73
 
73
74
  // src/client.ts
74
75
  var SUPPORTED_API_VERSIONS = ["2025-05-31"];
76
+ var logger = debug("phala::api-client");
77
+ function formatHeaders(headers) {
78
+ return Object.entries(headers).map(([key, value]) => ` -H "${key}: ${value}"`).join("\n");
79
+ }
80
+ function formatBody(body) {
81
+ if (!body) return "";
82
+ const bodyStr = typeof body === "string" ? body : JSON.stringify(body, null, 2);
83
+ return ` -d '${bodyStr.replace(/'/g, "\\'")}'`;
84
+ }
85
+ function formatResponse(status, statusText, headers, body) {
86
+ const headerEntries = [];
87
+ headers.forEach((value, key) => {
88
+ headerEntries.push(`${key}: ${value}`);
89
+ });
90
+ const headerStr = headerEntries.join("\n");
91
+ const bodyStr = typeof body === "string" ? body : JSON.stringify(body, null, 2);
92
+ return [
93
+ `< HTTP/1.1 ${status} ${statusText}`,
94
+ headerStr ? `< ${headerStr.replace(/\n/g, "\n< ")}` : "",
95
+ "",
96
+ bodyStr
97
+ ].filter(Boolean).join("\n");
98
+ }
75
99
  var Client = class {
76
100
  constructor(config = {}) {
77
101
  const resolvedConfig = {
@@ -87,19 +111,66 @@ var Client = class {
87
111
  );
88
112
  }
89
113
  const { apiKey, baseURL, timeout, headers, ...fetchOptions } = resolvedConfig;
114
+ const requestHeaders = {
115
+ "X-API-Key": apiKey,
116
+ "X-Phala-Version": version,
117
+ "Content-Type": "application/json",
118
+ ...headers || {}
119
+ };
90
120
  this.fetchInstance = ofetch.create({
91
121
  baseURL,
92
122
  timeout: timeout || 3e4,
93
- headers: {
94
- "X-API-Key": apiKey,
95
- "X-Phala-Version": version,
96
- "Content-Type": "application/json",
97
- ...headers || {}
98
- },
123
+ headers: requestHeaders,
99
124
  ...fetchOptions,
125
+ // Log request in cURL format
126
+ onRequest({ request, options }) {
127
+ if (logger.enabled) {
128
+ const method = options.method || "GET";
129
+ const url = typeof request === "string" ? request : request.url;
130
+ const fullUrl = url.startsWith("http") ? url : `${baseURL}${url}`;
131
+ const headerObj = {};
132
+ if (options.headers && typeof options.headers === "object") {
133
+ Object.entries(options.headers).forEach(([key, value]) => {
134
+ if (typeof value === "string") {
135
+ headerObj[key] = value;
136
+ }
137
+ });
138
+ }
139
+ const curlCommand = [
140
+ `> curl -X ${method} "${fullUrl}"`,
141
+ formatHeaders(headerObj),
142
+ options.body ? formatBody(options.body) : ""
143
+ ].filter(Boolean).join("\n");
144
+ logger("\n=== REQUEST ===\n%s\n", curlCommand);
145
+ }
146
+ },
147
+ // Log response in cURL format
148
+ onResponse({ request, response, options }) {
149
+ if (logger.enabled) {
150
+ const method = options.method || "GET";
151
+ const url = typeof request === "string" ? request : request.url;
152
+ logger(
153
+ "\n=== RESPONSE [%s %s] (%dms) ===\n%s\n",
154
+ method,
155
+ url,
156
+ response.headers.get("x-response-time") || "?",
157
+ formatResponse(response.status, response.statusText, response.headers, response._data)
158
+ );
159
+ }
160
+ },
100
161
  // Generic handlers for response error (similar to request.ts)
101
- onResponseError: ({ response }) => {
162
+ onResponseError: ({ request, response, options }) => {
102
163
  console.warn(`HTTP ${response.status}: ${response.url}`);
164
+ if (logger.enabled) {
165
+ const method = options.method || "GET";
166
+ const url = typeof request === "string" ? request : request.url;
167
+ logger(
168
+ "\n=== ERROR RESPONSE [%s %s] ===\n%s\n",
169
+ method,
170
+ url,
171
+ formatResponse(response.status, response.statusText, response.headers, response._data)
172
+ );
173
+ }
103
174
  }
104
175
  });
105
176
  }
@@ -284,7 +355,7 @@ var KMSInfoSchema = z3.object({
284
355
  slug: z3.string(),
285
356
  url: z3.string(),
286
357
  version: z3.string(),
287
- chain_id: z3.number().optional(),
358
+ chain_id: z3.number().optional().nullable(),
288
359
  kms_contract_address: z3.string().optional(),
289
360
  gateway_app_id: z3.string().optional()
290
361
  });
@@ -81,7 +81,7 @@ export declare const KMSInfoSchema: z.ZodObject<{
81
81
  slug: z.ZodString;
82
82
  url: z.ZodString;
83
83
  version: z.ZodString;
84
- chain_id: z.ZodOptional<z.ZodNumber>;
84
+ chain_id: z.ZodNullable<z.ZodOptional<z.ZodNumber>>;
85
85
  kms_contract_address: z.ZodOptional<z.ZodString>;
86
86
  gateway_app_id: z.ZodOptional<z.ZodString>;
87
87
  }, "strip", z.ZodTypeAny, {
@@ -89,7 +89,7 @@ export declare const KMSInfoSchema: z.ZodObject<{
89
89
  id: string;
90
90
  slug: string;
91
91
  url: string;
92
- chain_id?: number | undefined;
92
+ chain_id?: number | null | undefined;
93
93
  kms_contract_address?: string | undefined;
94
94
  gateway_app_id?: string | undefined;
95
95
  }, {
@@ -97,7 +97,7 @@ export declare const KMSInfoSchema: z.ZodObject<{
97
97
  id: string;
98
98
  slug: string;
99
99
  url: string;
100
- chain_id?: number | undefined;
100
+ chain_id?: number | null | undefined;
101
101
  kms_contract_address?: string | undefined;
102
102
  gateway_app_id?: string | undefined;
103
103
  }>;
@@ -183,7 +183,7 @@ export declare const CvmInfoSchema: z.ZodObject<{
183
183
  slug: z.ZodString;
184
184
  url: z.ZodString;
185
185
  version: z.ZodString;
186
- chain_id: z.ZodOptional<z.ZodNumber>;
186
+ chain_id: z.ZodNullable<z.ZodOptional<z.ZodNumber>>;
187
187
  kms_contract_address: z.ZodOptional<z.ZodString>;
188
188
  gateway_app_id: z.ZodOptional<z.ZodString>;
189
189
  }, "strip", z.ZodTypeAny, {
@@ -191,7 +191,7 @@ export declare const CvmInfoSchema: z.ZodObject<{
191
191
  id: string;
192
192
  slug: string;
193
193
  url: string;
194
- chain_id?: number | undefined;
194
+ chain_id?: number | null | undefined;
195
195
  kms_contract_address?: string | undefined;
196
196
  gateway_app_id?: string | undefined;
197
197
  }, {
@@ -199,7 +199,7 @@ export declare const CvmInfoSchema: z.ZodObject<{
199
199
  id: string;
200
200
  slug: string;
201
201
  url: string;
202
- chain_id?: number | undefined;
202
+ chain_id?: number | null | undefined;
203
203
  kms_contract_address?: string | undefined;
204
204
  gateway_app_id?: string | undefined;
205
205
  }>>>;
@@ -257,7 +257,7 @@ export declare const CvmInfoSchema: z.ZodObject<{
257
257
  id: string;
258
258
  slug: string;
259
259
  url: string;
260
- chain_id?: number | undefined;
260
+ chain_id?: number | null | undefined;
261
261
  kms_contract_address?: string | undefined;
262
262
  gateway_app_id?: string | undefined;
263
263
  } | null | undefined;
@@ -309,7 +309,7 @@ export declare const CvmInfoSchema: z.ZodObject<{
309
309
  id: string;
310
310
  slug: string;
311
311
  url: string;
312
- chain_id?: number | undefined;
312
+ chain_id?: number | null | undefined;
313
313
  kms_contract_address?: string | undefined;
314
314
  gateway_app_id?: string | undefined;
315
315
  } | null | undefined;
@@ -362,7 +362,7 @@ export declare const CvmLegacyDetailSchema: z.ZodObject<{
362
362
  slug: z.ZodString;
363
363
  url: z.ZodString;
364
364
  version: z.ZodString;
365
- chain_id: z.ZodOptional<z.ZodNumber>;
365
+ chain_id: z.ZodNullable<z.ZodOptional<z.ZodNumber>>;
366
366
  kms_contract_address: z.ZodOptional<z.ZodString>;
367
367
  gateway_app_id: z.ZodOptional<z.ZodString>;
368
368
  }, "strip", z.ZodTypeAny, {
@@ -370,7 +370,7 @@ export declare const CvmLegacyDetailSchema: z.ZodObject<{
370
370
  id: string;
371
371
  slug: string;
372
372
  url: string;
373
- chain_id?: number | undefined;
373
+ chain_id?: number | null | undefined;
374
374
  kms_contract_address?: string | undefined;
375
375
  gateway_app_id?: string | undefined;
376
376
  }, {
@@ -378,7 +378,7 @@ export declare const CvmLegacyDetailSchema: z.ZodObject<{
378
378
  id: string;
379
379
  slug: string;
380
380
  url: string;
381
- chain_id?: number | undefined;
381
+ chain_id?: number | null | undefined;
382
382
  kms_contract_address?: string | undefined;
383
383
  gateway_app_id?: string | undefined;
384
384
  }>>;
@@ -413,7 +413,7 @@ export declare const CvmLegacyDetailSchema: z.ZodObject<{
413
413
  id: string;
414
414
  slug: string;
415
415
  url: string;
416
- chain_id?: number | undefined;
416
+ chain_id?: number | null | undefined;
417
417
  kms_contract_address?: string | undefined;
418
418
  gateway_app_id?: string | undefined;
419
419
  } | null;
@@ -456,7 +456,7 @@ export declare const CvmLegacyDetailSchema: z.ZodObject<{
456
456
  id: string;
457
457
  slug: string;
458
458
  url: string;
459
- chain_id?: number | undefined;
459
+ chain_id?: number | null | undefined;
460
460
  kms_contract_address?: string | undefined;
461
461
  gateway_app_id?: string | undefined;
462
462
  } | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phala/cloud",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "TypeScript SDK for Phala Cloud API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -30,12 +30,14 @@
30
30
  "license": "Apache-2.0",
31
31
  "dependencies": {
32
32
  "@phala/dstack-sdk": "0.5.4-beta.6",
33
+ "debug": "^4.4.1",
33
34
  "ofetch": "^1.3.3",
34
35
  "viem": "^2.7.0",
35
36
  "zod": "^3.22.4"
36
37
  },
37
38
  "devDependencies": {
38
39
  "@biomejs/biome": "^1.9.4",
40
+ "@types/debug": "^4.1.12",
39
41
  "@types/node": "^20.10.0",
40
42
  "dotenv": "^16.5.0",
41
43
  "tsup": "^8.0.0",