@seam-rpc/client 1.0.4 → 1.1.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
@@ -2,3 +2,4 @@ import { SeamFile, ISeamFile } from "@seam-rpc/core";
2
2
  export { SeamFile, ISeamFile };
3
3
  export declare function setApiUrl(url: string): void;
4
4
  export declare function callApi(routerName: string, funcName: string, args: any[]): Promise<any>;
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAA6B,MAAM,gBAAgB,CAAC;AAEhF,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC;AAI/B,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,QAEpC;AAED,wBAAsB,OAAO,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,CAkF7F"}
package/dist/index.js CHANGED
@@ -1,95 +1,78 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.SeamFile = void 0;
13
- exports.setApiUrl = setApiUrl;
14
- exports.callApi = callApi;
15
- const core_1 = require("@seam-rpc/core");
16
- Object.defineProperty(exports, "SeamFile", { enumerable: true, get: function () { return core_1.SeamFile; } });
1
+ import { SeamFile, extractFiles, injectFiles } from "@seam-rpc/core";
2
+ export { SeamFile };
17
3
  let apiUrl = null;
18
- function setApiUrl(url) {
4
+ export function setApiUrl(url) {
19
5
  apiUrl = url;
20
6
  }
21
- function callApi(routerName, funcName, args) {
22
- return __awaiter(this, void 0, void 0, function* () {
23
- var _a, _b;
24
- if (!apiUrl)
25
- throw new Error("Missing API URL");
26
- let req;
27
- const { json, files, paths } = (0, core_1.extractFiles)(args);
28
- if (files.length > 0) {
29
- const formData = new FormData();
30
- formData.append("json", JSON.stringify(json));
31
- formData.append("paths", JSON.stringify(paths));
32
- for (let i = 0; i < files.length; i++) {
33
- const file = files[i];
34
- const blob = new Blob([new Uint8Array(file.data)], {
35
- type: file.mimeType || "application/octet-stream",
36
- });
37
- formData.append(`file-${i}`, blob, file.fileName || `file-${i}`);
38
- }
39
- req = {
40
- method: "POST",
41
- body: formData,
42
- };
43
- }
44
- else {
45
- req = {
46
- method: "POST",
47
- headers: {
48
- "Content-Type": "application/json",
49
- },
50
- body: JSON.stringify(args),
51
- };
52
- }
53
- const url = `${apiUrl}/${routerName}/${funcName}`;
54
- let res;
55
- try {
56
- res = yield fetch(url, req);
7
+ export async function callApi(routerName, funcName, args) {
8
+ if (!apiUrl)
9
+ throw new Error("Missing API URL");
10
+ let req;
11
+ const { json, files, paths } = extractFiles(args);
12
+ if (files.length > 0) {
13
+ const formData = new FormData();
14
+ formData.append("json", JSON.stringify(json));
15
+ formData.append("paths", JSON.stringify(paths));
16
+ for (let i = 0; i < files.length; i++) {
17
+ const file = files[i];
18
+ const blob = new Blob([new Uint8Array(file.data)], {
19
+ type: file.mimeType || "application/octet-stream",
20
+ });
21
+ formData.append(`file-${i}`, blob, file.fileName || `file-${i}`);
57
22
  }
58
- catch (err) {
59
- console.log(url, req, err);
60
- throw new Error("Failed to send request.\n" + err);
23
+ req = {
24
+ method: "POST",
25
+ body: formData,
26
+ };
27
+ }
28
+ else {
29
+ req = {
30
+ method: "POST",
31
+ headers: {
32
+ "Content-Type": "application/json",
33
+ },
34
+ body: JSON.stringify(args),
35
+ };
36
+ }
37
+ const url = `${apiUrl}/${routerName}/${funcName}`;
38
+ let res;
39
+ try {
40
+ res = await fetch(url, req);
41
+ }
42
+ catch (err) {
43
+ console.log(url, req, err);
44
+ throw new Error("Failed to send request.\n" + err);
45
+ }
46
+ if (!res.ok) {
47
+ if (res.status == 400) {
48
+ const resError = await res.json();
49
+ throw new Error(resError.error);
61
50
  }
62
- if (!res.ok) {
63
- if (res.status == 400) {
64
- const resError = yield res.json();
65
- throw new Error(resError.error);
66
- }
67
- throw new Error(`Request failed with status ${res.status} ${res.statusText}.`);
68
- }
69
- const contentType = res.headers.get("content-type") || "";
70
- if (contentType.startsWith("application/json")) {
71
- const data = yield res.json();
72
- return data.result;
73
- }
74
- else if (contentType.startsWith("multipart/form-data")) {
75
- const formData = yield res.formData();
76
- const jsonPart = JSON.parse(((_a = formData.get("json")) === null || _a === void 0 ? void 0 : _a.toString()) || "[]");
77
- const pathsPart = JSON.parse(((_b = formData.get("paths")) === null || _b === void 0 ? void 0 : _b.toString()) || "[]");
78
- const responseFiles = [];
79
- for (const [key, value] of formData.entries()) {
80
- if (key.startsWith("file-")) {
81
- const index = parseInt(key.replace("file-", ""));
82
- const blob = value;
83
- const arrayBuffer = yield blob.arrayBuffer();
84
- responseFiles.push({
85
- path: pathsPart[index],
86
- file: new core_1.SeamFile(new Uint8Array(arrayBuffer), blob.name, blob.type),
87
- });
88
- }
51
+ throw new Error(`Request failed with status ${res.status} ${res.statusText}.`);
52
+ }
53
+ const contentType = res.headers.get("content-type") || "";
54
+ if (contentType.startsWith("application/json")) {
55
+ const data = await res.json();
56
+ return data.result;
57
+ }
58
+ else if (contentType.startsWith("multipart/form-data")) {
59
+ const formData = await res.formData();
60
+ const jsonPart = JSON.parse(formData.get("json")?.toString() || "[]");
61
+ const pathsPart = JSON.parse(formData.get("paths")?.toString() || "[]");
62
+ const responseFiles = [];
63
+ for (const [key, value] of formData.entries()) {
64
+ if (key.startsWith("file-")) {
65
+ const index = parseInt(key.replace("file-", ""));
66
+ const blob = value;
67
+ const arrayBuffer = await blob.arrayBuffer();
68
+ responseFiles.push({
69
+ path: pathsPart[index],
70
+ file: new SeamFile(new Uint8Array(arrayBuffer), blob.name, blob.type),
71
+ });
89
72
  }
90
- (0, core_1.injectFiles)(jsonPart, responseFiles);
91
- return jsonPart.result;
92
73
  }
93
- });
74
+ injectFiles(jsonPart, responseFiles);
75
+ return jsonPart.result;
76
+ }
94
77
  }
95
78
  // console.log(JSON.stringify(extractFiles(["John", new SeamFile(new Uint8Array([1, 2, 3, 4]))]), null, 4))
package/package.json CHANGED
@@ -1,31 +1,32 @@
1
- {
2
- "name": "@seam-rpc/client",
3
- "version": "1.0.4",
4
- "main": "./dist/index.js",
5
- "files": [
6
- "dist"
7
- ],
8
- "scripts": {
9
- "test": "echo \"Error: no test specified\" && exit 1"
10
- },
11
- "keywords": [],
12
- "author": "Daniel Clímaco",
13
- "license": "ISC",
14
- "description": "",
15
- "repository": {
16
- "type": "git",
17
- "url": "git+https://github.com/DanielC49/seam-rpc.git",
18
- "directory": "packages/client"
19
- },
20
- "homepage": "https://github.com/DanielC49/seam-rpc#readme",
21
- "bugs": {
22
- "url": "https://github.com/DanielC49/seam-rpc/issues"
23
- },
24
- "devDependencies": {
25
- "@types/node": "^24.3.1",
26
- "typescript": "^5.9.2"
27
- },
28
- "dependencies": {
29
- "@seam-rpc/core": "1.0.0"
30
- }
31
- }
1
+ {
2
+ "name": "@seam-rpc/client",
3
+ "version": "1.1.0",
4
+ "main": "./dist/index.js",
5
+ "type": "module",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "scripts": {
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "keywords": [],
13
+ "author": "Daniel Clímaco",
14
+ "license": "ISC",
15
+ "description": "",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/DanielC49/seam-rpc.git",
19
+ "directory": "packages/client"
20
+ },
21
+ "homepage": "https://github.com/DanielC49/seam-rpc#readme",
22
+ "bugs": {
23
+ "url": "https://github.com/DanielC49/seam-rpc/issues"
24
+ },
25
+ "devDependencies": {
26
+ "@types/node": "^24.3.1",
27
+ "typescript": "^5.9.2"
28
+ },
29
+ "dependencies": {
30
+ "@seam-rpc/core": "^1.0.0"
31
+ }
32
+ }