|
| 1 | +/* ============================================================================ |
| 2 | + * Copyright (c) Palo Alto Networks |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * ========================================================================== */ |
| 7 | + |
| 8 | +import { createDescription } from "./createDescription"; |
| 9 | +import { createMethodEndpoint } from "./createMethodEndpoint"; |
| 10 | +import { createRequestBodyDetails } from "./createRequestBodyDetails"; |
| 11 | +import { createStatusCodes } from "./createStatusCodes"; |
| 12 | +import { create } from "./utils"; |
| 13 | +import { MediaTypeObject } from "../openapi/types"; |
| 14 | +import { ApiItem } from "../types"; |
| 15 | + |
| 16 | +interface Props { |
| 17 | + callbacks: ApiItem["callbacks"]; |
| 18 | +} |
| 19 | + |
| 20 | +interface RequestBodyProps { |
| 21 | + title: string; |
| 22 | + body: { |
| 23 | + content?: { |
| 24 | + [key: string]: MediaTypeObject; |
| 25 | + }; |
| 26 | + description?: string; |
| 27 | + required?: boolean; |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +export function createCallbacks({ callbacks }: Props) { |
| 32 | + if (callbacks === undefined) { |
| 33 | + return undefined; |
| 34 | + } |
| 35 | + |
| 36 | + const callbacksNames = Object.keys(callbacks); |
| 37 | + if (callbacksNames.length === 0) { |
| 38 | + return undefined; |
| 39 | + } |
| 40 | + |
| 41 | + return create("div", { |
| 42 | + children: [ |
| 43 | + create("div", { |
| 44 | + className: "openapi__divider", |
| 45 | + }), |
| 46 | + create("h2", { |
| 47 | + children: "Callbacks", |
| 48 | + id: "callbacks", |
| 49 | + }), |
| 50 | + create("OperationTabs", { |
| 51 | + className: "openapi-tabs__operation", |
| 52 | + children: callbacksNames.flatMap((name) => { |
| 53 | + const path = Object.keys(callbacks[name])[0]; |
| 54 | + const methods = new Map([ |
| 55 | + ["delete", callbacks[name][path].delete], |
| 56 | + ["get", callbacks[name][path].get], |
| 57 | + ["head", callbacks[name][path].head], |
| 58 | + ["options", callbacks[name][path].options], |
| 59 | + ["patch", callbacks[name][path].patch], |
| 60 | + ["post", callbacks[name][path].post], |
| 61 | + ["put", callbacks[name][path].put], |
| 62 | + ["trace", callbacks[name][path].trace], |
| 63 | + ]); |
| 64 | + |
| 65 | + return Array.from(methods).flatMap(([method, operationObject]) => { |
| 66 | + if (!operationObject) return []; |
| 67 | + |
| 68 | + const { description, requestBody, responses } = operationObject; |
| 69 | + |
| 70 | + return [ |
| 71 | + create("TabItem", { |
| 72 | + label: `${method.toUpperCase()} ${name}`, |
| 73 | + value: `${method}-${name}`, |
| 74 | + children: [ |
| 75 | + createMethodEndpoint(method, path), |
| 76 | + // TODO: add `deprecation notice` when markdown support is added |
| 77 | + createDescription(description), |
| 78 | + createRequestBodyDetails({ |
| 79 | + title: "Body", |
| 80 | + body: requestBody, |
| 81 | + } as RequestBodyProps), |
| 82 | + createStatusCodes({ |
| 83 | + id: "callbacks-responses", |
| 84 | + label: "Callbacks Responses", |
| 85 | + responses, |
| 86 | + }), |
| 87 | + ], |
| 88 | + }), |
| 89 | + ]; |
| 90 | + }); |
| 91 | + }), |
| 92 | + }), |
| 93 | + ], |
| 94 | + }); |
| 95 | +} |
0 commit comments