@alepha/ui 0.35.0 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -1
- package/dist/account/AccountBrand.d.ts +10 -3
- package/dist/account/AccountBrand.d.ts.map +1 -1
- package/dist/account/AccountBrand.js +12 -6
- package/dist/account/AccountBrand.js.map +1 -1
- package/dist/account/AccountLayout.d.ts.map +1 -1
- package/dist/account/AccountLayout.js +1 -1
- package/dist/account/AccountLayout.js.map +1 -1
- package/dist/admin/AdminUserDetail.d.ts +3 -3
- package/dist/admin/AdminUserDetail.js +3 -3
- package/dist/admin/AdminUserDetail.js.map +1 -1
- package/dist/ai/AiChat.d.ts +36 -6
- package/dist/ai/AiChat.d.ts.map +1 -1
- package/dist/ai/AiChat.js +101 -63
- package/dist/ai/AiChat.js.map +1 -1
- package/dist/ai/AiChatComposer.d.ts +22 -0
- package/dist/ai/AiChatComposer.d.ts.map +1 -0
- package/dist/ai/AiChatComposer.js +75 -0
- package/dist/ai/AiChatComposer.js.map +1 -0
- package/dist/ai/AiChatConfirmCard.d.ts +5 -2
- package/dist/ai/AiChatConfirmCard.d.ts.map +1 -1
- package/dist/ai/AiChatConfirmCard.js +7 -6
- package/dist/ai/AiChatConfirmCard.js.map +1 -1
- package/dist/ai/AiChatConfirmFields.d.ts +9 -0
- package/dist/ai/AiChatConfirmFields.d.ts.map +1 -0
- package/dist/ai/AiChatConfirmFields.js +45 -0
- package/dist/ai/AiChatConfirmFields.js.map +1 -0
- package/dist/ai/AiChatEmpty.d.ts +26 -0
- package/dist/ai/AiChatEmpty.d.ts.map +1 -0
- package/dist/ai/AiChatEmpty.js +47 -0
- package/dist/ai/AiChatEmpty.js.map +1 -0
- package/dist/ai/AiChatHistory.d.ts +3 -2
- package/dist/ai/AiChatHistory.d.ts.map +1 -1
- package/dist/ai/AiChatHistory.js +51 -22
- package/dist/ai/AiChatHistory.js.map +1 -1
- package/dist/ai/AiChatMessage.d.ts +14 -0
- package/dist/ai/AiChatMessage.d.ts.map +1 -1
- package/dist/ai/AiChatMessage.js +20 -12
- package/dist/ai/AiChatMessage.js.map +1 -1
- package/dist/ai/index.d.ts +3 -0
- package/dist/ai/index.d.ts.map +1 -1
- package/dist/ai/index.js +4 -1
- package/dist/ai/useAiChat.d.ts +4 -0
- package/dist/ai/useAiChat.d.ts.map +1 -1
- package/dist/ai/useAiChat.js +15 -1
- package/dist/ai/useAiChat.js.map +1 -1
- package/dist/core/Sidebar.js +2 -2
- package/dist/core/Sidebar.js.map +1 -1
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +2 -1
- package/dist/core/useToast.d.ts +22 -1
- package/dist/core/useToast.d.ts.map +1 -1
- package/dist/core/useToast.js +4 -1
- package/dist/core/useToast.js.map +1 -1
- package/dist/core/useUndoable.d.ts +93 -0
- package/dist/core/useUndoable.d.ts.map +1 -0
- package/dist/core/useUndoable.js +172 -0
- package/dist/core/useUndoable.js.map +1 -0
- package/dist/i18n/fr/uiFr.d.ts.map +1 -1
- package/dist/i18n/fr/uiFr.js +50 -1
- package/dist/i18n/fr/uiFr.js.map +1 -1
- package/dist/markdown/MarkdownView.d.ts +15 -0
- package/dist/markdown/MarkdownView.d.ts.map +1 -1
- package/dist/markdown/MarkdownView.js +4 -1
- package/dist/markdown/MarkdownView.js.map +1 -1
- package/dist/organizations/useInviteOrganizationMember.d.ts +2 -2
- package/dist/organizations/useInviteOrganizationMember.js.map +1 -1
- package/dist/shell/AppShellSidebarNavItem.d.ts.map +1 -1
- package/dist/shell/AppShellSidebarNavItem.js +25 -2
- package/dist/shell/AppShellSidebarNavItem.js.map +1 -1
- package/dist/shell/PlateTabBar.d.ts.map +1 -1
- package/dist/shell/PlateTabBar.js +2 -2
- package/dist/shell/PlateTabBar.js.map +1 -1
- package/dist/shell/SidebarBackHeader.d.ts +35 -0
- package/dist/shell/SidebarBackHeader.d.ts.map +1 -0
- package/dist/shell/SidebarBackHeader.js +34 -0
- package/dist/shell/SidebarBackHeader.js.map +1 -0
- package/dist/shell/appShellNav.d.ts +30 -0
- package/dist/shell/appShellNav.d.ts.map +1 -1
- package/dist/shell/appShellNav.js.map +1 -1
- package/dist/shell/index.d.ts +2 -1
- package/dist/shell/index.d.ts.map +1 -1
- package/dist/shell/index.js +2 -1
- package/dist/swagger/$uiSwagger.d.ts +75 -0
- package/dist/swagger/$uiSwagger.d.ts.map +1 -0
- package/dist/swagger/$uiSwagger.js +51 -0
- package/dist/swagger/$uiSwagger.js.map +1 -0
- package/dist/swagger/OpenApiSpec.d.ts +113 -0
- package/dist/swagger/OpenApiSpec.d.ts.map +1 -0
- package/dist/swagger/OpenApiSpec.js +186 -0
- package/dist/swagger/OpenApiSpec.js.map +1 -0
- package/dist/swagger/SwaggerAccessBadge.d.ts +16 -0
- package/dist/swagger/SwaggerAccessBadge.d.ts.map +1 -0
- package/dist/swagger/SwaggerAccessBadge.js +40 -0
- package/dist/swagger/SwaggerAccessBadge.js.map +1 -0
- package/dist/swagger/SwaggerAlephaMeta.d.ts +11 -0
- package/dist/swagger/SwaggerAlephaMeta.d.ts.map +1 -0
- package/dist/swagger/SwaggerAlephaMeta.js +103 -0
- package/dist/swagger/SwaggerAlephaMeta.js.map +1 -0
- package/dist/swagger/SwaggerCredentials.d.ts +11 -0
- package/dist/swagger/SwaggerCredentials.d.ts.map +1 -0
- package/dist/swagger/SwaggerCredentials.js +35 -0
- package/dist/swagger/SwaggerCredentials.js.map +1 -0
- package/dist/swagger/SwaggerExplorer.d.ts +31 -0
- package/dist/swagger/SwaggerExplorer.d.ts.map +1 -0
- package/dist/swagger/SwaggerExplorer.js +156 -0
- package/dist/swagger/SwaggerExplorer.js.map +1 -0
- package/dist/swagger/SwaggerMethodBadge.d.ts +10 -0
- package/dist/swagger/SwaggerMethodBadge.d.ts.map +1 -0
- package/dist/swagger/SwaggerMethodBadge.js +27 -0
- package/dist/swagger/SwaggerMethodBadge.js.map +1 -0
- package/dist/swagger/SwaggerNav.d.ts +16 -0
- package/dist/swagger/SwaggerNav.d.ts.map +1 -0
- package/dist/swagger/SwaggerNav.js +63 -0
- package/dist/swagger/SwaggerNav.js.map +1 -0
- package/dist/swagger/SwaggerOperationView.d.ts +22 -0
- package/dist/swagger/SwaggerOperationView.d.ts.map +1 -0
- package/dist/swagger/SwaggerOperationView.js +89 -0
- package/dist/swagger/SwaggerOperationView.js.map +1 -0
- package/dist/swagger/SwaggerParameters.d.ts +11 -0
- package/dist/swagger/SwaggerParameters.d.ts.map +1 -0
- package/dist/swagger/SwaggerParameters.js +80 -0
- package/dist/swagger/SwaggerParameters.js.map +1 -0
- package/dist/swagger/SwaggerRequestBuilder.d.ts +83 -0
- package/dist/swagger/SwaggerRequestBuilder.d.ts.map +1 -0
- package/dist/swagger/SwaggerRequestBuilder.js +231 -0
- package/dist/swagger/SwaggerRequestBuilder.js.map +1 -0
- package/dist/swagger/SwaggerResponseView.d.ts +25 -0
- package/dist/swagger/SwaggerResponseView.d.ts.map +1 -0
- package/dist/swagger/SwaggerResponseView.js +94 -0
- package/dist/swagger/SwaggerResponseView.js.map +1 -0
- package/dist/swagger/SwaggerResponses.d.ts +12 -0
- package/dist/swagger/SwaggerResponses.d.ts.map +1 -0
- package/dist/swagger/SwaggerResponses.js +75 -0
- package/dist/swagger/SwaggerResponses.js.map +1 -0
- package/dist/swagger/SwaggerSchemaView.d.ts +19 -0
- package/dist/swagger/SwaggerSchemaView.d.ts.map +1 -0
- package/dist/swagger/SwaggerSchemaView.js +98 -0
- package/dist/swagger/SwaggerSchemaView.js.map +1 -0
- package/dist/swagger/SwaggerSection.d.ts +14 -0
- package/dist/swagger/SwaggerSection.d.ts.map +1 -0
- package/dist/swagger/SwaggerSection.js +23 -0
- package/dist/swagger/SwaggerSection.js.map +1 -0
- package/dist/swagger/SwaggerTryIt.d.ts +31 -0
- package/dist/swagger/SwaggerTryIt.d.ts.map +1 -0
- package/dist/swagger/SwaggerTryIt.js +68 -0
- package/dist/swagger/SwaggerTryIt.js.map +1 -0
- package/dist/swagger/SwaggerTryItForm.d.ts +20 -0
- package/dist/swagger/SwaggerTryItForm.d.ts.map +1 -0
- package/dist/swagger/SwaggerTryItForm.js +52 -0
- package/dist/swagger/SwaggerTryItForm.js.map +1 -0
- package/dist/swagger/index.d.ts +29 -0
- package/dist/swagger/index.d.ts.map +1 -0
- package/dist/swagger/index.js +18 -0
- package/dist/swagger/useSwaggerAccess.d.ts +24 -0
- package/dist/swagger/useSwaggerAccess.d.ts.map +1 -0
- package/dist/swagger/useSwaggerAccess.js +33 -0
- package/dist/swagger/useSwaggerAccess.js.map +1 -0
- package/dist/table/DataTable.d.ts.map +1 -1
- package/dist/table/DataTable.js +50 -13
- package/dist/table/DataTable.js.map +1 -1
- package/dist/table/DataTableBody.d.ts +18 -1
- package/dist/table/DataTableBody.d.ts.map +1 -1
- package/dist/table/DataTableBody.js +83 -58
- package/dist/table/DataTableBody.js.map +1 -1
- package/dist/table/DataTableRowActionsInline.d.ts +17 -0
- package/dist/table/DataTableRowActionsInline.d.ts.map +1 -0
- package/dist/table/DataTableRowActionsInline.js +45 -0
- package/dist/table/DataTableRowActionsInline.js.map +1 -0
- package/dist/table/PermissionMatrix.d.ts +4 -4
- package/dist/table/PermissionMatrix.js +2 -2
- package/dist/table/PermissionMatrix.js.map +1 -1
- package/dist/table/dataTableBaseProps.d.ts +31 -0
- package/dist/table/dataTableBaseProps.d.ts.map +1 -1
- package/dist/table/dataTableTypes.d.ts +16 -2
- package/dist/table/dataTableTypes.d.ts.map +1 -1
- package/dist/table/useDataTableData.d.ts +3 -0
- package/dist/table/useDataTableData.d.ts.map +1 -1
- package/dist/table/useDataTableData.js +9 -2
- package/dist/table/useDataTableData.js.map +1 -1
- package/dist/table/useDataTableRefresh.d.ts +1 -0
- package/dist/table/useDataTableRefresh.d.ts.map +1 -1
- package/dist/table/useDataTableRefresh.js +13 -1
- package/dist/table/useDataTableRefresh.js.map +1 -1
- package/dist/table/useOptimisticRows.d.ts +64 -0
- package/dist/table/useOptimisticRows.d.ts.map +1 -0
- package/dist/table/useOptimisticRows.js +185 -0
- package/dist/table/useOptimisticRows.js.map +1 -0
- package/dist/table/useRowMotion.d.ts +27 -0
- package/dist/table/useRowMotion.d.ts.map +1 -0
- package/dist/table/useRowMotion.js +112 -0
- package/dist/table/useRowMotion.js.map +1 -0
- package/docs/packages-alepha-ui-core.md +1 -0
- package/docs/packages-alepha-ui-overview.md +53 -1
- package/docs/packages-alepha-ui-swagger.md +35 -0
- package/package.json +11 -3
- package/src/account/AccountBrand.tsx +25 -6
- package/src/account/AccountLayout.tsx +5 -1
- package/src/admin/AdminUserDetail.tsx +3 -3
- package/src/admin/__tests__/AdminUserDetail.spec.ts +2 -2
- package/src/ai/AiChat.tsx +160 -82
- package/src/ai/AiChatComposer.tsx +117 -0
- package/src/ai/AiChatConfirmCard.tsx +7 -5
- package/src/ai/AiChatConfirmFields.tsx +65 -0
- package/src/ai/AiChatEmpty.tsx +73 -0
- package/src/ai/AiChatHistory.tsx +64 -28
- package/src/ai/AiChatMessage.tsx +41 -14
- package/src/ai/__tests__/AiChat.browser.spec.tsx +146 -5
- package/src/ai/index.ts +3 -0
- package/src/ai/useAiChat.ts +18 -0
- package/src/core/Sidebar.tsx +2 -2
- package/src/core/__tests__/useUndoable.browser.spec.tsx +201 -0
- package/src/core/index.ts +7 -0
- package/src/core/useToast.tsx +30 -2
- package/src/core/useUndoable.tsx +285 -0
- package/src/form/__tests__/ControlChoices.browser.spec.tsx +2 -1
- package/src/i18n/fr/uiFr.ts +52 -0
- package/src/markdown/MarkdownView.tsx +27 -9
- package/src/organizations/useInviteOrganizationMember.ts +2 -2
- package/src/shell/AppShellSidebarNavItem.tsx +51 -1
- package/src/shell/PlateTabBar.tsx +8 -2
- package/src/shell/SidebarBackHeader.tsx +58 -0
- package/src/shell/appShellNav.tsx +31 -0
- package/src/shell/index.ts +5 -1
- package/src/swagger/$uiSwagger.tsx +95 -0
- package/src/swagger/OpenApiSpec.ts +300 -0
- package/src/swagger/SwaggerAccessBadge.tsx +64 -0
- package/src/swagger/SwaggerAlephaMeta.tsx +141 -0
- package/src/swagger/SwaggerCredentials.tsx +44 -0
- package/src/swagger/SwaggerExplorer.tsx +208 -0
- package/src/swagger/SwaggerMethodBadge.tsx +32 -0
- package/src/swagger/SwaggerNav.tsx +92 -0
- package/src/swagger/SwaggerOperationView.tsx +102 -0
- package/src/swagger/SwaggerParameters.tsx +74 -0
- package/src/swagger/SwaggerRequestBuilder.ts +331 -0
- package/src/swagger/SwaggerResponseView.tsx +107 -0
- package/src/swagger/SwaggerResponses.tsx +83 -0
- package/src/swagger/SwaggerSchemaView.tsx +134 -0
- package/src/swagger/SwaggerSection.tsx +29 -0
- package/src/swagger/SwaggerTryIt.tsx +95 -0
- package/src/swagger/SwaggerTryItForm.tsx +87 -0
- package/src/swagger/__tests__/$uiSwagger.spec.ts +52 -0
- package/src/swagger/__tests__/OpenApiSpec.spec.ts +139 -0
- package/src/swagger/__tests__/SwaggerAccess.browser.spec.tsx +124 -0
- package/src/swagger/__tests__/SwaggerOperationView.browser.spec.tsx +194 -0
- package/src/swagger/__tests__/SwaggerRequestBuilder.spec.ts +250 -0
- package/src/swagger/index.ts +75 -0
- package/src/swagger/useSwaggerAccess.ts +59 -0
- package/src/table/DataTable.tsx +85 -34
- package/src/table/DataTableBody.tsx +51 -7
- package/src/table/DataTableRowActionsInline.tsx +80 -0
- package/src/table/PermissionMatrix.tsx +4 -4
- package/src/table/__tests__/DataTableEmptyState.browser.spec.tsx +1 -1
- package/src/table/__tests__/DataTableListMode.browser.spec.tsx +174 -0
- package/src/table/__tests__/DataTableOptimistic.browser.spec.tsx +173 -0
- package/src/table/__tests__/PermissionMatrix.browser.spec.tsx +2 -2
- package/src/table/__tests__/queryFilters.spec.ts +10 -12
- package/src/table/dataTableBaseProps.ts +31 -0
- package/src/table/dataTableTypes.ts +16 -2
- package/src/table/useDataTableData.ts +18 -2
- package/src/table/useDataTableRefresh.ts +26 -2
- package/src/table/useOptimisticRows.ts +265 -0
- package/src/table/useRowMotion.ts +152 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { useI18n } from "alepha/react/i18n";
|
|
2
|
+
import type { OpenApiOperation } from "alepha/server/swagger";
|
|
3
|
+
|
|
4
|
+
import type { OpenApiSpec } from "./OpenApiSpec.ts";
|
|
5
|
+
|
|
6
|
+
export interface SwaggerParametersProps {
|
|
7
|
+
spec: OpenApiSpec;
|
|
8
|
+
parameters: NonNullable<OpenApiOperation["parameters"]>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The path, query and header parameters of an operation, one row each.
|
|
13
|
+
*/
|
|
14
|
+
export const SwaggerParameters = (props: SwaggerParametersProps) => {
|
|
15
|
+
const { tr } = useI18n();
|
|
16
|
+
return (
|
|
17
|
+
<div className="overflow-x-auto rounded-lg border">
|
|
18
|
+
<table className="w-full text-sm">
|
|
19
|
+
<thead className="text-muted-foreground bg-muted/40 text-left text-xs">
|
|
20
|
+
<tr>
|
|
21
|
+
<th className="px-3 py-2 font-medium">
|
|
22
|
+
{tr("swagger.parameters.name", { default: "Name" })}
|
|
23
|
+
</th>
|
|
24
|
+
<th className="px-3 py-2 font-medium">
|
|
25
|
+
{tr("swagger.parameters.in", { default: "In" })}
|
|
26
|
+
</th>
|
|
27
|
+
<th className="px-3 py-2 font-medium">
|
|
28
|
+
{tr("swagger.parameters.type", { default: "Type" })}
|
|
29
|
+
</th>
|
|
30
|
+
<th className="px-3 py-2 font-medium">
|
|
31
|
+
{tr("swagger.parameters.description", { default: "Description" })}
|
|
32
|
+
</th>
|
|
33
|
+
</tr>
|
|
34
|
+
</thead>
|
|
35
|
+
<tbody>
|
|
36
|
+
{props.parameters.map((param) => {
|
|
37
|
+
const resolved = props.spec.resolve(param.schema);
|
|
38
|
+
const example = (param as { example?: unknown }).example;
|
|
39
|
+
return (
|
|
40
|
+
<tr key={`${param.in}:${param.name}`} className="border-t">
|
|
41
|
+
<td className="px-3 py-2 align-top">
|
|
42
|
+
<span className="font-mono">{param.name}</span>
|
|
43
|
+
{param.required && (
|
|
44
|
+
<span className="text-danger-text ml-1" aria-hidden>
|
|
45
|
+
*
|
|
46
|
+
</span>
|
|
47
|
+
)}
|
|
48
|
+
</td>
|
|
49
|
+
<td className="text-muted-foreground px-3 py-2 align-top text-xs">
|
|
50
|
+
{param.in}
|
|
51
|
+
</td>
|
|
52
|
+
<td className="text-muted-foreground px-3 py-2 align-top font-mono text-xs break-all">
|
|
53
|
+
{props.spec.typeLabel(param.schema)}
|
|
54
|
+
</td>
|
|
55
|
+
<td className="px-3 py-2 align-top text-xs">
|
|
56
|
+
{param.description ?? resolved?.description}
|
|
57
|
+
{example !== undefined && (
|
|
58
|
+
<div className="text-muted-foreground mt-0.5">
|
|
59
|
+
{tr("swagger.parameters.example", { default: "Example" })}
|
|
60
|
+
{": "}
|
|
61
|
+
<code className="font-mono">
|
|
62
|
+
{JSON.stringify(example)}
|
|
63
|
+
</code>
|
|
64
|
+
</div>
|
|
65
|
+
)}
|
|
66
|
+
</td>
|
|
67
|
+
</tr>
|
|
68
|
+
);
|
|
69
|
+
})}
|
|
70
|
+
</tbody>
|
|
71
|
+
</table>
|
|
72
|
+
</div>
|
|
73
|
+
);
|
|
74
|
+
};
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { jsonSchemaToZod, type ZObject, type ZType, z } from "alepha";
|
|
2
|
+
|
|
3
|
+
import type { ControlProps } from "../form/Control.tsx";
|
|
4
|
+
import type { OpenApiSpec, SwaggerEntry } from "./OpenApiSpec.ts";
|
|
5
|
+
import type { SwaggerResponseResult } from "./SwaggerResponseView.tsx";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* How the request body is edited: as fields built from its schema, or as raw
|
|
9
|
+
* JSON in a textarea.
|
|
10
|
+
*/
|
|
11
|
+
export type SwaggerBodyMode = "form" | "json";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Where a form field goes in the request.
|
|
15
|
+
*/
|
|
16
|
+
export interface SwaggerFieldTarget {
|
|
17
|
+
in: "path" | "query" | "header" | "cookie" | "body" | "field" | "part";
|
|
18
|
+
name: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The form a "try it" panel renders for one operation, and how to read it back.
|
|
23
|
+
*/
|
|
24
|
+
export interface SwaggerRequestModel {
|
|
25
|
+
schema: ZObject;
|
|
26
|
+
fields: Record<string, Partial<Omit<ControlProps, "input">>>;
|
|
27
|
+
initialValues: Record<string, unknown>;
|
|
28
|
+
targets: Record<string, SwaggerFieldTarget>;
|
|
29
|
+
/**
|
|
30
|
+
* The body's media type, when the operation takes one.
|
|
31
|
+
*/
|
|
32
|
+
mediaType?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Whether the body can be edited as fields at all: an object body can, an
|
|
35
|
+
* array or a scalar only as JSON.
|
|
36
|
+
*/
|
|
37
|
+
formable: boolean;
|
|
38
|
+
mode: SwaggerBodyMode;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A request ready for `fetch`.
|
|
43
|
+
*/
|
|
44
|
+
export interface SwaggerRequest {
|
|
45
|
+
method: string;
|
|
46
|
+
url: string;
|
|
47
|
+
headers: Record<string, string>;
|
|
48
|
+
body?: BodyInit;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Turns an operation into a form, and a submitted form into a request.
|
|
53
|
+
*
|
|
54
|
+
* The form is an ordinary `useForm` schema, built with `jsonSchemaToZod` from
|
|
55
|
+
* the operation's (dereferenced) JSON schemas, so every field is a `Control`
|
|
56
|
+
* and validation is the one the rest of the UI uses:
|
|
57
|
+
*
|
|
58
|
+
* - each path, query and header parameter is a top-level field;
|
|
59
|
+
* - a JSON object body is flattened, one field per property (a nested object
|
|
60
|
+
* stays a group), or a textarea of raw JSON in `json` mode, which is also
|
|
61
|
+
* the only mode for a body that is not an object;
|
|
62
|
+
* - a `multipart/form-data` body is flattened, one field per part, a binary
|
|
63
|
+
* part becoming a file input.
|
|
64
|
+
*/
|
|
65
|
+
export class SwaggerRequestBuilder {
|
|
66
|
+
protected readonly spec: OpenApiSpec;
|
|
67
|
+
|
|
68
|
+
constructor(spec: OpenApiSpec) {
|
|
69
|
+
this.spec = spec;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public build(
|
|
73
|
+
entry: SwaggerEntry,
|
|
74
|
+
mode: SwaggerBodyMode,
|
|
75
|
+
): SwaggerRequestModel {
|
|
76
|
+
const shape: Record<string, ZType> = {};
|
|
77
|
+
const fields: SwaggerRequestModel["fields"] = {};
|
|
78
|
+
const initialValues: Record<string, unknown> = {};
|
|
79
|
+
const targets: Record<string, SwaggerFieldTarget> = {};
|
|
80
|
+
|
|
81
|
+
for (const param of entry.operation.parameters ?? []) {
|
|
82
|
+
const key = this.uniqueKey(param.name, shape);
|
|
83
|
+
const schema = this.spec.dereference(param.schema);
|
|
84
|
+
let field = jsonSchemaToZod(schema);
|
|
85
|
+
if (param.description) {
|
|
86
|
+
field = field.meta({ description: param.description }) as ZType;
|
|
87
|
+
}
|
|
88
|
+
shape[key] = param.required ? field : (field.optional() as ZType);
|
|
89
|
+
targets[key] = { in: param.in, name: param.name };
|
|
90
|
+
const example = (param as { example?: unknown }).example;
|
|
91
|
+
if (example !== undefined) {
|
|
92
|
+
initialValues[key] = example;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const content = entry.operation.requestBody?.content ?? {};
|
|
97
|
+
const [mediaType, media] = Object.entries(content)[0] ?? [];
|
|
98
|
+
const required = entry.operation.requestBody?.required !== false;
|
|
99
|
+
const bodySchema = media ? this.spec.dereference(media.schema) : undefined;
|
|
100
|
+
const formable = !!bodySchema?.properties;
|
|
101
|
+
const effective: SwaggerBodyMode = formable ? mode : "json";
|
|
102
|
+
|
|
103
|
+
if (mediaType === "multipart/form-data" && bodySchema?.properties) {
|
|
104
|
+
const requiredParts = new Set<string>(bodySchema.required ?? []);
|
|
105
|
+
for (const [name, part] of Object.entries<any>(bodySchema.properties)) {
|
|
106
|
+
const key = this.uniqueKey(name, shape);
|
|
107
|
+
const binary = part?.format === "binary";
|
|
108
|
+
const field = binary ? (z.file() as ZType) : jsonSchemaToZod(part);
|
|
109
|
+
shape[key] = requiredParts.has(name)
|
|
110
|
+
? field
|
|
111
|
+
: (field.optional() as ZType);
|
|
112
|
+
targets[key] = { in: "part", name };
|
|
113
|
+
if (binary) {
|
|
114
|
+
fields[key] = { file: true };
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
} else if (mediaType && bodySchema && effective === "form") {
|
|
118
|
+
const requiredProps = new Set<string>(bodySchema.required ?? []);
|
|
119
|
+
for (const [name, prop] of Object.entries<any>(bodySchema.properties)) {
|
|
120
|
+
const key = this.uniqueKey(name, shape);
|
|
121
|
+
const field = jsonSchemaToZod(prop);
|
|
122
|
+
shape[key] = requiredProps.has(name)
|
|
123
|
+
? field
|
|
124
|
+
: (field.optional() as ZType);
|
|
125
|
+
targets[key] = { in: "field", name };
|
|
126
|
+
}
|
|
127
|
+
} else if (mediaType && bodySchema) {
|
|
128
|
+
const key = this.uniqueKey("body", shape);
|
|
129
|
+
targets[key] = { in: "body", name: "body" };
|
|
130
|
+
shape[key] = required ? z.richText() : (z.richText().optional() as ZType);
|
|
131
|
+
fields[key] = { area: true, rows: 10 };
|
|
132
|
+
initialValues[key] = JSON.stringify(this.example(bodySchema), null, 2);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
schema: z.object(shape) as ZObject,
|
|
137
|
+
fields,
|
|
138
|
+
initialValues,
|
|
139
|
+
targets,
|
|
140
|
+
mediaType,
|
|
141
|
+
formable,
|
|
142
|
+
mode: effective,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The request a submitted form describes. `origin` prefixes the path; leave
|
|
148
|
+
* it empty for the page's own origin.
|
|
149
|
+
*/
|
|
150
|
+
public request(
|
|
151
|
+
entry: SwaggerEntry,
|
|
152
|
+
model: SwaggerRequestModel,
|
|
153
|
+
values: Record<string, unknown>,
|
|
154
|
+
origin = "",
|
|
155
|
+
): SwaggerRequest {
|
|
156
|
+
let path = entry.path;
|
|
157
|
+
const query = new URLSearchParams();
|
|
158
|
+
const headers: Record<string, string> = {};
|
|
159
|
+
const parts = new FormData();
|
|
160
|
+
let hasParts = false;
|
|
161
|
+
let body: BodyInit | undefined;
|
|
162
|
+
const json: Record<string, unknown> = {};
|
|
163
|
+
let hasJson = false;
|
|
164
|
+
|
|
165
|
+
for (const [key, target] of Object.entries(model.targets)) {
|
|
166
|
+
const value = values[key];
|
|
167
|
+
if (value === undefined || value === null || value === "") {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
switch (target.in) {
|
|
171
|
+
case "path":
|
|
172
|
+
path = path.replace(
|
|
173
|
+
`{${target.name}}`,
|
|
174
|
+
encodeURIComponent(this.scalar(value)),
|
|
175
|
+
);
|
|
176
|
+
break;
|
|
177
|
+
case "query":
|
|
178
|
+
for (const item of Array.isArray(value) ? value : [value]) {
|
|
179
|
+
query.append(target.name, this.scalar(item));
|
|
180
|
+
}
|
|
181
|
+
break;
|
|
182
|
+
case "header":
|
|
183
|
+
headers[target.name] = this.scalar(value);
|
|
184
|
+
break;
|
|
185
|
+
case "part":
|
|
186
|
+
hasParts = true;
|
|
187
|
+
if (value instanceof Blob) {
|
|
188
|
+
parts.append(target.name, value);
|
|
189
|
+
} else {
|
|
190
|
+
parts.append(target.name, this.scalar(value));
|
|
191
|
+
}
|
|
192
|
+
break;
|
|
193
|
+
case "field":
|
|
194
|
+
hasJson = true;
|
|
195
|
+
json[target.name] = value;
|
|
196
|
+
break;
|
|
197
|
+
case "body":
|
|
198
|
+
headers["content-type"] = model.mediaType ?? "application/json";
|
|
199
|
+
body =
|
|
200
|
+
model.mode === "json" && typeof value === "string"
|
|
201
|
+
? value
|
|
202
|
+
: JSON.stringify(value);
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (hasJson) {
|
|
208
|
+
headers["content-type"] = model.mediaType ?? "application/json";
|
|
209
|
+
body = JSON.stringify(json);
|
|
210
|
+
}
|
|
211
|
+
if (hasParts) {
|
|
212
|
+
// No content-type: the browser writes the multipart boundary itself.
|
|
213
|
+
body = parts;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const search = query.toString();
|
|
217
|
+
return {
|
|
218
|
+
method: entry.method.toUpperCase(),
|
|
219
|
+
url: `${origin}${path}${search ? `?${search}` : ""}`,
|
|
220
|
+
headers,
|
|
221
|
+
body,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Read a response as a stream, reporting it after the headers, after each
|
|
227
|
+
* chunk, and once more when it ends (`done`): a server-sent event or a
|
|
228
|
+
* chunked body shows as it arrives rather than when it closes. `elapsed`
|
|
229
|
+
* gives the milliseconds since the request left.
|
|
230
|
+
*/
|
|
231
|
+
public async read(
|
|
232
|
+
res: Response,
|
|
233
|
+
elapsed: () => number,
|
|
234
|
+
onResult: (result: SwaggerResponseResult) => void,
|
|
235
|
+
): Promise<SwaggerResponseResult> {
|
|
236
|
+
const base: SwaggerResponseResult = {
|
|
237
|
+
status: res.status,
|
|
238
|
+
statusText: res.statusText,
|
|
239
|
+
headers: [...res.headers.entries()],
|
|
240
|
+
contentType: res.headers.get("content-type") ?? undefined,
|
|
241
|
+
body: "",
|
|
242
|
+
durationMs: elapsed(),
|
|
243
|
+
done: !res.body,
|
|
244
|
+
};
|
|
245
|
+
onResult(base);
|
|
246
|
+
if (!res.body) {
|
|
247
|
+
return base;
|
|
248
|
+
}
|
|
249
|
+
const reader = res.body.getReader();
|
|
250
|
+
const decoder = new TextDecoder();
|
|
251
|
+
let body = "";
|
|
252
|
+
for (;;) {
|
|
253
|
+
const chunk = await reader.read();
|
|
254
|
+
if (chunk.done) break;
|
|
255
|
+
body += decoder.decode(chunk.value, { stream: true });
|
|
256
|
+
onResult({ ...base, body, durationMs: elapsed() });
|
|
257
|
+
}
|
|
258
|
+
body += decoder.decode();
|
|
259
|
+
const result = { ...base, body, durationMs: elapsed(), done: true };
|
|
260
|
+
onResult(result);
|
|
261
|
+
return result;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* A sample value for a schema, to seed the raw JSON editor: the schema's
|
|
266
|
+
* own example or default when it has one, else a value of the right shape.
|
|
267
|
+
*/
|
|
268
|
+
public example(schema: any, depth = 0): unknown {
|
|
269
|
+
if (!schema || typeof schema !== "object" || depth > 6) {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
if (schema.example !== undefined) return schema.example;
|
|
273
|
+
if (Array.isArray(schema.examples) && schema.examples.length) {
|
|
274
|
+
return schema.examples[0];
|
|
275
|
+
}
|
|
276
|
+
if (schema.default !== undefined) return schema.default;
|
|
277
|
+
if (schema.const !== undefined) return schema.const;
|
|
278
|
+
if (Array.isArray(schema.enum) && schema.enum.length) return schema.enum[0];
|
|
279
|
+
const variant = (schema.oneOf ?? schema.anyOf)?.find(
|
|
280
|
+
(it: any) => it?.type !== "null",
|
|
281
|
+
);
|
|
282
|
+
if (variant) return this.example(variant, depth + 1);
|
|
283
|
+
const type = Array.isArray(schema.type) ? schema.type[0] : schema.type;
|
|
284
|
+
switch (type) {
|
|
285
|
+
case "object": {
|
|
286
|
+
const out: Record<string, unknown> = {};
|
|
287
|
+
for (const [name, prop] of Object.entries<any>(
|
|
288
|
+
schema.properties ?? {},
|
|
289
|
+
)) {
|
|
290
|
+
out[name] = this.example(prop, depth + 1);
|
|
291
|
+
}
|
|
292
|
+
return out;
|
|
293
|
+
}
|
|
294
|
+
case "array":
|
|
295
|
+
return schema.items ? [this.example(schema.items, depth + 1)] : [];
|
|
296
|
+
case "integer":
|
|
297
|
+
case "number":
|
|
298
|
+
return 0;
|
|
299
|
+
case "boolean":
|
|
300
|
+
return false;
|
|
301
|
+
case "string":
|
|
302
|
+
return "";
|
|
303
|
+
default:
|
|
304
|
+
return schema.properties
|
|
305
|
+
? this.example({ ...schema, type: "object" }, depth)
|
|
306
|
+
: null;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
protected scalar(value: unknown): string {
|
|
311
|
+
if (value instanceof Date) return value.toISOString();
|
|
312
|
+
if (
|
|
313
|
+
typeof value === "string" ||
|
|
314
|
+
typeof value === "number" ||
|
|
315
|
+
typeof value === "boolean" ||
|
|
316
|
+
typeof value === "bigint"
|
|
317
|
+
) {
|
|
318
|
+
return String(value);
|
|
319
|
+
}
|
|
320
|
+
return JSON.stringify(value) ?? "";
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
protected uniqueKey(name: string, shape: Record<string, unknown>): string {
|
|
324
|
+
let key = name;
|
|
325
|
+
let index = 2;
|
|
326
|
+
while (key in shape) {
|
|
327
|
+
key = `${name}${index++}`;
|
|
328
|
+
}
|
|
329
|
+
return key;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { useI18n } from "alepha/react/i18n";
|
|
2
|
+
import { ChevronRight } from "lucide-react";
|
|
3
|
+
|
|
4
|
+
import { Badge, type BadgeTone } from "../core/Badge.tsx";
|
|
5
|
+
import { Spinner } from "../core/Spinner.tsx";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* What came back from a "try it" request, as it arrives.
|
|
9
|
+
*/
|
|
10
|
+
export interface SwaggerResponseResult {
|
|
11
|
+
status: number;
|
|
12
|
+
statusText: string;
|
|
13
|
+
headers: Array<[string, string]>;
|
|
14
|
+
/**
|
|
15
|
+
* The body as text so far. A streamed response grows while `done` is false.
|
|
16
|
+
*/
|
|
17
|
+
body: string;
|
|
18
|
+
contentType?: string;
|
|
19
|
+
durationMs: number;
|
|
20
|
+
done: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface SwaggerResponseViewProps {
|
|
24
|
+
result: SwaggerResponseResult;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The response of a sent request: status, time, size and headers, then the
|
|
29
|
+
* body, pretty-printed when it is JSON and left as text otherwise. A streamed
|
|
30
|
+
* body shows a spinner until the stream ends.
|
|
31
|
+
*/
|
|
32
|
+
export const SwaggerResponseView = (props: SwaggerResponseViewProps) => {
|
|
33
|
+
const { tr } = useI18n();
|
|
34
|
+
const result = props.result;
|
|
35
|
+
const pretty = swaggerPrettyBody(result.body, result.contentType);
|
|
36
|
+
const bytes = new TextEncoder().encode(result.body).length;
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<section
|
|
40
|
+
className="flex flex-col gap-2 rounded-lg border p-3"
|
|
41
|
+
aria-label={tr("swagger.response", { default: "Response" })}
|
|
42
|
+
>
|
|
43
|
+
<div className="flex flex-wrap items-center gap-2 text-sm">
|
|
44
|
+
<Badge
|
|
45
|
+
variant="tint"
|
|
46
|
+
tone={swaggerResponseTone(result.status)}
|
|
47
|
+
className="font-mono"
|
|
48
|
+
>
|
|
49
|
+
{result.status}
|
|
50
|
+
</Badge>
|
|
51
|
+
<span>{result.statusText}</span>
|
|
52
|
+
<span className="text-muted-foreground ml-auto font-mono text-xs">
|
|
53
|
+
{result.durationMs} ms · {bytes} B
|
|
54
|
+
</span>
|
|
55
|
+
{!result.done && <Spinner className="size-3.5" />}
|
|
56
|
+
</div>
|
|
57
|
+
|
|
58
|
+
<details className="group/headers">
|
|
59
|
+
<summary className="text-muted-foreground hover:text-foreground flex cursor-pointer list-none items-center gap-1 text-xs [&::-webkit-details-marker]:hidden">
|
|
60
|
+
<ChevronRight className="size-3.5 transition-transform group-open/headers:rotate-90" />
|
|
61
|
+
{tr("swagger.response.headers", { default: "Headers" })} (
|
|
62
|
+
{result.headers.length})
|
|
63
|
+
</summary>
|
|
64
|
+
<dl className="mt-1 grid grid-cols-[auto_1fr] gap-x-3 gap-y-0.5 pl-5 font-mono text-xs">
|
|
65
|
+
{result.headers.map(([name, value]) => (
|
|
66
|
+
<div key={name} className="contents">
|
|
67
|
+
<dt className="text-muted-foreground">{name}</dt>
|
|
68
|
+
<dd className="break-all">{value}</dd>
|
|
69
|
+
</div>
|
|
70
|
+
))}
|
|
71
|
+
</dl>
|
|
72
|
+
</details>
|
|
73
|
+
|
|
74
|
+
{result.body ? (
|
|
75
|
+
<pre className="bg-muted/40 max-h-[60vh] overflow-auto rounded-md p-3 font-mono text-xs whitespace-pre-wrap">
|
|
76
|
+
{pretty}
|
|
77
|
+
</pre>
|
|
78
|
+
) : (
|
|
79
|
+
<p className="text-muted-foreground text-xs">
|
|
80
|
+
{result.done
|
|
81
|
+
? tr("swagger.response.empty", { default: "No body." })
|
|
82
|
+
: tr("swagger.response.waiting", {
|
|
83
|
+
default: "Waiting for data...",
|
|
84
|
+
})}
|
|
85
|
+
</p>
|
|
86
|
+
)}
|
|
87
|
+
</section>
|
|
88
|
+
);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const swaggerPrettyBody = (body: string, contentType?: string): string => {
|
|
92
|
+
if (!contentType?.includes("json") || !body) {
|
|
93
|
+
return body;
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
return JSON.stringify(JSON.parse(body), null, 2);
|
|
97
|
+
} catch {
|
|
98
|
+
return body;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const swaggerResponseTone = (status: number): BadgeTone => {
|
|
103
|
+
if (status >= 200 && status < 300) return "success";
|
|
104
|
+
if (status >= 300 && status < 400) return "info";
|
|
105
|
+
if (status >= 400 && status < 500) return "warning";
|
|
106
|
+
return "danger";
|
|
107
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { OpenApiOperation } from "alepha/server/swagger";
|
|
2
|
+
import { ChevronRight } from "lucide-react";
|
|
3
|
+
|
|
4
|
+
import { Badge, type BadgeTone } from "../core/Badge.tsx";
|
|
5
|
+
import type { OpenApiSpec } from "./OpenApiSpec.ts";
|
|
6
|
+
import { SwaggerSchemaView } from "./SwaggerSchemaView.tsx";
|
|
7
|
+
|
|
8
|
+
export interface SwaggerResponsesProps {
|
|
9
|
+
spec: OpenApiSpec;
|
|
10
|
+
responses: OpenApiOperation["responses"];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The responses of an operation by status, each opening on its content
|
|
15
|
+
* schema. Statuses sort numerically, `default` last.
|
|
16
|
+
*/
|
|
17
|
+
export const SwaggerResponses = (props: SwaggerResponsesProps) => {
|
|
18
|
+
const statuses = Object.keys(props.responses).sort((a, b) =>
|
|
19
|
+
a === "default" ? 1 : b === "default" ? -1 : Number(a) - Number(b),
|
|
20
|
+
);
|
|
21
|
+
return (
|
|
22
|
+
<div className="flex flex-col divide-y rounded-lg border">
|
|
23
|
+
{statuses.map((status) => {
|
|
24
|
+
const response = props.responses[status];
|
|
25
|
+
const content = Object.entries(response?.content ?? {});
|
|
26
|
+
const head = (
|
|
27
|
+
<span className="flex items-center gap-2 px-3 py-2">
|
|
28
|
+
<Badge
|
|
29
|
+
variant="tint"
|
|
30
|
+
tone={swaggerStatusTone(status)}
|
|
31
|
+
className="w-12 font-mono"
|
|
32
|
+
>
|
|
33
|
+
{status}
|
|
34
|
+
</Badge>
|
|
35
|
+
<span className="text-sm">{response?.description}</span>
|
|
36
|
+
{content.map(([type]) => (
|
|
37
|
+
<span
|
|
38
|
+
key={type}
|
|
39
|
+
className="text-muted-foreground ml-auto font-mono text-xs"
|
|
40
|
+
>
|
|
41
|
+
{type}
|
|
42
|
+
</span>
|
|
43
|
+
))}
|
|
44
|
+
</span>
|
|
45
|
+
);
|
|
46
|
+
if (!content.some(([, media]) => media?.schema)) {
|
|
47
|
+
return (
|
|
48
|
+
<div key={status} className="pl-5">
|
|
49
|
+
{head}
|
|
50
|
+
</div>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
return (
|
|
54
|
+
<details key={status} className="group/response">
|
|
55
|
+
<summary className="hover:bg-hover flex cursor-pointer list-none items-center [&::-webkit-details-marker]:hidden">
|
|
56
|
+
<ChevronRight className="text-muted-foreground ml-1.5 size-3.5 shrink-0 transition-transform group-open/response:rotate-90" />
|
|
57
|
+
<span className="min-w-0 flex-1">{head}</span>
|
|
58
|
+
</summary>
|
|
59
|
+
<div className="flex flex-col gap-3 px-4 pb-3">
|
|
60
|
+
{content.map(([type, media]) => (
|
|
61
|
+
<div key={type} className="flex flex-col gap-1">
|
|
62
|
+
<span className="text-muted-foreground font-mono text-xs">
|
|
63
|
+
{props.spec.typeLabel(media?.schema)}
|
|
64
|
+
</span>
|
|
65
|
+
<SwaggerSchemaView spec={props.spec} schema={media?.schema} />
|
|
66
|
+
</div>
|
|
67
|
+
))}
|
|
68
|
+
</div>
|
|
69
|
+
</details>
|
|
70
|
+
);
|
|
71
|
+
})}
|
|
72
|
+
</div>
|
|
73
|
+
);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const swaggerStatusTone = (status: string): BadgeTone => {
|
|
77
|
+
const code = Number(status);
|
|
78
|
+
if (code >= 200 && code < 300) return "success";
|
|
79
|
+
if (code >= 300 && code < 400) return "info";
|
|
80
|
+
if (code >= 400 && code < 500) return "warning";
|
|
81
|
+
if (code >= 500) return "danger";
|
|
82
|
+
return "neutral";
|
|
83
|
+
};
|