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

Playground header defaults #626

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions examples/cosmo-cargo/schema/shipments.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,38 @@
}
],
"components": {
"parameters": {
"CorrelationId": {
"name": "X-Correlation-ID",
"in": "header",
"description": "Unique identifier for tracking requests across multiple services",
"schema": {
"type": "string",
"format": "uuid"
},
"example": "123e4567-e89b-12d3-a456-426614174000"
},
"IdempotencyKey": {
"name": "Idempotency-Key",
"in": "header",
"description": "Unique key to ensure idempotency of the request",
"schema": {
"type": "string",
"format": "uuid"
},
"example": "550e8400-e29b-41d4-a716-446655440000"
},
"ApiVersion": {
"name": "X-API-Version",
"in": "header",
"description": "API version requested by the client",
"schema": {
"type": "string",
"enum": ["2024-01", "2023-12"],
"default": "2024-01"
}
}
},
"securitySchemes": {
"ApiKeyAuth": {
"type": "apiKey",
Expand Down Expand Up @@ -252,6 +284,27 @@
"summary": "Create a new shipment",
"description": "Creates a new shipment with the provided details",
"operationId": "createShipment",
"parameters": [
{
"$ref": "#/components/parameters/CorrelationId"
},
{
"$ref": "#/components/parameters/IdempotencyKey"
},
{
"$ref": "#/components/parameters/ApiVersion"
},
{
"name": "X-Request-Priority",
"in": "header",
"description": "Priority level for processing the shipment request",
"schema": {
"type": "string",
"enum": ["high", "normal", "low"],
"default": "normal"
}
}
],
"requestBody": {
"required": true,
"content": {
Expand Down Expand Up @@ -439,6 +492,22 @@
"description": "Get the current status and tracking information for a shipment",
"operationId": "trackShipment",
"parameters": [
{
"$ref": "#/components/parameters/CorrelationId"
},
{
"$ref": "#/components/parameters/ApiVersion"
},
{
"name": "X-Cache-Control",
"in": "header",
"description": "Caching behavior for the tracking response",
"schema": {
"type": "string",
"enum": ["no-cache", "max-age=60"],
"default": "max-age=60"
}
},
{
"name": "trackingNumber",
"in": "path",
Expand Down Expand Up @@ -934,6 +1003,22 @@
"description": "Update or add customs documentation for international shipments",
"operationId": "updateCustoms",
"parameters": [
{
"$ref": "#/components/parameters/CorrelationId"
},
{
"$ref": "#/components/parameters/IdempotencyKey"
},
{
"name": "X-Customs-Region",
"in": "header",
"description": "Customs processing region for the shipment",
"schema": {
"type": "string",
"enum": ["EU", "NA", "APAC"],
"default": "NA"
}
},
{
"name": "shipmentId",
"in": "path",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { FragmentType, useFragment } from "./graphql/index.js";
import { SchemaView } from "./schema/SchemaView.js";
import { methodForColor } from "./util/methodToColor.js";

export const PARAM_GROUPS = ["path", "query", "header", "cookie"] as const;
const PARAM_GROUPS = ["path", "query", "header", "cookie"] as const;
export type ParameterGroup = (typeof PARAM_GROUPS)[number];

export const OperationListItem = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ export const PlaygroundDialogWrapper = ({
}) => {
const headers = operation.parameters
?.filter((p) => p.in === "header")
.sort((a, b) => (a.required && !b.required ? -1 : 1))
.map((p) => ({
name: p.name,
defaultValue: p.examples?.find((x) => x.value)?.value ?? "",
defaultActive: false,
defaultValue:
p.schema?.default ?? p.examples?.find((x) => x.value)?.value ?? "",
defaultActive: p.required ?? false,
isRequired: p.required ?? false,
enum: p.schema?.type == "array" ? p.schema?.items?.enum : p.schema?.enum,
type: p.schema?.type ?? "string",
}));
const queryParams = operation.parameters
?.filter((p) => p.in === "query")
Expand Down
14 changes: 5 additions & 9 deletions packages/zudoku/src/lib/plugins/openapi/playground/Headers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
Controller,
useFieldArray,
useFormContext,
UseFormRegister,
} from "react-hook-form";
import { Card } from "zudoku/ui/Card.js";
import { Checkbox } from "zudoku/ui/Checkbox.js";
Expand Down Expand Up @@ -44,13 +43,7 @@ const headerOptions = Object.freeze([
"X-Requested-With",
]);

export const Headers = ({
control,
register,
}: {
register: UseFormRegister<PlaygroundForm>;
control: Control<PlaygroundForm>;
}) => {
export const Headers = ({ control }: { control: Control<PlaygroundForm> }) => {
const { fields, append, remove } = useFieldArray<PlaygroundForm>({
control,
name: "headers",
Expand Down Expand Up @@ -81,7 +74,10 @@ export const Headers = ({
<Card className="overflow-hidden">
<ParamsGrid>
{fields.map((header, i) => (
<div key={i} className="group grid col-span-full grid-cols-subgrid">
<div
key={header.name}
className="group grid col-span-full grid-cols-subgrid"
>
<div className="flex items-center gap-2 ">
<Controller
control={control}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export type Header = {
name: string;
defaultValue?: string;
defaultActive?: boolean;
isRequired?: boolean;
enum?: string[];
type?: string;
};

export type QueryParam = {
Expand Down Expand Up @@ -337,7 +340,7 @@ export const Playground = ({
</TabsList>
</div>
<TabsContent value="headers">
<Headers control={control} register={register} />
<Headers control={control} />
</TabsContent>
<TabsContent value="parameters">
{pathParams.length > 0 && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ const PlaygroundDialog = (props: PlaygroundDialogProps) => {
<Dialog onOpenChange={(open) => setOpen(open)}>
<DialogTrigger asChild>
{props.children ?? (
<button className="flex gap-1 items-center px-2 py-1 rounded-md transition text-xs bg-primary text-primary-foreground shadow-sm hover:bg-primary/80">
<button
type="button"
className="flex gap-1 items-center px-2 py-1 rounded-md transition text-xs bg-primary text-primary-foreground shadow-sm hover:bg-primary/80"
>
Test
<HeroPlayIcon className="" size={14} />
<HeroPlayIcon size={14} />
</button>
)}
</DialogTrigger>
Expand Down