From e1c01a4d6ec510fd049195f0885b64018779f48f Mon Sep 17 00:00:00 2001 From: Daniel Lehr Date: Thu, 30 Jan 2025 14:39:16 +0100 Subject: [PATCH] Playground header defaults --- examples/cosmo-cargo/schema/shipments.json | 85 +++++++++++++++++++ .../lib/plugins/openapi/OperationListItem.tsx | 2 +- .../openapi/PlaygroundDialogWrapper.tsx | 9 +- .../plugins/openapi/playground/Headers.tsx | 14 ++- .../plugins/openapi/playground/Playground.tsx | 5 +- .../openapi/playground/PlaygroundDialog.tsx | 7 +- 6 files changed, 107 insertions(+), 15 deletions(-) diff --git a/examples/cosmo-cargo/schema/shipments.json b/examples/cosmo-cargo/schema/shipments.json index 8f96e841..1954de93 100644 --- a/examples/cosmo-cargo/schema/shipments.json +++ b/examples/cosmo-cargo/schema/shipments.json @@ -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", @@ -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": { @@ -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", @@ -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", diff --git a/packages/zudoku/src/lib/plugins/openapi/OperationListItem.tsx b/packages/zudoku/src/lib/plugins/openapi/OperationListItem.tsx index fba4de49..68399c9a 100644 --- a/packages/zudoku/src/lib/plugins/openapi/OperationListItem.tsx +++ b/packages/zudoku/src/lib/plugins/openapi/OperationListItem.tsx @@ -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 = ({ diff --git a/packages/zudoku/src/lib/plugins/openapi/PlaygroundDialogWrapper.tsx b/packages/zudoku/src/lib/plugins/openapi/PlaygroundDialogWrapper.tsx index cd6f5cca..61cdf27c 100644 --- a/packages/zudoku/src/lib/plugins/openapi/PlaygroundDialogWrapper.tsx +++ b/packages/zudoku/src/lib/plugins/openapi/PlaygroundDialogWrapper.tsx @@ -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") diff --git a/packages/zudoku/src/lib/plugins/openapi/playground/Headers.tsx b/packages/zudoku/src/lib/plugins/openapi/playground/Headers.tsx index 7f99234c..a7f0c2e8 100644 --- a/packages/zudoku/src/lib/plugins/openapi/playground/Headers.tsx +++ b/packages/zudoku/src/lib/plugins/openapi/playground/Headers.tsx @@ -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"; @@ -44,13 +43,7 @@ const headerOptions = Object.freeze([ "X-Requested-With", ]); -export const Headers = ({ - control, - register, -}: { - register: UseFormRegister; - control: Control; -}) => { +export const Headers = ({ control }: { control: Control }) => { const { fields, append, remove } = useFieldArray({ control, name: "headers", @@ -81,7 +74,10 @@ export const Headers = ({ {fields.map((header, i) => ( -
+
- + {pathParams.length > 0 && ( diff --git a/packages/zudoku/src/lib/plugins/openapi/playground/PlaygroundDialog.tsx b/packages/zudoku/src/lib/plugins/openapi/playground/PlaygroundDialog.tsx index c3abae30..8a3e9595 100644 --- a/packages/zudoku/src/lib/plugins/openapi/playground/PlaygroundDialog.tsx +++ b/packages/zudoku/src/lib/plugins/openapi/playground/PlaygroundDialog.tsx @@ -39,9 +39,12 @@ const PlaygroundDialog = (props: PlaygroundDialogProps) => { setOpen(open)}> {props.children ?? ( - )}