Skip to content

Commit

Permalink
feat(core): Allow relative paths via z.require() [PDE-5116] (#809)
Browse files Browse the repository at this point in the history
* Update z.require to support relative paths
  • Loading branch information
kreddlear authored Jun 26, 2024
1 parent 2a9a877 commit 0eb608b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
5 changes: 4 additions & 1 deletion packages/core/src/app-middlewares/before/z-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ const injectZObject = (input) => {
generateCallbackUrl: createCallbackHigherOrderFunction(input),
hash: hashing.hashify,
JSON: createJSONtool(),
require: (moduleName) => require(moduleName),
require: (moduleName) =>
require(require.resolve(moduleName, {
paths: module.paths.concat([process.cwd()]),
})),
stashFile: createFileStasher(input),
};

Expand Down
10 changes: 10 additions & 0 deletions packages/core/test/create-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,16 @@ describe('create-app', () => {
/Cannot find module 'non-existing-package'/
);
});

it('should be able to import from other paths using zRequire', async () => {
const input = createTestInput(
'resources.listrequire.list.operation.perform'
);
const appPass = createApp(appDefinition);

const { results } = await appPass(input);
results.should.eql('www.base-url.com');
});
});

describe('response.content parsing', () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/test/userapp/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const BASE_URL = 'www.base-url.com';

module.exports = {
BASE_URL,
};
23 changes: 20 additions & 3 deletions packages/core/test/userapp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ const List = {
},
};

const ListRequire = {
key: 'listrequire',
noun: 'List',
list: {
display: {
description: 'Trigger on new thing in list.',
label: 'List',
},
operation: {
perform: (z, bundle) => {
// in prod, process.cwd will return the app root directory
const { BASE_URL } = z.require('./test/userapp/constants.js');
return BASE_URL;
},
},
},
};

const Contact = {
key: 'contact',
noun: 'Contact',
Expand Down Expand Up @@ -386,9 +404,7 @@ const CachedCustomInputFields = {
description: 'Get/Set custom input fields in zcache',
},
operation: {
inputFields: [
helpers.getCustomFields,
],
inputFields: [helpers.getCustomFields],
perform: () => {},
},
},
Expand Down Expand Up @@ -582,6 +598,7 @@ const App = {
afterResponse: [],
resources: {
[List.key]: List,
[ListRequire.key]: ListRequire,
[Contact.key]: Contact,
[ContactError.key]: ContactError,
[ContactSource.key]: ContactSource,
Expand Down

0 comments on commit 0eb608b

Please sign in to comment.