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

chore(cli): Update TypeScript Example & Template #845

Merged
merged 13 commits into from
Oct 10, 2024
Merged
8 changes: 4 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [16.x, 18.x]
node-version: [18.x, 20.x]

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -83,7 +83,7 @@ jobs:
- ubuntu-latest
# TODO: Fix tests on Windows
# - windows-latest
node-version: [16.x, 18.x]
node-version: [18.x, 20.x]

steps:
- uses: actions/checkout@v3
Expand All @@ -108,7 +108,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [16.x, 18.x]
node-version: [18.x, 20.x]

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -142,7 +142,7 @@ jobs:
- ubuntu-latest
# TODO: Fix tests on Windows
# - windows-latest
node-version: [16.x, 18.x]
node-version: [18.x, 20.x]

steps:
- uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions example-apps/typescript/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ bower_components

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

# Dependency directories
node_modules/
Expand Down
18 changes: 0 additions & 18 deletions example-apps/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,3 @@ zapier push
```

Find out more on the latest docs: https://github.com/zapier/zapier-platform/blob/main/packages/cli/README.md.

# The "typescript" Template

This example is mainly a proof-of-concept for using features not yet available
in Node.

In `package.json`, we've define some commonly-used scripts:

```bash
# Watch and compile as you edit code
npm run watch

# There's also a non-watch compile command
npm run build

# To push to Zapier, make sure you compile first
npm run build && zapier push
```
2 changes: 1 addition & 1 deletion example-apps/typescript/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./lib').default;
module.exports = require('./dist').default;
16 changes: 6 additions & 10 deletions example-apps/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@
"name": "typescript",
"version": "1.0.0",
"description": "",
"main": "index.js",
"main": "dist/index.js",
"scripts": {
"test": "npm run build && jest --testTimeout 10000 --rootDir ./lib/test",
"test": "vitest",
"build": "npm run clean && tsc",
"clean": "rimraf ./lib ./build",
"watch": "npm run clean && tsc --watch",
"clean": "rimraf ./dist ./build",
"_zapier-build": "npm run build"
},
"dependencies": {
"zapier-platform-core": "15.12.0"
},
"devDependencies": {
"jest": "^25.5.3",
tkcranny marked this conversation as resolved.
Show resolved Hide resolved
"@types/jest": "^25.2.1",
"@types/node": "^13.13.5",
"@types/node-fetch": "^2.6.11",
"rimraf": "^3.0.2",
"typescript": "^5.5.3"
"rimraf": "^5.0.10",
"typescript": "^5.5.4",
"vitest": "^2.0.5"
},
"private": true
}
11 changes: 3 additions & 8 deletions example-apps/typescript/src/creates/movie.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { Bundle, ZObject } from 'zapier-platform-core';
import { Create, PerformFunction } from 'zapier-platform-core';

// You can optionally add add the shape of the inputData in bundle, which will pass that
// info down into the function and tests
const perform = async (
z: ZObject,
bundle: Bundle<{ title: string; year: number }>
) => {
const perform: PerformFunction = async (z, bundle) => {
const response = await z.request({
method: 'POST',
url: 'https://auth-json-server.zapier-staging.com/movies',
Expand Down Expand Up @@ -37,4 +32,4 @@ export default {
title: 'example',
},
},
};
} satisfies Create;
14 changes: 5 additions & 9 deletions example-apps/typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import { Bundle, HttpRequestOptions, ZObject } from 'zapier-platform-core';
import type { App, BeforeRequestMiddleware } from 'zapier-platform-core';

import MovieCreate from './creates/movie';
import MovieTrigger from './triggers/movie';
import { version as platformVersion } from 'zapier-platform-core';

const { version } = require('../package.json');
import packageJson from '../package.json';

const addApiKeyHeader = (
req: HttpRequestOptions,
z: ZObject,
bundle: Bundle
) => {
const addApiKeyHeader: BeforeRequestMiddleware = (req, z, bundle) => {
// Hard-coded api key just to demo. DON'T do auth like this for your production app!
req.headers = req.headers || {};
req.headers['X-Api-Key'] = 'secret';
return req;
};

export default {
version,
version: packageJson.version,
platformVersion,

beforeRequest: [addApiKeyHeader],
Expand All @@ -30,4 +26,4 @@ export default {
creates: {
[MovieCreate.key]: MovieCreate,
},
};
} satisfies App;
3 changes: 1 addition & 2 deletions example-apps/typescript/src/test/creates.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* globals describe, expect, test */

import { createAppTester, tools } from 'zapier-platform-core';
import { describe, test, expect } from 'vitest';

import App from '../index';

Expand Down
5 changes: 2 additions & 3 deletions example-apps/typescript/src/test/triggers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* globals describe, expect, test */

import { Bundle, createAppTester, tools } from 'zapier-platform-core';
import { createAppTester, tools } from 'zapier-platform-core';
import { describe, test, expect } from 'vitest';

import App from '../index';

Expand Down
7 changes: 4 additions & 3 deletions example-apps/typescript/src/triggers/movie.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bundle, ZObject } from 'zapier-platform-core';
import { PerformFunction, Trigger } from 'zapier-platform-core';

const perform = async (z: ZObject, bundle: Bundle) => {
const perform: PerformFunction = async (z, bundle) => {
const response = await z.request(
'https://auth-json-server.zapier-staging.com/movies'
);
Expand All @@ -17,10 +17,11 @@ export default {
},

operation: {
type: 'polling',
perform,
sample: {
id: '1',
title: 'example',
},
},
};
} satisfies Trigger;
18 changes: 11 additions & 7 deletions example-apps/typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "commonjs",
"moduleResolution": "node",
"lib": ["esnext"],
"outDir": "./lib",
"module": "CommonJS",
"moduleResolution": "Node",
"resolveJsonModule": true,
"esModuleInterop": true,
"isolatedModules": true,
"skipLibCheck": true,
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"skipLibCheck": true
}
"strict": true
},
"include": ["./src/**/*.ts"],
"exclude": ["./**/*.test.ts"]
}
60 changes: 49 additions & 11 deletions packages/cli/src/generators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@ const writeGenericPackageJson = (gen, packageJsonExtension) => {
);
};

const writeTypeScriptPackageJson = (gen, packageJsonExtension) => {
gen.fs.writeJSON(
gen.destinationPath('package.json'),
merge(
{
name: gen.options.packageName,
version: '1.0.0',
description: '',
main: 'src/index.js',
scripts: {
test: 'vitest',
},
dependencies: {
[PLATFORM_PACKAGE]: PACKAGE_VERSION,
},
devDependencies: {
vitest: '^2.1.2',
},
private: true,
},
packageJsonExtension
)
);
};

const writeGenericIndex = (gen, hasAuth) => {
gen.fs.copyTpl(
gen.templatePath('index.template.js'),
Expand Down Expand Up @@ -116,7 +141,6 @@ const writeForMinimalTemplate = (gen) => {
// example directory
const writeForStandaloneTemplate = (gen) => {
writeGitignore(gen);

writeGenericReadme(gen);
appendReadme(gen);

Expand All @@ -128,24 +152,38 @@ const writeForStandaloneTemplate = (gen) => {
'form-data': '4.0.0',
},
},
}[gen.options.template];

writeGenericPackageJson(gen, packageJsonExtension);

gen.fs.copy(
gen.templatePath(gen.options.template, '**', '*.{js,json,ts}'),
gen.destinationPath()
);
};

const writeForStandaloneTypeScriptTemplate = (gen) => {
writeGitignore(gen);
writeGenericReadme(gen);
appendReadme(gen);

const packageJsonExtension = {
typescript: {
scripts: {
test: 'vitest',
tkcranny marked this conversation as resolved.
Show resolved Hide resolved
clean: 'rimraf ./dist ./build',
build: 'npm run clean && tsc',
clean: 'rimraf ./lib ./build',
watch: 'npm run clean && tsc --watch',
test: 'npm run build && jest --testTimeout 10000 --rootDir ./lib/test',
'_zapier-build': 'npm run build',
},
devDependencies: {
'@types/jest': '^26.0.23',
'@types/node': '^14',
'@types/node-fetch': '^2.6.11',
rimraf: '^3.0.2',
typescript: '5.5.3',
rimraf: '^5.0.10',
typescript: '5.6.2',
vitest: '^2.1.2',
},
},
}[gen.options.template];

writeGenericPackageJson(gen, packageJsonExtension);
writeTypeScriptPackageJson(gen, packageJsonExtension);

gen.fs.copy(
gen.templatePath(gen.options.template, '**', '*.{js,json,ts}'),
Expand All @@ -165,7 +203,7 @@ const TEMPLATE_ROUTES = {
oauth2: writeForAuthTemplate,
'search-or-create': writeForStandaloneTemplate,
'session-auth': writeForAuthTemplate,
typescript: writeForStandaloneTemplate,
typescript: writeForStandaloneTypeScriptTemplate,
};

const TEMPLATE_CHOICES = Object.keys(TEMPLATE_ROUTES);
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/generators/templates/gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ bower_components

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

# Dependency directories
node_modules/
Expand Down
18 changes: 2 additions & 16 deletions packages/cli/src/generators/templates/typescript/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# The "typescript" Template
# TypeScript Template

This example is mainly a proof-of-concept for using features not yet available
in Node.

In `package.json`, we've define some commonly-used scripts:

```bash
# Watch and compile as you edit code
npm run watch

# There's also a non-watch compile command
npm run build

# To push to Zapier, make sure you compile first
npm run build && zapier push
```
An TypeScript template for Zapier Integrations.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./lib').default;
module.exports = require('./dist').default;
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { Bundle, ZObject } from 'zapier-platform-core';
import { Create, PerformFunction } from 'zapier-platform-core';

// You can optionally add add the shape of the inputData in bundle, which will pass that
// info down into the function and tests
const perform = async (
z: ZObject,
bundle: Bundle<{ title: string; year: number }>
) => {
const perform: PerformFunction = async (z, bundle) => {
const response = await z.request({
method: 'POST',
url: 'https://auth-json-server.zapier-staging.com/movies',
Expand Down Expand Up @@ -37,4 +32,4 @@ export default {
title: 'example',
},
},
};
} satisfies Create;
Loading
Loading