@balena/pinejs 17.1.0-build-joshbwlng-tasks-0c116dc98102e3ddd4aa6e8a4f350a1e18891e1a-1 → 17.1.0-build-joshbwlng-tasks-61ce10e444abec6afea3fec43e9a5c37c7cedea6-1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1546,7 +1546,8 @@ const runRequest = async (
1546
1546
  if (env.DEBUG) {
1547
1547
  log.log('Running', req.method, req.url);
1548
1548
  }
1549
- let result: Db.Result | number | undefined;
1549
+ let resultGet: Db.Result | undefined;
1550
+ let resultPost: number | undefined;
1550
1551
 
1551
1552
  try {
1552
1553
  try {
@@ -1555,18 +1556,18 @@ const runRequest = async (
1555
1556
 
1556
1557
  switch (request.method) {
1557
1558
  case 'GET':
1558
- result = await runGet(req, request, tx);
1559
+ resultGet = await runGet(req, request, tx);
1559
1560
  break;
1560
1561
  case 'POST':
1561
- result = await runPost(req, request, tx);
1562
+ resultPost = await runPost(req, request, tx);
1562
1563
  break;
1563
1564
  case 'PUT':
1564
1565
  case 'PATCH':
1565
1566
  case 'MERGE':
1566
- result = await runPut(req, request, tx);
1567
+ await runPut(req, request, tx);
1567
1568
  break;
1568
1569
  case 'DELETE':
1569
- result = await runDelete(req, request, tx);
1570
+ await runDelete(req, request, tx);
1570
1571
  break;
1571
1572
  }
1572
1573
  } catch (err: any) {
@@ -1592,7 +1593,12 @@ const runRequest = async (
1592
1593
  throw err;
1593
1594
  }
1594
1595
 
1595
- await runHooks('POSTRUN', request.hooks, { req, request, result, tx });
1596
+ await runHooks('POSTRUN', request.hooks, {
1597
+ req,
1598
+ request,
1599
+ result: resultGet ?? resultPost,
1600
+ tx,
1601
+ });
1596
1602
  } catch (err: any) {
1597
1603
  await runHooks('POSTRUN-ERROR', request.hooks, {
1598
1604
  req,
@@ -1602,7 +1608,23 @@ const runRequest = async (
1602
1608
  });
1603
1609
  throw err;
1604
1610
  }
1605
- return await prepareResponse(req, request, result, tx);
1611
+
1612
+ switch (request.method) {
1613
+ case 'GET':
1614
+ return await respondGet(req, request, resultGet, tx);
1615
+ case 'POST':
1616
+ return await respondPost(req, request, resultPost, tx);
1617
+ case 'PUT':
1618
+ case 'PATCH':
1619
+ case 'MERGE':
1620
+ return await respondPut(req, request, tx);
1621
+ case 'DELETE':
1622
+ return await respondDelete(req, request, tx);
1623
+ case 'OPTIONS':
1624
+ return await respondOptions(req, request, tx);
1625
+ default:
1626
+ throw new MethodNotAllowedError();
1627
+ }
1606
1628
  };
1607
1629
 
1608
1630
  const runChangeSet =
@@ -1650,30 +1672,6 @@ const updateBinds = (
1650
1672
  return request;
1651
1673
  };
1652
1674
 
1653
- const prepareResponse = async (
1654
- req: Express.Request,
1655
- request: uriParser.ODataRequest,
1656
- result: any,
1657
- tx: Db.Tx,
1658
- ): Promise<Response> => {
1659
- switch (request.method) {
1660
- case 'GET':
1661
- return await respondGet(req, request, result, tx);
1662
- case 'POST':
1663
- return await respondPost(req, request, result, tx);
1664
- case 'PUT':
1665
- case 'PATCH':
1666
- case 'MERGE':
1667
- return await respondPut(req, request, result, tx);
1668
- case 'DELETE':
1669
- return await respondDelete(req, request, result, tx);
1670
- case 'OPTIONS':
1671
- return await respondOptions(req, request, result, tx);
1672
- default:
1673
- throw new MethodNotAllowedError();
1674
- }
1675
- };
1676
-
1677
1675
  const checkReadOnlyRequests = (request: uriParser.ODataRequest) => {
1678
1676
  if (request.method !== 'GET') {
1679
1677
  // Only GET requests can be read-only
@@ -1775,11 +1773,17 @@ const runGet = async (
1775
1773
  const respondGet = async (
1776
1774
  req: Express.Request,
1777
1775
  request: uriParser.ODataRequest,
1778
- result: any,
1776
+ result: Db.Result | undefined,
1779
1777
  tx: Db.Tx,
1780
1778
  ): Promise<Response> => {
1781
1779
  const vocab = request.vocabulary;
1782
1780
  if (request.sqlQuery != null) {
1781
+ if (result == null) {
1782
+ // This shouldn't be able to happen because the result should only be null if there's no sqlQuery
1783
+ throw new Error(
1784
+ 'Null result passed to respond GET that has a sqlQuery defined',
1785
+ );
1786
+ }
1783
1787
  const format = request.odataQuery.options?.$format;
1784
1788
  const metadata =
1785
1789
  format != null && typeof format === 'object'
@@ -1844,7 +1848,7 @@ const runPost = async (
1844
1848
  const respondPost = async (
1845
1849
  req: Express.Request,
1846
1850
  request: uriParser.ODataRequest,
1847
- id: number,
1851
+ id: number | undefined,
1848
1852
  tx: Db.Tx,
1849
1853
  ): Promise<Response> => {
1850
1854
  const vocab = request.vocabulary;
@@ -1894,7 +1898,7 @@ const runPut = async (
1894
1898
  _req: Express.Request,
1895
1899
  request: uriParser.ODataRequest,
1896
1900
  tx: Db.Tx,
1897
- ): Promise<undefined> => {
1901
+ ): Promise<void> => {
1898
1902
  let rowsAffected: number;
1899
1903
  // If request.sqlQuery is an array it means it's an UPSERT, ie two queries: [InsertQuery, UpdateQuery]
1900
1904
  if (Array.isArray(request.sqlQuery)) {
@@ -1910,13 +1914,11 @@ const runPut = async (
1910
1914
  if (rowsAffected > 0) {
1911
1915
  await validateModel(tx, _.last(request.translateVersions)!, request);
1912
1916
  }
1913
- return undefined;
1914
1917
  };
1915
1918
 
1916
1919
  const respondPut = async (
1917
1920
  req: Express.Request,
1918
1921
  request: uriParser.ODataRequest,
1919
- result: any,
1920
1922
  tx: Db.Tx,
1921
1923
  ): Promise<Response> => {
1922
1924
  const response = {
@@ -1925,7 +1927,6 @@ const respondPut = async (
1925
1927
  await runHooks('PRERESPOND', request.hooks, {
1926
1928
  req,
1927
1929
  request,
1928
- result,
1929
1930
  response,
1930
1931
  tx,
1931
1932
  });
@@ -1938,13 +1939,11 @@ const runDelete = async (
1938
1939
  _req: Express.Request,
1939
1940
  request: uriParser.ODataRequest,
1940
1941
  tx: Db.Tx,
1941
- ): Promise<undefined> => {
1942
+ ): Promise<void> => {
1942
1943
  const { rowsAffected } = await runQuery(tx, request, undefined, true);
1943
1944
  if (rowsAffected > 0) {
1944
1945
  await validateModel(tx, _.last(request.translateVersions)!, request);
1945
1946
  }
1946
-
1947
- return undefined;
1948
1947
  };
1949
1948
 
1950
1949
  export const executeStandardModels = async (tx: Db.Tx): Promise<void> => {