Skip to content

chore: remove apitypings specific go.mod #15899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix ts types
  • Loading branch information
Emyrk committed Dec 17, 2024
commit 39e1aacafd7c216ccec1d1b6f3eed2ce8b69e671
36 changes: 35 additions & 1 deletion scripts/apitypings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/coder/guts"
"github.com/coder/guts/bindings"
"github.com/coder/guts/bindings/walk"
"github.com/coder/guts/config"
)

Expand Down Expand Up @@ -33,7 +34,8 @@ func main() {
"github.com/coder/serpent": "Serpent",
"tailscale.com/derp": "",
// Conflicting name "DERPRegion"
"tailscale.com/tailcfg": "Tail",
"tailscale.com/tailcfg": "Tail",
"tailscale.com/net/netcheck": "Netcheck",
}
for pkg, prefix := range referencePackages {
err = gen.IncludeReference(pkg, prefix)
Expand Down Expand Up @@ -75,6 +77,7 @@ func TsMutations(ts *guts.Typescript) {
// Omitempty + null is just '?' in golang json marshal
// number?: number | null --> number?: number
config.SimplifyOmitEmpty,
SimplifyUndefinedUnionFields,
)
}

Expand All @@ -84,6 +87,8 @@ func TypeMappings(gen *guts.GoParser) error {

gen.IncludeCustomDeclaration(map[string]guts.TypeOverride{
"github.com/coder/coder/v2/codersdk.NullTime": config.OverrideNullable(config.OverrideLiteral(bindings.KeywordString)),
// opt.Bool can return 'null' if unset
"tailscale.com/types/opt.Bool": config.OverrideNullable(config.OverrideLiteral(bindings.KeywordBoolean)),
})

err := gen.IncludeCustom(map[string]string{
Expand Down Expand Up @@ -144,3 +149,32 @@ func FixSerpentStruct(gen *guts.Typescript) {
}
})
}

// SimplifyUndefinedUnionFields converts 'foo: string | undefined' to 'foo?: string'
func SimplifyUndefinedUnionFields(gen *guts.Typescript) {
gen.ForEach(func(key string, originalNode bindings.Node) {
walk.Walk(questionTokenWalker{}, originalNode)
})
}

type questionTokenWalker struct {
}

func (q questionTokenWalker) Visit(node bindings.Node) (w walk.Visitor) {
switch n := node.(type) {
case *bindings.PropertySignature:
isUnion, ok := n.Type.(*bindings.UnionType)
if !ok {
return q
}
for i, t := range isUnion.Types {
if keyword, ok := t.(*bindings.LiteralKeyword); ok && *keyword == bindings.KeywordUndefined {
n.QuestionToken = true
// Remove undefined
isUnion.Types = append(isUnion.Types[:i], isUnion.Types[i+1:]...)
break
}
}
}
return q
}
26 changes: 24 additions & 2 deletions site/src/api/typesGenerated.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion site/src/pages/HealthPage/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export const Pill = forwardRef<HTMLDivElement, PillProps>((props, ref) => {
});

type BooleanPillProps = Omit<ComponentProps<typeof Pill>, "icon" | "value"> & {
value: boolean;
value: boolean | null;
};

export const BooleanPill: FC<BooleanPillProps> = ({
Expand Down
18 changes: 13 additions & 5 deletions site/src/pages/HealthPage/DERPPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
HealthMessage,
HealthSeverity,
HealthcheckReport,
NetcheckReport,
} from "api/typesGenerated";
import { Alert } from "components/Alert/Alert";
import type { FC } from "react";
Expand All @@ -24,7 +25,7 @@ import {
import { DismissWarningButton } from "./DismissWarningButton";
import { healthyColor } from "./healthyColor";

const flags = [
const flags: BooleanKeys<NetcheckReport>[] = [
"UDP",
"IPv6",
"IPv4",
Expand All @@ -39,9 +40,15 @@ const flags = [
"PCP",
];

type BooleanKeys<T> = {
[K in keyof T]: T[K] extends boolean | null ? K : never;
}[keyof T];


export const DERPPage: FC = () => {
const { derp } = useOutletContext<HealthcheckReport>();
const { netcheck, regions, netcheck_logs: logs } = derp;
const safeNetcheck = netcheck || {} as NetcheckReport;
const theme = useTheme();

return (
Expand Down Expand Up @@ -75,7 +82,7 @@ export const DERPPage: FC = () => {
<SectionLabel>Flags</SectionLabel>
<div css={{ display: "flex", flexWrap: "wrap", gap: 12 }}>
{flags.map((flag) => (
<BooleanPill key={flag} value={netcheck[flag]}>
<BooleanPill key={flag} value={safeNetcheck[flag]}>
{flag}
</BooleanPill>
))}
Expand All @@ -94,6 +101,7 @@ export const DERPPage: FC = () => {
if (a.region && b.region) {
return a.region.RegionName.localeCompare(b.region.RegionName);
}
return 0;
})
.map(({ severity, region }) => {
return (
Expand All @@ -111,10 +119,10 @@ export const DERPPage: FC = () => {
/>
}
component={Link}
to={`/health/derp/regions/${region.RegionID}`}
key={region.RegionID}
to={`/health/derp/regions/${region!.RegionID}`}
key={region!.RegionID}
>
{region.RegionName}
{region!.RegionName}
</Button>
);
})}
Expand Down
2 changes: 1 addition & 1 deletion site/src/pages/HealthPage/DERPRegionPage.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const meta: Meta = {
...generateMeta({
path: "/health/derp/regions/:regionId",
element: <DERPRegionPage />,
params: { regionId: firstRegionId },
params: { regionId: firstRegionId?.toString() || "" },
}),
};

Expand Down