Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/auth-types-gen-export-wildcard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"everything-dev": patch
---

Fix stale auth type name in type generation by replacing the hard-coded re-export list in `auth-types.gen.ts` with `export type *`. New types added to the auth plugin's `auth-export.ts` now flow through automatically without generator changes, preventing the class of `TS2724` errors caused by stale type names.
2 changes: 2 additions & 0 deletions api/src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import type {
AuthPluginContext,
} from "./auth-types.gen";

export type * from "./auth-types.gen";

export type AuthContext = AuthPluginContext;
export type RequestAuthUser = NonNullable<AuthContext["user"]>;
export type ApiKeyContext = NonNullable<AuthContext["apiKey"]>;
Expand Down
10 changes: 5 additions & 5 deletions bun.lock

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

17 changes: 2 additions & 15 deletions host/src/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import type {
AuthPluginContext,
AuthRequestContext,
AuthSession,
AuthSessionData,
AuthSessionUser,
AuthServices as GeneratedAuthServices,
} from "@/lib/auth-types.gen";

export type {
AuthPluginContext,
AuthRequestContext,
AuthSession,
AuthSessionData,
AuthSessionUser,
};
export type AuthUser = AuthSessionUser;
export type * from "@/lib/auth-types.gen";

interface AuthServices extends GeneratedAuthServices {
auth: GeneratedAuthServices["auth"];
}
export type AuthUser = AuthSessionUser;

export interface AuthClient {
getSession(): Promise<AuthSession | null>;
Expand All @@ -38,5 +27,3 @@ export type HonoEnv = { Variables: AuthVariables };
export function toAuthClientContext(headers: Headers): Record<string, string> {
return Object.fromEntries(headers.entries());
}

export type { AuthServices };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"better-auth": "1.6.25",
"@better-auth/api-key": "1.6.25",
"@better-auth/passkey": "1.6.25",
"better-near-auth": "1.8.2",
"better-near-auth": "1.10.0",
"every-plugin": "^2.10.1",
"everything-dev": "^1.53.0",
"typescript": "^5.9.3",
Expand Down
126 changes: 24 additions & 102 deletions packages/everything-dev/src/api-contract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createHash } from "node:crypto";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, join, relative } from "node:path";
import { buildAuthTypesGenContent } from "./auth-types-gen";
import { fetchJsonOrNull, fetchResponse } from "./http-client";
import type { JsonObject, RuntimeConfig, RuntimePluginConfig } from "./types";

Expand Down Expand Up @@ -187,106 +188,6 @@ async function fetchAuthExportTypes(opts: {
return generatedPath;
}

function writeAuthTypesGen(targetPath: string, authExportPath: string) {
const exportImportPath = toImportPath(targetPath, authExportPath);
const content = [
`export type {`,
` Auth,`,
` AuthOrganizationContext,`,
` AuthOrganization,`,
` AuthOrganizationSummary,`,
` AuthOrganizationMember,`,
` AuthApiKey,`,
` AuthInvitation,`,
` GetActiveMemberInput,`,
` GetOrganizationInput,`,
` ListMembersInput,`,
` ListInvitationsInput,`,
` ListApiKeysInput,`,
` AuthServices,`,
` createAuthInstance,`,
`} from "${exportImportPath}";`,
`import type { InferOutput, ContractType as AuthContract } from "${toImportPath(targetPath, join(dirname(authExportPath), "contract.d.ts"))}";`,
`import type { Auth as BaseAuth } from "${exportImportPath}";`,
"",
'type RawAuthSession = InferOutput<"getSession">;',
'type RawAuthRequestContext = InferOutput<"getContext">;',
'type RawAuthActiveMember = InferOutput<"getActiveMember">;',
"",
'export type AuthSessionUser = NonNullable<RawAuthSession["user"]>;',
'export type AuthSessionData = NonNullable<RawAuthSession["session"]>;',
"export type AuthSession = {",
" user: AuthSessionUser | null;",
" session: AuthSessionData | null;",
"};",
"export type AuthRequestContext = RawAuthRequestContext;",
"export type AuthPluginContext = Partial<AuthRequestContext> & {",
" reqHeaders?: Headers;",
" getRawBody?: () => Promise<string>;",
"};",
"export type AuthActiveMember = RawAuthActiveMember;",
'export type AuthBaseSession = BaseAuth["$Infer"]["Session"];',
"export type AuthContractType = AuthContract;",
"",
].join("\n");
mkdirSync(dirname(targetPath), { recursive: true });
writeFileIfChanged(targetPath, content);
}

function writeContractBasedAuthTypesGen(targetPath: string, configDir: string) {
const authExportRel = toImportPath(
targetPath,
join(configDir, ".bos", "generated", "auth", "auth-export.d.ts"),
);
const contractRel = toImportPath(
targetPath,
join(configDir, ".bos", "generated", "auth", "contract.d.ts"),
);

const content = [
`export type {`,
` Auth,`,
` AuthOrganizationContext,`,
` AuthOrganization,`,
` AuthOrganizationSummary,`,
` AuthOrganizationMember,`,
` AuthApiKey,`,
` AuthInvitation,`,
` GetActiveMemberInput,`,
` GetOrganizationInput,`,
` ListMembersInput,`,
` ListInvitationsInput,`,
` ListApiKeysInput,`,
` AuthServices,`,
` createAuthInstance,`,
`} from "${authExportRel}";`,
`import type { InferOutput, ContractType as AuthContract } from "${contractRel}";`,
`import type { Auth as BaseAuth } from "${authExportRel}";`,
"",
'type RawAuthSession = InferOutput<"getSession">;',
'type RawAuthRequestContext = InferOutput<"getContext">;',
'type RawAuthActiveMember = InferOutput<"getActiveMember">;',
"",
'export type AuthSessionUser = NonNullable<RawAuthSession["user"]>;',
'export type AuthSessionData = NonNullable<RawAuthSession["session"]>;',
"export type AuthSession = {",
" user: AuthSessionUser | null;",
" session: AuthSessionData | null;",
"};",
"export type AuthRequestContext = RawAuthRequestContext;",
"export type AuthPluginContext = Partial<AuthRequestContext> & {",
" reqHeaders?: Headers;",
" getRawBody?: () => Promise<string>;",
"};",
"export type AuthActiveMember = RawAuthActiveMember;",
'export type AuthBaseSession = BaseAuth["$Infer"]["Session"];',
"export type AuthContractType = AuthContract;",
"",
].join("\n");
mkdirSync(dirname(targetPath), { recursive: true });
writeFileIfChanged(targetPath, content);
}

async function resolveContractSource(opts: {
configDir: string;
runtimeDir: string;
Expand Down Expand Up @@ -534,11 +435,32 @@ export function writeGeneratedFiles(opts: {

if (opts.authExportPath) {
for (const authTypesPath of authTypeTargets) {
writeAuthTypesGen(authTypesPath, opts.authExportPath);
const exportImportPath = toImportPath(authTypesPath, opts.authExportPath);
const contractImportPath = toImportPath(
authTypesPath,
join(dirname(opts.authExportPath), "contract.d.ts"),
);
mkdirSync(dirname(authTypesPath), { recursive: true });
writeFileIfChanged(
authTypesPath,
buildAuthTypesGenContent(exportImportPath, contractImportPath),
);
}
} else if (opts.authSource) {
for (const authTypesPath of authTypeTargets) {
writeContractBasedAuthTypesGen(authTypesPath, opts.configDir);
const exportImportPath = toImportPath(
authTypesPath,
join(opts.configDir, ".bos", "generated", "auth", "auth-export.d.ts"),
);
const contractImportPath = toImportPath(
authTypesPath,
join(opts.configDir, ".bos", "generated", "auth", "contract.d.ts"),
);
mkdirSync(dirname(authTypesPath), { recursive: true });
writeFileIfChanged(
authTypesPath,
buildAuthTypesGenContent(exportImportPath, contractImportPath),
);
}
}

Expand Down
48 changes: 48 additions & 0 deletions packages/everything-dev/src/auth-types-gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export const AUTH_DERIVED_TYPES_BODY = `
export type AuthSessionUser = NonNullable<InferOutput<"getSession">["user"]>;
export type AuthSessionData = NonNullable<InferOutput<"getSession">["session"]>;
export type AuthSession = {
user: AuthSessionUser | null;
session: AuthSessionData | null;
};
export type AuthRequestContext = InferOutput<"getContext">;
export type AuthPluginContext = Partial<AuthRequestContext> & {
reqHeaders?: Headers;
getRawBody?: () => Promise<string>;
};
`;

export function buildAuthTypesGenContent(
authExportImportPath: string,
contractImportPath: string,
): string {
return `export type * from "${authExportImportPath}";
import type { InferOutput } from "${contractImportPath}";
${AUTH_DERIVED_TYPES_BODY}
`;
}

export function buildAuthExportStub(): string {
return `export type Auth = any;
export type AuthOrganizationContext = any;
export type AuthOrganization = any;
export type AuthOrganizationSummary = any;
export type AuthOrganizationMember = any;
export type AuthApiKey = any;
export type AuthInvitation = any;
export type AuthTeam = any;
export type GetActiveMemberInput = any;
export type GetFullOrganizationInput = any;
export type ListMembersInput = any;
export type ListInvitationsInput = any;
export type ListApiKeysInput = any;
export type AuthServices = any;
export type createAuthInstance = any;
`;
}

export function buildAuthContractStub(): string {
return `export type ContractType = any;
export type InferOutput<_TRoute extends string> = any;
`;
}
Loading
Loading