fumadocs-openapi 12.1.1 → 12.2.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.
Files changed (37) hide show
  1. package/css/generated/shared.css +1 -1
  2. package/dist/.translations/index.d.ts +62 -17
  3. package/dist/.translations/keys.js +62 -17
  4. package/dist/operation.d.ts +6 -1
  5. package/dist/operation.js +2 -4
  6. package/dist/packages/shared-api/dist/components/select.js +8 -7
  7. package/dist/playground/auth.d.ts +5 -1
  8. package/dist/playground/auth.js +7 -2
  9. package/dist/playground/fetcher.d.ts +8 -0
  10. package/dist/playground/fetcher.js +30 -1
  11. package/dist/playground/index.d.ts +2 -1
  12. package/dist/playground/index.js +2 -1
  13. package/dist/playground/use-playground.d.ts +62 -0
  14. package/dist/playground/use-playground.js +140 -0
  15. package/dist/ui/components/endpoint.js +37 -0
  16. package/dist/ui/operation/index.js +6 -14
  17. package/dist/ui/playground/client.d.ts +9 -26
  18. package/dist/ui/playground/client.js +149 -389
  19. package/dist/ui/playground/components/fields.js +693 -0
  20. package/dist/ui/playground/components/oauth-panel.js +182 -0
  21. package/dist/ui/playground/components/request-panel.js +480 -0
  22. package/dist/ui/playground/components/response-panel.js +150 -0
  23. package/dist/ui/playground/components/result-display.d.ts +1 -0
  24. package/dist/ui/playground/components/result-display.js +185 -168
  25. package/dist/ui/playground/components/segmented.js +21 -0
  26. package/dist/ui/playground/components/server-select.js +100 -89
  27. package/dist/ui/playground/components/url-bar.js +96 -0
  28. package/dist/ui/playground/status-info.js +32 -45
  29. package/dist/utils/document/load.js +1 -1
  30. package/package.json +10 -10
  31. package/dist/packages/shared-api/dist/components/collapsible.d.ts +0 -7
  32. package/dist/packages/shared-api/dist/components/collapsible.js +0 -16
  33. package/dist/packages/shared-api/dist/components/dialog.js +0 -55
  34. package/dist/packages/shared-api/dist/components/input.js +0 -14
  35. package/dist/packages/shared-api/dist/components/label.js +0 -11
  36. package/dist/packages/shared-api/dist/components/playground/inputs.js +0 -439
  37. package/dist/ui/playground/components/oauth-dialog.js +0 -259
@@ -1,439 +0,0 @@
1
- "use client";
2
- import { cn } from "../../utils/cn.js";
3
- import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../select.js";
4
- import { anyFields, useFieldInfo, useResolvedSchema, useSchemaContext, useSchemaUtils } from "./schema.js";
5
- import { Input } from "../input.js";
6
- import { useState } from "react";
7
- import { stringify } from "@fumadocs/json-schema";
8
- import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
9
- import { useArray, useDataEngine, useFieldValue, useObject } from "@fumari/stf";
10
- import { stringifyFieldKey } from "@fumari/stf/lib/utils";
11
- import { useTranslations } from "@fuma-translate/react";
12
- import { cva } from "class-variance-authority";
13
- import { ChevronRight, Plus, Trash2, X } from "lucide-react";
14
- import { buttonVariants } from "fumadocs-ui/components/ui/button";
15
- //#region ../shared-api/dist/components/playground/inputs.js
16
- const fieldLabelVariants = cva("w-full inline-flex items-center gap-0.5");
17
- function FieldLabelType(props) {
18
- return /* @__PURE__ */ jsx("code", {
19
- ...props,
20
- className: cn("text-xs text-fd-muted-foreground", props.className),
21
- children: props.children
22
- });
23
- }
24
- const labelClassName = "text-xs font-medium text-fd-foreground peer-disabled:cursor-not-allowed peer-disabled:opacity-70";
25
- function ObjectInput({ field: _field, fieldName, ...props }) {
26
- const { generateDefault } = useSchemaUtils();
27
- const field = useResolvedSchema(_field);
28
- const schemaPropKeys = field.properties ? Object.keys(field.properties) : [];
29
- const { patternProperties = {}, additionalProperties, "x-playground-lazy": isLazy = schemaPropKeys.length > 100 } = field;
30
- const isDynamic = Object.keys(patternProperties).length > 0 || additionalProperties;
31
- const [nextName, setNextName] = useState("");
32
- const { properties, onAppend, onDelete, _objectKeys } = useObject(fieldName, {
33
- lazy: isLazy,
34
- defaultValue: () => generateDefault(field),
35
- properties: field.properties ?? {},
36
- fallback: additionalProperties,
37
- patternProperties
38
- });
39
- const hiddenProperties = isLazy ? schemaPropKeys.filter((key) => !_objectKeys.includes(key)) : [];
40
- const t = useTranslations({ note: "playground" });
41
- return /* @__PURE__ */ jsxs("div", {
42
- ...props,
43
- className: cn("grid grid-cols-1 gap-4 @md:grid-cols-2 *:data-[collapsible=true]:order-last", props.className),
44
- children: [
45
- isLazy && hiddenProperties.length > 0 && /* @__PURE__ */ jsxs(Select, {
46
- value: "",
47
- onValueChange: (v) => v !== null && onAppend(v),
48
- children: [/* @__PURE__ */ jsx(SelectTrigger, {
49
- className: "col-span-full",
50
- children: /* @__PURE__ */ jsx(SelectValue, { placeholder: t("Show Property") })
51
- }), /* @__PURE__ */ jsx(SelectContent, { children: hiddenProperties.map((key) => /* @__PURE__ */ jsx(SelectItem, {
52
- value: key,
53
- children: key
54
- }, key)) })]
55
- }),
56
- properties.map((child) => {
57
- let toolbar = null;
58
- if (child.kind === "pattern" || child.kind === "fallback") toolbar = /* @__PURE__ */ jsx("button", {
59
- type: "button",
60
- "aria-label": t("Remove Item", { note: "aria-label" }),
61
- className: cn(buttonVariants({
62
- variant: "outline",
63
- size: "icon-xs"
64
- })),
65
- onClick: () => {
66
- onDelete(child.key);
67
- },
68
- children: /* @__PURE__ */ jsx(Trash2, {})
69
- });
70
- return /* @__PURE__ */ jsx(FieldSet, {
71
- name: child.key,
72
- field: child.info,
73
- fieldName: child.field,
74
- isRequired: field.required?.includes(child.key),
75
- toolbar
76
- }, child.key);
77
- }),
78
- isDynamic && /* @__PURE__ */ jsxs("div", {
79
- className: "flex gap-2 order-last col-span-full",
80
- children: [/* @__PURE__ */ jsx(Input, {
81
- value: nextName,
82
- placeholder: t("Enter Property Name"),
83
- onChange: (e) => setNextName(e.target.value),
84
- onKeyDown: (e) => {
85
- if (e.key === "Enter") {
86
- setNextName("");
87
- onAppend(nextName);
88
- e.preventDefault();
89
- }
90
- }
91
- }), /* @__PURE__ */ jsx("button", {
92
- type: "button",
93
- className: cn(buttonVariants({
94
- variant: "secondary",
95
- size: "sm"
96
- }), "px-4"),
97
- onClick: () => {
98
- onAppend(nextName);
99
- setNextName("");
100
- },
101
- children: t("New")
102
- })]
103
- })
104
- ]
105
- });
106
- }
107
- function JsonInput({ fieldName }) {
108
- const engine = useDataEngine();
109
- const [error, setError] = useState(null);
110
- const [value, setValue] = useState(() => JSON.stringify(engine.init(fieldName, {}), null, 2));
111
- return /* @__PURE__ */ jsxs("div", {
112
- className: "flex flex-col bg-fd-secondary text-fd-secondary-foreground overflow-hidden border rounded-lg",
113
- children: [/* @__PURE__ */ jsx("textarea", {
114
- value,
115
- className: "p-2 h-[240px] text-sm font-mono resize-none focus-visible:outline-none",
116
- onChange: (v) => {
117
- setValue(v.target.value);
118
- try {
119
- engine.update(fieldName, JSON.parse(v.target.value));
120
- setError(null);
121
- } catch (e) {
122
- if (e instanceof Error) setError(e.message);
123
- }
124
- }
125
- }), /* @__PURE__ */ jsx("p", {
126
- className: "p-2 text-xs font-mono border-t text-red-400 empty:hidden",
127
- children: error
128
- })]
129
- });
130
- }
131
- function FieldInput({ field, fieldName, isRequired, ...props }) {
132
- const [value, setValue] = useFieldValue(fieldName);
133
- const id = stringifyFieldKey(fieldName);
134
- const t = useTranslations({ note: "playground" });
135
- if (field.type === "null") return;
136
- if (field.type === "string" && field.format === "binary") return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("label", {
137
- htmlFor: id,
138
- className: cn(buttonVariants({
139
- variant: "secondary",
140
- className: "w-full h-9 gap-2 truncate"
141
- })),
142
- children: value instanceof File ? /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("span", {
143
- className: "text-fd-muted-foreground text-xs",
144
- children: t("Selected")
145
- }), /* @__PURE__ */ jsx("span", {
146
- className: "truncate w-0 flex-1 text-end",
147
- children: value.name
148
- })] }) : /* @__PURE__ */ jsx("span", {
149
- className: "text-fd-muted-foreground",
150
- children: t("Upload")
151
- })
152
- }), /* @__PURE__ */ jsx("input", {
153
- id,
154
- type: "file",
155
- multiple: false,
156
- onChange: (e) => {
157
- if (!e.target.files || e.target.files.length === 0) return;
158
- setValue(e.target.files.item(0));
159
- },
160
- hidden: true
161
- })] });
162
- if (field.enum && field.enum.length > 0) {
163
- const idx = field.enum.indexOf(value);
164
- const items = [...field.enum.map((item, i) => ({
165
- value: String(i),
166
- label: typeof item === "string" ? item : JSON.stringify(item, null, 2)
167
- })), ...!isRequired ? [{
168
- value: "-1",
169
- label: /* @__PURE__ */ jsx("span", {
170
- className: "text-fd-muted-foreground",
171
- children: t("Unset")
172
- })
173
- }] : []];
174
- return /* @__PURE__ */ jsxs(Select, {
175
- items,
176
- value: idx === -1 && isRequired ? "" : String(idx),
177
- onValueChange: (v) => setValue(field.enum[Number(v)]),
178
- children: [/* @__PURE__ */ jsx(SelectTrigger, {
179
- id,
180
- ...props,
181
- children: /* @__PURE__ */ jsx(SelectValue, { placeholder: t("Select") })
182
- }), /* @__PURE__ */ jsx(SelectContent, { children: items.map((item) => /* @__PURE__ */ jsx(SelectItem, {
183
- value: item.value,
184
- children: item.label
185
- }, item.value)) })]
186
- });
187
- }
188
- if (field.type === "boolean") {
189
- const items = [
190
- {
191
- value: "true",
192
- label: "True"
193
- },
194
- {
195
- value: "false",
196
- label: "False"
197
- },
198
- ...!isRequired ? [{
199
- value: "undefined",
200
- label: /* @__PURE__ */ jsx("span", {
201
- className: "text-fd-muted-foreground",
202
- children: t("Unset")
203
- })
204
- }] : []
205
- ];
206
- return /* @__PURE__ */ jsxs(Select, {
207
- items,
208
- value: value === void 0 && isRequired ? "" : String(value),
209
- onValueChange: (value) => setValue(value === "undefined" ? void 0 : value === "true"),
210
- children: [/* @__PURE__ */ jsx(SelectTrigger, {
211
- id,
212
- ...props,
213
- children: /* @__PURE__ */ jsx(SelectValue, {})
214
- }), /* @__PURE__ */ jsx(SelectContent, { children: items.map((item) => /* @__PURE__ */ jsx(SelectItem, {
215
- value: item.value,
216
- children: item.label
217
- }, item.value)) })]
218
- });
219
- }
220
- const isNumber = field.type === "integer" || field.type === "number";
221
- return /* @__PURE__ */ jsx(Input, {
222
- id,
223
- placeholder: t("Enter value"),
224
- type: isNumber ? "number" : "text",
225
- step: field.type === "integer" ? 1 : void 0,
226
- value: String(value ?? ""),
227
- onChange: (e) => {
228
- if (isNumber) setValue(Number.isNaN(e.target.valueAsNumber) ? void 0 : e.target.valueAsNumber);
229
- else if (!isNumber) setValue(e.target.value);
230
- }
231
- });
232
- }
233
- function FieldSet({ field: _field, fieldName, toolbar, name, isRequired, depth = 0, slotType, collapsible = true, ...props }) {
234
- const { readOnly, writeOnly } = useSchemaContext();
235
- const { generateDefault } = useSchemaUtils();
236
- const field = useResolvedSchema(_field);
237
- const [show, setShow] = useState(!collapsible);
238
- const { info, updateInfo } = useFieldInfo(fieldName, field, depth);
239
- const id = stringifyFieldKey(fieldName);
240
- const dataEngine = useDataEngine();
241
- const [isDefined] = useFieldValue(fieldName, { compute(currentValue) {
242
- return currentValue !== void 0;
243
- } });
244
- if (_field === false) return;
245
- if (field.readOnly && !readOnly) return;
246
- if (field.writeOnly && !writeOnly) return;
247
- if (collapsible && !isDefined && show) setShow(false);
248
- function renderLabelTrigger(schema = field) {
249
- if (!collapsible) return renderLabelName();
250
- return /* @__PURE__ */ jsxs("button", {
251
- type: "button",
252
- className: cn(labelClassName, "inline-flex items-center gap-1 font-mono me-auto"),
253
- onClick: () => {
254
- dataEngine.init(fieldName, generateDefault(schema));
255
- setShow((prev) => !prev);
256
- },
257
- children: [
258
- /* @__PURE__ */ jsx(ChevronRight, { className: cn("size-3.5 text-fd-muted-foreground", show && "rotate-90") }),
259
- name,
260
- isRequired && /* @__PURE__ */ jsx("span", {
261
- className: "text-red-400/80",
262
- children: "*"
263
- })
264
- ]
265
- });
266
- }
267
- function renderLabelName() {
268
- return /* @__PURE__ */ jsxs("span", {
269
- className: cn(labelClassName, "font-mono me-auto"),
270
- children: [name, isRequired && /* @__PURE__ */ jsx("span", {
271
- className: "text-red-400/80 mx-1",
272
- children: "*"
273
- })]
274
- });
275
- }
276
- function renderUnsetButton() {
277
- return /* @__PURE__ */ jsx("button", {
278
- type: "button",
279
- onClick: () => dataEngine.delete(fieldName),
280
- className: "text-fd-muted-foreground hover:text-fd-accent-foreground",
281
- children: /* @__PURE__ */ jsx(X, { className: "size-3.5" })
282
- });
283
- }
284
- if (info.unionField && field[info.unionField] && field[info.unionField].length > 0) {
285
- const union = field[info.unionField];
286
- const showSelect = union.length > 1;
287
- return /* @__PURE__ */ jsx(FieldSet, {
288
- ...props,
289
- name,
290
- fieldName,
291
- isRequired,
292
- field: union[info.oneOf],
293
- depth: depth + 1,
294
- slotType: showSelect ? false : slotType,
295
- collapsible,
296
- toolbar: /* @__PURE__ */ jsxs(Fragment$1, { children: [showSelect && /* @__PURE__ */ jsx("select", {
297
- className: "text-xs font-mono",
298
- value: info.oneOf,
299
- onChange: (e) => {
300
- updateInfo({ oneOf: Number(e.target.value) });
301
- },
302
- children: union.map((item, i) => /* @__PURE__ */ jsx("option", {
303
- value: i,
304
- className: "bg-fd-popover text-fd-popover-foreground",
305
- children: stringify(item, { alias: true })
306
- }, i))
307
- }), toolbar] })
308
- });
309
- }
310
- if (Array.isArray(field.type)) {
311
- const showSelect = field.type.length > 1;
312
- return /* @__PURE__ */ jsx(FieldSet, {
313
- ...props,
314
- name,
315
- fieldName,
316
- isRequired,
317
- field: {
318
- ...field,
319
- type: info.selectedType
320
- },
321
- collapsible,
322
- depth: depth + 1,
323
- slotType: showSelect ? false : slotType,
324
- toolbar: /* @__PURE__ */ jsxs(Fragment$1, { children: [showSelect && /* @__PURE__ */ jsx("select", {
325
- className: "text-xs font-mono",
326
- value: info.selectedType,
327
- onChange: (e) => {
328
- updateInfo({ selectedType: e.target.value });
329
- },
330
- children: field.type.map((item) => /* @__PURE__ */ jsx("option", {
331
- value: item,
332
- className: "bg-fd-popover text-fd-popover-foreground",
333
- children: item
334
- }, item))
335
- }), toolbar] })
336
- });
337
- }
338
- if (field.type === "object") return /* @__PURE__ */ jsxs("fieldset", {
339
- ...props,
340
- "data-collapsible": collapsible,
341
- className: cn("flex flex-col gap-1.5 col-span-full @container", props.className),
342
- children: [/* @__PURE__ */ jsxs("div", {
343
- className: fieldLabelVariants(),
344
- children: [
345
- renderLabelTrigger(field),
346
- slotType ?? /* @__PURE__ */ jsx(FieldLabelType, { children: stringify(field) }),
347
- toolbar,
348
- !isRequired && isDefined && renderUnsetButton()
349
- ]
350
- }), show && /* @__PURE__ */ jsx(ObjectInput, {
351
- field,
352
- fieldName,
353
- className: "rounded-lg border border-fd-primary/20 bg-fd-background/50 p-2 shadow-sm"
354
- })]
355
- });
356
- if (field.type === "array") return /* @__PURE__ */ jsxs("fieldset", {
357
- ...props,
358
- "data-collapsible": collapsible,
359
- className: cn("flex flex-col gap-1.5 col-span-full", props.className),
360
- children: [/* @__PURE__ */ jsxs("div", {
361
- className: fieldLabelVariants(),
362
- children: [
363
- renderLabelTrigger(),
364
- slotType ?? /* @__PURE__ */ jsx(FieldLabelType, { children: stringify(field) }),
365
- toolbar,
366
- !isRequired && isDefined && renderUnsetButton()
367
- ]
368
- }), show && /* @__PURE__ */ jsx(ArrayInput, {
369
- fieldName,
370
- items: field.items ?? anyFields,
371
- className: "rounded-lg border border-fd-primary/20 bg-fd-background/50 p-2 shadow-sm"
372
- })]
373
- });
374
- return /* @__PURE__ */ jsxs("fieldset", {
375
- ...props,
376
- className: cn("flex flex-col gap-1.5", props.className),
377
- children: [/* @__PURE__ */ jsxs("label", {
378
- className: fieldLabelVariants(),
379
- htmlFor: id,
380
- children: [
381
- renderLabelName(),
382
- slotType ?? /* @__PURE__ */ jsx(FieldLabelType, { children: stringify(field) }),
383
- toolbar,
384
- !isRequired && isDefined && renderUnsetButton()
385
- ]
386
- }), /* @__PURE__ */ jsx(FieldInput, {
387
- field,
388
- fieldName,
389
- isRequired
390
- })]
391
- });
392
- }
393
- function ArrayInput({ fieldName, items: itemSchema, ...props }) {
394
- const name = fieldName.at(-1) ?? "";
395
- const { generateDefault } = useSchemaUtils();
396
- const { items, insertItem, removeItem } = useArray(fieldName);
397
- const t = useTranslations({ note: "playground" });
398
- return /* @__PURE__ */ jsxs("div", {
399
- ...props,
400
- className: cn("flex flex-col gap-2", props.className),
401
- children: [items.map((item) => /* @__PURE__ */ jsx(FieldSet, {
402
- name: /* @__PURE__ */ jsxs("span", {
403
- className: "text-fd-muted-foreground",
404
- children: [
405
- name,
406
- "[",
407
- item.index,
408
- "]"
409
- ]
410
- }),
411
- field: itemSchema,
412
- isRequired: true,
413
- fieldName: item.field,
414
- toolbar: /* @__PURE__ */ jsx("button", {
415
- type: "button",
416
- "aria-label": t("Remove Item", { note: "aria-label" }),
417
- className: cn(buttonVariants({
418
- variant: "outline",
419
- size: "icon-xs"
420
- })),
421
- onClick: () => removeItem(item.index),
422
- children: /* @__PURE__ */ jsx(Trash2, {})
423
- })
424
- }, item.index)), /* @__PURE__ */ jsxs("button", {
425
- type: "button",
426
- className: cn(buttonVariants({
427
- variant: "secondary",
428
- className: "gap-1.5 py-2",
429
- size: "sm"
430
- })),
431
- onClick: () => {
432
- insertItem(generateDefault(itemSchema));
433
- },
434
- children: [/* @__PURE__ */ jsx(Plus, { className: "size-4" }), t("New Item")]
435
- })]
436
- });
437
- }
438
- //#endregion
439
- export { FieldInput, FieldSet, JsonInput, ObjectInput };
@@ -1,259 +0,0 @@
1
- import { useQuery } from "../../../packages/shared-api/dist/utils/use-query.js";
2
- import { requestOAuthToken, usePlaygroundAuth } from "../../../playground/auth.js";
3
- import { useServer } from "../../../utils/use-server.js";
4
- import { useOpenAPI } from "../../../utils/create-page.js";
5
- import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../../packages/shared-api/dist/components/select.js";
6
- import { cn } from "../../../utils/cn.js";
7
- import { Label } from "../../../packages/shared-api/dist/components/label.js";
8
- import { Input } from "../../../packages/shared-api/dist/components/input.js";
9
- import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from "../../../packages/shared-api/dist/components/dialog.js";
10
- import { useMemo, useState } from "react";
11
- import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
12
- import { useTranslations } from "@fuma-translate/react";
13
- import { buttonVariants } from "fumadocs-ui/components/ui/button";
14
- //#region src/ui/playground/components/oauth-dialog.tsx
15
- const OAuthDialog = Dialog;
16
- function OAuthDialogContent(props) {
17
- const t = useTranslations({ note: "OAuth dialog" });
18
- return /* @__PURE__ */ jsxs(DialogContent, { children: [/* @__PURE__ */ jsxs(DialogHeader, { children: [/* @__PURE__ */ jsx(DialogTitle, { children: t("Authorization") }), /* @__PURE__ */ jsx(DialogDescription, { children: t("Obtain the access token for API.") })] }), /* @__PURE__ */ jsx(Content, { ...props })] });
19
- }
20
- function Content({ schemeId, scopes, setToken, setOpen }) {
21
- const { doc, oauthRedirectUrl } = useOpenAPI();
22
- const { dereferenced, resolve } = doc;
23
- const schemes = dereferenced.components?.securitySchemes;
24
- const tokenInfo = usePlaygroundAuth().store[schemeId];
25
- const { resolveUrl } = useServer();
26
- const scheme = resolve(schemes?.[schemeId]);
27
- if (!scheme || scheme.type !== "oauth2") throw new Error("unexpected schemaId: must be type oauth2");
28
- const [type, setType] = useState(() => {
29
- return Object.keys(scheme.flows)[0];
30
- });
31
- const [clientAuth, setClientAuth] = useState(() => tokenInfo?.type === "authorization_code" ? tokenInfo.client_auth : "header");
32
- const t = useTranslations({ note: "OAuth dialog" });
33
- const clientAuthMethods = {
34
- header: {
35
- name: t("Send as Basic Auth header"),
36
- description: t("Send the client ID and secret in the Authorization header.")
37
- },
38
- body: {
39
- name: t("Send client credentials in body"),
40
- description: t("Include the client ID and secret in the token request body.")
41
- }
42
- };
43
- const allFlows = useMemo(() => ({
44
- password: {
45
- name: t("Resource Owner Password Flow"),
46
- description: t("Authenticate using username and password."),
47
- supported: true
48
- },
49
- clientCredentials: {
50
- name: t("Client Credentials"),
51
- description: t("Intended for the server-to-server authentication."),
52
- supported: true
53
- },
54
- authorizationCode: {
55
- name: t("Authorization code"),
56
- description: t("Authenticate with 3rd party services"),
57
- supported: true
58
- },
59
- implicit: {
60
- name: t("Implicit"),
61
- description: t("Retrieve the access token directly."),
62
- supported: true
63
- },
64
- deviceAuthorization: {
65
- name: t("Device Authorization"),
66
- description: t("Authenticate with device."),
67
- supported: false
68
- }
69
- }), [t]);
70
- const defaultValues = useMemo(() => {
71
- return {
72
- clientId: tokenInfo?.client_id ?? "",
73
- clientSecret: tokenInfo?.type === "authorization_code" ? tokenInfo.client_secret : "",
74
- username: "",
75
- password: ""
76
- };
77
- }, [tokenInfo]);
78
- const authorize = useQuery(async (values) => {
79
- if (!type) return;
80
- const token = await requestOAuthToken(scheme, type, {
81
- ...values,
82
- schemeId,
83
- scopes,
84
- clientAuth,
85
- serverUrl: resolveUrl(),
86
- redirectUrl: oauthRedirectUrl
87
- });
88
- if (!token) return;
89
- setToken(token);
90
- setOpen(false);
91
- });
92
- const isLoading = authorize.isLoading;
93
- return /* @__PURE__ */ jsxs("form", {
94
- className: "flex flex-col gap-6",
95
- onSubmit: (e) => {
96
- const formData = new FormData(e.target);
97
- authorize.start(Object.fromEntries(formData.entries()));
98
- e.preventDefault();
99
- e.stopPropagation();
100
- },
101
- children: [
102
- /* @__PURE__ */ jsxs(Select, {
103
- items: Object.keys(scheme.flows).map((key) => {
104
- const { name, description } = allFlows[key];
105
- return {
106
- value: key,
107
- label: /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("p", {
108
- className: "font-medium",
109
- children: name
110
- }), /* @__PURE__ */ jsx("p", {
111
- className: "text-fd-muted-foreground",
112
- children: description
113
- })] })
114
- };
115
- }),
116
- value: type,
117
- onValueChange: setType,
118
- children: [/* @__PURE__ */ jsx(SelectTrigger, { children: /* @__PURE__ */ jsx(SelectValue, { placeholder: t("Select a flow") }) }), /* @__PURE__ */ jsx(SelectContent, { children: Object.keys(scheme.flows).map((key) => {
119
- const { name, description } = allFlows[key];
120
- return /* @__PURE__ */ jsxs(SelectItem, {
121
- value: key,
122
- children: [/* @__PURE__ */ jsx("p", {
123
- className: "font-medium",
124
- children: name
125
- }), /* @__PURE__ */ jsx("p", {
126
- className: "text-fd-muted-foreground",
127
- children: description
128
- })]
129
- }, key);
130
- }) })]
131
- }),
132
- (type === "authorizationCode" || type === "clientCredentials" || type === "implicit" || type === "password") && /* @__PURE__ */ jsxs("fieldset", {
133
- className: "flex flex-col gap-1.5",
134
- children: [
135
- /* @__PURE__ */ jsx(Label, {
136
- htmlFor: "client_id",
137
- children: t("Client ID")
138
- }),
139
- /* @__PURE__ */ jsx("p", {
140
- className: "text-fd-muted-foreground text-sm",
141
- children: t("The client ID of your OAuth application.")
142
- }),
143
- /* @__PURE__ */ jsx(Input, {
144
- id: "client_id",
145
- name: "clientId",
146
- placeholder: t("Enter value"),
147
- type: "text",
148
- autoComplete: "off",
149
- disabled: isLoading,
150
- defaultValue: defaultValues.clientId,
151
- required: type !== "password"
152
- })
153
- ]
154
- }),
155
- (type === "authorizationCode" || type === "clientCredentials" || type === "password") && /* @__PURE__ */ jsxs("fieldset", {
156
- className: "flex flex-col gap-1.5",
157
- children: [
158
- /* @__PURE__ */ jsx(Label, {
159
- htmlFor: "client_secret",
160
- children: t("Client Secret")
161
- }),
162
- /* @__PURE__ */ jsx("p", {
163
- className: "text-fd-muted-foreground text-sm",
164
- children: t("The client secret of your OAuth application.")
165
- }),
166
- /* @__PURE__ */ jsx(Input, {
167
- id: "client_secret",
168
- name: "clientSecret",
169
- placeholder: t("Enter value"),
170
- type: "password",
171
- autoComplete: "off",
172
- disabled: isLoading,
173
- defaultValue: defaultValues.clientSecret,
174
- required: type !== "password"
175
- })
176
- ]
177
- }),
178
- (type === "authorizationCode" || type === "clientCredentials" || type === "password") && /* @__PURE__ */ jsxs("fieldset", {
179
- className: "flex flex-col gap-1.5",
180
- children: [/* @__PURE__ */ jsx(Label, {
181
- htmlFor: "client_auth",
182
- children: t("Client Authentication")
183
- }), /* @__PURE__ */ jsxs(Select, {
184
- items: Object.entries(clientAuthMethods).map(([key, method]) => ({
185
- label: /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("p", {
186
- className: "font-medium",
187
- children: method.name
188
- }), /* @__PURE__ */ jsx("p", {
189
- className: "text-fd-muted-foreground",
190
- children: method.description
191
- })] }),
192
- value: key
193
- })),
194
- value: clientAuth,
195
- onValueChange: (v) => v !== null && setClientAuth(v),
196
- children: [/* @__PURE__ */ jsx(SelectTrigger, {
197
- id: "client_auth",
198
- disabled: isLoading,
199
- children: /* @__PURE__ */ jsx(SelectValue, {})
200
- }), /* @__PURE__ */ jsx(SelectContent, { children: Object.entries(clientAuthMethods).map(([key, method]) => /* @__PURE__ */ jsxs(SelectItem, {
201
- value: key,
202
- children: [/* @__PURE__ */ jsx("p", {
203
- className: "font-medium",
204
- children: method.name
205
- }), /* @__PURE__ */ jsx("p", {
206
- className: "text-fd-muted-foreground",
207
- children: method.description
208
- })]
209
- }, key)) })]
210
- })]
211
- }),
212
- type === "password" && /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsxs("fieldset", {
213
- className: "flex flex-col gap-1.5",
214
- children: [/* @__PURE__ */ jsx(Label, {
215
- htmlFor: "username",
216
- children: t("Username")
217
- }), /* @__PURE__ */ jsx(Input, {
218
- id: "username",
219
- name: "username",
220
- placeholder: t("Enter value"),
221
- type: "text",
222
- autoComplete: "off",
223
- disabled: isLoading,
224
- defaultValue: defaultValues.username,
225
- required: true
226
- })]
227
- }), /* @__PURE__ */ jsxs("fieldset", {
228
- className: "flex flex-col gap-1.5",
229
- children: [/* @__PURE__ */ jsx(Label, {
230
- htmlFor: "password",
231
- children: t("Password")
232
- }), /* @__PURE__ */ jsx(Input, {
233
- id: "password",
234
- name: "password",
235
- placeholder: t("Enter value"),
236
- type: "password",
237
- autoComplete: "off",
238
- disabled: isLoading,
239
- defaultValue: defaultValues.password,
240
- required: true
241
- })]
242
- })] }),
243
- type && allFlows[type].supported ? /* @__PURE__ */ jsxs(Fragment$1, { children: [authorize.error ? /* @__PURE__ */ jsx("p", {
244
- className: "text-red-400 font-medium text-sm",
245
- children: String(authorize.error)
246
- }) : null, /* @__PURE__ */ jsx("button", {
247
- type: "submit",
248
- className: cn(buttonVariants({ variant: "default" })),
249
- children: t("Submit")
250
- })] }) : /* @__PURE__ */ jsx("p", {
251
- className: "text-fd-muted-foreground bg-fd-muted p-2 rounded-lg border",
252
- children: t("Unsupported")
253
- })
254
- ]
255
- });
256
- }
257
- const OAuthDialogTrigger = DialogTrigger;
258
- //#endregion
259
- export { OAuthDialog, OAuthDialogContent, OAuthDialogTrigger };