Skip to content
This repository was archived by the owner on Mar 9, 2023. It is now read-only.

Commit 2a4d683

Browse files
authored
Merge pull request #1 from patch-technology/tate/add-init-proj
Tate/add init proj
2 parents b25afcf + 5373245 commit 2a4d683

File tree

16 files changed

+8162
-0
lines changed

16 files changed

+8162
-0
lines changed

.eslintrc.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
root: true,
3+
parser: "@typescript-eslint/parser",
4+
parserOptions: {
5+
project: "./tsconfig.json",
6+
},
7+
plugins: ["@typescript-eslint"],
8+
extends: [
9+
"eslint:recommended",
10+
"plugin:@typescript-eslint/recommended",
11+
"plugin:@typescript-eslint/recommended-requiring-type-checking",
12+
],
13+
rules: {
14+
"@typescript-eslint/prefer-nullish-coalescing": "error",
15+
"@typescript-eslint/no-unnecessary-condition": "error",
16+
"@typescript-eslint/prefer-optional-chain": "error",
17+
},
18+
};

.gitignore

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# output
15+
lib
16+
17+
# Directory for instrumented libs generated by jscoverage/JSCover
18+
lib-cov
19+
20+
# Coverage directory used by tools like istanbul
21+
coverage
22+
23+
# nyc test coverage
24+
.nyc_output
25+
26+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
27+
.grunt
28+
29+
# Bower dependency directory (https://bower.io/)
30+
bower_components
31+
32+
# node-waf configuration
33+
.lock-wscript
34+
35+
# Compiled binary addons (https://nodejs.org/api/addons.html)
36+
build/
37+
38+
# Dependency directories
39+
node_modules/
40+
jspm_packages/
41+
42+
# Typescript v1 declaration files
43+
typings/
44+
45+
# Optional npm cache directory
46+
.npm
47+
48+
# Optional eslint cache
49+
.eslintcache
50+
51+
# Optional REPL history
52+
.node_repl_history
53+
54+
# Output of 'npm pack'
55+
*.tgz
56+
57+
# Yarn Integrity file
58+
.yarn-integrity
59+
60+
# environment variables file
61+
.env
62+
.environment
63+
64+
# next.js build output
65+
.next

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
12.18.3

.zapierapprc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"id": 116761,
3+
"key": "App116761"
4+
}

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Patch Zapier App
2+
3+
## First time setup
4+
5+
### Dependencies
6+
7+
You'll need to have `yarn` installed.
8+
9+
All Zapier CLI apps are run using Node.js v12. The current node version for the project can be found in `.nvmrc`. You can use `nvm` to manage your node version, or the tool of your choice.
10+
11+
The rest of the project dependencies can be installed by running `yarn install`.
12+
13+
## Developer commands
14+
15+
There are a number of helpful scripts in `package.json`.
16+
17+
Run tests
18+
`yarn test`
19+
20+
Run typescript in watch mode:
21+
`yarn watch`
22+
23+
## Docs
24+
25+
[Zapier](https://github.com/zapier/zapier-platform/blob/master/packages/cli/README.md.)
26+
[The Zapier CLI Tutorial](https://platform.zapier.com/cli_tutorials/getting-started)is a good resource to understand Zapier app architecture and key concepts.
27+
28+
## Zapier CLI
29+
30+
The zapier cli can be run via `yarn zapier`. Some helpful recipes:
31+
32+
### Validate
33+
34+
Run Zapier validations:
35+
`yarn build && yarn zapier validate`
36+
37+
### Logs
38+
39+
All HTTP requests
40+
`yarn zapier logs --type=http --detailed`
41+
42+
Details log information:
43+
`yarn zapier logs --type=http --detailed`
44+
45+
### Manual deployment
46+
47+
`yarn build && yarn zapier push`

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib').default;

package.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"private": true,
3+
"name": "patch",
4+
"description": "",
5+
"version": "1.0.0",
6+
"main": "index.js",
7+
"scripts": {
8+
"build": "yarn run clean && yarn tsc",
9+
"clean": "rimraf ./lib ./build",
10+
"lint:fix": "yarn lint:package && yarn lint:ts --fix && prettier --write ./src && yarn typecheck",
11+
"lint:fix:md": "prettier --write *.md",
12+
"lint:package": "prettier-package-json --write ./package.json",
13+
"lint:ts": "eslint './src/**/*.ts'",
14+
"test": "yarn run build && jest --testTimeout 10000 --rootDir ./lib/test",
15+
"typecheck": "yarn tsc --noEmit",
16+
"watch": "yarn run clean && yarn tsc --watch"
17+
},
18+
"dependencies": {
19+
"zapier-platform-core": "10.1.1"
20+
},
21+
"devDependencies": {
22+
"@types/jest": "^25.2.1",
23+
"@types/node": "^13.13.5",
24+
"@typescript-eslint/eslint-plugin": "^4.1.0",
25+
"@typescript-eslint/parser": "^4.1.0",
26+
"eslint": "^7.8.1",
27+
"husky": "^4.3.0",
28+
"jest": "^25.5.3",
29+
"lint-staged": "^10.3.0",
30+
"prettier": "^2.1.1",
31+
"prettier-package-json": "^2.1.3",
32+
"rimraf": "^3.0.2",
33+
"typescript": "^4.0.2",
34+
"zapier-platform-cli": "^10.1.1"
35+
},
36+
"husky": {
37+
"hooks": {
38+
"pre-commit": "lint-staged"
39+
}
40+
},
41+
"lint-staged": {
42+
"package.json": "yarn lint:package",
43+
"*.md": "yarn lint:fix:md",
44+
"*.{ts,tsx}": "yarn lint:fix"
45+
}
46+
}

src/creates/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./order";

src/creates/order.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Bundle, ZObject } from "zapier-platform-core";
2+
import { ZapierCreate, PatchResponse } from "../types";
3+
4+
type CreateData = {
5+
mass_g: number;
6+
project_id?: string;
7+
metadata?: Record<string, string>;
8+
};
9+
10+
const perform = async (
11+
z: ZObject,
12+
bundle: Bundle<CreateData>
13+
): Promise<unknown> => {
14+
const { mass_g, project_id, metadata } = bundle.inputData;
15+
const { data } = await z.request({
16+
method: "POST",
17+
url: "https://api.usepatch.com/v1/orders",
18+
body: { mass_g, project_id, metadata },
19+
});
20+
return (data as PatchResponse).data;
21+
};
22+
23+
export const OrderCreate: ZapierCreate<CreateData> = {
24+
key: "order",
25+
noun: "order",
26+
display: {
27+
label: "Create Order",
28+
description: "Creates a new order.",
29+
},
30+
operation: {
31+
perform,
32+
inputFields: [
33+
{ key: "mass_g", label: "Mass (grams)", required: true, type: "integer" },
34+
// will call the trigger with a key of project
35+
{
36+
key: "project_id",
37+
label: "Project",
38+
dynamic: "project.id.name",
39+
},
40+
{ key: "metadata", dict: true },
41+
],
42+
sample: {
43+
mass_g: 100,
44+
project_id: "pro_test_1234",
45+
metadata: { user: "john doe" },
46+
},
47+
},
48+
};

src/index.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Bundle, HttpRequestOptions, ZObject } from "zapier-platform-core";
2+
import { OrderTrigger, ProjectTrigger } from "./triggers";
3+
import { OrderCreate } from "./creates";
4+
import { version as platformVersion } from "zapier-platform-core";
5+
const { version } = require("../package.json"); // eslint-disable-line
6+
7+
const addApiKeyHeader = (
8+
req: HttpRequestOptions,
9+
_z: ZObject,
10+
bundle: Bundle
11+
): HttpRequestOptions => {
12+
req.headers ??= {};
13+
req.headers["Authorization"] = `Bearer ${bundle.authData.apiKey}`;
14+
return req;
15+
};
16+
17+
export default {
18+
version, // eslint-disable-line
19+
platformVersion,
20+
beforeRequest: [addApiKeyHeader],
21+
authentication: {
22+
type: "custom",
23+
fields: [{ key: "apiKey", type: "string" }],
24+
test: async (z: ZObject): Promise<void> => {
25+
const response = await z.request("https://api.usepatch.com/v1/orders", {
26+
json: { page: 1 },
27+
});
28+
if (response.status !== 200) {
29+
throw new Error("Invalid API Key");
30+
}
31+
},
32+
},
33+
34+
triggers: {
35+
[ProjectTrigger.key]: ProjectTrigger,
36+
[OrderTrigger.key]: OrderTrigger,
37+
},
38+
39+
creates: {
40+
[OrderCreate.key]: OrderCreate,
41+
},
42+
};

0 commit comments

Comments
 (0)