Skip to content

Commit c8a10b4

Browse files
committed
[Docs Site] Create WorkersTypesInterface component
1 parent 50c753e commit c8a10b4

File tree

6 files changed

+255
-8
lines changed

6 files changed

+255
-8
lines changed

.prettierignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ dist
55
public/__redirects
66
public/analytics/static/downloads/main.css
77
src/content/workers-ai-models/*.json
8-
src/content/partials/images/*.mdx
8+
src/content/partials/images/*.mdx
9+
src/content/style-guide/components/*.mdx

package-lock.json

Lines changed: 77 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
"tailwindcss": "4.1.4",
128128
"tippy.js": "6.3.7",
129129
"ts-blank-space": "0.6.2",
130+
"ts-morph": "26.0.0",
130131
"tsx": "4.20.5",
131132
"typescript": "5.9.2",
132133
"typescript-eslint": "8.41.0",
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
}

src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export { default as Width } from "./Width.astro";
6666
export { default as WorkersArchitectureDiagram } from "./WorkersArchitectureDiagram.astro";
6767
export { default as WorkersIsolateDiagram } from "./WorkersIsolateDiagram.astro";
6868
export { default as WorkersTemplates } from "./WorkersTemplates.astro";
69+
export { default as WorkersTypesInterface } from "./WorkersTypesInterface.astro";
6970
export { default as YouTube } from "./YouTube.astro";
7071
export { default as YouTubeVideos } from "./YouTubeVideos.astro";
7172

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
title: Workers Types Interface
3+
styleGuide:
4+
component: WorkersTypesInterface
5+
---
6+
7+
:::caution
8+
9+
This component should be considered a work-in-progress and there may be parts of an interface not properly represented yet.
10+
11+
:::
12+
13+
This component uses [`ts-morph`](https://ts-morph.com/) to generate documentation about the methods on an interface from `@cloudflare/workers-types`, including their parameters and return types.
14+
15+
## Import
16+
17+
```mdx
18+
import { WorkersTypesInterface } from "~/components";
19+
```
20+
21+
## Usage
22+
23+
```mdx live
24+
import { WorkersTypesInterface } from "~/components";
25+
26+
<WorkersTypesInterface name="ForwardableEmailMessage" />
27+
```
28+
29+
## `<WorkersTypesInterface>` Props
30+
31+
### `name`
32+
33+
**required**
34+
35+
**type:** `string`
36+
37+
The name of the TypeScript interface.

0 commit comments

Comments
 (0)