@rpcbase/client 0.344.0 → 0.346.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.
@@ -1 +1 @@
1
- {"version":3,"file":"getServerApiClient.d.ts","sourceRoot":"","sources":["../../src/apiClient/getServerApiClient.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAA8B,MAAM,SAAS,CAAA;AAGjE,KAAK,GAAG,GAAG,GAAG,CAAA;AAGd,eAAO,MAAM,kBAAkB,GAAS,KAAK,WAAW;UAqGtC,SAAS,kCACf,MAAM,WACH,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,QAC1B,GAAG,KACR,OAAO,CAAC,SAAS,CAAC;WAJP,SAAS,kCACf,MAAM,WACH,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,QAC1B,GAAG,KACR,OAAO,CAAC,SAAS,CAAC;UAJP,SAAS,kCACf,MAAM,WACH,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,QAC1B,GAAG,KACR,OAAO,CAAC,SAAS,CAAC;aAJP,SAAS,kCACf,MAAM,WACH,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,QAC1B,GAAG,KACR,OAAO,CAAC,SAAS,CAAC;EAkBxB,CAAA;AAED,eAAe,kBAAkB,CAAA"}
1
+ {"version":3,"file":"getServerApiClient.d.ts","sourceRoot":"","sources":["../../src/apiClient/getServerApiClient.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAA8B,MAAM,SAAS,CAAA;AAGjE,KAAK,GAAG,GAAG,GAAG,CAAA;AAGd,eAAO,MAAM,kBAAkB,GAAS,KAAK,WAAW;UAuKtC,SAAS,kCACf,MAAM,WACH,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,QAC1B,GAAG,KACR,OAAO,CAAC,SAAS,CAAC;WAJP,SAAS,kCACf,MAAM,WACH,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,QAC1B,GAAG,KACR,OAAO,CAAC,SAAS,CAAC;UAJP,SAAS,kCACf,MAAM,WACH,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,QAC1B,GAAG,KACR,OAAO,CAAC,SAAS,CAAC;aAJP,SAAS,kCACf,MAAM,WACH,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,QAC1B,GAAG,KACR,OAAO,CAAC,SAAS,CAAC;EAyBxB,CAAA;AAED,eAAe,kBAAkB,CAAA"}
@@ -1,11 +1,11 @@
1
1
  const getServerApiClient = async (app) => {
2
- const callRoute = async (app2, method, path, req, res) => {
2
+ const callRoute = async (app2, method, matchPath, requestUrl, req, res) => {
3
3
  return new Promise((resolve, reject) => {
4
4
  let isEnded = false;
5
5
  const mockReq = {
6
6
  ...req,
7
7
  method: method.toUpperCase(),
8
- url: path
8
+ url: requestUrl
9
9
  };
10
10
  const mockRes = {
11
11
  ...res,
@@ -29,7 +29,7 @@ const getServerApiClient = async (app) => {
29
29
  const processLayer = async (index) => {
30
30
  if (index >= apiStack.length || isEnded) return;
31
31
  const layer = apiStack[index];
32
- const isNonMatchingLayer = !layer.match(path);
32
+ const isNonMatchingLayer = !layer.match(matchPath);
33
33
  if (isNonMatchingLayer) {
34
34
  await processLayer(index + 1);
35
35
  return;
@@ -71,13 +71,70 @@ const getServerApiClient = async (app) => {
71
71
  }, 3e4);
72
72
  });
73
73
  };
74
+ const mergeQueryValues = (target, key, value) => {
75
+ const existing = target[key];
76
+ if (Array.isArray(existing)) {
77
+ existing.push(value);
78
+ return;
79
+ }
80
+ if (typeof existing === "string") {
81
+ target[key] = [existing, value];
82
+ return;
83
+ }
84
+ target[key] = value;
85
+ };
86
+ const toQueryValue = (value) => {
87
+ if (typeof value === "string") return value;
88
+ if (typeof value === "number") return Number.isFinite(value) ? String(value) : null;
89
+ if (typeof value === "boolean") return value ? "true" : "false";
90
+ if (typeof value === "bigint") return String(value);
91
+ if (value instanceof Date) return value.toISOString();
92
+ return null;
93
+ };
94
+ const buildQuery = (path, payload) => {
95
+ const url = new URL(path, "http://localhost");
96
+ const query = {};
97
+ for (const [key, value] of url.searchParams) {
98
+ mergeQueryValues(query, key, value);
99
+ }
100
+ for (const [key, rawValue] of Object.entries(payload)) {
101
+ if (!key) continue;
102
+ if (Array.isArray(rawValue)) {
103
+ for (const item of rawValue) {
104
+ const converted2 = toQueryValue(item);
105
+ if (converted2 !== null) mergeQueryValues(query, key, converted2);
106
+ }
107
+ continue;
108
+ }
109
+ const converted = toQueryValue(rawValue);
110
+ if (converted !== null) mergeQueryValues(query, key, converted);
111
+ }
112
+ const searchParams = new URLSearchParams();
113
+ for (const [key, rawValue] of Object.entries(query)) {
114
+ if (Array.isArray(rawValue)) {
115
+ for (const value of rawValue) {
116
+ searchParams.append(key, value);
117
+ }
118
+ continue;
119
+ }
120
+ searchParams.append(key, rawValue);
121
+ }
122
+ const matchPath = url.pathname;
123
+ const requestUrl = `${matchPath}${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
124
+ return { matchPath, requestUrl, query };
125
+ };
74
126
  const createMethod = (method) => {
75
127
  return async (path, payload, ctx) => {
76
128
  if (!ctx) {
77
129
  throw new Error("Context must be provided in SSR mode");
78
130
  }
79
- ctx.req.body = payload;
80
- return callRoute(app, method, path, ctx.req, ctx.res);
131
+ const normalizedMethod = method.toLowerCase();
132
+ if (normalizedMethod === "get") {
133
+ const { matchPath: matchPath2, requestUrl: requestUrl2, query: query2 } = buildQuery(path, payload);
134
+ return callRoute(app, method, matchPath2, requestUrl2, { ...ctx.req, body: {}, query: query2 }, ctx.res);
135
+ }
136
+ const { matchPath, requestUrl, query } = buildQuery(path, {});
137
+ return callRoute(app, method, matchPath, requestUrl, { ...ctx.req, body: payload, query }, ctx.res);
81
138
  };
82
139
  };
83
140
  const apiClient = {
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ const initApiClient = async (args) => {
14
14
  if (!args) {
15
15
  throw new Error("Server args must be provided in SSR mode");
16
16
  }
17
- const { getServerApiClient } = await import("./getServerApiClient-CHA4nyjq.js");
17
+ const { getServerApiClient } = await import("./getServerApiClient-C51U2cM0.js");
18
18
  apiClient = await getServerApiClient(args.app);
19
19
  } else {
20
20
  const axios = (await import("axios/dist/browser/axios.cjs")).default;
@@ -30,7 +30,7 @@ const initApiClient = async (args) => {
30
30
  const config = {
31
31
  method,
32
32
  url: path,
33
- data: payload,
33
+ ...method === "get" ? { params: payload } : { data: payload },
34
34
  headers: {
35
35
  // ...(typeof ctxOrPath !== 'string' && {
36
36
  // // 'X-Custom-Header': ctxOrPath.someHeaderValue,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/client",
3
- "version": "0.344.0",
3
+ "version": "0.346.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"