Skip to content

Commit

Permalink
PDE-5540 Add support for providing the cache expiration for hydrators
Browse files Browse the repository at this point in the history
  • Loading branch information
kola-er committed Nov 19, 2024
1 parent ff3d882 commit 049f125
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
12 changes: 9 additions & 3 deletions packages/core/src/tools/create-dehydrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
};

Expand Down
15 changes: 15 additions & 0 deletions packages/core/test/hydration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
(() => {
Expand Down Expand Up @@ -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) };
(() => {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/types/zapier.custom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 049f125

Please sign in to comment.