Skip to content

Commit d6f6823

Browse files
mattvagnijordrakeandrewblaney
authored
Generate types for webhooks (#103)
* first commit * Generate zod model * Fix ajv * Remove console.log * Remove unused packages * Move some deps to dev deps * Fix build to support importing JSON * Add generated file to prettierignore * Drop commented out script * add isStartOfThread --------- Co-authored-by: Jordan Drake <jordan@plain.com> Co-authored-by: Andrew Blaney <andrew@plain.com>
1 parent 13fe372 commit d6f6823

20 files changed

+5182
-131
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ src/graphql/types.ts
99
CHANGELOG.md
1010
package-lock.json
1111
package.json
12+
src/webhooks/webhook-schema.ts

README.md

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
[Changelog]('./CHANGELOG.md')
44

5+
## Plain Client
6+
57
This is the typescript/node SDK for Plain.com's Core GraphQL API. It makes it easy to make common API calls in just a few lines of code.
68

79
If you run into any issues please open an issue or get in touch with us at <help@plain.com>.
810

9-
## Basic example
11+
#### Basic example
1012

1113
```ts
1214
import { PlainClient } from '@team-plain/typescript-sdk';
@@ -26,7 +28,7 @@ if (result.error) {
2628

2729
You can find out how to make an API key in our documentation: <https://docs.plain.com/core-api/authentication>
2830

29-
## Documentation
31+
#### Documentation
3032

3133
Every method in the SDK corresponds to a graphql [query](./src/graphql/queries/) or [mutation](./src/graphql/mutations/).
3234

@@ -36,7 +38,7 @@ You can find the generated documentation here:
3638

3739
If you would like to add a query or mutation please open an issue and we can add it for you.
3840

39-
## Error handling
41+
#### Error handling
4042

4143
Every SDK method will return an object with either data or an error.
4244

@@ -62,7 +64,7 @@ function doThing() {
6264

6365
An error can be **one of** the below:
6466

65-
### MutationError
67+
###### MutationError
6668

6769
[(view source)](./src/error.ts)
6870
This is the richest error type. It is called `MutationError` since it maps to the `MutationError` type in our GraphQL schema and is returned as part of every mutation in our API.
@@ -80,28 +82,43 @@ Every mutation error will contain:
8082
type: one of `VALIDATION`, `REQUIRED`, `NOT_FOUND`. See [Error codes
8183
](https://www.plain.com/docs/graphql/error-codes) in our docs for a description of each value.
8284

83-
### BadRequestError
85+
###### BadRequestError
8486

8587
[(view source)](./src/error.ts)
8688
Equivalent to a 400 response. If you are using typescript it's unlikely you will run into this since types will prevent this but if you are using javascript this likely means you are providing a wrong input/argument to a query or mutation.
8789

88-
### ForbiddenError
90+
###### ForbiddenError
8991

9092
[(view source)](./src/error.ts)
9193
Equivalent to a 401 or 403 response. Normally means your API key doesn't exist or that you are trying to query something that you do not have permissions for.
9294

93-
### InternalServerError
95+
###### InternalServerError
9496

9597
[(view source)](./src/error.ts)
9698
Equivalent to a 500 response. If this happens something unexpected within Plain happened.
9799

98-
### UnknownError
100+
###### UnknownError
99101

100102
[(view source)](./src/error.ts)
101103
Fallback error type when something unexpected happens.
102104

105+
## Webhooks
106+
107+
This package also provides functionality to validate our [Webhook payloads](https://www.plain.com/docs/api-reference/webhooks).
108+
109+
```ts
110+
import { parsePlainWebhook } from '@team-plain/typescript-sdk';
111+
112+
const payload = { ... };
113+
114+
if(parsePlainWebhook(payload)) {
115+
// payload is now typed!
116+
doYourThing(payload);
117+
}
118+
```
119+
103120
## Contributing
104121

105122
When submitting a PR, remember to run `pnpm changeset` and provide an easy to understand description of the changes you're making so that the changelog is populated.
106123

107-
When a PR with a changelog is merged a seperate PR will be automatically raised which rolls up any merged changes, handles assigning a new version for release and publishing to NPM.
124+
When a PR with a changelog is merged a separate PR will be automatically raised which rolls up any merged changes and handles assigning a new version for release and publishing to NPM.

package.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"scripts": {
99
"build": "rm -rf dist && rollup -c",
1010
"build:docs": "typedoc --plugin typedoc-plugin-missing-exports src/index.ts",
11-
"codegen": "graphql-codegen",
11+
"codegen": "pnpm run codegen:graphql && pnpm run codegen:webhooks",
12+
"codegen:graphql": "graphql-codegen",
13+
"codegen:webhooks": "sh ./scripts/codegen-webhooks.sh",
1214
"typecheck": "tsc --noEmit",
1315
"lint": "pnpm run lint:eslint && pnpm run lint:prettier",
1416
"lint:eslint": "eslint 'src/**/*.ts'",
@@ -26,24 +28,31 @@
2628
"@graphql-codegen/typescript": "^3.0.4",
2729
"@graphql-codegen/typescript-document-nodes": "^3.0.4",
2830
"@graphql-codegen/typescript-operations": "^3.0.4",
31+
"@rollup/plugin-json": "^6.1.0",
2932
"@typescript-eslint/eslint-plugin": "^5.59.2",
3033
"@typescript-eslint/parser": "^5.59.2",
3134
"esbuild": "^0.17.18",
3235
"eslint": "^8.40.0",
3336
"eslint-plugin-import": "^2.27.5",
3437
"eslint-plugin-simple-import-sort": "^10.0.0",
3538
"eslint-plugin-unused-imports": "^2.0.0",
39+
"json-refs": "^3.0.15",
40+
"json-schema-to-typescript": "^13.1.2",
3641
"rollup": "^3.21.5",
3742
"rollup-plugin-dts": "^5.3.0",
3843
"rollup-plugin-esbuild": "^5.0.0",
44+
"ts-to-zod": "^3.7.3",
3945
"typedoc": "^0.24.7",
4046
"typedoc-plugin-missing-exports": "^2.0.0",
41-
"typescript": "^5.1.6",
47+
"typescript": "5.0.4",
4248
"vitest": "^0.31.0"
4349
},
4450
"dependencies": {
4551
"@graphql-typed-document-node/core": "^3.2.0",
52+
"ajv": "^8.12.0",
53+
"ajv-formats": "^2.1.1",
4654
"graphql": "^16.6.0",
47-
"zod": "^3.21.4"
55+
"graphql-anywhere": "^4.2.8",
56+
"zod": "3.22.4"
4857
}
4958
}

0 commit comments

Comments
 (0)