Skip to content

Commit

Permalink
Merge branch 'main' into ajiang/api-definition-migrate-2
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Oct 9, 2024
2 parents 7a398e9 + 67a8acc commit 80614e5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
1 change: 1 addition & 0 deletions fern/apis/fdr/generators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ groups:
url: npm.buildwithfern.com
package-name: "@fern-fern/fdr-cjs-sdk"
config:
useBrandedStringAliases: true
noSerdeLayer: true
outputSourceFiles: true
neverThrowErrors: true
Expand Down
37 changes: 17 additions & 20 deletions packages/ui/docs-bundle/src/server/DocsLoader.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { DocsV1Read, DocsV2Read } from "@fern-api/fdr-sdk";
import * as FernNavigation from "@fern-api/fdr-sdk/navigation";
import type { AuthEdgeConfig, FernUser } from "@fern-ui/fern-docs-auth";
import type { AuthEdgeConfig } from "@fern-ui/fern-docs-auth";
import { getAuthEdgeConfig } from "@fern-ui/fern-docs-edge-config";
import { verifyFernJWTConfig } from "./auth/FernJWT";
import { AuthProps, withAuthProps } from "./authProps";
import { loadWithUrl } from "./loadWithUrl";
import { pruneWithBasicTokenPublic } from "./withBasicTokenPublic";
Expand Down Expand Up @@ -31,31 +30,26 @@ export class DocsLoader {
return this;
}

private user: FernUser | undefined;
private auth: AuthEdgeConfig | undefined;
public withAuth(auth: AuthEdgeConfig | undefined, user: FernUser | undefined): DocsLoader {
public withAuth(auth: AuthEdgeConfig | undefined): DocsLoader {
this.auth = auth;
this.user = user;
return this;
}

private async loadAuth(): Promise<AuthProps | undefined> {
private async loadAuth(): Promise<[AuthProps | undefined, AuthEdgeConfig | undefined]> {
if (!this.auth) {
this.auth = await getAuthEdgeConfig(this.xFernHost);

try {
if (this.fernToken) {
this.user = await verifyFernJWTConfig(this.fernToken, this.auth);
}
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
}
if (!this.auth) {
return undefined;
return [undefined, undefined];
}
try {
return [await withAuthProps(this.auth, this.fernToken), this.auth];
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
return [undefined, this.auth];
}
return withAuthProps(this.auth, this.fernToken);
}

#loadForDocsUrlResponse: DocsV2Read.LoadDocsForUrlResponse | undefined;
Expand All @@ -72,7 +66,7 @@ export class DocsLoader {

private async loadDocs(): Promise<DocsV2Read.LoadDocsForUrlResponse | undefined> {
if (!this.#loadForDocsUrlResponse) {
const authProps = await this.loadAuth();
const [authProps] = await this.loadAuth();

const response = await loadWithUrl(this.xFernHost, authProps);

Expand Down Expand Up @@ -100,17 +94,20 @@ export class DocsLoader {
}

public async root(): Promise<FernNavigation.RootNode | undefined> {
const { authConfig, user } = await this.loadAuth();
const [auth, authConfig] = await this.loadAuth();
let node = await this.unprunedRoot();

// if the user is not authenticated, and the page requires authentication, prune the navigation tree
// to only show pages that are allowed to be viewed without authentication.
// note: the middleware will not show this page at all if the user is not authenticated.
if (node && authConfig?.type === "basic_token_verification" && !user) {
if (node && authConfig?.type === "basic_token_verification" && !auth) {
try {
// TODO: store this in cache
node = pruneWithBasicTokenPublic(authConfig, node);
} catch (e) {
// TODO: sentry
// eslint-disable-next-line no-console
console.error(e);
return undefined;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/docs-bundle/src/server/loadWithUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type LoadWithUrlResponse = APIResponse<
export async function loadWithUrl(url: string, auth?: AuthProps): Promise<LoadWithUrlResponse> {
url = withoutStaging(url);

if (auth != null && auth.user.partner === "workos") {
if (auth != null && auth.partner === "workos") {
return getRegistryServiceWithToken(auth.token).docs.v2.read.getPrivateDocsForUrl({
url: FdrAPI.Url(url),
});
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/docs-bundle/src/server/withInitialProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export async function withInitialProps({
const authConfig = await getAuthEdgeConfig(xFernHost);
const loader = DocsLoader.for(xFernHost, auth?.token)
.withFeatureFlags(featureFlags)
.withAuth(authConfig, auth?.user)
.withAuth(authConfig)
.withLoadDocsForUrlResponse(docs);

const root = await loader.root();
Expand Down

0 comments on commit 80614e5

Please sign in to comment.