Skip to content

Commit d075247

Browse files
committed
format
1 parent 4f390eb commit d075247

File tree

7 files changed

+68
-45
lines changed

7 files changed

+68
-45
lines changed

example/convex/_generated/dataModel.d.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,29 @@
88
* @module
99
*/
1010

11-
import { AnyDataModel } from "convex/server";
11+
import type {
12+
DataModelFromSchemaDefinition,
13+
DocumentByName,
14+
TableNamesInDataModel,
15+
SystemTableNames,
16+
} from "convex/server";
1217
import type { GenericId } from "convex/values";
13-
14-
/**
15-
* No `schema.ts` file found!
16-
*
17-
* This generated code has permissive types like `Doc = any` because
18-
* Convex doesn't know your schema. If you'd like more type safety, see
19-
* https://docs.convex.dev/using/schemas for instructions on how to add a
20-
* schema file.
21-
*
22-
* After you change a schema, rerun codegen with `npx convex dev`.
23-
*/
18+
import schema from "../schema.js";
2419

2520
/**
2621
* The names of all of your Convex tables.
2722
*/
28-
export type TableNames = string;
23+
export type TableNames = TableNamesInDataModel<DataModel>;
2924

3025
/**
3126
* The type of a document stored in Convex.
27+
*
28+
* @typeParam TableName - A string literal type of the table name (like "users").
3229
*/
33-
export type Doc = any;
30+
export type Doc<TableName extends TableNames> = DocumentByName<
31+
DataModel,
32+
TableName
33+
>;
3434

3535
/**
3636
* An identifier for a document in Convex.
@@ -42,8 +42,10 @@ export type Doc = any;
4242
*
4343
* IDs are just strings at runtime, but this type can be used to distinguish them from other
4444
* strings when type checking.
45+
*
46+
* @typeParam TableName - A string literal type of the table name (like "users").
4547
*/
46-
export type Id<TableName extends TableNames = TableNames> =
48+
export type Id<TableName extends TableNames | SystemTableNames> =
4749
GenericId<TableName>;
4850

4951
/**
@@ -55,4 +57,4 @@ export type Id<TableName extends TableNames = TableNames> =
5557
* This type is used to parameterize methods like `queryGeneric` and
5658
* `mutationGeneric` to make them type-safe.
5759
*/
58-
export type DataModel = AnyDataModel;
60+
export type DataModel = DataModelFromSchemaDefinition<typeof schema>;

example/convex/schema.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { defineSchema } from "convex/server";
2+
3+
export default defineSchema({
4+
// Any tables used by the example app go here.
5+
});

example/convex/setup.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference types="vite/client" />
2+
import { test } from "vitest";
3+
import { convexTest } from "convex-test";
4+
import schema from "./schema.js";
5+
import component from "@convex-dev/twilio/test";
6+
7+
const modules = import.meta.glob("./**/*.*s");
8+
// When users want to write tests that use your component, they need to
9+
// explicitly register it with its schema and modules.
10+
export function initConvexTest() {
11+
const t = convexTest(schema, modules);
12+
component.register(t);
13+
return t;
14+
}
15+
16+
test("setup", () => {});

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, {
@@ -300,7 +300,7 @@ function convertToDatabaseMessage(message: any) {
300300

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

src/component/phone_numbers.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const create = action({
2525
path,
2626
args.account_sid,
2727
args.auth_token,
28-
body,
28+
body
2929
);
3030
const id = await ctx.runMutation(internal.phone_numbers.insert, {
3131
phone_number: data,
@@ -66,13 +66,13 @@ export const get = internalQuery({
6666
_id: v.id("phone_numbers"),
6767
_creationTime: v.number(),
6868
}),
69-
v.null(),
69+
v.null()
7070
),
7171
handler: async (ctx, args) => {
7272
return await ctx.db
7373
.query("phone_numbers")
7474
.withIndex("by_sid", (q) =>
75-
q.eq("account_sid", args.account_sid).eq("sid", args.sid),
75+
q.eq("account_sid", args.account_sid).eq("sid", args.sid)
7676
)
7777
.first();
7878
},
@@ -89,15 +89,15 @@ export const queryByPhoneNumber = internalQuery({
8989
_id: v.id("phone_numbers"),
9090
_creationTime: v.number(),
9191
}),
92-
v.null(),
92+
v.null()
9393
),
9494
handler: async (ctx, args) => {
9595
return await ctx.db
9696
.query("phone_numbers")
9797
.withIndex("by_phone_number", (q) =>
9898
q
9999
.eq("account_sid", args.account_sid)
100-
.eq("phone_number", args.phone_number),
100+
.eq("phone_number", args.phone_number)
101101
)
102102
.first();
103103
},
@@ -124,7 +124,7 @@ export const updateSmsUrl = action({
124124
args.account_sid,
125125
args.auth_token,
126126
{},
127-
"GET",
127+
"GET"
128128
);
129129
console.log("Inserting into table");
130130
convexId = await ctx.runMutation(internal.phone_numbers.insert, {
@@ -141,7 +141,7 @@ export const updateSmsUrl = action({
141141
args.account_sid,
142142
args.auth_token,
143143
body,
144-
"POST",
144+
"POST"
145145
);
146146
await ctx.runMutation(internal.phone_numbers.patch, {
147147
convexId,
@@ -167,11 +167,11 @@ export const getByPhoneNumber = internalAction({
167167
{
168168
phone_number: args.phone_number,
169169
account_sid: args.account_sid,
170-
},
170+
}
171171
);
172172
if (!phone_number) {
173173
console.log(
174-
`Phone number ${args.phone_number} not found in table - fetching from Twilio`,
174+
`Phone number ${args.phone_number} not found in table - fetching from Twilio`
175175
);
176176
const phone_number = encodeURIComponent(args.phone_number);
177177
const path = `IncomingPhoneNumbers.json?PhoneNumber=${phone_number}`;
@@ -180,7 +180,7 @@ export const getByPhoneNumber = internalAction({
180180
args.account_sid,
181181
args.auth_token,
182182
{},
183-
"GET",
183+
"GET"
184184
);
185185
if (data.incoming_phone_numbers.length === 0) {
186186
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)