Skip to content

Commit 28502be

Browse files
authored
Merge pull request #13 from get-convex/component-api
component api
2 parents 71428c4 + 073ef0b commit 28502be

38 files changed

+3986
-6021
lines changed

.github/workflows/test.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Test and lint
2+
concurrency:
3+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
4+
cancel-in-progress: true
5+
6+
on:
7+
push:
8+
branches: [main]
9+
pull_request:
10+
branches: ["**"]
11+
12+
jobs:
13+
check:
14+
name: Test and lint
15+
runs-on: ubuntu-latest
16+
timeout-minutes: 30
17+
18+
steps:
19+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
20+
21+
- name: Node setup
22+
uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5
23+
with:
24+
cache-dependency-path: package.json
25+
node-version: "20.x"
26+
cache: "npm"
27+
28+
- name: Install and build
29+
run: |
30+
npm i
31+
npm run build
32+
- name: Publish package for testing branch
33+
run: npx pkg-pr-new publish || echo "Have you set up pkg-pr-new for this repo?"
34+
- name: Test
35+
run: |
36+
npm run test
37+
npm run typecheck
38+
npm run lint

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ dist-ssr
99
explorations
1010
node_modules
1111
.eslintcache
12-
# components are libraries!
13-
.package-lock.json
1412

1513
# this is a package-json-redirect stub dir, see https://github.com/andrewbranch/example-subpath-exports-ts-compat?tab=readme-ov-file
1614
frontend/package.json
1715
# npm pack output
1816
*.tgz
17+
*.tsbuildinfo

.prettierrc.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"trailingComma": "all",
3+
"proseWrap": "always"
4+
}

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changelog
2+
3+
## 0.2.0
4+
5+
- Adds /test and /\_generated/component.js entrypoints
6+
- Drops commonjs support
7+
- Improves source mapping for generated files
8+
- Changes to a statically generated component API

CONTRIBUTING.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Developing guide
2+
3+
## Running locally
4+
5+
```sh
6+
npm i
7+
npm run dev
8+
```
9+
10+
## Testing
11+
12+
```sh
13+
npm run clean
14+
npm run build
15+
npm run typecheck
16+
npm run lint
17+
npm run test
18+
```
19+
20+
## Deploying
21+
22+
### Building a one-off package
23+
24+
```sh
25+
npm run clean
26+
npm ci
27+
npm pack
28+
```
29+
30+
### Deploying a new version
31+
32+
```sh
33+
npm run release
34+
```
35+
36+
or for alpha release:
37+
38+
```sh
39+
npm run alpha
40+
```

README.md

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ export const sendSms = internalAction({
2828

2929
### Twilio Phone Number
3030

31-
Create a Twilio account and, if you haven't already, create a [Twilio Phone Number](https://www.twilio.com/docs/phone-numbers).
31+
Create a Twilio account and, if you haven't already, create a
32+
[Twilio Phone Number](https://www.twilio.com/docs/phone-numbers).
3233

33-
Note the **Phone Number SID** of the phone number you'll be using, you'll need it in a moment.
34+
Note the **Phone Number SID** of the phone number you'll be using, you'll need
35+
it in a moment.
3436

3537
### Convex App
3638

37-
You'll need a Convex App to use the component. Follow any of the [Convex quickstarts](https://docs.convex.dev/home) to set one up.
39+
You'll need a Convex App to use the component. Follow any of the
40+
[Convex quickstarts](https://docs.convex.dev/home) to set one up.
3841

3942
## Installation
4043

@@ -44,12 +47,13 @@ Install the component package:
4447
npm install @convex-dev/twilio
4548
```
4649

47-
Create a `convex.config.ts` file in your app's `convex/` folder and install the component by calling `use`:
50+
Create a `convex.config.ts` file in your app's `convex/` folder and install the
51+
component by calling `use`:
4852

4953
```ts
5054
// convex/convex.config.ts
5155
import { defineApp } from "convex/server";
52-
import twilio from "@convex-dev/twilio/convex.config";
56+
import twilio from "@convex-dev/twilio/convex.config.js";
5357

5458
const app = defineApp();
5559
app.use(twilio);
@@ -78,7 +82,8 @@ export const twilio = new Twilio(components.twilio, {
7882
});
7983
```
8084

81-
Register Twilio webhook handlers by creating an `http.ts` file in your `convex/` folder and use the client you've exported above:
85+
Register Twilio webhook handlers by creating an `http.ts` file in your `convex/`
86+
folder and use the client you've exported above:
8287

8388
```ts
8489
// http.ts
@@ -110,21 +115,30 @@ export const sendSms = internalAction({
110115
});
111116
```
112117

113-
By querying the message (see [below](#querying-messages)) you can check for the status ([Twilio Statuses](https://www.twilio.com/docs/messaging/api/message-resource#message-status-values)). The component subscribes to status updates and writes the most up-to-date status into the database.
118+
By querying the message (see [below](#querying-messages)) you can check for the
119+
status
120+
([Twilio Statuses](https://www.twilio.com/docs/messaging/api/message-resource#message-status-values)).
121+
The component subscribes to status updates and writes the most up-to-date status
122+
into the database.
114123

115124
## Receiving Messages
116125

117-
To receive messages, you will associate a webhook handler provided by the component with the Twilio phone number you'd like to use.
126+
To receive messages, you will associate a webhook handler provided by the
127+
component with the Twilio phone number you'd like to use.
118128

119-
`twilio.registerRoutes` registers two webhook HTTP handlers in your your Convex app's deployment:
129+
`twilio.registerRoutes` registers two webhook HTTP handlers in your your Convex
130+
app's deployment:
120131

121-
- `YOUR_CONVEX_SITE_URL/twilio/message-status` - capture and store delivery status of messages you **send**.
122-
- `YOUR_CONVEX_SITE_URL/twilio/incoming-message` - capture and store messages **sent to** your Twilio phone number.
132+
- `YOUR_CONVEX_SITE_URL/twilio/message-status` - capture and store delivery
133+
status of messages you **send**.
134+
- `YOUR_CONVEX_SITE_URL/twilio/incoming-message` - capture and store messages
135+
**sent to** your Twilio phone number.
123136

124-
Note: You may pass a custom `httpPrefix` to `Twilio` if you want to
125-
route Twilio endpoints somewhere other than `YOUR_CONVEX_SITE_URL/twilio/*`.
137+
Note: You may pass a custom `httpPrefix` to `Twilio` if you want to route Twilio
138+
endpoints somewhere other than `YOUR_CONVEX_SITE_URL/twilio/*`.
126139

127-
For instance, to route to `YOUR_CONVEX_SITE_URL/custom-twilio/message-status`, set:
140+
For instance, to route to `YOUR_CONVEX_SITE_URL/custom-twilio/message-status`,
141+
set:
128142

129143
```ts
130144
export const twilio = new Twilio(components.twilio, {
@@ -134,9 +148,12 @@ export const twilio = new Twilio(components.twilio, {
134148

135149
You can associate it with your Twilio phone number in two ways:
136150

137-
1. Using the [Twilio console](https://console.twilio.com/) in the "Configure" tab of the phone number, under "Messaging Configuration" -> "A messsage comes in" -> "URL".
151+
1. Using the [Twilio console](https://console.twilio.com/) in the "Configure"
152+
tab of the phone number, under "Messaging Configuration" -> "A messsage comes
153+
in" -> "URL".
138154

139-
2. By calling `registerIncomingSmsHandler` exposed by the component client, passing it the phone number's SID:
155+
2. By calling `registerIncomingSmsHandler` exposed by the component client,
156+
passing it the phone number's SID:
140157

141158
```ts
142159
export const registerIncomingSmsHandler = internalAction({
@@ -149,9 +166,11 @@ export const registerIncomingSmsHandler = internalAction({
149166
});
150167
```
151168

152-
Once it is configured, incoming messages will be captured by the component and logged in the `messages` table.
169+
Once it is configured, incoming messages will be captured by the component and
170+
logged in the `messages` table.
153171

154-
You can execute your own logic upon receiving an incoming message, by providing a callback when instantiating the Twilio Component client:
172+
You can execute your own logic upon receiving an incoming message, by providing
173+
a callback when instantiating the Twilio Component client:
155174

156175
```ts
157176
// convex/example.ts
@@ -181,7 +200,8 @@ but you can replay them manually from the Twilio "Error logs" console.
181200

182201
To list all the mssages, use the `list` method in your Convex function.
183202

184-
To list all the incoming or outgoing messages, use `listIncoming` and `listOutgoing` methods:
203+
To list all the incoming or outgoing messages, use `listIncoming` and
204+
`listOutgoing` methods:
185205

186206
```ts
187207
// convex/messages.ts

commonjs.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

convex.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "./node_modules/convex/schemas/convex.schema.json",
3+
"functions": "example/convex",
4+
"codegen": {
5+
"legacyComponentApi": false
6+
}
7+
}

eslint.config.js

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
11
import globals from "globals";
22
import pluginJs from "@eslint/js";
33
import tseslint from "typescript-eslint";
4+
import reactHooks from "eslint-plugin-react-hooks";
5+
import reactRefresh from "eslint-plugin-react-refresh";
46

57
export default [
6-
{ files: ["src/**/*.{js,mjs,cjs,ts,tsx}"] },
78
{
89
ignores: [
910
"dist/**",
1011
"eslint.config.js",
12+
"vitest.config.ts",
1113
"**/_generated/",
1214
"node10stubs.mjs",
1315
],
1416
},
1517
{
18+
files: ["src/**/*.{js,mjs,cjs,ts,tsx}", "example/**/*.{js,mjs,cjs,ts,tsx}"],
1619
languageOptions: {
17-
globals: globals.worker,
1820
parser: tseslint.parser,
19-
2021
parserOptions: {
21-
project: true,
22-
tsconfigRootDir: ".",
22+
project: ["./tsconfig.json", "./example/convex/tsconfig.json"],
23+
tsconfigRootDir: import.meta.dirname,
2324
},
2425
},
2526
},
2627
pluginJs.configs.recommended,
2728
...tseslint.configs.recommended,
29+
// Convex code - Worker environment
2830
{
31+
files: ["src/**/*.{ts,tsx}", "example/convex/**/*.{ts,tsx}"],
32+
ignores: ["src/react/**"],
33+
languageOptions: {
34+
globals: globals.worker,
35+
},
2936
rules: {
3037
"@typescript-eslint/no-floating-promises": "error",
31-
"eslint-comments/no-unused-disable": "off",
32-
33-
// allow (_arg: number) => {} and const _foo = 1;
38+
"@typescript-eslint/no-explicit-any": "off",
3439
"no-unused-vars": "off",
3540
"@typescript-eslint/no-unused-vars": [
3641
"warn",
@@ -39,6 +44,52 @@ export default [
3944
varsIgnorePattern: "^_",
4045
},
4146
],
47+
"@typescript-eslint/no-unused-expressions": [
48+
"error",
49+
{
50+
allowShortCircuit: true,
51+
allowTernary: true,
52+
allowTaggedTemplates: true,
53+
},
54+
],
55+
},
56+
},
57+
// React app code - Browser environment
58+
{
59+
files: ["src/react/**/*.{ts,tsx}", "example/src/**/*.{ts,tsx}"],
60+
languageOptions: {
61+
ecmaVersion: 2020,
62+
globals: globals.browser,
63+
},
64+
plugins: {
65+
"react-hooks": reactHooks,
66+
"react-refresh": reactRefresh,
67+
},
68+
rules: {
69+
...reactHooks.configs.recommended.rules,
70+
"react-refresh/only-export-components": [
71+
"warn",
72+
{ allowConstantExport: true },
73+
],
74+
"@typescript-eslint/no-explicit-any": "off",
75+
"no-unused-vars": "off",
76+
"@typescript-eslint/no-unused-vars": [
77+
"warn",
78+
{
79+
argsIgnorePattern: "^_",
80+
varsIgnorePattern: "^_",
81+
},
82+
],
83+
},
84+
},
85+
// Example config files (vite.config.ts, etc.) - Node environment
86+
{
87+
files: ["example/vite.config.ts", "example/**/*.config.{js,ts}"],
88+
languageOptions: {
89+
globals: {
90+
...globals.node,
91+
...globals.browser,
92+
},
4293
},
4394
},
4495
];

esm.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)