@seam-rpc/client 3.0.1 → 4.0.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.
package/dist/index.d.ts CHANGED
@@ -1,12 +1,10 @@
1
- import { SeamFile, ISeamFile } from "@seam-rpc/core";
2
- export { SeamFile, ISeamFile };
3
1
  export type SeamRequestMiddleware = (context: SeamRequestMiddlewareContext) => void | Promise<void>;
4
2
  export type SeamResponseMiddleware = (context: SeamResponseMiddlewareContext) => void | Promise<void>;
5
3
  export interface SeamRequestMiddlewareContext {
6
4
  request: RequestInit;
7
5
  routerName: string;
8
6
  funcName: string;
9
- args: any[];
7
+ input?: Record<string, any>;
10
8
  }
11
9
  export type SeamResponseMiddlewareContext = SeamRequestMiddlewareContext & {
12
10
  response: Response;
@@ -20,11 +18,11 @@ export interface SeamClientOptions {
20
18
  }
21
19
  export declare class SeamClient {
22
20
  readonly baseUrl: string;
23
- readonly options?: SeamClientOptions | undefined;
24
21
  static _instance: SeamClient;
25
- constructor(baseUrl: string, options?: SeamClientOptions | undefined);
22
+ options: SeamClientOptions;
23
+ constructor(baseUrl: string, options?: SeamClientOptions);
26
24
  preRequest(middleware: SeamRequestMiddleware): void;
27
25
  postRequest(middleware: SeamResponseMiddleware): void;
28
26
  }
29
27
  export declare function createClient(baseUrl: string, options?: SeamClientOptions): SeamClient;
30
- export declare function callApi(routerName: string, funcName: string, args: any[]): Promise<any>;
28
+ export declare function callApi(routerName: string, funcName: string, input?: Record<string, any>): Promise<any>;
package/dist/index.js CHANGED
@@ -1,10 +1,14 @@
1
- import { SeamFile, extractFiles, injectFiles } from "@seam-rpc/core";
2
- export { SeamFile };
1
+ import { extractFiles, injectFiles } from "@seam-rpc/core";
3
2
  export class SeamClient {
4
3
  constructor(baseUrl, options) {
5
4
  this.baseUrl = baseUrl;
6
- this.options = options;
7
5
  SeamClient._instance = this;
6
+ this.options = {
7
+ middleware: {
8
+ request: options?.middleware?.request || [],
9
+ response: options?.middleware?.response || [],
10
+ }
11
+ };
8
12
  }
9
13
  preRequest(middleware) {
10
14
  this.options?.middleware?.request?.push(middleware);
@@ -16,11 +20,11 @@ export class SeamClient {
16
20
  export function createClient(baseUrl, options) {
17
21
  return new SeamClient(baseUrl, options);
18
22
  }
19
- export async function callApi(routerName, funcName, args) {
23
+ export async function callApi(routerName, funcName, input) {
20
24
  if (!SeamClient._instance)
21
25
  throw new Error("Seam Client not instantiated.");
22
26
  const seamClient = SeamClient._instance;
23
- const req = buildRequest(args);
27
+ const req = buildRequest(input);
24
28
  const url = `${seamClient.baseUrl}/${routerName}/${funcName}`;
25
29
  if (seamClient.options?.middleware?.request) {
26
30
  for (const mw of seamClient.options.middleware.request) {
@@ -28,7 +32,7 @@ export async function callApi(routerName, funcName, args) {
28
32
  request: req,
29
33
  routerName,
30
34
  funcName,
31
- args,
35
+ input,
32
36
  });
33
37
  }
34
38
  }
@@ -45,7 +49,7 @@ export async function callApi(routerName, funcName, args) {
45
49
  const resError = await res.json();
46
50
  throw new Error(resError.error);
47
51
  }
48
- throw new Error(`Request failed with status ${res.status} ${res.statusText}.`);
52
+ throw new Error(`Request failed at router ${routerName} at function ${funcName}, with status ${res.status} ${res.statusText}.`);
49
53
  }
50
54
  const contentType = res.headers.get("content-type") || "";
51
55
  if (contentType.startsWith("application/json")) {
@@ -61,10 +65,9 @@ export async function callApi(routerName, funcName, args) {
61
65
  if (key.startsWith("file-")) {
62
66
  const index = parseInt(key.replace("file-", ""));
63
67
  const blob = value;
64
- const arrayBuffer = await blob.arrayBuffer();
65
68
  responseFiles.push({
66
69
  path: pathsPart[index],
67
- file: new SeamFile(new Uint8Array(arrayBuffer), blob.name, blob.type),
70
+ file: new File([blob], blob.name),
68
71
  });
69
72
  }
70
73
  }
@@ -77,26 +80,23 @@ export async function callApi(routerName, funcName, args) {
77
80
  parsedResponse: jsonPart.result,
78
81
  routerName,
79
82
  funcName,
80
- args,
83
+ input,
81
84
  });
82
85
  }
83
86
  }
84
87
  return jsonPart.result;
85
88
  }
86
89
  }
87
- function buildRequest(args) {
90
+ function buildRequest(input = {}) {
88
91
  let req;
89
- const { json, files, paths } = extractFiles(args);
92
+ const { json, files, paths } = extractFiles(input);
90
93
  if (files.length > 0) {
91
94
  const formData = new FormData();
92
95
  formData.append("json", JSON.stringify(json));
93
96
  formData.append("paths", JSON.stringify(paths));
94
97
  for (let i = 0; i < files.length; i++) {
95
98
  const file = files[i];
96
- const blob = new Blob([new Uint8Array(file.data)], {
97
- type: file.mimeType || "application/octet-stream",
98
- });
99
- formData.append(`file-${i}`, blob, file.fileName || `file-${i}`);
99
+ formData.append(`file-${i}`, file, file.name || `file-${i}`);
100
100
  }
101
101
  req = {
102
102
  method: "POST",
@@ -109,7 +109,7 @@ function buildRequest(args) {
109
109
  headers: {
110
110
  "Content-Type": "application/json",
111
111
  },
112
- body: JSON.stringify(args),
112
+ body: JSON.stringify(input),
113
113
  };
114
114
  }
115
115
  return req;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seam-rpc/client",
3
- "version": "3.0.1",
3
+ "version": "4.0.0",
4
4
  "main": "./dist/index.js",
5
5
  "type": "module",
6
6
  "files": [
@@ -27,6 +27,6 @@
27
27
  "typescript": "^5.9.2"
28
28
  },
29
29
  "dependencies": {
30
- "@seam-rpc/core": "^1.0.0"
30
+ "@seam-rpc/core": "*"
31
31
  }
32
32
  }