@balena/pinejs 17.0.2 → 17.0.3-build-improve-result-typing-adec8e6cfb12d75109627a582c948d6f950d0989-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.
@@ -1541,7 +1541,8 @@ const runRequest = async (
1541
1541
  if (env.DEBUG) {
1542
1542
  log.log('Running', req.method, req.url);
1543
1543
  }
1544
- let result: Db.Result | number | undefined;
1544
+ let resultGet: Db.Result | undefined;
1545
+ let resultPost: number | undefined;
1545
1546
 
1546
1547
  try {
1547
1548
  try {
@@ -1550,18 +1551,18 @@ const runRequest = async (
1550
1551
 
1551
1552
  switch (request.method) {
1552
1553
  case 'GET':
1553
- result = await runGet(req, request, tx);
1554
+ resultGet = await runGet(req, request, tx);
1554
1555
  break;
1555
1556
  case 'POST':
1556
- result = await runPost(req, request, tx);
1557
+ resultPost = await runPost(req, request, tx);
1557
1558
  break;
1558
1559
  case 'PUT':
1559
1560
  case 'PATCH':
1560
1561
  case 'MERGE':
1561
- result = await runPut(req, request, tx);
1562
+ await runPut(req, request, tx);
1562
1563
  break;
1563
1564
  case 'DELETE':
1564
- result = await runDelete(req, request, tx);
1565
+ await runDelete(req, request, tx);
1565
1566
  break;
1566
1567
  }
1567
1568
  } catch (err: any) {
@@ -1587,7 +1588,12 @@ const runRequest = async (
1587
1588
  throw err;
1588
1589
  }
1589
1590
 
1590
- await runHooks('POSTRUN', request.hooks, { req, request, result, tx });
1591
+ await runHooks('POSTRUN', request.hooks, {
1592
+ req,
1593
+ request,
1594
+ result: resultGet ?? resultPost,
1595
+ tx,
1596
+ });
1591
1597
  } catch (err: any) {
1592
1598
  await runHooks('POSTRUN-ERROR', request.hooks, {
1593
1599
  req,
@@ -1597,7 +1603,23 @@ const runRequest = async (
1597
1603
  });
1598
1604
  throw err;
1599
1605
  }
1600
- return await prepareResponse(req, request, result, tx);
1606
+
1607
+ switch (request.method) {
1608
+ case 'GET':
1609
+ return await respondGet(req, request, resultGet, tx);
1610
+ case 'POST':
1611
+ return await respondPost(req, request, resultPost, tx);
1612
+ case 'PUT':
1613
+ case 'PATCH':
1614
+ case 'MERGE':
1615
+ return await respondPut(req, request, tx);
1616
+ case 'DELETE':
1617
+ return await respondDelete(req, request, tx);
1618
+ case 'OPTIONS':
1619
+ return await respondOptions(req, request, tx);
1620
+ default:
1621
+ throw new MethodNotAllowedError();
1622
+ }
1601
1623
  };
1602
1624
 
1603
1625
  const runChangeSet =
@@ -1645,30 +1667,6 @@ const updateBinds = (
1645
1667
  return request;
1646
1668
  };
1647
1669
 
1648
- const prepareResponse = async (
1649
- req: Express.Request,
1650
- request: uriParser.ODataRequest,
1651
- result: any,
1652
- tx: Db.Tx,
1653
- ): Promise<Response> => {
1654
- switch (request.method) {
1655
- case 'GET':
1656
- return await respondGet(req, request, result, tx);
1657
- case 'POST':
1658
- return await respondPost(req, request, result, tx);
1659
- case 'PUT':
1660
- case 'PATCH':
1661
- case 'MERGE':
1662
- return await respondPut(req, request, result, tx);
1663
- case 'DELETE':
1664
- return await respondDelete(req, request, result, tx);
1665
- case 'OPTIONS':
1666
- return await respondOptions(req, request, result, tx);
1667
- default:
1668
- throw new MethodNotAllowedError();
1669
- }
1670
- };
1671
-
1672
1670
  const checkReadOnlyRequests = (request: uriParser.ODataRequest) => {
1673
1671
  if (request.method !== 'GET') {
1674
1672
  // Only GET requests can be read-only
@@ -1770,11 +1768,17 @@ const runGet = async (
1770
1768
  const respondGet = async (
1771
1769
  req: Express.Request,
1772
1770
  request: uriParser.ODataRequest,
1773
- result: any,
1771
+ result: Db.Result | undefined,
1774
1772
  tx: Db.Tx,
1775
1773
  ): Promise<Response> => {
1776
1774
  const vocab = request.vocabulary;
1777
1775
  if (request.sqlQuery != null) {
1776
+ if (result == null) {
1777
+ // This shouldn't be able to happen because the result should only be null if there's no sqlQuery
1778
+ throw new Error(
1779
+ 'Null result passed to respond GET that has a sqlQuery defined',
1780
+ );
1781
+ }
1778
1782
  const format = request.odataQuery.options?.$format;
1779
1783
  const metadata =
1780
1784
  format != null && typeof format === 'object'
@@ -1839,7 +1843,7 @@ const runPost = async (
1839
1843
  const respondPost = async (
1840
1844
  req: Express.Request,
1841
1845
  request: uriParser.ODataRequest,
1842
- id: number,
1846
+ id: number | undefined,
1843
1847
  tx: Db.Tx,
1844
1848
  ): Promise<Response> => {
1845
1849
  const vocab = request.vocabulary;
@@ -1889,7 +1893,7 @@ const runPut = async (
1889
1893
  _req: Express.Request,
1890
1894
  request: uriParser.ODataRequest,
1891
1895
  tx: Db.Tx,
1892
- ): Promise<undefined> => {
1896
+ ): Promise<void> => {
1893
1897
  let rowsAffected: number;
1894
1898
  // If request.sqlQuery is an array it means it's an UPSERT, ie two queries: [InsertQuery, UpdateQuery]
1895
1899
  if (Array.isArray(request.sqlQuery)) {
@@ -1905,13 +1909,11 @@ const runPut = async (
1905
1909
  if (rowsAffected > 0) {
1906
1910
  await validateModel(tx, _.last(request.translateVersions)!, request);
1907
1911
  }
1908
- return undefined;
1909
1912
  };
1910
1913
 
1911
1914
  const respondPut = async (
1912
1915
  req: Express.Request,
1913
1916
  request: uriParser.ODataRequest,
1914
- result: any,
1915
1917
  tx: Db.Tx,
1916
1918
  ): Promise<Response> => {
1917
1919
  const response = {
@@ -1920,7 +1922,6 @@ const respondPut = async (
1920
1922
  await runHooks('PRERESPOND', request.hooks, {
1921
1923
  req,
1922
1924
  request,
1923
- result,
1924
1925
  response,
1925
1926
  tx,
1926
1927
  });
@@ -1933,13 +1934,11 @@ const runDelete = async (
1933
1934
  _req: Express.Request,
1934
1935
  request: uriParser.ODataRequest,
1935
1936
  tx: Db.Tx,
1936
- ): Promise<undefined> => {
1937
+ ): Promise<void> => {
1937
1938
  const { rowsAffected } = await runQuery(tx, request, undefined, true);
1938
1939
  if (rowsAffected > 0) {
1939
1940
  await validateModel(tx, _.last(request.translateVersions)!, request);
1940
1941
  }
1941
-
1942
- return undefined;
1943
1942
  };
1944
1943
 
1945
1944
  export const executeStandardModels = async (tx: Db.Tx): Promise<void> => {