Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Add support for providing the cache expiration for hydrators #918

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading