Skip to content

Commit 65bf1a7

Browse files
authored
Merge pull request #15 from get-convex/component-api
component api
2 parents 971584b + a8e5cb5 commit 65bf1a7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+13708
-2791
lines changed

.github/workflows/node.js.yml

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

.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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ dist-ssr
99
explorations
1010
node_modules
1111
.eslintcache
12-
# components are libraries!
13-
package-lock.json
1412

15-
# this is a package-json-redirect stub dir, see https://github.com/andrewbranch/example-subpath-exports-ts-compat?tab=readme-ov-file
16-
react/package.json
1713
# npm pack output
1814
*.tgz
15+
*.tsbuildinfo

.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
}

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.8.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: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,37 @@
44

55
```sh
66
npm i
7-
cd example
8-
npm i
9-
npx convex dev
7+
npm run dev
108
```
119

1210
## Testing
1311

1412
```sh
15-
rm -rf dist/ && npm run build
13+
npm run clean
14+
npm run build
1615
npm run typecheck
17-
npm run test
18-
cd example
1916
npm run lint
20-
cd ..
17+
npm run test
2118
```
2219

2320
## Deploying
2421

2522
### Building a one-off package
2623

2724
```sh
28-
rm -rf dist/ && npm run build
25+
npm run clean
26+
npm ci
2927
npm pack
3028
```
3129

3230
### Deploying a new version
3331

3432
```sh
35-
# this will change the version and commit it (if you run it in the root directory)
36-
npm version patch
37-
npm publish --dry-run
38-
# sanity check files being included
39-
npm publish
40-
git push --tags
33+
npm run release
4134
```
4235

43-
#### Alpha release
44-
45-
The same as above, but it requires extra flags so the release is only installed with `@alpha`:
36+
or for alpha release:
4637

4738
```sh
48-
npm version prerelease --preid alpha
49-
npm publish --tag alpha
39+
npm run alpha
5040
```

README.md

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ const response = await fetch(url);
2929
- [Create a Cloudflare account](https://cloudflare.com)
3030
- [Create an R2 bucket](https://developers.cloudflare.com/r2/buckets/create-buckets/)
3131
- Set the bucket name as an environment variable `R2_BUCKET` in your Convex
32-
deployment
33-
- [Add a CORS policy](https://developers.cloudflare.com/r2/buckets/cors/#add-cors-policies-from-the-dashboard) to the bucket allowing GET and PUT requests from your
34-
Convex app. You can also use '\*' to allow all origins (use with caution).
32+
deployment via `npx convex env set R2_BUCKET <bucket-name>`.
33+
- [Add a CORS policy](https://developers.cloudflare.com/r2/buckets/cors/#add-cors-policies-from-the-dashboard)
34+
to the bucket allowing GET and PUT requests from your Convex app. You can also
35+
use '\*' to allow all origins (use with caution).
3536
```json
3637
[
3738
{
@@ -58,7 +59,8 @@ const response = await fetch(url);
5859

5960
### Convex App
6061

61-
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.
62+
You'll need a Convex App to use the component. Follow any of the
63+
[Convex quickstarts](https://docs.convex.dev/home) to set one up.
6264

6365
## Installation
6466

@@ -68,12 +70,13 @@ Install the component package:
6870
npm install @convex-dev/r2
6971
```
7072

71-
Create a `convex.config.ts` file in your app's `convex/` folder and install the component by calling `use`:
73+
Create a `convex.config.ts` file in your app's `convex/` folder and install the
74+
component by calling `use`:
7275

7376
```ts
7477
// convex/convex.config.ts
7578
import { defineApp } from "convex/server";
76-
import r2 from "@convex-dev/r2/convex.config";
79+
import r2 from "@convex-dev/r2/convex.config.js";
7780

7881
const app = defineApp();
7982
app.use(r2);
@@ -92,12 +95,13 @@ npx convex env set R2_BUCKET xxxxx
9295
```
9396

9497
Note: All of these values can also be supplied as the second argument to `R2`.
95-
This enables storing values in multiple buckets using the same component.
96-
It also enables dynamically setting the values at runtime.
98+
This enables storing values in multiple buckets using the same component. It
99+
also enables dynamically setting the values at runtime.
97100

98101
## Uploading files
99102

100-
File uploads to R2 typically use signed urls. The R2 component provides hooks for React and Svelte that handle the entire upload process:
103+
File uploads to R2 typically use signed urls. The R2 component provides hooks
104+
for React and Svelte that handle the entire upload process:
101105

102106
- generates the signed url
103107
- uploads the file to R2
@@ -117,12 +121,12 @@ File uploads to R2 typically use signed urls. The R2 component provides hooks fo
117121
// const user = await userFromAuth(ctx);
118122
// ...validate that the user can upload to this bucket
119123
},
120-
onUpload: async (ctx, bucket, key) => {
121-
// ...do something with the key
122-
// This technically runs in the `syncMetadata` mutation, as the upload
123-
// is performed from the client side. Will run if using the `useUploadFile`
124-
// hook, or if `syncMetadata` function is called directly. Runs after the
125-
// `checkUpload` callback.
124+
onUpload: async (ctx, bucket, key) => {
125+
// ...do something with the key
126+
// This technically runs in the `syncMetadata` mutation, as the upload
127+
// is performed from the client side. Will run if using the `useUploadFile`
128+
// hook, or if `syncMetadata` function is called directly. Runs after the
129+
// `checkUpload` callback.
126130
},
127131
});
128132
```
@@ -210,11 +214,12 @@ File uploads to R2 typically use signed urls. The R2 component provides hooks fo
210214
### Using a custom object key
211215

212216
The `r2.generateUploadUrl` function generates a uuid to use as the object key by
213-
default, but a custom key can be provided if desired. Note: the `generateUploadUrl`
214-
function returned by `r2.clientApi` does not accept a custom key, as that
215-
function is a mutation to be called from the client side and you don't want your
216-
client defining your object keys. Providing a custom key requires making your
217-
own mutation that calls the `generateUploadUrl` method of the `r2` instance.
217+
default, but a custom key can be provided if desired. Note: the
218+
`generateUploadUrl` function returned by `r2.clientApi` does not accept a custom
219+
key, as that function is a mutation to be called from the client side and you
220+
don't want your client defining your object keys. Providing a custom key
221+
requires making your own mutation that calls the `generateUploadUrl` method of
222+
the `r2` instance.
218223

219224
```ts
220225
// convex/example.ts
@@ -241,7 +246,9 @@ export const generateUploadUrlWithCustomKey = mutation({
241246

242247
## Storing Files from Actions
243248

244-
Files can be stored in R2 directly from actions using the `r2.store` method. This is useful when you need to store files that are generated or downloaded on the server side.
249+
Files can be stored in R2 directly from actions using the `r2.store` method.
250+
This is useful when you need to store files that are generated or downloaded on
251+
the server side.
245252

246253
```ts
247254
// convex/example.ts
@@ -280,11 +287,13 @@ The `store` method:
280287

281288
## Serving Files
282289

283-
Files stored in R2 can be served to your users by generating a URL pointing to a given file.
290+
Files stored in R2 can be served to your users by generating a URL pointing to a
291+
given file.
284292

285293
### Generating file URLs in queries
286294

287-
The simplest way to serve files is to return URLs along with other data required by your app from queries and mutations.
295+
The simplest way to serve files is to return URLs along with other data required
296+
by your app from queries and mutations.
288297

289298
A file URL can be generated from a object key by the `r2.getUrl` function of the
290299
R2 component client.
@@ -311,9 +320,9 @@ export const list = query({
311320
{
312321
// Custom expiration time in seconds, default is 900 (15 minutes)
313322
expiresIn: 60 * 60 * 24, // 1 day
314-
}
323+
},
315324
),
316-
}))
325+
})),
317326
);
318327
},
319328
});
@@ -330,7 +339,8 @@ function Image({ message }: { message: { url: string } }) {
330339

331340
## Deleting Files
332341

333-
Files stored in R2 can be deleted from actions or mutations via the `r2.deleteObject` function, which accepts an object key.
342+
Files stored in R2 can be deleted from actions or mutations via the
343+
`r2.deleteObject` function, which accepts an object key.
334344

335345
```ts
336346
// convex/images.ts
@@ -390,7 +400,8 @@ The returned document has the following fields:
390400

391401
### Listing and paginating metadata
392402

393-
Metadata can be listed or paginated from actions via `r2.listMetadata` and `r2.pageMetadata`.
403+
Metadata can be listed or paginated from actions via `r2.listMetadata` and
404+
`r2.pageMetadata`.
394405

395406
```ts
396407
// convex/example.ts
@@ -451,7 +462,7 @@ export const { generateUploadUrl, syncMetadata, onSyncMetadata } = r2.clientApi(
451462
// log metadata of synced object
452463
console.log("metadata", metadata);
453464
},
454-
}
465+
},
455466
);
456467
```
457468

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+
}

0 commit comments

Comments
 (0)