@kedaruma/revlm-client 1.0.10 → 1.0.11

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.mts CHANGED
@@ -173,6 +173,7 @@ type RevlmOptions = {
173
173
  provisionalAuthSecretMaster?: string;
174
174
  provisionalAuthDomain?: string;
175
175
  autoSetToken?: boolean;
176
+ autoRefreshOn401?: boolean;
176
177
  };
177
178
  type RevlmResponse<T = any> = {
178
179
  ok: boolean;
@@ -191,6 +192,7 @@ declare class Revlm {
191
192
  private provisionalAuthSecretMaster;
192
193
  private provisionalAuthDomain;
193
194
  private autoSetToken;
195
+ private autoRefreshOn401;
194
196
  constructor(baseUrl: string, opts?: RevlmOptions);
195
197
  setToken(token: string): void;
196
198
  getToken(): string | undefined;
@@ -201,6 +203,8 @@ declare class Revlm {
201
203
  private makeHeaders;
202
204
  private parseResponse;
203
205
  private request;
206
+ private shouldSkipAuthRetry;
207
+ private requestWithRetry;
204
208
  login(authId: string, password: string): Promise<LoginResponse>;
205
209
  provisionalLogin(authId: string): Promise<ProvisionalLoginResponse>;
206
210
  registerUser(user: UserInput, password: string): Promise<RevlmResponse<any>>;
package/dist/index.d.ts CHANGED
@@ -173,6 +173,7 @@ type RevlmOptions = {
173
173
  provisionalAuthSecretMaster?: string;
174
174
  provisionalAuthDomain?: string;
175
175
  autoSetToken?: boolean;
176
+ autoRefreshOn401?: boolean;
176
177
  };
177
178
  type RevlmResponse<T = any> = {
178
179
  ok: boolean;
@@ -191,6 +192,7 @@ declare class Revlm {
191
192
  private provisionalAuthSecretMaster;
192
193
  private provisionalAuthDomain;
193
194
  private autoSetToken;
195
+ private autoRefreshOn401;
194
196
  constructor(baseUrl: string, opts?: RevlmOptions);
195
197
  setToken(token: string): void;
196
198
  getToken(): string | undefined;
@@ -201,6 +203,8 @@ declare class Revlm {
201
203
  private makeHeaders;
202
204
  private parseResponse;
203
205
  private request;
206
+ private shouldSkipAuthRetry;
207
+ private requestWithRetry;
204
208
  login(authId: string, password: string): Promise<LoginResponse>;
205
209
  provisionalLogin(authId: string): Promise<ProvisionalLoginResponse>;
206
210
  registerUser(user: UserInput, password: string): Promise<RevlmResponse<any>>;
package/dist/index.js CHANGED
@@ -155,6 +155,7 @@ var Revlm = class {
155
155
  provisionalAuthSecretMaster;
156
156
  provisionalAuthDomain;
157
157
  autoSetToken;
158
+ autoRefreshOn401;
158
159
  constructor(baseUrl, opts = {}) {
159
160
  if (!baseUrl) throw new Error("baseUrl is required");
160
161
  this.baseUrl = baseUrl.replace(/\/$/, "");
@@ -164,6 +165,7 @@ var Revlm = class {
164
165
  this.provisionalAuthSecretMaster = opts.provisionalAuthSecretMaster || "";
165
166
  this.provisionalAuthDomain = opts.provisionalAuthDomain || "";
166
167
  this.autoSetToken = opts.autoSetToken ?? true;
168
+ this.autoRefreshOn401 = opts.autoRefreshOn401 || false;
167
169
  if (!this.fetchImpl) {
168
170
  throw new Error("No fetch implementation available. Provide fetchImpl in options or run in Node 18+ with global fetch.");
169
171
  }
@@ -185,7 +187,7 @@ var Revlm = class {
185
187
  // On success, if autoSetToken is true and res.token is set, update the client token.
186
188
  async refreshToken() {
187
189
  if (!this._token) return { ok: false, error: "No token set" };
188
- const res = await this.request("/refresh-token", "POST");
190
+ const res = await this.requestWithRetry("/refresh-token", "POST", void 0, { allowAuthRetry: false, retrying: false });
189
191
  if (this.autoSetToken && res && res.ok && res.token) {
190
192
  this.setToken(res.token);
191
193
  }
@@ -230,6 +232,14 @@ var Revlm = class {
230
232
  }
231
233
  }
232
234
  async request(path, method = "POST", body) {
235
+ return this.requestWithRetry(path, method, body, { allowAuthRetry: this.autoRefreshOn401, retrying: false });
236
+ }
237
+ shouldSkipAuthRetry(path) {
238
+ const pathname = path.startsWith("http") ? new URL(path).pathname : path;
239
+ return pathname.includes("/login") || pathname.includes("/provisional-login") || pathname.includes("/refresh-token") || pathname.includes("/verify-token");
240
+ }
241
+ async requestWithRetry(path, method = "POST", body, opts = { allowAuthRetry: false, retrying: false }) {
242
+ const { allowAuthRetry, retrying } = opts;
233
243
  const url = path.startsWith("http") ? path : `${this.baseUrl}${path.startsWith("/") ? "" : "/"}${path}`;
234
244
  const hasBody = body !== void 0;
235
245
  const headers = this.makeHeaders(hasBody);
@@ -249,6 +259,12 @@ var Revlm = class {
249
259
  if (out && out.ok === false && !out.error) {
250
260
  out.error = parsed?.reason || parsed?.message || "Unknown error";
251
261
  }
262
+ if (allowAuthRetry && !retrying && res.status === 401 && !this.shouldSkipAuthRetry(path)) {
263
+ const refreshRes = await this.refreshToken();
264
+ if (refreshRes && refreshRes.ok && refreshRes.token) {
265
+ return this.requestWithRetry(path, method, body, { allowAuthRetry: false, retrying: true });
266
+ }
267
+ }
252
268
  return out;
253
269
  } catch (err) {
254
270
  return { ok: false, error: err?.message || String(err) };
package/dist/index.mjs CHANGED
@@ -110,6 +110,7 @@ var Revlm = class {
110
110
  provisionalAuthSecretMaster;
111
111
  provisionalAuthDomain;
112
112
  autoSetToken;
113
+ autoRefreshOn401;
113
114
  constructor(baseUrl, opts = {}) {
114
115
  if (!baseUrl) throw new Error("baseUrl is required");
115
116
  this.baseUrl = baseUrl.replace(/\/$/, "");
@@ -119,6 +120,7 @@ var Revlm = class {
119
120
  this.provisionalAuthSecretMaster = opts.provisionalAuthSecretMaster || "";
120
121
  this.provisionalAuthDomain = opts.provisionalAuthDomain || "";
121
122
  this.autoSetToken = opts.autoSetToken ?? true;
123
+ this.autoRefreshOn401 = opts.autoRefreshOn401 || false;
122
124
  if (!this.fetchImpl) {
123
125
  throw new Error("No fetch implementation available. Provide fetchImpl in options or run in Node 18+ with global fetch.");
124
126
  }
@@ -140,7 +142,7 @@ var Revlm = class {
140
142
  // On success, if autoSetToken is true and res.token is set, update the client token.
141
143
  async refreshToken() {
142
144
  if (!this._token) return { ok: false, error: "No token set" };
143
- const res = await this.request("/refresh-token", "POST");
145
+ const res = await this.requestWithRetry("/refresh-token", "POST", void 0, { allowAuthRetry: false, retrying: false });
144
146
  if (this.autoSetToken && res && res.ok && res.token) {
145
147
  this.setToken(res.token);
146
148
  }
@@ -185,6 +187,14 @@ var Revlm = class {
185
187
  }
186
188
  }
187
189
  async request(path, method = "POST", body) {
190
+ return this.requestWithRetry(path, method, body, { allowAuthRetry: this.autoRefreshOn401, retrying: false });
191
+ }
192
+ shouldSkipAuthRetry(path) {
193
+ const pathname = path.startsWith("http") ? new URL(path).pathname : path;
194
+ return pathname.includes("/login") || pathname.includes("/provisional-login") || pathname.includes("/refresh-token") || pathname.includes("/verify-token");
195
+ }
196
+ async requestWithRetry(path, method = "POST", body, opts = { allowAuthRetry: false, retrying: false }) {
197
+ const { allowAuthRetry, retrying } = opts;
188
198
  const url = path.startsWith("http") ? path : `${this.baseUrl}${path.startsWith("/") ? "" : "/"}${path}`;
189
199
  const hasBody = body !== void 0;
190
200
  const headers = this.makeHeaders(hasBody);
@@ -204,6 +214,12 @@ var Revlm = class {
204
214
  if (out && out.ok === false && !out.error) {
205
215
  out.error = parsed?.reason || parsed?.message || "Unknown error";
206
216
  }
217
+ if (allowAuthRetry && !retrying && res.status === 401 && !this.shouldSkipAuthRetry(path)) {
218
+ const refreshRes = await this.refreshToken();
219
+ if (refreshRes && refreshRes.ok && refreshRes.token) {
220
+ return this.requestWithRetry(path, method, body, { allowAuthRetry: false, retrying: true });
221
+ }
222
+ }
207
223
  return out;
208
224
  } catch (err) {
209
225
  return { ok: false, error: err?.message || String(err) };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kedaruma/revlm-client",
3
- "version": "1.0.10",
3
+ "version": "1.0.11",
4
4
  "private": false,
5
5
  "description": "TypeScript client SDK for talking to the Revlm server replacement for MongoDB Realm.",
6
6
  "keywords": [