|
| 1 | +--- |
| 2 | +import { JSDocTagInfo, Project } from "ts-morph"; |
| 3 | +import RawTypes from "@cloudflare/workers-types/experimental/index.d.ts?raw"; |
| 4 | +import { marked } from "marked"; |
| 5 | +import AnchorHeading from "./AnchorHeading.astro"; |
| 6 | +import Type from "./Type.astro"; |
| 7 | +import { Code } from "@astrojs/starlight/components"; |
| 8 | +import { z } from "astro:schema"; |
| 9 | +
|
| 10 | +type Props = z.input<typeof props>; |
| 11 | +
|
| 12 | +const props = z |
| 13 | + .object({ |
| 14 | + name: z.string(), |
| 15 | + }) |
| 16 | + .strict(); |
| 17 | +
|
| 18 | +const { name: interfaceName } = props.parse(Astro.props); |
| 19 | +
|
| 20 | +function JSDocToMarkdown(jsdoc: JSDocTagInfo[]) { |
| 21 | + return marked.parse( |
| 22 | + jsdoc |
| 23 | + .map((tag) => { |
| 24 | + return tag |
| 25 | + .getText() |
| 26 | + .filter((part) => part.kind === "text") |
| 27 | + .map((part) => { |
| 28 | + return part.text; |
| 29 | + }) |
| 30 | + .join(" "); |
| 31 | + }) |
| 32 | + .join("\n"), |
| 33 | + { async: false }, |
| 34 | + ); |
| 35 | +} |
| 36 | +
|
| 37 | +const project = new Project(); |
| 38 | +const sourceFile = project.createSourceFile("index.d.ts", RawTypes); |
| 39 | +
|
| 40 | +const int = sourceFile.getInterface(interfaceName); |
| 41 | +
|
| 42 | +if (!int) { |
| 43 | + throw new Error( |
| 44 | + `[WorkersTypesInterface] Could not find interface "${interfaceName}"`, |
| 45 | + ); |
| 46 | +} |
| 47 | +
|
| 48 | +const methods = int.getMethods(); |
| 49 | +
|
| 50 | +if (!methods || methods.length === 0) { |
| 51 | + throw new Error( |
| 52 | + `[WorkersTypesInterface] Could not find any methods for interface "${interfaceName}"`, |
| 53 | + ); |
| 54 | +} |
| 55 | +--- |
| 56 | + |
| 57 | +<AnchorHeading title={interfaceName} depth={2} /> |
| 58 | +<Code lang="ts" code={int.print()} wrap={true} /> |
| 59 | +<AnchorHeading title="Methods" depth={3} slug={`${interfaceName}-methods`} /> |
| 60 | +{ |
| 61 | + methods.map((method) => { |
| 62 | + const methodName = method.getName(); |
| 63 | + const signature = method.getSignature(); |
| 64 | + |
| 65 | + if (!signature) { |
| 66 | + throw new Error( |
| 67 | + `[WorkersTypesInterface] Could not find signature for method "${interfaceName}#${methodName}"`, |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + const comment = signature |
| 72 | + .getDocumentationComments() |
| 73 | + .map((comment) => { |
| 74 | + return comment.getText(); |
| 75 | + }) |
| 76 | + .join("\n"); |
| 77 | + |
| 78 | + const jsdoc = signature.getJsDocTags(); |
| 79 | + const returnsJsdoc = jsdoc.find((tag) => tag.getName() === "returns"); |
| 80 | + |
| 81 | + const parameters = signature.getParameters(); |
| 82 | + const returns = signature.getReturnType(); |
| 83 | + |
| 84 | + return ( |
| 85 | + <> |
| 86 | + <AnchorHeading |
| 87 | + title={`\`${methodName}()\``} |
| 88 | + depth={4} |
| 89 | + slug={`${interfaceName}-${methodName}`} |
| 90 | + /> |
| 91 | + <p>{comment}</p> |
| 92 | + <AnchorHeading |
| 93 | + title="Signature" |
| 94 | + depth={5} |
| 95 | + slug={`${interfaceName}-${methodName}-signature`} |
| 96 | + /> |
| 97 | + <Code lang="ts" code={method.print()} wrap={true} /> |
| 98 | + <AnchorHeading |
| 99 | + title="Parameters" |
| 100 | + depth={5} |
| 101 | + slug={`${interfaceName}-${methodName}-parameters`} |
| 102 | + /> |
| 103 | + <ul> |
| 104 | + {parameters?.map((param) => { |
| 105 | + const type = param.getDeclarations()[0].getType(); |
| 106 | + const jsdoc = param.getJsDocTags(); |
| 107 | + |
| 108 | + const markdown = JSDocToMarkdown(jsdoc); |
| 109 | + |
| 110 | + return ( |
| 111 | + <li> |
| 112 | + <span> |
| 113 | + <code>{param.getName()}</code> |
| 114 | + </span>{" "} |
| 115 | + <Type text={type.getText()} /> |
| 116 | + <Fragment set:html={markdown} /> |
| 117 | + </li> |
| 118 | + ); |
| 119 | + })} |
| 120 | + </ul> |
| 121 | + <AnchorHeading |
| 122 | + title="Returns" |
| 123 | + depth={5} |
| 124 | + slug={`${interfaceName}-${methodName}-returns`} |
| 125 | + /> |
| 126 | + <ul> |
| 127 | + <li> |
| 128 | + <Type text={returns?.getText()} /> |
| 129 | + <Fragment |
| 130 | + set:html={returnsJsdoc ? JSDocToMarkdown([returnsJsdoc]) : ""} |
| 131 | + /> |
| 132 | + </li> |
| 133 | + </ul> |
| 134 | + </> |
| 135 | + ); |
| 136 | + }) |
| 137 | +} |
0 commit comments