]> git.ardis.lu - envpass/commitdiff
Refactor to isolate `filterNullValues` logic
authorArdis Lu <ardisglu@gmail.com>
Sun, 24 May 2026 06:06:42 +0000 (23:06 -0700)
committerArdis Lu <ardisglu@gmail.com>
Sun, 24 May 2026 06:06:42 +0000 (23:06 -0700)
This change allows a more precise type annotation to assert that there is no null value in the resulting object.

test/getPrf.test.js

index 8260ccc002e16fbb934ead9e25855ea6d3b6418d..6f02b5ea7bc0a40b87b9c140c99393e4220c59ea 100644 (file)
@@ -6,6 +6,18 @@ import { request } from 'undici';
 import { WINDOW_EXPIRATION_DURATION } from '#src/getPrf.js';
 import { setupPlaywright, setupServer } from '#test/setup.js';
 
+/**
+ * Return a new object where all properties in the object with the value `null` are *deleted*.
+ * @template T
+ * @param {Record<string,T|null>} obj An object that may have properties with a `null` value.
+ * @returns {Record<string,NonNullable<T>>}
+ */
+function filterNullValues(obj) {
+  return /** @type {Record<string,NonNullable<T>>} */(
+    Object.fromEntries(Object.entries(obj).filter(([, v]) => v !== null))
+  );
+}
+
 /**
  * @typedef {Object} MakePostRequestOptions
  * @property {Record<string,string|null>} [headers] Headers to set on the request. Set a property to
@@ -34,8 +46,8 @@ function makePostRequest(url, options = {}) {
     'Sec-Fetch-Dest': 'empty',
     ...headers
   }
-  const filteredHeaders = Object.fromEntries(Object.entries(mergedHeaders).filter(([, v]) => v !== null));
-  const body = Object.fromEntries(Object.entries({ challenge, prf }).filter(([, v]) => v !== null));
+  const filteredHeaders = filterNullValues(mergedHeaders);
+  const body = filterNullValues({ challenge, prf });
   return new Request(origin, {
     method: 'POST',
     headers: filteredHeaders,
This page took 0.038301 seconds and 4 git commands to generate.