Skip to content

Commit 964d7bb

Browse files
committed
[Artifacts] Improve get-started guide for Workers
- Add try/catch error handling for repo creation with 409 conflict response - Clarify returned values and their purposes - Add bullet points explaining name and token fields - Add step to switch to Worker directory before deploy - Improve explanations throughout the guide
1 parent 6d18938 commit 964d7bb

1 file changed

Lines changed: 27 additions & 19 deletions

File tree

src/content/docs/artifacts/get-started/workers.mdx

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,20 @@ export default {
119119
};
120120
const repoName = body.name ?? "starter-repo";
121121

122-
// Create the repo and return the remote URL plus initial write token.
123-
const created = await env.ARTIFACTS.create(repoName);
124-
125-
return Response.json({
126-
name: created.name,
127-
remote: created.remote,
128-
token: created.token,
129-
});
122+
try {
123+
// Create the repo and return the remote URL plus initial write token.
124+
const created = await env.ARTIFACTS.create(repoName);
125+
return Response.json({
126+
name: created.name,
127+
remote: created.remote,
128+
token: created.token,
129+
});
130+
} catch (err) {
131+
// Return an error if the repo name is already taken.
132+
const message = err instanceof Error ? err.message : String(err);
133+
const status = message.includes("already exists") ? 409 : 500;
134+
return Response.json({ error: message }, { status });
135+
}
130136
}
131137

132138
return new Response("Use POST /repos to create an Artifacts repo.", {
@@ -139,7 +145,7 @@ export default {
139145

140146
</TypeScriptExample>
141147

142-
This Worker does one job: create an Artifacts repo and return the values your Git client needs next.
148+
This Worker creates an Artifacts repo and returns the remote URL and token your Git client needs to push and pull.
143149

144150
:::caution[Protect token-issuing routes]
145151
This example omits authentication so it can focus on the Artifacts flow. In production, authorize the caller before creating repos or returning write tokens.
@@ -153,9 +159,7 @@ Start local development:
153159

154160
<PackageManagers type="exec" pkg="wrangler" args="dev" />
155161

156-
In a second terminal, choose one of the following ways to create a repo through your Worker.
157-
158-
If you rerun this guide, use a different repo name in the request body.
162+
Then, open a second terminal and send a request to your Worker to create a new Artifacts repo:
159163

160164
<Tabs>
161165
<TabItem label="Manual">
@@ -168,7 +172,7 @@ curl http://localhost:8787/repos \
168172
}'
169173
```
170174

171-
The response resembles the following:
175+
Your Worker will call `env.ARTIFACTS.create()` and return three values you'll need for Git operations:
172176

173177
```json
174178
{
@@ -177,10 +181,9 @@ The response resembles the following:
177181
"token": "art_v1_0123456789abcdef0123456789abcdef01234567?expires=1760000000"
178182
}
179183
```
180-
181-
Use the exact `remote` value from the response. The example above uses `<ACCOUNT_ID>` as a placeholder for your Cloudflare account ID.
182-
183-
The returned token encodes its expiry directly in the `?expires=` suffix.
184+
- `name`: the repo name. Must be unique within the namespace.
185+
- `remote`: the Git remote URL for this repo. `<ACCOUNT_ID>` will be your actual Cloudflare account ID.
186+
- `token`: a short-lived credential for Git operations. The token encodes its expiry directly in the `?expires=` suffix as a Unix timestamp.
184187

185188
Copy the `remote` and `token` values into local shell variables:
186189

@@ -207,7 +210,7 @@ export ARTIFACTS_TOKEN=$(printf '%s' "$RESPONSE" | jq -r '.token')
207210

208211
## 5. Push your first commit with git
209212

210-
Create a local repository and push it to Artifacts:
213+
In the previous step, your Worker created an empty Artifacts repo. Now you'll create a local Git repo, add a file, and push it to Artifacts — the same way you'd push to any Git remote.
211214

212215
```sh
213216
mkdir artifacts-demo
@@ -220,7 +223,7 @@ git remote add origin "$ARTIFACTS_REMOTE"
220223
git -c http.extraHeader="Authorization: Bearer $ARTIFACTS_TOKEN" push -u origin main
221224
```
222225

223-
This uses the recommended header-based form and keeps the token out of the remote URL.
226+
The `-c http.extraHeader` flag passes the token as a request header, which keeps it out of your Git config and shell history.
224227

225228
If you need a self-contained remote URL for a short-lived command, build one from the token secret instead:
226229

@@ -250,6 +253,11 @@ git clone "$ARTIFACTS_AUTH_REMOTE" artifacts-clone
250253

251254
## 7. Deploy your Worker
252255

256+
Switch back to your Worker project directory:
257+
```sh
258+
cd artifacts-worker
259+
```
260+
253261
Deploy the Worker so you can create repos without running `wrangler dev`:
254262

255263
<PackageManagers type="exec" pkg="wrangler" args="deploy" />

0 commit comments

Comments
 (0)