keq 5.0.0-alpha.32 → 5.0.0-alpha.34

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 CHANGED
@@ -1,3 +1,15 @@
1
+ ## 5.0.0-alpha.34
2
+
3
+ ### Major Changes
4
+
5
+ - f8abc63: **BREAKING CHANGE:** Remove `end()`, add `derive()` for reusing request configuration and `fire()` for fire-and-forget invocation.
6
+
7
+ ### Minor Changes
8
+
9
+ - a7a83da: **Perf:** Core now extends Promise, making keq instances true Promises instead of thenables.
10
+
11
+ ## 5.0.0-alpha.33
12
+
1
13
  ## 5.0.0-alpha.32
2
14
 
3
15
  ### Minor Changes
package/README.md CHANGED
@@ -36,7 +36,7 @@ Keq wraps the Fetch APIs, adding chain calls and middleware functions.
36
36
  ### Send Request
37
37
 
38
38
  A request can be initiated by invoking the appropriate method on the request object,
39
- then calling `.then()` (or `.end()` or `await`) to send the request.
39
+ then calling `.then()` (or `await`) to send the request.
40
40
  For example a simple GET request:
41
41
 
42
42
  ```javascript
package/dist/index.js CHANGED
@@ -1296,13 +1296,27 @@ function queryStringify(query, options) {
1296
1296
  }
1297
1297
 
1298
1298
  // src/request/core.ts
1299
- var Core = class {
1299
+ var import_json2 = require("klona/json");
1300
+ var Core = class extends Promise {
1300
1301
  constructor(url, options) {
1302
+ let resolve;
1303
+ let reject;
1304
+ super((res, rej) => {
1305
+ resolve = res;
1306
+ reject = rej;
1307
+ });
1301
1308
  /**
1302
1309
  * The unique identifier of the request's location in the code
1303
1310
  */
1304
1311
  __publicField(this, "__locationId__");
1305
- __publicField(this, "requestPromise");
1312
+ /** Deferred resolve callback captured from the Promise executor. */
1313
+ __publicField(this, "__resolve__");
1314
+ /** Deferred reject callback captured from the Promise executor. */
1315
+ __publicField(this, "__reject__");
1316
+ /** Whether the request has been lazily triggered by `.then()`. */
1317
+ __publicField(this, "__triggered__", false);
1318
+ /** Cached promise for idempotent `end()` calls. */
1319
+ __publicField(this, "__requestPromise__");
1306
1320
  __publicField(this, "requestInit");
1307
1321
  __publicField(this, "__listeners__", {});
1308
1322
  __publicField(this, "__global__");
@@ -1313,6 +1327,8 @@ var Core = class {
1313
1327
  resolveWithFullResponse: false,
1314
1328
  resolveWith: "intelligent"
1315
1329
  });
1330
+ this.__resolve__ = resolve;
1331
+ this.__reject__ = reject;
1316
1332
  this.__global__ = options.global || {};
1317
1333
  this.__locationId__ = options.locationId;
1318
1334
  this.__qs__ = options.qs;
@@ -1328,6 +1344,12 @@ var Core = class {
1328
1344
  this.__append_middlewares__.push(...options.middlewares);
1329
1345
  }
1330
1346
  }
1347
+ /**
1348
+ * Ensures `.then()`, `.catch()`, `.finally()` return plain Promise instances instead of Core/Keq instances.
1349
+ */
1350
+ static get [Symbol.species]() {
1351
+ return Promise;
1352
+ }
1331
1353
  get __middlewares__() {
1332
1354
  return [...this.__prepend_middlewares__, ...this.__append_middlewares__];
1333
1355
  }
@@ -1363,6 +1385,36 @@ var Core = class {
1363
1385
  }
1364
1386
  return this;
1365
1387
  }
1388
+ derive() {
1389
+ const derived = new this.constructor(
1390
+ this.requestInit.url,
1391
+ {
1392
+ method: this.requestInit.method,
1393
+ headers: this.requestInit.headers,
1394
+ body: this.requestInit.body,
1395
+ pathParameters: this.requestInit.pathParameters,
1396
+ cache: this.requestInit.cache,
1397
+ credentials: this.requestInit.credentials,
1398
+ integrity: this.requestInit.integrity,
1399
+ keepalive: this.requestInit.keepalive,
1400
+ mode: this.requestInit.mode,
1401
+ redirect: this.requestInit.redirect,
1402
+ referrer: this.requestInit.referrer,
1403
+ referrerPolicy: this.requestInit.referrerPolicy,
1404
+ locationId: this.__locationId__,
1405
+ global: this.__global__,
1406
+ qs: this.__qs__ ? { ...this.__qs__ } : void 0,
1407
+ middlewares: [...this.__append_middlewares__]
1408
+ }
1409
+ );
1410
+ derived.__prepend_middlewares__ = [...this.__prepend_middlewares__];
1411
+ derived.__listeners__ = {};
1412
+ for (const key in this.__listeners__) {
1413
+ derived.__listeners__[key] = [...this.__listeners__[key]];
1414
+ }
1415
+ derived.__options__ = (0, import_json2.klona)(this.__options__);
1416
+ return derived;
1417
+ }
1366
1418
  buildSharedContext() {
1367
1419
  const coreContext = new KeqSharedContext({
1368
1420
  locationId: this.__locationId__,
@@ -1414,45 +1466,47 @@ var Core = class {
1414
1466
  attempt = attemptWithDefault + 1;
1415
1467
  }
1416
1468
  }
1417
- async end() {
1418
- var _a6;
1419
- const coreContext = await this.run();
1420
- const resolveWithMode = coreContext.options.resolveWith;
1421
- if (resolveWithMode === "response") {
1422
- return (_a6 = coreContext.response) == null ? void 0 : _a6.clone();
1423
- }
1424
- const response = coreContext.response;
1425
- if (!resolveWithMode || resolveWithMode === "intelligent") {
1426
- const output = coreContext.output;
1427
- if (output !== void 0) {
1428
- return output;
1429
- }
1430
- return await intelligentParseResponse(response);
1469
+ __execute__() {
1470
+ if (!this.__requestPromise__) {
1471
+ this.__requestPromise__ = (async () => {
1472
+ var _a6;
1473
+ const coreContext = await this.run();
1474
+ const resolveWithMode = coreContext.options.resolveWith;
1475
+ if (resolveWithMode === "response") {
1476
+ return (_a6 = coreContext.response) == null ? void 0 : _a6.clone();
1477
+ }
1478
+ const response = coreContext.response;
1479
+ if (!resolveWithMode || resolveWithMode === "intelligent") {
1480
+ const output = coreContext.output;
1481
+ if (output !== void 0) {
1482
+ return output;
1483
+ }
1484
+ return await intelligentParseResponse(response);
1485
+ }
1486
+ if (!response) {
1487
+ throw new Exception([
1488
+ "Unable to process the response with '".concat(resolveWithMode, "'. Possible causes:"),
1489
+ "1. The request was never initiated or sent",
1490
+ "2. The request failed before a response was received."
1491
+ ].join("\n"));
1492
+ }
1493
+ return await resolveWith(response, resolveWithMode);
1494
+ })();
1431
1495
  }
1432
- if (!response) {
1433
- throw new Exception([
1434
- "Unable to process the response with '".concat(resolveWithMode, "'. Possible causes:"),
1435
- "1. The request was never initiated or sent",
1436
- "2. The request failed before a response was received."
1437
- ].join("\n"));
1496
+ return this.__requestPromise__;
1497
+ }
1498
+ fire() {
1499
+ if (!this.__triggered__) {
1500
+ this.__triggered__ = true;
1501
+ this.__execute__().then(this.__resolve__, this.__reject__);
1438
1502
  }
1439
- return await resolveWith(response, resolveWithMode);
1440
1503
  }
1441
- /**
1442
- * Attaches callbacks for the resolution and/or rejection of the Promise.
1443
- * @param onfulfilled The callback to execute when the Promise is resolved.
1444
- * @param onrejected The callback to execute when the Promise is rejected.
1445
- * @returns A Promise for the completion of which ever callback is executed.
1446
- */
1447
1504
  then(onfulfilled, onrejected) {
1448
- if (!this.requestPromise) this.requestPromise = this.end();
1449
- return this.requestPromise.then(onfulfilled, onrejected);
1450
- }
1451
- catch(onrejected) {
1452
- return this.end().catch(onrejected);
1453
- }
1454
- finally(onfinally) {
1455
- return this.end().finally(onfinally);
1505
+ if (!this.__triggered__) {
1506
+ this.__triggered__ = true;
1507
+ this.__execute__().then(this.__resolve__, this.__reject__);
1508
+ }
1509
+ return super.then(onfulfilled, onrejected);
1456
1510
  }
1457
1511
  };
1458
1512
 
@@ -1632,8 +1686,8 @@ var Keq = class extends Core {
1632
1686
  this.option("timeout", { millisecond: milliseconds });
1633
1687
  return this;
1634
1688
  }
1635
- resolveWith(m2) {
1636
- this.option("resolveWith", m2);
1689
+ resolveWith(m) {
1690
+ this.option("resolveWith", m);
1637
1691
  return this;
1638
1692
  }
1639
1693
  };
@@ -1676,10 +1730,11 @@ function keqModuleRoute(moduleName) {
1676
1730
  }
1677
1731
 
1678
1732
  // src/router/keq-pathname-route.ts
1679
- var m = __toESM(require("minimatch"));
1733
+ var import_picomatch = __toESM(require("picomatch"));
1680
1734
  function keqPathnameRoute(pathname) {
1735
+ const isMatch = (0, import_picomatch.default)(pathname);
1681
1736
  return (ctx) => {
1682
- return m.minimatch(ctx.request.url.pathname, pathname);
1737
+ return isMatch(ctx.request.url.pathname);
1683
1738
  };
1684
1739
  }
1685
1740