Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added JsDoc example to fetchValues function #497

Closed
wants to merge 7 commits into from
25 changes: 25 additions & 0 deletions utils/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@
import type { RedirectStatus, Status } from "std/http/http_status.ts";

/**
* Generates a redirection response to the specified location with an optional status code.
*
* @param location A relative (to the request URL) or absolute URL.
* @param status HTTP status
*
* @example
* // Redirect to a new page with a default status code (303 - See Other).
* const redirectResponse = redirect("/new-page");
*
* // Redirect to a different URL with a custom status code (301 - Moved Permanently).
* const customRedirectResponse = redirect("/different-url", 301);
*
* // Use the response object to initiate the redirection.
* event.respondWith(redirectResponse);
* // or
* event.respondWith(customRedirectResponse);
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Location}
*/
export function redirect(
Expand All @@ -18,6 +33,16 @@ export function redirect(
});
}

/**
* Retrieves the "cursor" parameter value from the given URL's query string.
*
* @example
* const urlString = "https://example.com/data?cursor=12345&page=1";
* const url = new URL(urlString);
*
* const cursorValue = getCursor(url);
* console.log(cursorValue); // Output: "12345"
*/
export function getCursor(url: URL) {
return url.searchParams.get("cursor") ?? "";
}
11 changes: 11 additions & 0 deletions utils/islands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
// Copyright 2023 the Deno authors. All rights reserved. MIT license.

/**
* Asynchronously fetches values from the specified endpoint with an optional cursor.
*
* @example
* // Fetch values without a cursor
* const valuesWithoutCursor = await fetchValues("/api/data", "");
*
* // Fetch values with a cursor
* const cursor = "123456";
* const valuesWithCursor = await fetchValues("/api/data", cursor);
*/
export async function fetchValues<T>(endpoint: string, cursor: string) {
let url = endpoint;
if (cursor !== "") url += "?cursor=" + cursor;
Expand Down
49 changes: 40 additions & 9 deletions utils/payments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,47 @@ export type StripProductWithPrice = Stripe.Product & {
default_price: Stripe.Price;
};

export function isProductWithPrice(
product: Stripe.Product,
): product is StripProductWithPrice {
return product.default_price !== undefined &&


/**
* Checks if a Stripe product has a valid price.
*
* @example
* const product = {
* id: 'prod_123',
* name: 'Example Product',
* default_price: 1000, // A valid numeric price
* // ... other product properties
* };
*
* if (isProductWithPrice(product)) {
* console.log('This product has a valid price.');
* } else {
* console.log('This product does not have a valid price.');
* }
*/
export function isProductWithPrice(product: Stripe.Product): product is StripProductWithPrice {
return (
product.default_price !== undefined &&
product.default_price !== null &&
typeof product.default_price !== "string";
typeof product.default_price !== "string"
);
}

export function formatAmountForDisplay(
amount: number,
currency: string,
): string {

/**
* Formats a numeric amount for display as a currency with the specified currency code.
*
* @example
* const amount = 1234.56; // Numeric amount
* const currencyCode = 'USD'; // Currency code
*
* const formattedAmount = formatAmountForDisplay(amount, currencyCode);
* console.log(formattedAmount); // Output: "$1,235" (formatted as USD currency)
*
* @see {@link https://developer.mozilla.org/en-US/search?q=NumberFormat%257CIntl.NumberFormat on MDN}
*/
export function formatAmountForDisplay(amount: number, currency: string): string {
const numberFormat = new Intl.NumberFormat(
navigator.language,
{
Expand All @@ -55,3 +84,5 @@ export function formatAmountForDisplay(
);
return numberFormat.format(amount);
}


44 changes: 44 additions & 0 deletions utils/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ export interface Post {
summary: string;
}

/**
* Asynchronously retrieves a list of posts from the "./data/posts" directory and sorts them by published date.
*
* @returns {Promise<Post[]>} - A promise that resolves to an array of Post objects sorted by published date.
*
* @example
* // Retrieve and display a list of posts
* try {
* const posts = await getPosts();
* console.log("List of Posts:");
* for (const post of posts) {
* console.log(`Title: ${post.title}`);
* console.log(`Published At: ${post.publishedAt}`);
* console.log(`Content: ${post.content}`);
* console.log("------");
* }
* } catch (error) {
* console.error("Error fetching posts:", error);
* }
*/
export async function getPosts(): Promise<Post[]> {
const files = Deno.readDir("./data/posts");
const promises = [];
Expand All @@ -22,6 +42,30 @@ export async function getPosts(): Promise<Post[]> {
return posts;
}

/**
* Asynchronously retrieves a post by its slug from the "./data/posts" directory and returns it as a Post object.
* If no post with the given slug is found, it returns null.
*
* @param {string} slug - The slug of the post to retrieve.
* @returns {Promise<Post | null>} - A promise that resolves to the retrieved Post object or null if not found.
*
* @example
* // Retrieve a post by its slug
* const postSlug = "example-post";
*
* try {
* const post = await getPost(postSlug);
* if (post) {
* console.log("Post Title:", post.title);
* console.log("Published At:", post.publishedAt);
* console.log("Content:", post.content);
* } else {
* console.log("Post not found.");
* }
* } catch (error) {
* console.error("Error fetching post:", error);
* }
*/
export async function getPost(slug: string): Promise<Post | null> {
try {
const text = await Deno.readTextFile(
Expand Down