From 049f12527c2936526372543d9aa82f39a9c5058b Mon Sep 17 00:00:00 2001
From: Kolawole Erinoso <kola.erinoso@gmail.com>
Date: Tue, 19 Nov 2024 18:59:18 +0100
Subject: [PATCH] PDE-5540 Add support for providing the cache expiration for
 hydrators

---
 packages/core/src/tools/create-dehydrator.js | 12 +++++++++---
 packages/core/test/hydration.js              | 15 +++++++++++++++
 packages/core/types/zapier.custom.d.ts       |  3 ++-
 3 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/packages/core/src/tools/create-dehydrator.js b/packages/core/src/tools/create-dehydrator.js
index 0eac42273..58c26536e 100644
--- a/packages/core/src/tools/create-dehydrator.js
+++ b/packages/core/src/tools/create-dehydrator.js
@@ -9,19 +9,25 @@ const wrapHydrate = require('./wrap-hydrate');
 const createDehydrator = (input, type = 'method') => {
   const app = _.get(input, '_zapier.app');
 
-  return (func, inputData) => {
+  return (func, inputData, cacheExpiration) => {
     inputData = inputData || {};
     if (inputData.inputData) {
       throw new DehydrateError(
         'Oops! You passed a full `bundle` - really you should pass what you want under `inputData`!'
       );
     }
-    return wrapHydrate({
+    const payload = {
       type,
       method: resolveMethodPath(app, func),
       // inputData vs. bundle is a legacy oddity
       bundle: _.omit(inputData, 'environment'), // don't leak the environment
-    });
+    };
+
+    if (cacheExpiration) {
+      payload.cacheExpiration = cacheExpiration;
+    }
+
+    return wrapHydrate(payload);
   };
 };
 
diff --git a/packages/core/test/hydration.js b/packages/core/test/hydration.js
index cec73dd34..4a37d712d 100644
--- a/packages/core/test/hydration.js
+++ b/packages/core/test/hydration.js
@@ -52,6 +52,13 @@ describe('hydration', () => {
       );
     });
 
+    it('should allow passing of cache expiration argument along in the dehydrated data', () => {
+      const result = dehydrate(funcToFind, {}, 60);
+      result.should.eql(
+        'hydrate|||{"type":"method","method":"some.path.to","bundle":{},"cacheExpiration":60}|||hydrate'
+      );
+    });
+
     it('should not accept payload size bigger than 12000 bytes.', () => {
       const inputData = { key: 'a'.repeat(12001) };
       (() => {
@@ -91,6 +98,14 @@ describe('hydration', () => {
       );
     });
 
+    it('should allow passing of cache expiration argument along in the dehydrated data', () => {
+      const inputData = { key: 'value' };
+      const result = dehydrateFile(funcToFind, inputData, 60);
+      result.should.eql(
+        'hydrate|||{"type":"file","method":"some.path.to","bundle":{"key":"value"},"cacheExpiration":60}|||hydrate'
+      );
+    });
+
     it('should not accept payload size bigger than 12000 bytes.', () => {
       const inputData = { key: 'a'.repeat(12001) };
       (() => {
diff --git a/packages/core/types/zapier.custom.d.ts b/packages/core/types/zapier.custom.d.ts
index fe23f4e89..1d9fd9e06 100644
--- a/packages/core/types/zapier.custom.d.ts
+++ b/packages/core/types/zapier.custom.d.ts
@@ -133,7 +133,8 @@ export interface RawHttpResponse<T = any> extends BaseHttpResponse {
 
 type DehydrateFunc = <T>(
   func: (z: ZObject, bundle: Bundle<T>) => any,
-  inputData: T
+  inputData?: T,
+  cacheExpiration?: number,
 ) => string;
 
 export interface ZObject {