@seam-rpc/client 5.4.0 → 5.6.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,6 +1,6 @@
1
- import { ApiError, type Result } from "@seam-rpc/core";
1
+ import { ApiError, type Result, Ok, Err } from "@seam-rpc/core";
2
2
  export { ApiError };
3
- export type { Result };
3
+ export type { Result, Ok, Err };
4
4
  export type SeamRequestMiddleware = (context: SeamRequestMiddlewareContext) => void | Promise<void>;
5
5
  export type SeamResponseMiddleware = (context: SeamResponseMiddlewareContext) => void | Promise<void>;
6
6
  export type SeamOnErrorHandler = (error: SeamClientError) => void | Promise<void>;
@@ -14,29 +14,36 @@ export type SeamResponseMiddlewareContext = SeamRequestMiddlewareContext & {
14
14
  response: Response;
15
15
  parsedResponse: any;
16
16
  };
17
- export interface SeamClientOptions {
17
+ export interface SeamClientOptionsConstructor {
18
18
  middleware?: {
19
19
  request?: SeamRequestMiddleware[];
20
20
  response?: SeamResponseMiddleware[];
21
21
  };
22
22
  onError?: SeamOnErrorHandler[];
23
23
  }
24
+ export interface SeamClientOptions {
25
+ middleware: {
26
+ request: SeamRequestMiddleware[];
27
+ response: SeamResponseMiddleware[];
28
+ };
29
+ onError: SeamOnErrorHandler[];
30
+ }
24
31
  export declare class SeamClient<ApiType> {
25
32
  readonly baseUrl: string;
26
33
  options: SeamClientOptions;
27
34
  readonly api: ApiType;
28
- constructor(baseUrl: string, options?: SeamClientOptions);
35
+ constructor(baseUrl: string, options?: SeamClientOptionsConstructor);
29
36
  preRequest(middleware: SeamRequestMiddleware): void;
30
37
  postRequest(middleware: SeamResponseMiddleware): void;
31
38
  onError(handler: SeamOnErrorHandler): void;
32
39
  }
33
- type SeamClientErrorType = "REQUEST_FAILED" | "INVALID_CONTENT_TYPE";
40
+ type SeamClientErrorType = "REQUEST_FAILED" | "INVALID_CONTENT_TYPE" | "UNEXPECTED";
34
41
  export declare class SeamClientError extends Error {
35
42
  readonly type: SeamClientErrorType;
36
43
  readonly url: string;
37
- readonly request: RequestInit;
44
+ readonly request: RequestInit | null;
38
45
  readonly cause: unknown;
39
- constructor(type: SeamClientErrorType, message: string, url: string, request: RequestInit, cause: unknown);
46
+ constructor(type: SeamClientErrorType, message: string, url: string, request: RequestInit | null, cause: unknown);
40
47
  }
41
48
  export declare function createSeamClient<ApiType>(baseUrl: string, options?: SeamClientOptions): SeamClient<ApiType>;
42
- export declare function callApi(seamClient: SeamClient<any>, routerName: string, funcName: string, input?: Record<string, any>): Promise<any>;
49
+ export declare function callApi(seamClient: SeamClient<any>, url: string, routerName: string, funcName: string, input?: Record<string, any>): Promise<any>;
package/dist/index.js CHANGED
@@ -16,14 +16,18 @@ export class SeamClient {
16
16
  return new Proxy({}, {
17
17
  get(_subTarget, procName) {
18
18
  return async (input) => {
19
+ const url = `${client.baseUrl}/${String(routerName)}/${String(procName)}`;
19
20
  try {
20
- return await callApi(client, String(routerName), String(procName), input);
21
+ return await callApi(client, url, String(routerName), String(procName), input);
21
22
  }
22
23
  catch (err) {
23
- if (!client.options.onError || !(err instanceof SeamClientError))
24
- throw err;
24
+ const error = err instanceof SeamClientError
25
+ ? err
26
+ : new SeamClientError("UNEXPECTED", "Unexpected error when calling API procedure.", url, null, err);
27
+ if (client.options.onError.length == 0)
28
+ throw error;
25
29
  for (const handler of client.options?.onError) {
26
- handler(err);
30
+ handler(error);
27
31
  }
28
32
  return { ok: false, error: new ApiError("") };
29
33
  }
@@ -56,9 +60,8 @@ export class SeamClientError extends Error {
56
60
  export function createSeamClient(baseUrl, options) {
57
61
  return new SeamClient(baseUrl, options);
58
62
  }
59
- export async function callApi(seamClient, routerName, funcName, input) {
63
+ export async function callApi(seamClient, url, routerName, funcName, input) {
60
64
  const req = buildRequest(input);
61
- const url = `${seamClient.baseUrl}/${routerName}/${funcName}`;
62
65
  if (seamClient.options?.middleware?.request) {
63
66
  for (const mw of seamClient.options.middleware.request) {
64
67
  await mw({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seam-rpc/client",
3
- "version": "5.4.0",
3
+ "version": "5.6.0",
4
4
  "main": "./dist/index.js",
5
5
  "type": "module",
6
6
  "files": [