@gradio/client 0.1.0 → 0.1.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @gradio/client
2
2
 
3
+ ## 0.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#4201](https://github.com/gradio-app/gradio/pull/4201) [`da5b4ee1`](https://github.com/gradio-app/gradio/commit/da5b4ee11721175858ded96e5710225369097f74) Thanks [@pngwn](https://github.com/pngwn)! - Ensure semiver is bundled so CDN links work correctly.
8
+
9
+ - [#4202](https://github.com/gradio-app/gradio/pull/4202) [`a26e9afd`](https://github.com/gradio-app/gradio/commit/a26e9afde319382993e6ddc77cc4e56337a31248) Thanks [@pngwn](https://github.com/pngwn)! - Ensure all URLs returned by the client are complete URLs with the correct host instead of an absolute path relative to a server.
10
+
3
11
  ## 0.1.0
4
12
 
5
13
  ### Minor Changes
package/README.md CHANGED
@@ -7,7 +7,7 @@ A javascript (and typescript) client to call Gradio APIs.
7
7
  The Gradio JavaScript client is available on npm as `@gradio/client`. You can install it as below:
8
8
 
9
9
  ```sh
10
- npm i -D @gradio/client
10
+ npm i @gradio/client
11
11
  ```
12
12
 
13
13
  ## Usage
package/dist/client.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { hardware_types } from "./utils.js";
2
- import type { EventType, EventListener, PostResponse, UploadResponse, SpaceStatusCallback } from "./types.js";
2
+ import type { EventType, EventListener, PostResponse, UploadResponse, SpaceStatusCallback, FileData } from "./types.js";
3
3
  import type { Config } from "./types.js";
4
4
  declare type event = <K extends EventType>(eventType: K, listener: EventListener<K>) => SubmitReturn;
5
5
  declare type predict = (endpoint: string | number, data?: unknown[], event_data?: unknown) => Promise<unknown>;
@@ -12,7 +12,7 @@ declare type client_return = {
12
12
  declare type SubmitReturn = {
13
13
  on: event;
14
14
  off: event;
15
- cancel: () => void;
15
+ cancel: () => Promise<void>;
16
16
  destroy: () => void;
17
17
  };
18
18
  export declare function post_data(url: string, body: unknown, token?: `hf_${string}`): Promise<[PostResponse, number]>;
@@ -28,7 +28,9 @@ export declare function duplicate(app_reference: string, options: {
28
28
  export declare function client(app_reference: string, options?: {
29
29
  hf_token?: `hf_${string}`;
30
30
  status_callback?: SpaceStatusCallback;
31
+ normalise_files?: boolean;
31
32
  }): Promise<client_return>;
33
+ export declare function normalise_file(file: Array<FileData> | FileData | string | null, root: string, root_url: string | null): Array<FileData> | FileData | null;
32
34
  export declare function handle_blob(endpoint: string, data: unknown[], api_info: any, token?: `hf_${string}`): Promise<unknown[]>;
33
35
  export declare function walk_and_store_blobs(param: any, type?: any, path?: any[], root?: boolean, api_info?: any): Promise<any[]>;
34
36
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAEA,OAAO,EAQN,cAAc,EACd,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EACX,SAAS,EACT,aAAa,EAIb,YAAY,EACZ,cAAc,EAGd,mBAAmB,EACnB,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC,aAAK,KAAK,GAAG,CAAC,CAAC,SAAS,SAAS,EAChC,SAAS,EAAE,CAAC,EACZ,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,KACtB,YAAY,CAAC;AAClB,aAAK,OAAO,GAAG,CACd,QAAQ,EAAE,MAAM,GAAG,MAAM,EACzB,IAAI,CAAC,EAAE,OAAO,EAAE,EAChB,UAAU,CAAC,EAAE,OAAO,KAChB,OAAO,CAAC,OAAO,CAAC,CAAC;AAEtB,aAAK,aAAa,GAAG;IACpB,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,CACP,QAAQ,EAAE,MAAM,GAAG,MAAM,EACzB,IAAI,CAAC,EAAE,OAAO,EAAE,EAChB,UAAU,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G,QAAQ,GAAG,IAAI,CA4BnC;AAyJD,wBAAsB,WAAW,CAChC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,OAAO,EAAE,EACf,QAAQ,KAAA,EACR,KAAK,CAAC,EAAE,MAAM,MAAM,EAAE,GACpB,OAAO,CAAC,OAAO,EAAE,CAAC,CA0CpB;AAUD,wBAAsB,oBAAoB,CACzC,KAAK,KAAA,EACL,IAAI,MAAY,EAChB,IAAI,QAAK,EACT,IAAI,UAAQ,EACZ,QAAQ,MAAY,kBAyEpB"}
package/dist/index.js CHANGED
@@ -1,4 +1,9 @@
1
- import semiver from "semiver";
1
+ var fn = new Intl.Collator(0, { numeric: 1 }).compare;
2
+ function semiver(a, b, bool) {
3
+ a = a.split(".");
4
+ b = b.split(".");
5
+ return fn(a[0], b[0]) || fn(a[1], b[1]) || (b[2] = b.slice(2).join("."), bool = /[.-]/.test(a[2] = a.slice(2).join(".")), bool == /[.-]/.test(b[2]) ? fn(a[2], b[2]) : bool ? -1 : 1);
6
+ }
2
7
  function determine_protocol(endpoint) {
3
8
  if (endpoint.startsWith("http")) {
4
9
  const { protocol, host } = new URL(endpoint);
@@ -249,15 +254,16 @@ async function duplicate(app_reference, options) {
249
254
  throw new Error(e);
250
255
  }
251
256
  }
252
- async function client(app_reference, options = {}) {
257
+ async function client(app_reference, options = { normalise_files: true }) {
253
258
  return new Promise(async (res) => {
254
- const { status_callback, hf_token } = options;
259
+ const { status_callback, hf_token, normalise_files } = options;
255
260
  const return_obj = {
256
261
  predict,
257
262
  submit,
258
263
  view_api
259
264
  // duplicate
260
265
  };
266
+ let transform_files = normalise_files ?? true;
261
267
  if (typeof window === "undefined" || !("WebSocket" in window)) {
262
268
  const ws = await import("./wrapper-b7460963.js");
263
269
  NodeBlob = (await import("node:buffer")).Blob;
@@ -268,7 +274,6 @@ async function client(app_reference, options = {}) {
268
274
  const last_status = {};
269
275
  let config;
270
276
  let api_map = {};
271
- const listener_map = {};
272
277
  let jwt = false;
273
278
  if (hf_token && space_id) {
274
279
  jwt = await get_jwt(space_id, hf_token);
@@ -307,13 +312,10 @@ async function client(app_reference, options = {}) {
307
312
  }
308
313
  }
309
314
  try {
310
- console.log(`${http_protocol}//${host}`);
311
315
  config = await resolve_config(`${http_protocol}//${host}`, hf_token);
312
- console.log(config);
313
316
  const _config = await config_success(config);
314
317
  res(_config);
315
318
  } catch (e) {
316
- console.log(space_id, e);
317
319
  if (space_id) {
318
320
  check_space_status(
319
321
  space_id,
@@ -372,6 +374,8 @@ async function client(app_reference, options = {}) {
372
374
  let websocket;
373
375
  const _endpoint = typeof endpoint === "number" ? "/predict" : endpoint;
374
376
  let payload;
377
+ let complete = false;
378
+ const listener_map = {};
375
379
  handle_blob(
376
380
  `${http_protocol}//${host + config.path}`,
377
381
  data,
@@ -396,21 +400,27 @@ async function client(app_reference, options = {}) {
396
400
  },
397
401
  hf_token
398
402
  ).then(([output, status_code]) => {
403
+ transform_files ? transform_output(
404
+ output.data,
405
+ api_info,
406
+ config.root,
407
+ config.root_url
408
+ ) : output.data;
399
409
  if (status_code == 200) {
400
410
  fire_event({
401
- type: "status",
411
+ type: "data",
402
412
  endpoint: _endpoint,
403
413
  fn_index,
404
- stage: "complete",
405
- eta: output.average_duration,
406
- queue: false,
414
+ data: output.data,
407
415
  time: /* @__PURE__ */ new Date()
408
416
  });
409
417
  fire_event({
410
- type: "data",
418
+ type: "status",
411
419
  endpoint: _endpoint,
412
420
  fn_index,
413
- data: output.data,
421
+ stage: "complete",
422
+ eta: output.average_duration,
423
+ queue: false,
414
424
  time: /* @__PURE__ */ new Date()
415
425
  });
416
426
  } else {
@@ -469,7 +479,7 @@ async function client(app_reference, options = {}) {
469
479
  _data,
470
480
  last_status[fn_index]
471
481
  );
472
- if (type === "update" && status) {
482
+ if (type === "update" && status && !complete) {
473
483
  fire_event({
474
484
  type: "status",
475
485
  endpoint: _endpoint,
@@ -486,16 +496,7 @@ async function client(app_reference, options = {}) {
486
496
  } else if (type === "data") {
487
497
  websocket.send(JSON.stringify({ ...payload, session_hash }));
488
498
  } else if (type === "complete") {
489
- fire_event({
490
- type: "status",
491
- time: /* @__PURE__ */ new Date(),
492
- ...status,
493
- stage: status == null ? void 0 : status.stage,
494
- queue: true,
495
- endpoint: _endpoint,
496
- fn_index
497
- });
498
- websocket.close();
499
+ complete = status;
499
500
  } else if (type === "generating") {
500
501
  fire_event({
501
502
  type: "status",
@@ -511,10 +512,27 @@ async function client(app_reference, options = {}) {
511
512
  fire_event({
512
513
  type: "data",
513
514
  time: /* @__PURE__ */ new Date(),
514
- data: data2.data,
515
+ data: transform_files ? transform_output(
516
+ data2.data,
517
+ api_info,
518
+ config.root,
519
+ config.root_url
520
+ ) : data2.data,
515
521
  endpoint: _endpoint,
516
522
  fn_index
517
523
  });
524
+ if (complete) {
525
+ fire_event({
526
+ type: "status",
527
+ time: /* @__PURE__ */ new Date(),
528
+ ...complete,
529
+ stage: status == null ? void 0 : status.stage,
530
+ queue: true,
531
+ endpoint: _endpoint,
532
+ fn_index
533
+ });
534
+ websocket.close();
535
+ }
518
536
  }
519
537
  };
520
538
  if (semiver(config.version || "2.0.0", "3.6") < 0) {
@@ -545,37 +563,41 @@ async function client(app_reference, options = {}) {
545
563
  return { on, off, cancel, destroy };
546
564
  }
547
565
  async function cancel() {
548
- fire_event({
549
- type: "status",
550
- endpoint: _endpoint,
551
- fn_index,
566
+ const _status = {
552
567
  stage: "complete",
553
568
  queue: false,
554
569
  time: /* @__PURE__ */ new Date()
570
+ };
571
+ complete = _status;
572
+ fire_event({
573
+ ..._status,
574
+ type: "status",
575
+ endpoint: _endpoint,
576
+ fn_index
555
577
  });
578
+ if (websocket && websocket.readyState === 0) {
579
+ websocket.addEventListener("open", () => {
580
+ websocket.close();
581
+ });
582
+ } else {
583
+ websocket.close();
584
+ }
556
585
  try {
557
586
  await fetch(`${http_protocol}//${host + config.path}/reset`, {
587
+ headers: { "Content-Type": "application/json" },
558
588
  method: "POST",
559
- body: JSON.stringify(session_hash)
589
+ body: JSON.stringify({ fn_index, session_hash })
560
590
  });
561
591
  } catch (e) {
562
592
  console.warn(
563
593
  "The `/reset` endpoint could not be called. Subsequent endpoint results may be unreliable."
564
594
  );
565
595
  }
566
- if (websocket && websocket.readyState === 0) {
567
- websocket.addEventListener("open", () => {
568
- websocket.close();
569
- });
570
- } else {
571
- websocket.close();
572
- }
573
- destroy();
574
596
  }
575
597
  function destroy() {
576
598
  for (const event_type in listener_map) {
577
- listener_map[event_type].forEach((fn) => {
578
- off(event_type, fn);
599
+ listener_map[event_type].forEach((fn2) => {
600
+ off(event_type, fn2);
579
601
  });
580
602
  }
581
603
  }
@@ -627,6 +649,50 @@ async function client(app_reference, options = {}) {
627
649
  }
628
650
  });
629
651
  }
652
+ function transform_output(data, api_info, root_url, remote_url) {
653
+ let transformed_data = data.map((d, i) => {
654
+ var _a, _b, _c, _d;
655
+ if (((_b = (_a = api_info.returns) == null ? void 0 : _a[i]) == null ? void 0 : _b.component) === "File") {
656
+ return normalise_file(d, root_url, remote_url);
657
+ } else if (((_d = (_c = api_info.returns) == null ? void 0 : _c[i]) == null ? void 0 : _d.component) === "Gallery") {
658
+ return d.map((img) => {
659
+ return Array.isArray(img) ? [normalise_file(img[0], root_url, remote_url), img[1]] : [normalise_file(img, root_url, remote_url), null];
660
+ });
661
+ } else if (typeof d === "object" && d.is_file) {
662
+ return normalise_file(d, root_url, remote_url);
663
+ } else {
664
+ return d;
665
+ }
666
+ });
667
+ return transformed_data;
668
+ }
669
+ function normalise_file(file, root, root_url) {
670
+ if (file == null)
671
+ return null;
672
+ if (typeof file === "string") {
673
+ return {
674
+ name: "file_data",
675
+ data: file
676
+ };
677
+ } else if (Array.isArray(file)) {
678
+ const normalized_file = [];
679
+ for (const x of file) {
680
+ if (x === null) {
681
+ normalized_file.push(null);
682
+ } else {
683
+ normalized_file.push(normalise_file(x, root, root_url));
684
+ }
685
+ }
686
+ return normalized_file;
687
+ } else if (file.is_file) {
688
+ if (!root_url) {
689
+ file.data = root + "/file=" + file.name;
690
+ } else {
691
+ file.data = "/proxy=" + root_url + "/file=" + file.name;
692
+ }
693
+ }
694
+ return file;
695
+ }
630
696
  function get_type(type, component, serializer, signature_type) {
631
697
  switch (type.type) {
632
698
  case "string":
@@ -841,9 +907,7 @@ async function resolve_config(endpoint, token) {
841
907
  config.root = endpoint + config.root;
842
908
  return { ...config, path };
843
909
  } else if (endpoint) {
844
- console.log(`${endpoint}/config`, headers);
845
910
  let response = await fetch(`${endpoint}/config`, { headers });
846
- console.log(response);
847
911
  if (response.status === 200) {
848
912
  const config = await response.json();
849
913
  config.path = config.path ?? "";
package/dist/types.d.ts CHANGED
@@ -9,6 +9,7 @@ export interface Config {
9
9
  layout: any;
10
10
  mode: "blocks" | "interface";
11
11
  root: string;
12
+ root_url?: string;
12
13
  theme: string;
13
14
  title: string;
14
15
  version: string;
@@ -82,4 +83,14 @@ export declare type EventListener<K extends EventType> = (event: Event<K>) => vo
82
83
  export declare type ListenerMap<K extends EventType> = {
83
84
  [P in K]?: EventListener<K>[];
84
85
  };
86
+ export interface FileData {
87
+ name: string;
88
+ orig_name?: string;
89
+ size?: number;
90
+ data: string;
91
+ blob?: File;
92
+ is_file?: boolean;
93
+ mime_type?: string;
94
+ alt_text?: string;
95
+ }
85
96
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,MAAM;IACtB,aAAa,EAAE,OAAO,GAAG,SAAS,CAAC;IACnC,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,GAAG,EAAE,CAAC;IAClB,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,YAAY,EAAE,GAAG,EAAE,CAAC;IACpB,QAAQ,EAAE,OAAO,CAAC;IAClB,YAAY,EAAE,OAAO,CAAC;IACtB,MAAM,EAAE,GAAG,CAAC;IACZ,IAAI,EAAE,QAAQ,GAAG,WAAW,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0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Gradio UI packages",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
package/src/client.ts CHANGED
@@ -21,7 +21,8 @@ import type {
21
21
  UploadResponse,
22
22
  Status,
23
23
  SpaceStatus,
24
- SpaceStatusCallback
24
+ SpaceStatusCallback,
25
+ FileData
25
26
  } from "./types.js";
26
27
 
27
28
  import type { Config } from "./types.js";
@@ -50,7 +51,7 @@ type client_return = {
50
51
  type SubmitReturn = {
51
52
  on: event;
52
53
  off: event;
53
- cancel: () => void;
54
+ cancel: () => Promise<void>;
54
55
  destroy: () => void;
55
56
  };
56
57
 
@@ -201,10 +202,11 @@ export async function client(
201
202
  options: {
202
203
  hf_token?: `hf_${string}`;
203
204
  status_callback?: SpaceStatusCallback;
204
- } = {}
205
+ normalise_files?: boolean;
206
+ } = { normalise_files: true }
205
207
  ): Promise<client_return> {
206
208
  return new Promise(async (res) => {
207
- const { status_callback, hf_token } = options;
209
+ const { status_callback, hf_token, normalise_files } = options;
208
210
  const return_obj = {
209
211
  predict,
210
212
  submit,
@@ -212,6 +214,7 @@ export async function client(
212
214
  // duplicate
213
215
  };
214
216
 
217
+ let transform_files = normalise_files ?? true;
215
218
  if (typeof window === "undefined" || !("WebSocket" in window)) {
216
219
  const ws = await import("ws");
217
220
  NodeBlob = (await import("node:buffer")).Blob;
@@ -227,8 +230,6 @@ export async function client(
227
230
  let config: Config;
228
231
  let api_map: Record<string, number> = {};
229
232
 
230
- const listener_map: ListenerMap<EventType> = {};
231
-
232
233
  let jwt: false | string = false;
233
234
 
234
235
  if (hf_token && space_id) {
@@ -271,13 +272,11 @@ export async function client(
271
272
  }
272
273
 
273
274
  try {
274
- console.log(`${http_protocol}//${host}`);
275
275
  config = await resolve_config(`${http_protocol}//${host}`, hf_token);
276
- console.log(config);
276
+
277
277
  const _config = await config_success(config);
278
278
  res(_config);
279
279
  } catch (e) {
280
- console.log(space_id, e);
281
280
  if (space_id) {
282
281
  check_space_status(
283
282
  space_id,
@@ -334,6 +333,7 @@ export async function client(
334
333
  ): SubmitReturn {
335
334
  let fn_index: number;
336
335
  let api_info;
336
+
337
337
  if (typeof endpoint === "number") {
338
338
  fn_index = endpoint;
339
339
  api_info = api.unnamed_endpoints[fn_index];
@@ -354,6 +354,8 @@ export async function client(
354
354
 
355
355
  const _endpoint = typeof endpoint === "number" ? "/predict" : endpoint;
356
356
  let payload: Payload;
357
+ let complete: false | Record<string, any> = false;
358
+ const listener_map: ListenerMap<EventType> = {};
357
359
 
358
360
  //@ts-ignore
359
361
  handle_blob(
@@ -384,22 +386,30 @@ export async function client(
384
386
  hf_token
385
387
  )
386
388
  .then(([output, status_code]) => {
389
+ const data = transform_files
390
+ ? transform_output(
391
+ output.data,
392
+ api_info,
393
+ config.root,
394
+ config.root_url
395
+ )
396
+ : output.data;
387
397
  if (status_code == 200) {
388
398
  fire_event({
389
- type: "status",
399
+ type: "data",
390
400
  endpoint: _endpoint,
391
401
  fn_index,
392
- stage: "complete",
393
- eta: output.average_duration,
394
- queue: false,
402
+ data: output.data,
395
403
  time: new Date()
396
404
  });
397
405
 
398
406
  fire_event({
399
- type: "data",
407
+ type: "status",
400
408
  endpoint: _endpoint,
401
409
  fn_index,
402
- data: output.data,
410
+ stage: "complete",
411
+ eta: output.average_duration,
412
+ queue: false,
403
413
  time: new Date()
404
414
  });
405
415
  } else {
@@ -465,7 +475,7 @@ export async function client(
465
475
  last_status[fn_index]
466
476
  );
467
477
 
468
- if (type === "update" && status) {
478
+ if (type === "update" && status && !complete) {
469
479
  // call 'status' listeners
470
480
  fire_event({
471
481
  type: "status",
@@ -483,16 +493,7 @@ export async function client(
483
493
  } else if (type === "data") {
484
494
  websocket.send(JSON.stringify({ ...payload, session_hash }));
485
495
  } else if (type === "complete") {
486
- fire_event({
487
- type: "status",
488
- time: new Date(),
489
- ...status,
490
- stage: status?.stage!,
491
- queue: true,
492
- endpoint: _endpoint,
493
- fn_index
494
- });
495
- websocket.close();
496
+ complete = status;
496
497
  } else if (type === "generating") {
497
498
  fire_event({
498
499
  type: "status",
@@ -508,10 +509,30 @@ export async function client(
508
509
  fire_event({
509
510
  type: "data",
510
511
  time: new Date(),
511
- data: data.data,
512
+ data: transform_files
513
+ ? transform_output(
514
+ data.data,
515
+ api_info,
516
+ config.root,
517
+ config.root_url
518
+ )
519
+ : data.data,
512
520
  endpoint: _endpoint,
513
521
  fn_index
514
522
  });
523
+
524
+ if (complete) {
525
+ fire_event({
526
+ type: "status",
527
+ time: new Date(),
528
+ ...complete,
529
+ stage: status?.stage!,
530
+ queue: true,
531
+ endpoint: _endpoint,
532
+ fn_index
533
+ });
534
+ websocket.close();
535
+ }
515
536
  }
516
537
  };
517
538
 
@@ -556,35 +577,38 @@ export async function client(
556
577
  }
557
578
 
558
579
  async function cancel() {
559
- fire_event({
560
- type: "status",
561
- endpoint: _endpoint,
562
- fn_index: fn_index,
580
+ const _status: Status = {
563
581
  stage: "complete",
564
582
  queue: false,
565
583
  time: new Date()
584
+ };
585
+ complete = _status;
586
+ fire_event({
587
+ ..._status,
588
+ type: "status",
589
+ endpoint: _endpoint,
590
+ fn_index: fn_index
566
591
  });
567
592
 
593
+ if (websocket && websocket.readyState === 0) {
594
+ websocket.addEventListener("open", () => {
595
+ websocket.close();
596
+ });
597
+ } else {
598
+ websocket.close();
599
+ }
600
+
568
601
  try {
569
602
  await fetch(`${http_protocol}//${host + config.path}/reset`, {
603
+ headers: { "Content-Type": "application/json" },
570
604
  method: "POST",
571
- body: JSON.stringify(session_hash)
605
+ body: JSON.stringify({ fn_index, session_hash })
572
606
  });
573
607
  } catch (e) {
574
608
  console.warn(
575
609
  "The `/reset` endpoint could not be called. Subsequent endpoint results may be unreliable."
576
610
  );
577
611
  }
578
-
579
- if (websocket && websocket.readyState === 0) {
580
- websocket.addEventListener("open", () => {
581
- websocket.close();
582
- });
583
- } else {
584
- websocket.close();
585
- }
586
-
587
- destroy();
588
612
  }
589
613
 
590
614
  function destroy() {
@@ -659,6 +683,65 @@ export async function client(
659
683
  });
660
684
  }
661
685
 
686
+ function transform_output(
687
+ data: any[],
688
+ api_info: any,
689
+ root_url: string,
690
+ remote_url?: string
691
+ ): unknown[] {
692
+ let transformed_data = data.map((d, i) => {
693
+ if (api_info.returns?.[i]?.component === "File") {
694
+ return normalise_file(d, root_url, remote_url);
695
+ } else if (api_info.returns?.[i]?.component === "Gallery") {
696
+ return d.map((img) => {
697
+ return Array.isArray(img)
698
+ ? [normalise_file(img[0], root_url, remote_url), img[1]]
699
+ : [normalise_file(img, root_url, remote_url), null];
700
+ });
701
+ } else if (typeof d === "object" && d.is_file) {
702
+ return normalise_file(d, root_url, remote_url);
703
+ } else {
704
+ return d;
705
+ }
706
+ });
707
+
708
+ return transformed_data;
709
+ }
710
+
711
+ export function normalise_file(
712
+ file: Array<FileData> | FileData | string | null,
713
+ root: string,
714
+ root_url: string | null
715
+ ): Array<FileData> | FileData | null {
716
+ if (file == null) return null;
717
+ if (typeof file === "string") {
718
+ return {
719
+ name: "file_data",
720
+ data: file
721
+ };
722
+ } else if (Array.isArray(file)) {
723
+ const normalized_file: Array<FileData | null> = [];
724
+
725
+ for (const x of file) {
726
+ if (x === null) {
727
+ normalized_file.push(null);
728
+ } else {
729
+ //@ts-ignore
730
+ normalized_file.push(normalise_file(x, root, root_url));
731
+ }
732
+ }
733
+
734
+ return normalized_file as Array<FileData>;
735
+ } else if (file.is_file) {
736
+ if (!root_url) {
737
+ file.data = root + "/file=" + file.name;
738
+ } else {
739
+ file.data = "/proxy=" + root_url + "/file=" + file.name;
740
+ }
741
+ }
742
+ return file;
743
+ }
744
+
662
745
  interface ApiData {
663
746
  label: string;
664
747
  type: {
@@ -981,9 +1064,8 @@ async function resolve_config(
981
1064
  config.root = endpoint + config.root;
982
1065
  return { ...config, path: path };
983
1066
  } else if (endpoint) {
984
- console.log(`${endpoint}/config`, headers);
985
1067
  let response = await fetch(`${endpoint}/config`, { headers });
986
- console.log(response);
1068
+
987
1069
  if (response.status === 200) {
988
1070
  const config = await response.json();
989
1071
  config.path = config.path ?? "";
package/src/types.ts CHANGED
@@ -9,6 +9,7 @@ export interface Config {
9
9
  layout: any;
10
10
  mode: "blocks" | "interface";
11
11
  root: string;
12
+ root_url?: string;
12
13
  theme: string;
13
14
  title: string;
14
15
  version: string;
@@ -91,3 +92,13 @@ export type EventListener<K extends EventType> = (event: Event<K>) => void;
91
92
  export type ListenerMap<K extends EventType> = {
92
93
  [P in K]?: EventListener<K>[];
93
94
  };
95
+ export interface FileData {
96
+ name: string;
97
+ orig_name?: string;
98
+ size?: number;
99
+ data: string;
100
+ blob?: File;
101
+ is_file?: boolean;
102
+ mime_type?: string;
103
+ alt_text?: string;
104
+ }
package/vite.config.js CHANGED
@@ -18,6 +18,6 @@ export default defineConfig({
18
18
  ssr: {
19
19
  target: "node",
20
20
  format: "esm",
21
- noExternal: "ws"
21
+ noExternal: ["ws", "semiver"]
22
22
  }
23
23
  });