Skip to content

Commit

Permalink
add example esm app, copy dir to fix broken import
Browse files Browse the repository at this point in the history
  • Loading branch information
kreddlear committed Feb 20, 2025
1 parent 49d9a50 commit da76631
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 1 deletion.
62 changes: 62 additions & 0 deletions example-apps/esmodule/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# environment variables file
.env
.environment

# next.js build output
.next
19 changes: 19 additions & 0 deletions example-apps/esmodule/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fs from 'fs';
import zapier from 'zapier-platform-core';

import recipeTrigger from './triggers/recipe.js';

const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8'));

export default {
// This is just shorthand to reference the installed dependencies you have.
// Zapier will need to know these before we can upload.
version: packageJson.version,
platformVersion: zapier.version,

triggers: {
[recipeTrigger.key]: recipeTrigger,
},
creates: {},
searches: {},
};
17 changes: 17 additions & 0 deletions example-apps/esmodule/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "esmodule-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest --testTimeout 10000"
},
"dependencies": {
"openapi-fetch": "^0.10.5",
"zapier-platform-core": "16.1.0"
},
"devDependencies": {
"jest": "^29.7.0"
},
"type": "module"
}
7 changes: 7 additions & 0 deletions example-apps/esmodule/test/example.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* globals describe, it, expect */

describe('addition ', () => {
it('should work', () => {
expect(1 + 1).toEqual(2);
});
});
26 changes: 26 additions & 0 deletions example-apps/esmodule/test/triggers/recipe.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* globals describe, expect, test */

import zapier from 'zapier-platform-core';

import App from '../../index.js';

const appTester = zapier.createAppTester(App);
zapier.tools.env.inject();

describe('trigger recipe', () => {
test('basic case', async () => {
const bundle = {};
const results = await appTester(
App.triggers.recipe.operation.perform,
bundle,
);

expect(results.length).toBe(2);

expect(results[0].id).toBe(1);
expect(results[0].url).toBe('https://httpbin.zapier-tooling.com/get');

expect(results[1].id).toBe(2);
expect(results[1].slideshow.author).toBe('Yours Truly');
});
});
31 changes: 31 additions & 0 deletions example-apps/esmodule/triggers/recipe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import createClient from 'openapi-fetch';

const client = createClient({ baseUrl: 'https://httpbin.zapier-tooling.com' });

const perform = async (z, bundle) => {
const response1 = await z.request('https://httpbin.zapier-tooling.com/get');
const data1 = await response1.json();
data1.id = 1; // just to pass the check

const response2 = await client.GET('/json');
const data2 = response2.data;
data2.id = 2;

return [data1, data2];
};

export default {
key: 'recipe',
noun: 'Recipe',
display: {
label: 'New Recipe',
description: 'Triggers when a new recipe is created.',
},
operation: {
perform,
inputFields: [],
sample: {
id: 1,
},
},
};
7 changes: 7 additions & 0 deletions packages/cli/src/utils/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ const writeZipFromPaths = (dir, zipPath, paths) => {
const makeZip = async (dir, zipPath, disableDependencyDetection) => {
const entryPoints = [
path.resolve(dir, 'zapierwrapper.js'),
// TODO do not hardcode index.js here!
path.resolve(dir, 'index.js'),
];

Expand Down Expand Up @@ -399,6 +400,12 @@ const _buildFunc = async ({
zapierWrapperBuf.toString(),
);

// copy root directory to node_modules, excluding root directory itself
// this allows the import in _appCommandZapierWrapper to succeed
await copyDir(wdir, path.join(tmpDir, 'node_modules', path.basename(wdir)), {
filter: (src) => src !== wdir,
});

if (printProgress) {
endSpinner();
startSpinner('Building app definition.json');
Expand Down
2 changes: 1 addition & 1 deletion packages/core/include/zapierwrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
import path from 'node:path';
import zapier from 'zapier-platform-core';

const appPath = path.resolve(process.cwd(), 'index.js');
const appPath = path.basename(process.cwd());

export const handler = zapier.createAppHandler(appPath);

0 comments on commit da76631

Please sign in to comment.