Skip to content

Open-source status page, incident response and uptime monitoring

feat: curl builder tool (#1098)

Maximilian Kaskef2ee3bac7c1e
diff --git a/apps/web/public/assets/changelog/curl-builder-playground.png b/apps/web/public/assets/changelog/curl-builder-playground.png
new file mode 100644
index 000000000..17dc2316b
Binary files /dev/null and b/apps/web/public/assets/changelog/curl-builder-playground.png differ
diff --git a/apps/web/src/app/play/checker/page.tsx b/apps/web/src/app/play/checker/page.tsx
index 405a94d3e..de4cacec6 100644
--- a/apps/web/src/app/play/checker/page.tsx
+++ b/apps/web/src/app/play/checker/page.tsx
@@ -1,5 +1,10 @@
import type { Metadata } from "next";
+import {
+ defaultMetadata,
+ ogMetadata,
+ twitterMetadata,
+} from "@/app/shared-metadata";
import { BottomCTA } from "@/components/marketing/in-between-cta";
import { getCheckerDataById } from "@/components/ping-response-analysis/utils";
import { redirect } from "next/navigation";
@@ -8,14 +13,25 @@ import { GlobalMonitoring } from "./_components/global-monitoring";
import { Testimonial } from "./_components/testimonial";
import { searchParamsCache } from "./search-params";
+const title = "Global Speed Checker";
+const description =
+ "Test the performance of your api, website from multiple regions. Get speed insights for free.";
+
export const metadata: Metadata = {
- title: "Global Speed Checker",
- description:
- "Test the performance of your api, website from multiple regions. Get speed insights for free.",
+ ...defaultMetadata,
+ title,
+ description,
+ twitter: {
+ ...twitterMetadata,
+ title,
+ description,
+ images: [`/api/og?title=${title}&description=${description}`],
+ },
openGraph: {
- title: "Global Speed Checker",
- description:
- "Test the performance of your api, website from multiple regions. Get speed insights for free.",
+ ...ogMetadata,
+ title,
+ description,
+ images: [`/api/og?title=${title}&description=${description}`],
},
};
diff --git a/apps/web/src/app/play/curl/_components/curl-form.tsx b/apps/web/src/app/play/curl/_components/curl-form.tsx
new file mode 100644
index 000000000..3acaf2eb6
--- /dev/null
+++ b/apps/web/src/app/play/curl/_components/curl-form.tsx
@@ -0,0 +1,297 @@
+"use client";
+
+import { useDebounce } from "@/hooks/use-debounce";
+import { toast } from "@/lib/toast";
+import { copyToClipboard } from "@/lib/utils";
+import { zodResolver } from "@hookform/resolvers/zod";
+import {
+ Button,
+ Checkbox,
+ Form,
+ FormControl,
+ FormDescription,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormMessage,
+ Input,
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+ Separator,
+ Textarea,
+} from "@openstatus/ui";
+import { X } from "lucide-react";
+import { useCallback } from "react";
+import { useFieldArray, useForm } from "react-hook-form";
+import { z } from "zod";
+
+const methods = ["GET", "POST", "PUT", "PATCH", "DELETE"] as const;
+
+const formSchema = z.object({
+ method: z.enum(methods).default("GET"),
+ url: z.string().url().default(""),
+ body: z.string().default(""),
+ verbose: z.boolean().default(false),
+ insecure: z.boolean().default(false),
+ json: z.boolean().default(false),
+ headers: z
+ .array(z.object({ key: z.string(), value: z.string() }))
+ .default([]),
+});
+
+export function CurlForm({
+ defaultValues,
+}: {
+ defaultValues?: z.infer<typeof formSchema>;
+}) {
+ const form = useForm<z.infer<typeof formSchema>>({
+ resolver: zodResolver(formSchema),
+ defaultValues,
+ });
+ const { fields, append, remove } = useFieldArray({
+ name: "headers",
+ control: form.control,
+ });
+
+ const formValues = form.watch();
+ const debouncedformValues = useDebounce(formValues, 300);
+
+ const onSubmit = useCallback((values: z.infer<typeof formSchema>) => {
+ copyToClipboard(generateCurlCommand(values));
+ toast("CURL copied to clipboard");
+ }, []);
+
+ return (
+ <Form {...form}>
+ <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-3">
+ <FormField
+ control={form.control}
+ name="method"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Method</FormLabel>
+ <Select onValueChange={field.onChange} defaultValue={field.value}>
+ <FormControl>
+ <SelectTrigger>
+ <SelectValue placeholder="Select an HTTP method" />
+ </SelectTrigger>
+ </FormControl>
+ <SelectContent>
+ {methods.map((method) => (
+ <SelectItem key={method} value={method}>
+ {method}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ <FormDescription>
+ Specify the request method to use.
+ </FormDescription>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="url"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>URL</FormLabel>
+ <FormControl>
+ <Input placeholder="https://openstat.us" {...field} />
+ </FormControl>
+ <FormDescription>The URL to send the request to.</FormDescription>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <div className="space-y-2 sm:col-span-full">
+ <FormLabel>Request Header</FormLabel>
+ {fields.map((field, index) => (
+ <div key={field.id} className="grid grid-cols-6 gap-4">
+ <FormField
+ control={form.control}
+ name={`headers.${index}.key`}
+ render={({ field }) => (
+ <FormItem className="col-span-2">
+ <FormControl>
+ <Input placeholder="Authorization" {...field} />
+ </FormControl>
+ </FormItem>
+ )}
+ />
+ <div className="col-span-4 flex items-center space-x-2">
+ <FormField
+ control={form.control}
+ name={`headers.${index}.value`}
+ render={({ field }) => (
+ <FormItem className="w-full">
+ <FormControl>
+ <Input placeholder="Bearer <your-token>" {...field} />
+ </FormControl>
+ </FormItem>
+ )}
+ />
+ <Button
+ size="icon"
+ variant="ghost"
+ type="button"
+ onClick={() => remove(index)}
+ >
+ <X className="h-4 w-4" />
+ </Button>
+ </div>
+ </div>
+ ))}
+ <div>
+ <Button
+ type="button"
+ variant="secondary"
+ size="sm"
+ onClick={() => append({ key: "", value: "" })}
+ >
+ Add a custom header
+ </Button>
+ </div>
+ </div>
+ <FormField
+ control={form.control}
+ name="body"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Body</FormLabel>
+ <FormControl>
+ <Textarea
+ placeholder={`{ "status": "operational" }`}
+ {...field}
+ />
+ </FormControl>
+ <FormDescription>
+ The data payload to send with the request.
+ </FormDescription>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <Separator />
+ <FormField
+ control={form.control}
+ name="json"
+ render={({ field }) => (
+ <FormItem className="flex flex-row items-start space-x-3 space-y-0">
+ <FormControl>
+ <Checkbox
+ checked={field.value}
+ onCheckedChange={field.onChange}
+ />
+ </FormControl>
+ <div className="space-y-1 leading-none">
+ <FormLabel>JSON Content-Type</FormLabel>
+ <FormDescription>
+ Set the <code>Content-Type</code> header to{" "}
+ <code>application/json</code>.
+ </FormDescription>
+ </div>
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="verbose"
+ render={({ field }) => (
+ <FormItem className="flex flex-row items-start space-x-3 space-y-0">
+ <FormControl>
+ <Checkbox
+ checked={field.value}
+ onCheckedChange={field.onChange}
+ />
+ </FormControl>
+ <div className="space-y-1 leading-none">
+ <FormLabel>Verbose</FormLabel>
+ <FormDescription>
+ Make the operation more talkative.
+ </FormDescription>
+ </div>
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="insecure"
+ render={({ field }) => (
+ <FormItem className="flex flex-row items-start space-x-3 space-y-0">
+ <FormControl>
+ <Checkbox
+ checked={field.value}
+ onCheckedChange={field.onChange}
+ />
+ </FormControl>
+ <div className="space-y-1 leading-none">
+ <FormLabel>Accept self-signed certificats</FormLabel>
+ <FormDescription>
+ Allow insecure server connections.
+ </FormDescription>
+ </div>
+ </FormItem>
+ )}
+ />
+ <Separator />
+ <Textarea
+ value={generateCurlCommand(debouncedformValues)}
+ className="font-mono"
+ aria-readonly
+ readOnly
+ />
+ <Button type="submit" className="w-full">
+ Copy to Clipboard
+ </Button>
+ </form>
+ </Form>
+ );
+}
+
+function generateCurlCommand(form: z.infer<typeof formSchema>) {
+ const { method, url, body, verbose, insecure, json, headers } = form;
+
+ let curlCommand = "curl";
+
+ if (method) {
+ curlCommand += ` -X ${method}`;
+ }
+
+ if (url) {
+ curlCommand += ` "${url}" \\\n`;
+ } else {
+ // force a new line if there is no URL
+ curlCommand += " \\\n";
+ }
+
+ for (const header of headers) {
+ const { key, value } = header;
+ if (key && value) {
+ curlCommand += ` -H "${key}: ${value}" \\\n`;
+ }
+ }
+
+ if (json) {
+ curlCommand += ' -H "Content-Type: application/json" \\\n';
+ }
+
+ if (body?.trim()) {
+ curlCommand += ` -d '${body.trim()}' \\\n`;
+ }
+
+ if (verbose) {
+ curlCommand += " -v \\\n";
+ }
+
+ if (insecure) {
+ curlCommand += " -k \\\n";
+ }
+
+ // Remove the trailing ` \` at the end
+ return curlCommand.trim().slice(0, -2);
+}
diff --git a/apps/web/src/app/play/curl/page.tsx b/apps/web/src/app/play/curl/page.tsx
new file mode 100644
index 000000000..dd9c0e8bc
--- /dev/null
+++ b/apps/web/src/app/play/curl/page.tsx
@@ -0,0 +1,64 @@
+import {
+ defaultMetadata,
+ ogMetadata,
+ twitterMetadata,
+} from "@/app/shared-metadata";
+import {
+ CardContainer,
+ CardDescription,
+ CardHeader,
+ CardIcon,
+ CardTitle,
+} from "@/components/marketing/card";
+import type { Metadata } from "next";
+import { CurlForm } from "./_components/curl-form";
+
+const title = "cURL Builder";
+const description =
+ "An online curl command line builder. Generate curl commands to test your API endpoints.";
+
+export const metadata: Metadata = {
+ ...defaultMetadata,
+ title,
+ description,
+ twitter: {
+ ...twitterMetadata,
+ title,
+ description,
+ images: [`/api/og?title=${title}&description=${description}`],
+ },
+ openGraph: {
+ ...ogMetadata,
+ title,
+ description,
+ images: [`/api/og?title=${title}&description=${description}`],
+ },
+};
+
+export default function CurlBuilder() {
+ return (
+ <CardContainer>
+ <CardHeader>
+ <CardIcon icon="terminal" />
+ <CardTitle>cURL Builder</CardTitle>
+ <CardDescription className="max-w-md">
+ An online curl command line builder. Generate curl commands to test
+ your API endpoints.
+ </CardDescription>
+ </CardHeader>
+ <div className="mx-auto grid w-full max-w-xl gap-6">
+ <CurlForm
+ defaultValues={{
+ method: "GET",
+ url: "",
+ body: "",
+ verbose: false,
+ insecure: false,
+ json: false,
+ headers: [],
+ }}
+ />
+ </div>
+ </CardContainer>
+ );
+}
diff --git a/apps/web/src/app/play/curl/search-params.ts b/apps/web/src/app/play/curl/search-params.ts
new file mode 100644
index 000000000..0c20ad993
--- /dev/null
+++ b/apps/web/src/app/play/curl/search-params.ts
@@ -0,0 +1,29 @@
+import {
+ createSearchParamsCache,
+ parseAsArrayOf,
+ parseAsBoolean,
+ parseAsJson,
+ parseAsString,
+ parseAsStringLiteral,
+} from "nuqs/server";
+import { z } from "zod";
+
+// REMINDER: not used, but could be useful for future reference
+export const searchParamsParsers = {
+ method: parseAsStringLiteral(["GET", "POST", "PUT", "PATCH", "DELETE"]),
+ url: parseAsString,
+ body: parseAsString,
+ verbose: parseAsBoolean,
+ insecure: parseAsBoolean,
+ json: parseAsBoolean,
+ headers: parseAsArrayOf(
+ parseAsJson(
+ z.object({
+ key: z.string(),
+ value: z.string(),
+ }).parse,
+ ),
+ ),
+};
+
+export const searchParamsCache = createSearchParamsCache(searchParamsParsers);
diff --git a/apps/web/src/app/play/page.tsx b/apps/web/src/app/play/page.tsx
index 2fa8c3938..bee3a759f 100644
--- a/apps/web/src/app/play/page.tsx
+++ b/apps/web/src/app/play/page.tsx
@@ -6,6 +6,7 @@ import {
Palette,
PanelTop,
Table,
+ Terminal,
} from "lucide-react";
import type { Metadata } from "next";
@@ -59,6 +60,12 @@ const playgrounds: CardProps[] = [
icon: Gauge,
variant: "primary",
},
+ {
+ href: "/play/curl",
+ title: "cURL Builder",
+ description: "Easily generate curl commands to test your API endpoints.",
+ icon: Terminal,
+ },
{
href: "/public/monitors/1",
title: "Public Dashboard",
diff --git a/apps/web/src/app/shared-metadata.ts b/apps/web/src/app/shared-metadata.ts
index 707e66031..b9f762678 100644
--- a/apps/web/src/app/shared-metadata.ts
+++ b/apps/web/src/app/shared-metadata.ts
@@ -2,7 +2,7 @@ import type { Metadata } from "next";
export const TITLE = "OpenStatus";
export const DESCRIPTION =
- "A better way to monitor your API and your website. Don't let downtime or a slow response time ruin your user experience. Try OpenStatus the open-source synthetic monitoring platform for free!";
+ "A better way to monitor your API and your website. Don't let downtime or a slow response time ruin your user experience. Try the open-source synthetic monitoring platform for free!";
export const defaultMetadata: Metadata = {
title: {
diff --git a/apps/web/src/components/icons.tsx b/apps/web/src/components/icons.tsx
index 4ff4d4da1..45473e92e 100644
--- a/apps/web/src/components/icons.tsx
+++ b/apps/web/src/components/icons.tsx
@@ -51,6 +51,7 @@ import {
SunMedium,
Table,
Tag,
+ Terminal,
Timer,
ToyBrick,
Trash,
@@ -89,6 +90,7 @@ export const Icons = {
tag: Tag,
trash: Trash,
twitter: TwitterIcon,
+ terminal: Terminal,
globe: Globe,
plug: Plug,
copy: Copy,
diff --git a/apps/web/src/components/layout/marketing-footer.tsx b/apps/web/src/components/layout/marketing-footer.tsx
index 17b19311e..fc65f05f5 100644
--- a/apps/web/src/components/layout/marketing-footer.tsx
+++ b/apps/web/src/components/layout/marketing-footer.tsx
@@ -50,6 +50,7 @@ export function MarketingFooter({ className }: Props) {
<div className="order-3 flex flex-col gap-3 text-sm">
<p className="font-semibold text-foreground">Tools</p>
<FooterLink href="/play/checker" label="Speed Checker" />
+ <FooterLink href="/play/curl" label="cURL Builder" />
<FooterLink href="https://openstat.us" label="All Status Codes" />
</div>
</div>
diff --git a/apps/web/src/content/changelog/curl-builder-playground.mdx b/apps/web/src/content/changelog/curl-builder-playground.mdx
new file mode 100644
index 000000000..70ba4f818
--- /dev/null
+++ b/apps/web/src/content/changelog/curl-builder-playground.mdx
@@ -0,0 +1,10 @@
+---
+title: cURL Builder
+description: An online curl command line builder. Generate curl commands to test your API endpoints.
+image: /assets/changelog/curl-builder-playground.png
+publishedAt: 2024-11-14
+---
+
+An online curl command line builder. Generate curl commands to test your API endpoints.
+
+Go to the **[Playground](/play/curl)** and test it.
\ No newline at end of file