Skip to content

Commit 69ad665

Browse files
📝 Add docstrings to jacob/feat-snippet-resource
Docstrings generation was requested by @Mkassabov. * #1073 (comment) The following files were modified: * `alchemy/src/cloudflare/snippet-rule.ts` * `alchemy/src/cloudflare/snippet.ts`
1 parent c610d5e commit 69ad665

File tree

2 files changed

+60
-16
lines changed

2 files changed

+60
-16
lines changed

alchemy/src/cloudflare/snippet-rule.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,14 @@ export const SnippetRule = Resource(
292292
);
293293

294294
/**
295-
* Delete a snippet rule from a zone by its ID
295+
* Removes a snippet rule identified by its ruleId from the specified zone.
296+
*
297+
* If the rule is the only snippet rule remaining, all snippet rules for the zone are deleted; otherwise the rule is removed and the remaining rules are updated.
298+
*
299+
* @param api - Cloudflare API client used to list and modify snippet rules
300+
* @param zoneId - Zone identifier containing the snippet rule
301+
* @param ruleId - Identifier of the snippet rule to remove
302+
* @throws Errors encountered while listing, updating, or deleting rules are logged and rethrown
296303
* @internal
297304
*/
298305
async function deleteSnippetRuleById(
@@ -343,8 +350,10 @@ async function deleteSnippetRuleById(
343350
}
344351

345352
/**
346-
* List all snippet rules in a zone
353+
* Retrieve the snippet rules configured for the given zone.
354+
*
347355
* @internal
356+
* @returns An array of `SnippetRuleResponse` objects representing the zone's snippet rules.
348357
*/
349358
export async function listSnippetRules(
350359
api: CloudflareApi,
@@ -357,7 +366,11 @@ export async function listSnippetRules(
357366
}
358367

359368
/**
360-
* Update snippet rules in a zone (replaces all rules)
369+
* Replace all snippet rules for a Cloudflare zone with the provided set of rules.
370+
*
371+
* @param zoneId - The target zone identifier
372+
* @param rules - Array of snippet rule inputs; each item should include `expression`, `snippetName`, optional `description`, and optional `enabled` (defaults to `true`)
373+
* @returns The updated list of snippet rules as returned by the Cloudflare API
361374
* @internal
362375
*/
363376
export async function updateSnippetRules(
@@ -381,7 +394,10 @@ export async function updateSnippetRules(
381394
}
382395

383396
/**
384-
* Delete all snippet rules in a zone
397+
* Remove all snippet rules for the given zone.
398+
*
399+
* Treats a missing resource (HTTP 404) as a successful no-op; throws an error for any other non-OK response.
400+
*
385401
* @internal
386402
*/
387403
export async function deleteSnippetRules(
@@ -393,4 +409,4 @@ export async function deleteSnippetRules(
393409
if (!response.ok && response.status !== 404) {
394410
throw new Error(`Failed to delete snippet rules: ${response.statusText}`);
395411
}
396-
}
412+
}

alchemy/src/cloudflare/snippet.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,11 @@ export const Snippet = Resource(
239239
);
240240

241241
/**
242-
* Get script content from either inline script or file entrypoint
242+
* Resolve and return the snippet's JavaScript source from inline `script` or a filesystem `entrypoint`.
243+
*
244+
* @param props - Snippet properties containing either an inline `script` or an `entrypoint` path
245+
* @returns The snippet source code as a UTF-8 string
246+
* @throws Error if neither `script` nor `entrypoint` is provided
243247
* @internal
244248
*/
245249
async function getScriptContent(props: SnippetProps): Promise<string> {
@@ -264,8 +268,13 @@ interface SnippetResponse {
264268
}
265269

266270
/**
267-
* Create or update a snippet
271+
* Uploads a JavaScript snippet to a Cloudflare zone, creating or replacing the named snippet.
272+
*
268273
* @internal
274+
* @param zoneId - The Cloudflare zone identifier where the snippet will be stored
275+
* @param snippetName - The unique name of the snippet within the zone
276+
* @param content - The JavaScript source to upload as the snippet
277+
* @throws If the Cloudflare API returns an error while creating or updating the snippet
269278
*/
270279
export async function createOrUpdateSnippet(
271280
api: CloudflareApi,
@@ -302,8 +311,10 @@ export async function createOrUpdateSnippet(
302311
}
303312

304313
/**
305-
* Check if a snippet exists
314+
* Determine whether a Cloudflare snippet with the given name exists in the specified zone.
315+
*
306316
* @internal
317+
* @returns `true` if the snippet exists, `false` otherwise.
307318
*/
308319
export async function snippetExists(
309320
api: CloudflareApi,
@@ -315,8 +326,13 @@ export async function snippetExists(
315326
}
316327

317328
/**
318-
* Get a snippet by name
329+
* Retrieve metadata for a Cloudflare snippet by name.
330+
*
319331
* @internal
332+
* @param api - Cloudflare API client used to perform the request
333+
* @param zoneId - Identifier of the Cloudflare zone containing the snippet
334+
* @param snippetName - The name of the snippet to retrieve
335+
* @returns The snippet metadata (`SnippetResponse`)
320336
*/
321337
export async function getSnippet(
322338
api: CloudflareApi,
@@ -330,8 +346,11 @@ export async function getSnippet(
330346
}
331347

332348
/**
333-
* Get the content of a snippet
349+
* Retrieve the raw content of a Cloudflare snippet.
350+
*
334351
* @internal
352+
* @returns The snippet's raw JavaScript content as a string.
353+
* @throws When the Cloudflare API responds with an error for the snippet content request.
335354
*/
336355
export async function getSnippetContent(
337356
api: CloudflareApi,
@@ -350,8 +369,10 @@ export async function getSnippetContent(
350369
}
351370

352371
/**
353-
* List all snippets in a zone
372+
* List all snippets for the given zone.
373+
*
354374
* @internal
375+
* @returns An array of snippet metadata objects for the zone.
355376
*/
356377
export async function listSnippets(
357378
api: CloudflareApi,
@@ -364,8 +385,15 @@ export async function listSnippets(
364385
}
365386

366387
/**
367-
* Delete a snippet from Cloudflare
388+
* Remove a named snippet from a Cloudflare zone.
389+
*
390+
* Deletes the snippet identified by `snippetName` in the given `zoneId`. Treats a 404 response
391+
* and the Cloudflare 400 message "requested snippet not found" as successful deletions; other
392+
* non-OK responses are propagated via API error handling.
393+
*
368394
* @internal
395+
* @param zoneId - The Cloudflare zone identifier containing the snippet
396+
* @param snippetName - The name of the snippet to delete
369397
*/
370398
export async function deleteSnippet(
371399
api: CloudflareApi,
@@ -388,10 +416,10 @@ export async function deleteSnippet(
388416
}
389417

390418
/**
391-
* Validates that a snippet name meets Cloudflare requirements due to strong restrictions on the name.
392-
* Cloudflare Snippets only allow lowercase letters (a-z), numbers (0-9), and underscores (_)
419+
* Ensures a snippet name conforms to Cloudflare's naming rules.
393420
*
394-
* @throws Error if the name does not meet Cloudflare snippet requirements
421+
* @returns `true` if the name is valid.
422+
* @throws Error if the name is empty, longer than 255 characters, or contains characters other than lowercase letters (a-z), numbers (0-9), or underscores (_).
395423
* @internal
396424
*/
397425
export function validateSnippetName(name: string): boolean {
@@ -410,4 +438,4 @@ export function validateSnippetName(name: string): boolean {
410438
}
411439

412440
return true;
413-
}
441+
}

0 commit comments

Comments
 (0)