pgbeam 0.2.1 → 0.2.2
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.cjs +30 -30
- package/dist/index.mjs +30 -30
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
|
|
3
|
-
var _extends = require('@swc/helpers/_/_extends');
|
|
4
|
-
|
|
5
3
|
/**
|
|
6
4
|
* Generated by scripts/generate-maps.ts — do not edit manually.
|
|
7
5
|
* Run: pnpm --filter pgbeam generate
|
|
@@ -328,8 +326,7 @@ const operationsByPath = {
|
|
|
328
326
|
|
|
329
327
|
class ApiError extends Error {
|
|
330
328
|
constructor(status, statusText, body){
|
|
331
|
-
|
|
332
|
-
const message = typeof body === "object" && body !== null ? (_extractMessage = extractMessage(body)) != null ? _extractMessage : statusText : statusText;
|
|
329
|
+
const message = typeof body === "object" && body !== null ? extractMessage(body) ?? statusText : statusText;
|
|
333
330
|
super(message);
|
|
334
331
|
this.name = "ApiError";
|
|
335
332
|
this.status = status;
|
|
@@ -358,14 +355,18 @@ const RETRYABLE_STATUS_CODES = new Set([
|
|
|
358
355
|
const DEFAULT_RETRY = {
|
|
359
356
|
maxRetries: 5,
|
|
360
357
|
initialDelayMs: 500,
|
|
361
|
-
maxDelayMs:
|
|
358
|
+
maxDelayMs: 30_000,
|
|
362
359
|
idempotencyKeys: true
|
|
363
360
|
};
|
|
364
361
|
function resolveRetry(config) {
|
|
365
|
-
if (!config) return
|
|
362
|
+
if (!config) return {
|
|
363
|
+
...DEFAULT_RETRY,
|
|
366
364
|
maxRetries: 0
|
|
367
|
-
}
|
|
368
|
-
return
|
|
365
|
+
};
|
|
366
|
+
return {
|
|
367
|
+
...DEFAULT_RETRY,
|
|
368
|
+
...config
|
|
369
|
+
};
|
|
369
370
|
}
|
|
370
371
|
/** Parse Retry-After header as milliseconds. Returns undefined if unparseable. */ function parseRetryAfter(response) {
|
|
371
372
|
const header = response.headers.get("Retry-After");
|
|
@@ -403,7 +404,6 @@ function isMutatingMethod(method) {
|
|
|
403
404
|
return upper === "POST" || upper === "PATCH";
|
|
404
405
|
}
|
|
405
406
|
async function fetcher(options) {
|
|
406
|
-
var _options_fetchImpl;
|
|
407
407
|
let url = options.path;
|
|
408
408
|
if (options.pathParams) {
|
|
409
409
|
for (const [key, value] of Object.entries(options.pathParams)){
|
|
@@ -425,20 +425,21 @@ async function fetcher(options) {
|
|
|
425
425
|
if (options.body !== undefined) {
|
|
426
426
|
headers["Content-Type"] = "application/json";
|
|
427
427
|
}
|
|
428
|
-
const fetchFn =
|
|
428
|
+
const fetchFn = options.fetchImpl ?? globalThis.fetch;
|
|
429
429
|
const retry = resolveRetry(options.retry);
|
|
430
430
|
const urlStr = fullUrl.toString();
|
|
431
431
|
const fetchBody = options.body !== undefined ? JSON.stringify(options.body) : undefined;
|
|
432
432
|
// Generate idempotency key once, reused across all retry attempts.
|
|
433
433
|
const idempotencyKey = retry.maxRetries > 0 && retry.idempotencyKeys && isMutatingMethod(options.method) ? generateUUID() : undefined;
|
|
434
434
|
for(let attempt = 0; attempt <= retry.maxRetries; attempt++){
|
|
435
|
-
const reqHeaders =
|
|
435
|
+
const reqHeaders = {
|
|
436
|
+
...headers
|
|
437
|
+
};
|
|
436
438
|
// Attach idempotency key on all attempts for mutating requests.
|
|
437
439
|
if (idempotencyKey) {
|
|
438
440
|
reqHeaders["Idempotency-Key"] = idempotencyKey;
|
|
439
441
|
}
|
|
440
442
|
try {
|
|
441
|
-
var _parseRetryAfter;
|
|
442
443
|
const response = await fetchFn(urlStr, {
|
|
443
444
|
method: options.method,
|
|
444
445
|
headers: reqHeaders,
|
|
@@ -456,7 +457,7 @@ async function fetcher(options) {
|
|
|
456
457
|
throw new ApiError(response.status, response.statusText, errorBody);
|
|
457
458
|
}
|
|
458
459
|
// Retryable error — wait and retry.
|
|
459
|
-
const delay =
|
|
460
|
+
const delay = parseRetryAfter(response) ?? backoff(attempt, retry.initialDelayMs, retry.maxDelayMs);
|
|
460
461
|
await sleep(delay);
|
|
461
462
|
} catch (err) {
|
|
462
463
|
// If it's already an ApiError, rethrow (we threw it above).
|
|
@@ -473,7 +474,7 @@ async function fetcher(options) {
|
|
|
473
474
|
async function parseResponseBody(response) {
|
|
474
475
|
if (response.status === 204) return undefined;
|
|
475
476
|
const contentType = response.headers.get("content-type");
|
|
476
|
-
if (contentType
|
|
477
|
+
if (contentType?.includes("application/json")) {
|
|
477
478
|
return response.json();
|
|
478
479
|
}
|
|
479
480
|
// Fallback: try JSON, return undefined if body is empty/non-JSON
|
|
@@ -481,7 +482,7 @@ async function parseResponseBody(response) {
|
|
|
481
482
|
if (!text) return undefined;
|
|
482
483
|
try {
|
|
483
484
|
return JSON.parse(text);
|
|
484
|
-
} catch
|
|
485
|
+
} catch {
|
|
485
486
|
return undefined;
|
|
486
487
|
}
|
|
487
488
|
}
|
|
@@ -490,6 +491,17 @@ function sleep(ms) {
|
|
|
490
491
|
}
|
|
491
492
|
|
|
492
493
|
class PgBeamClient {
|
|
494
|
+
constructor(options){
|
|
495
|
+
this._baseUrl = options.baseUrl;
|
|
496
|
+
this._tokenOrFn = options.token;
|
|
497
|
+
this._fetchImpl = options.fetch;
|
|
498
|
+
this._onResponse = options.onResponse;
|
|
499
|
+
this._retry = options.retry === false ? {
|
|
500
|
+
maxRetries: 0
|
|
501
|
+
} : options.retry ?? {
|
|
502
|
+
maxRetries: 5
|
|
503
|
+
};
|
|
504
|
+
}
|
|
493
505
|
async _resolveToken() {
|
|
494
506
|
if (typeof this._tokenOrFn === "function") {
|
|
495
507
|
return this._tokenOrFn();
|
|
@@ -522,9 +534,9 @@ class PgBeamClient {
|
|
|
522
534
|
return fetcher({
|
|
523
535
|
method: meta.method,
|
|
524
536
|
path: meta.path,
|
|
525
|
-
pathParams: params
|
|
526
|
-
queryParams: params
|
|
527
|
-
body: params
|
|
537
|
+
pathParams: params?.pathParams,
|
|
538
|
+
queryParams: params?.queryParams,
|
|
539
|
+
body: params?.body,
|
|
528
540
|
baseUrl: this._baseUrl,
|
|
529
541
|
token,
|
|
530
542
|
fetchImpl: this._fetchImpl,
|
|
@@ -549,18 +561,6 @@ class PgBeamClient {
|
|
|
549
561
|
});
|
|
550
562
|
return this._api;
|
|
551
563
|
}
|
|
552
|
-
constructor(options){
|
|
553
|
-
var _options_retry;
|
|
554
|
-
this._baseUrl = options.baseUrl;
|
|
555
|
-
this._tokenOrFn = options.token;
|
|
556
|
-
this._fetchImpl = options.fetch;
|
|
557
|
-
this._onResponse = options.onResponse;
|
|
558
|
-
this._retry = options.retry === false ? {
|
|
559
|
-
maxRetries: 0
|
|
560
|
-
} : (_options_retry = options.retry) != null ? _options_retry : {
|
|
561
|
-
maxRetries: 5
|
|
562
|
-
};
|
|
563
|
-
}
|
|
564
564
|
}
|
|
565
565
|
|
|
566
566
|
exports.ApiError = ApiError;
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { _ } from '@swc/helpers/_/_extends';
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* Generated by scripts/generate-maps.ts — do not edit manually.
|
|
5
3
|
* Run: pnpm --filter pgbeam generate
|
|
@@ -326,8 +324,7 @@ const operationsByPath = {
|
|
|
326
324
|
|
|
327
325
|
class ApiError extends Error {
|
|
328
326
|
constructor(status, statusText, body){
|
|
329
|
-
|
|
330
|
-
const message = typeof body === "object" && body !== null ? (_extractMessage = extractMessage(body)) != null ? _extractMessage : statusText : statusText;
|
|
327
|
+
const message = typeof body === "object" && body !== null ? extractMessage(body) ?? statusText : statusText;
|
|
331
328
|
super(message);
|
|
332
329
|
this.name = "ApiError";
|
|
333
330
|
this.status = status;
|
|
@@ -356,14 +353,18 @@ const RETRYABLE_STATUS_CODES = new Set([
|
|
|
356
353
|
const DEFAULT_RETRY = {
|
|
357
354
|
maxRetries: 5,
|
|
358
355
|
initialDelayMs: 500,
|
|
359
|
-
maxDelayMs:
|
|
356
|
+
maxDelayMs: 30_000,
|
|
360
357
|
idempotencyKeys: true
|
|
361
358
|
};
|
|
362
359
|
function resolveRetry(config) {
|
|
363
|
-
if (!config) return
|
|
360
|
+
if (!config) return {
|
|
361
|
+
...DEFAULT_RETRY,
|
|
364
362
|
maxRetries: 0
|
|
365
|
-
}
|
|
366
|
-
return
|
|
363
|
+
};
|
|
364
|
+
return {
|
|
365
|
+
...DEFAULT_RETRY,
|
|
366
|
+
...config
|
|
367
|
+
};
|
|
367
368
|
}
|
|
368
369
|
/** Parse Retry-After header as milliseconds. Returns undefined if unparseable. */ function parseRetryAfter(response) {
|
|
369
370
|
const header = response.headers.get("Retry-After");
|
|
@@ -401,7 +402,6 @@ function isMutatingMethod(method) {
|
|
|
401
402
|
return upper === "POST" || upper === "PATCH";
|
|
402
403
|
}
|
|
403
404
|
async function fetcher(options) {
|
|
404
|
-
var _options_fetchImpl;
|
|
405
405
|
let url = options.path;
|
|
406
406
|
if (options.pathParams) {
|
|
407
407
|
for (const [key, value] of Object.entries(options.pathParams)){
|
|
@@ -423,20 +423,21 @@ async function fetcher(options) {
|
|
|
423
423
|
if (options.body !== undefined) {
|
|
424
424
|
headers["Content-Type"] = "application/json";
|
|
425
425
|
}
|
|
426
|
-
const fetchFn =
|
|
426
|
+
const fetchFn = options.fetchImpl ?? globalThis.fetch;
|
|
427
427
|
const retry = resolveRetry(options.retry);
|
|
428
428
|
const urlStr = fullUrl.toString();
|
|
429
429
|
const fetchBody = options.body !== undefined ? JSON.stringify(options.body) : undefined;
|
|
430
430
|
// Generate idempotency key once, reused across all retry attempts.
|
|
431
431
|
const idempotencyKey = retry.maxRetries > 0 && retry.idempotencyKeys && isMutatingMethod(options.method) ? generateUUID() : undefined;
|
|
432
432
|
for(let attempt = 0; attempt <= retry.maxRetries; attempt++){
|
|
433
|
-
const reqHeaders =
|
|
433
|
+
const reqHeaders = {
|
|
434
|
+
...headers
|
|
435
|
+
};
|
|
434
436
|
// Attach idempotency key on all attempts for mutating requests.
|
|
435
437
|
if (idempotencyKey) {
|
|
436
438
|
reqHeaders["Idempotency-Key"] = idempotencyKey;
|
|
437
439
|
}
|
|
438
440
|
try {
|
|
439
|
-
var _parseRetryAfter;
|
|
440
441
|
const response = await fetchFn(urlStr, {
|
|
441
442
|
method: options.method,
|
|
442
443
|
headers: reqHeaders,
|
|
@@ -454,7 +455,7 @@ async function fetcher(options) {
|
|
|
454
455
|
throw new ApiError(response.status, response.statusText, errorBody);
|
|
455
456
|
}
|
|
456
457
|
// Retryable error — wait and retry.
|
|
457
|
-
const delay =
|
|
458
|
+
const delay = parseRetryAfter(response) ?? backoff(attempt, retry.initialDelayMs, retry.maxDelayMs);
|
|
458
459
|
await sleep(delay);
|
|
459
460
|
} catch (err) {
|
|
460
461
|
// If it's already an ApiError, rethrow (we threw it above).
|
|
@@ -471,7 +472,7 @@ async function fetcher(options) {
|
|
|
471
472
|
async function parseResponseBody(response) {
|
|
472
473
|
if (response.status === 204) return undefined;
|
|
473
474
|
const contentType = response.headers.get("content-type");
|
|
474
|
-
if (contentType
|
|
475
|
+
if (contentType?.includes("application/json")) {
|
|
475
476
|
return response.json();
|
|
476
477
|
}
|
|
477
478
|
// Fallback: try JSON, return undefined if body is empty/non-JSON
|
|
@@ -479,7 +480,7 @@ async function parseResponseBody(response) {
|
|
|
479
480
|
if (!text) return undefined;
|
|
480
481
|
try {
|
|
481
482
|
return JSON.parse(text);
|
|
482
|
-
} catch
|
|
483
|
+
} catch {
|
|
483
484
|
return undefined;
|
|
484
485
|
}
|
|
485
486
|
}
|
|
@@ -488,6 +489,17 @@ function sleep(ms) {
|
|
|
488
489
|
}
|
|
489
490
|
|
|
490
491
|
class PgBeamClient {
|
|
492
|
+
constructor(options){
|
|
493
|
+
this._baseUrl = options.baseUrl;
|
|
494
|
+
this._tokenOrFn = options.token;
|
|
495
|
+
this._fetchImpl = options.fetch;
|
|
496
|
+
this._onResponse = options.onResponse;
|
|
497
|
+
this._retry = options.retry === false ? {
|
|
498
|
+
maxRetries: 0
|
|
499
|
+
} : options.retry ?? {
|
|
500
|
+
maxRetries: 5
|
|
501
|
+
};
|
|
502
|
+
}
|
|
491
503
|
async _resolveToken() {
|
|
492
504
|
if (typeof this._tokenOrFn === "function") {
|
|
493
505
|
return this._tokenOrFn();
|
|
@@ -520,9 +532,9 @@ class PgBeamClient {
|
|
|
520
532
|
return fetcher({
|
|
521
533
|
method: meta.method,
|
|
522
534
|
path: meta.path,
|
|
523
|
-
pathParams: params
|
|
524
|
-
queryParams: params
|
|
525
|
-
body: params
|
|
535
|
+
pathParams: params?.pathParams,
|
|
536
|
+
queryParams: params?.queryParams,
|
|
537
|
+
body: params?.body,
|
|
526
538
|
baseUrl: this._baseUrl,
|
|
527
539
|
token,
|
|
528
540
|
fetchImpl: this._fetchImpl,
|
|
@@ -547,18 +559,6 @@ class PgBeamClient {
|
|
|
547
559
|
});
|
|
548
560
|
return this._api;
|
|
549
561
|
}
|
|
550
|
-
constructor(options){
|
|
551
|
-
var _options_retry;
|
|
552
|
-
this._baseUrl = options.baseUrl;
|
|
553
|
-
this._tokenOrFn = options.token;
|
|
554
|
-
this._fetchImpl = options.fetch;
|
|
555
|
-
this._onResponse = options.onResponse;
|
|
556
|
-
this._retry = options.retry === false ? {
|
|
557
|
-
maxRetries: 0
|
|
558
|
-
} : (_options_retry = options.retry) != null ? _options_retry : {
|
|
559
|
-
maxRetries: 5
|
|
560
|
-
};
|
|
561
|
-
}
|
|
562
562
|
}
|
|
563
563
|
|
|
564
564
|
export { ApiError, PgBeamClient, extractMessage };
|