prividium 0.17.0 → 0.17.1

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.
@@ -2,6 +2,7 @@
2
2
  * Checks if a Response contains a Prividium unauthorized/forbidden error.
3
3
  */
4
4
  export declare function hasPrividiumUnauthorizedError(response: Response): Promise<boolean>;
5
+ export declare function isPrividiumUnauthorizedRpcError(error: unknown): boolean;
5
6
  /**
6
7
  * Extracts a human-readable error string from a failed HTTP response.
7
8
  * Attempts to parse the server's `{ error: { code, message } }` JSON structure,
@@ -17,13 +17,27 @@ const apiErrorSchema = z.object({
17
17
  * Checks if a Response contains a Prividium unauthorized/forbidden error.
18
18
  */
19
19
  export async function hasPrividiumUnauthorizedError(response) {
20
- const clonedResponse = response.clone();
21
- const parsed = jsonRpcErrorSchema.safeParse(await clonedResponse.json());
22
- // { success: false, error: {...} } | { success: true, data: {...} }
23
- if (parsed.success) {
24
- return parsed.data.error.code === UNAUTHORIZED_ERROR_CODE;
20
+ try {
21
+ const clonedResponse = response.clone();
22
+ const parsed = jsonRpcErrorSchema.safeParse(await clonedResponse.json());
23
+ if (parsed.success) {
24
+ return parsed.data.error.code === UNAUTHORIZED_ERROR_CODE;
25
+ }
26
+ return false;
27
+ }
28
+ catch {
29
+ return false;
30
+ }
31
+ }
32
+ export function isPrividiumUnauthorizedRpcError(error) {
33
+ if (!error || typeof error !== 'object') {
34
+ return false;
35
+ }
36
+ const value = error;
37
+ if (value.code === UNAUTHORIZED_ERROR_CODE) {
38
+ return true;
25
39
  }
26
- return false;
40
+ return isPrividiumUnauthorizedRpcError(value.cause);
27
41
  }
28
42
  /**
29
43
  * Extracts a human-readable error string from a failed HTTP response.
@@ -1,6 +1,6 @@
1
1
  import { http } from 'viem';
2
2
  import { buildChainObject, createApiMethods, rpcUrl } from './chain-core.js';
3
- import { extractResponseError, hasPrividiumUnauthorizedError } from './error-utils.js';
3
+ import { extractResponseError, isPrividiumUnauthorizedRpcError } from './error-utils.js';
4
4
  import { PrividiumSessionError } from './errors.js';
5
5
  import { MemoryStorage } from './memory-storage.js';
6
6
  import { SiweAuth } from './siwe-auth.js';
@@ -112,16 +112,12 @@ export function createPrividiumSiweChain(config) {
112
112
  return parsed.data;
113
113
  }
114
114
  // Create transport with auth integration using viem callbacks
115
- const transport = http(rpcUrl(config.prividiumApiBaseUrl), {
115
+ const baseTransport = http(rpcUrl(config.prividiumApiBaseUrl), {
116
116
  batch: false,
117
117
  fetchOptions: {
118
118
  headers: getAuthHeaders() || {}
119
119
  },
120
120
  onFetchRequest(_request, init) {
121
- // Don't throw on expired token — let the request go through
122
- // so onFetchResponse can handle the 401 and trigger reauthentication.
123
- // Throwing here creates a deadlock: no request is sent, so onFetchResponse
124
- // (which is the only reauth trigger for RPC calls) never fires.
125
121
  const authHeaders = getAuthHeaders();
126
122
  if (authHeaders) {
127
123
  init.headers = {
@@ -129,24 +125,32 @@ export function createPrividiumSiweChain(config) {
129
125
  ...authHeaders
130
126
  };
131
127
  }
132
- },
133
- onFetchResponse: async (response) => {
134
- if (await hasPrividiumUnauthorizedError(response)) {
135
- tokenManager.clearToken();
136
- if (autoReauth) {
137
- try {
138
- await reauthenticate();
128
+ }
129
+ });
130
+ const transport = (parameters) => {
131
+ const base = baseTransport(parameters);
132
+ return {
133
+ ...base,
134
+ async request(request) {
135
+ await ensureAuthorized();
136
+ try {
137
+ return await base.request(request);
138
+ }
139
+ catch (error) {
140
+ if (!isPrividiumUnauthorizedRpcError(error)) {
141
+ throw error;
139
142
  }
140
- catch {
143
+ if (!autoReauth) {
141
144
  config.onAuthExpiry?.();
145
+ throw error;
142
146
  }
143
- }
144
- else {
145
- config.onAuthExpiry?.();
147
+ tokenManager.clearToken();
148
+ await reauthenticate();
149
+ return base.request(request);
146
150
  }
147
151
  }
148
- }
149
- });
152
+ };
153
+ };
150
154
  const apiMethods = createApiMethods({ prividiumApiCall, prividiumApiBaseUrl: config.prividiumApiBaseUrl });
151
155
  return {
152
156
  chain: buildChainObject(config),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prividium",
3
- "version": "0.17.0",
3
+ "version": "0.17.1",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "prividium": "bin/cli.js"