@veloxts/client 0.6.84 → 0.6.86
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 +12 -0
- package/dist/client.js +72 -2
- package/dist/index.d.ts +1 -1
- package/dist/types.d.ts +22 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/client.js
CHANGED
|
@@ -208,7 +208,67 @@ function extractPathParams(path) {
|
|
|
208
208
|
// Request Building
|
|
209
209
|
// ============================================================================
|
|
210
210
|
/**
|
|
211
|
-
*
|
|
211
|
+
* Detects client mode from configuration
|
|
212
|
+
*
|
|
213
|
+
* Auto-detects 'trpc' mode if baseUrl ends with '/trpc'
|
|
214
|
+
*
|
|
215
|
+
* @internal
|
|
216
|
+
*/
|
|
217
|
+
function detectMode(config) {
|
|
218
|
+
if (config.mode) {
|
|
219
|
+
return config.mode;
|
|
220
|
+
}
|
|
221
|
+
// Auto-detect: if baseUrl ends with /trpc, use tRPC mode
|
|
222
|
+
return config.baseUrl.endsWith('/trpc') ? 'trpc' : 'rest';
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Determines if a procedure is a query based on naming convention
|
|
226
|
+
*
|
|
227
|
+
* @internal
|
|
228
|
+
*/
|
|
229
|
+
function isQueryProcedure(procedureName) {
|
|
230
|
+
return (procedureName.startsWith('get') ||
|
|
231
|
+
procedureName.startsWith('list') ||
|
|
232
|
+
procedureName.startsWith('find'));
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Builds a tRPC-compatible request
|
|
236
|
+
*
|
|
237
|
+
* - Queries: GET /trpc/namespace.procedure?input={encoded json}
|
|
238
|
+
* - Mutations: POST /trpc/namespace.procedure with JSON body
|
|
239
|
+
*
|
|
240
|
+
* @internal
|
|
241
|
+
*/
|
|
242
|
+
function buildTrpcRequest(call, baseUrl, headers) {
|
|
243
|
+
const procedurePath = `${call.namespace}.${call.procedureName}`;
|
|
244
|
+
const isQuery = isQueryProcedure(call.procedureName);
|
|
245
|
+
if (isQuery) {
|
|
246
|
+
// GET request with input as query parameter
|
|
247
|
+
let url = `${baseUrl}/${procedurePath}`;
|
|
248
|
+
if (call.input != null) {
|
|
249
|
+
const inputParam = encodeURIComponent(JSON.stringify(call.input));
|
|
250
|
+
url = `${url}?input=${inputParam}`;
|
|
251
|
+
}
|
|
252
|
+
return {
|
|
253
|
+
url,
|
|
254
|
+
options: {
|
|
255
|
+
method: 'GET',
|
|
256
|
+
headers,
|
|
257
|
+
},
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
// POST request with input as JSON body
|
|
261
|
+
return {
|
|
262
|
+
url: `${baseUrl}/${procedurePath}`,
|
|
263
|
+
options: {
|
|
264
|
+
method: 'POST',
|
|
265
|
+
headers,
|
|
266
|
+
body: call.input != null ? JSON.stringify(call.input) : undefined,
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Resolves both method and path for a procedure call (REST mode)
|
|
212
272
|
*
|
|
213
273
|
* Uses explicit route mapping if available, otherwise falls back to naming convention.
|
|
214
274
|
*
|
|
@@ -228,16 +288,26 @@ function resolveMethodAndPath(namespace, procedureName, routes) {
|
|
|
228
288
|
/**
|
|
229
289
|
* Builds the full URL and request options for a procedure call
|
|
230
290
|
*
|
|
291
|
+
* Supports two modes:
|
|
292
|
+
* - REST: RESTful paths based on naming conventions
|
|
293
|
+
* - tRPC: tRPC-compatible paths with namespace.procedure format
|
|
294
|
+
*
|
|
231
295
|
* @internal
|
|
232
296
|
*/
|
|
233
297
|
function buildRequest(call, baseUrl, config) {
|
|
234
|
-
const { method, path } = resolveMethodAndPath(call.namespace, call.procedureName, config.routes);
|
|
235
298
|
// Prepare headers - support both static object and dynamic function
|
|
236
299
|
const customHeaders = typeof config.headers === 'function' ? config.headers() : (config.headers ?? {});
|
|
237
300
|
const headers = {
|
|
238
301
|
'Content-Type': 'application/json',
|
|
239
302
|
...customHeaders,
|
|
240
303
|
};
|
|
304
|
+
// Detect mode and route accordingly
|
|
305
|
+
const mode = detectMode(config);
|
|
306
|
+
if (mode === 'trpc') {
|
|
307
|
+
return buildTrpcRequest(call, baseUrl, headers);
|
|
308
|
+
}
|
|
309
|
+
// REST mode
|
|
310
|
+
const { method, path } = resolveMethodAndPath(call.namespace, call.procedureName, config.routes);
|
|
241
311
|
let finalPath = path;
|
|
242
312
|
let body;
|
|
243
313
|
// Handle input based on method
|
package/dist/index.d.ts
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
* @module @veloxts/client
|
|
22
22
|
*/
|
|
23
23
|
export { createClient } from './client.js';
|
|
24
|
-
export type { ClientConfig, ClientError, ClientFromCollection, ClientFromRouter, ClientProcedure, HttpMethod, InferProcedureInput, InferProcedureOutput, ProcedureCall, ProcedureCollection, ProcedureRecord, } from './types.js';
|
|
24
|
+
export type { ClientConfig, ClientError, ClientFromCollection, ClientFromRouter, ClientMode, ClientProcedure, HttpMethod, InferProcedureInput, InferProcedureOutput, ProcedureCall, ProcedureCollection, ProcedureRecord, } from './types.js';
|
|
25
25
|
export { ClientNotFoundError, ClientValidationError, NetworkError, ServerError, VeloxClientError, } from './errors.js';
|
|
26
26
|
export { isClientNotFoundError, isClientValidationError, isNetworkError, isNotFoundErrorResponse, isServerError, isValidationErrorResponse, isVeloxClientError, } from './errors.js';
|
|
27
27
|
export type { ErrorResponse } from './errors.js';
|
package/dist/types.d.ts
CHANGED
|
@@ -173,12 +173,34 @@ export interface RouteEntry {
|
|
|
173
173
|
* ```
|
|
174
174
|
*/
|
|
175
175
|
export type RouteMap = Record<string, Record<string, RouteEntry | string>>;
|
|
176
|
+
/**
|
|
177
|
+
* Client mode determines how URLs are generated
|
|
178
|
+
*
|
|
179
|
+
* - `'rest'` - RESTful URLs based on naming conventions (e.g., GET /users/:id)
|
|
180
|
+
* - `'trpc'` - tRPC-compatible URLs (e.g., GET /trpc/users.getUser?input={...})
|
|
181
|
+
*/
|
|
182
|
+
export type ClientMode = 'rest' | 'trpc';
|
|
176
183
|
/**
|
|
177
184
|
* Configuration for creating a client instance
|
|
178
185
|
*/
|
|
179
186
|
export interface ClientConfig {
|
|
180
187
|
/** Base URL for API requests (e.g., 'https://api.example.com' or '/api') */
|
|
181
188
|
baseUrl: string;
|
|
189
|
+
/**
|
|
190
|
+
* Client mode: 'rest' or 'trpc'
|
|
191
|
+
*
|
|
192
|
+
* - `'rest'` - Generates RESTful paths based on naming conventions
|
|
193
|
+
* e.g., `getUser` → `GET /users/:id`, `createUser` → `POST /users`
|
|
194
|
+
*
|
|
195
|
+
* - `'trpc'` - Generates tRPC-compatible paths
|
|
196
|
+
* e.g., `getUser` → `GET /trpc/users.getUser?input={...}`
|
|
197
|
+
* e.g., `createUser` → `POST /trpc/users.createUser` with JSON body
|
|
198
|
+
*
|
|
199
|
+
* Auto-detected if not specified:
|
|
200
|
+
* - If `baseUrl` ends with '/trpc', defaults to 'trpc'
|
|
201
|
+
* - Otherwise defaults to 'rest'
|
|
202
|
+
*/
|
|
203
|
+
mode?: ClientMode;
|
|
182
204
|
/**
|
|
183
205
|
* Optional custom headers to include in all requests
|
|
184
206
|
*
|