Skip to content

Commit efd1d1c

Browse files
committed
Standardize prettier config and format
1 parent f58beb6 commit efd1d1c

File tree

9 files changed

+77
-60
lines changed

9 files changed

+77
-60
lines changed

.prettierrc.json

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

README.md

Lines changed: 38 additions & 18 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,7 +47,8 @@ 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
@@ -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

src/client/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class Twilio<
4141
httpPrefix?: string;
4242
incomingMessageCallback?: MessageHandler;
4343
defaultOutgoingMessageCallback?: MessageHandler;
44-
} & From
44+
} & From,
4545
) {
4646
this.accountSid =
4747
options?.TWILIO_ACCOUNT_SID ?? process.env.TWILIO_ACCOUNT_SID!;
@@ -51,7 +51,7 @@ export class Twilio<
5151
throw new Error(
5252
"Missing Twilio credentials\n\n" +
5353
"npx convex env set TWILIO_ACCOUNT_SID=ACxxxxx\n" +
54-
"npx convex env set TWILIO_AUTH_TOKEN=xxxxx"
54+
"npx convex env set TWILIO_AUTH_TOKEN=xxxxx",
5555
);
5656
}
5757
this.defaultFrom = options.defaultFrom;
@@ -103,7 +103,7 @@ export class Twilio<
103103
incomingMessageCallback:
104104
this.incomingMessageCallback &&
105105
(await createFunctionHandle(this.incomingMessageCallback)),
106-
}
106+
},
107107
);
108108

109109
const emptyResponseTwiML = `
@@ -141,7 +141,7 @@ export class Twilio<
141141
} & (From["defaultFrom"] extends string
142142
? { from?: string }
143143
: { from: string })
144-
>
144+
>,
145145
) {
146146
const from = args.from ?? this.defaultFrom;
147147
if (!from) {
@@ -267,7 +267,7 @@ export class Twilio<
267267
*/
268268
async getMessagesFrom(
269269
ctx: RunQueryCtx,
270-
args: { from: string; limit?: number }
270+
args: { from: string; limit?: number },
271271
) {
272272
return ctx.runQuery(this.componentApi.messages.getFrom, {
273273
...args,
@@ -286,7 +286,7 @@ export class Twilio<
286286
*/
287287
async getMessagesByCounterparty(
288288
ctx: RunQueryCtx,
289-
args: { counterparty: string; limit?: number }
289+
args: { counterparty: string; limit?: number },
290290
) {
291291
return ctx.runQuery(this.componentApi.messages.getByCounterparty, {
292292
...args,
@@ -303,7 +303,7 @@ declare global {
303303

304304
if (typeof Convex === "undefined") {
305305
throw new Error(
306-
"this is Convex backend code, but it's running somewhere else!"
306+
"this is Convex backend code, but it's running somewhere else!",
307307
);
308308
}
309309

src/component/messages.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const create = action({
4242
To: args.to,
4343
Body: args.body,
4444
StatusCallback: args.status_callback,
45-
}
45+
},
4646
);
4747
// store some properties as fields and the rest as a nested object
4848
const databaseMessage = convertToDatabaseMessage(message);
@@ -83,9 +83,9 @@ export const list = query({
8383
ctx.db
8484
.query("messages")
8585
.withIndex("by_account_sid", (q) =>
86-
q.eq("account_sid", args.account_sid)
86+
q.eq("account_sid", args.account_sid),
8787
),
88-
args.limit
88+
args.limit,
8989
);
9090
},
9191
});
@@ -101,9 +101,9 @@ export const listIncoming = query({
101101
ctx.db
102102
.query("messages")
103103
.withIndex("by_account_sid_and_direction", (q) =>
104-
q.eq("account_sid", args.account_sid).eq("direction", "inbound")
104+
q.eq("account_sid", args.account_sid).eq("direction", "inbound"),
105105
),
106-
args.limit
106+
args.limit,
107107
);
108108
},
109109
});
@@ -118,9 +118,9 @@ export const listOutgoing = query({
118118
ctx.db
119119
.query("messages")
120120
.withIndex("by_account_sid_and_direction", (q) =>
121-
q.eq("account_sid", args.account_sid).eq("direction", "outbound-api")
121+
q.eq("account_sid", args.account_sid).eq("direction", "outbound-api"),
122122
),
123-
args.limit
123+
args.limit,
124124
);
125125
},
126126
});
@@ -135,7 +135,7 @@ export const getBySid = query({
135135
const message = await ctx.db
136136
.query("messages")
137137
.withIndex("by_sid", (q) =>
138-
q.eq("account_sid", args.account_sid).eq("sid", args.sid)
138+
q.eq("account_sid", args.account_sid).eq("sid", args.sid),
139139
)
140140
.first();
141141
return message && withoutSystemFields(message);
@@ -154,9 +154,9 @@ export const getTo = query({
154154
ctx.db
155155
.query("messages")
156156
.withIndex("by_to", (q) =>
157-
q.eq("account_sid", args.account_sid).eq("to", args.to)
157+
q.eq("account_sid", args.account_sid).eq("to", args.to),
158158
),
159-
args.limit
159+
args.limit,
160160
);
161161
},
162162
});
@@ -173,9 +173,9 @@ export const getFrom = query({
173173
ctx.db
174174
.query("messages")
175175
.withIndex("by_from", (q) =>
176-
q.eq("account_sid", args.account_sid).eq("from", args.from)
176+
q.eq("account_sid", args.account_sid).eq("from", args.from),
177177
),
178-
args.limit
178+
args.limit,
179179
);
180180
},
181181
});
@@ -194,9 +194,9 @@ export const getByCounterparty = query({
194194
.withIndex("by_counterparty", (q) =>
195195
q
196196
.eq("account_sid", args.account_sid)
197-
.eq("counterparty", args.counterparty)
197+
.eq("counterparty", args.counterparty),
198198
),
199-
args.limit
199+
args.limit,
200200
);
201201
},
202202
});
@@ -212,7 +212,7 @@ export const updateStatus = mutation({
212212
const message = await ctx.db
213213
.query("messages")
214214
.withIndex("by_sid", (q) =>
215-
q.eq("account_sid", args.account_sid).eq("sid", args.sid)
215+
q.eq("account_sid", args.account_sid).eq("sid", args.sid),
216216
)
217217
.first();
218218
if (!message) {
@@ -236,7 +236,7 @@ export const getFromTwilioBySidAndInsert = action({
236236
args.account_sid,
237237
args.auth_token,
238238
{},
239-
"GET"
239+
"GET",
240240
);
241241
const databaseMessage = convertToDatabaseMessage(message);
242242
return ctx.runMutation(internal.messages.insert, {
@@ -299,7 +299,7 @@ function convertToDatabaseMessage(message: any) {
299299

300300
async function takeOrCollectFields(
301301
query: Query<NamedTableInfo<DataModel, "messages">>,
302-
limit: number | undefined
302+
limit: number | undefined,
303303
): Promise<Message[]> {
304304
let messagesPromise;
305305
if (limit) {

src/component/phone_numbers.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const create = action({
2424
path,
2525
args.account_sid,
2626
args.auth_token,
27-
body
27+
body,
2828
);
2929
const id = await ctx.runMutation(internal.phone_numbers.insert, {
3030
phone_number: data,
@@ -65,13 +65,13 @@ export const get = internalQuery({
6565
_id: v.id("phone_numbers"),
6666
_creationTime: v.number(),
6767
}),
68-
v.null()
68+
v.null(),
6969
),
7070
handler: async (ctx, args) => {
7171
return await ctx.db
7272
.query("phone_numbers")
7373
.withIndex("by_sid", (q) =>
74-
q.eq("account_sid", args.account_sid).eq("sid", args.sid)
74+
q.eq("account_sid", args.account_sid).eq("sid", args.sid),
7575
)
7676
.first();
7777
},
@@ -88,15 +88,15 @@ export const queryByPhoneNumber = internalQuery({
8888
_id: v.id("phone_numbers"),
8989
_creationTime: v.number(),
9090
}),
91-
v.null()
91+
v.null(),
9292
),
9393
handler: async (ctx, args) => {
9494
return await ctx.db
9595
.query("phone_numbers")
9696
.withIndex("by_phone_number", (q) =>
9797
q
9898
.eq("account_sid", args.account_sid)
99-
.eq("phone_number", args.phone_number)
99+
.eq("phone_number", args.phone_number),
100100
)
101101
.first();
102102
},
@@ -123,7 +123,7 @@ export const updateSmsUrl = action({
123123
args.account_sid,
124124
args.auth_token,
125125
{},
126-
"GET"
126+
"GET",
127127
);
128128
console.log("Inserting into table");
129129
convexId = await ctx.runMutation(internal.phone_numbers.insert, {
@@ -160,11 +160,11 @@ export const getByPhoneNumber = internalAction({
160160
{
161161
phone_number: args.phone_number,
162162
account_sid: args.account_sid,
163-
}
163+
},
164164
);
165165
if (!phone_number) {
166166
console.log(
167-
`Phone number ${args.phone_number} not found in table - fetching from Twilio`
167+
`Phone number ${args.phone_number} not found in table - fetching from Twilio`,
168168
);
169169
const phone_number = encodeURIComponent(args.phone_number);
170170
const path = `IncomingPhoneNumbers.json?PhoneNumber=${phone_number}`;
@@ -173,7 +173,7 @@ export const getByPhoneNumber = internalAction({
173173
args.account_sid,
174174
args.auth_token,
175175
{},
176-
"GET"
176+
"GET",
177177
);
178178
if (data.incoming_phone_numbers.length === 0) {
179179
throw new Error("Phone number not found");

src/component/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default defineSchema({
2323
status: v.string(),
2424
subresource_uris: v.union(
2525
v.object({ media: v.string(), feedback: v.optional(v.string()) }),
26-
v.null()
26+
v.null(),
2727
),
2828
to: v.string(),
2929
uri: v.string(),

src/component/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export const twilioRequest = async function (
33
account_sid: string,
44
auth_token: string,
55
body: Record<string, string>,
6-
method: "POST" | "GET" = "POST"
6+
method: "POST" | "GET" = "POST",
77
) {
88
const url = `https://api.twilio.com/2010-04-01/Accounts/${account_sid}/${path}`;
99
const auth = btoa(`${account_sid}:${auth_token}`);

0 commit comments

Comments
 (0)