From 82661a8d9618ecf31c34573e20789dec9694b589 Mon Sep 17 00:00:00 2001 From: Andrew Jiang Date: Fri, 3 May 2024 17:56:18 -0400 Subject: [PATCH] fix: use urljoin and fix awkward uri encoding (#792) --- packages/commons/fdr-utils/package.json | 8 +- .../no-version-no-tabs.test.ts.snap | 16 ++-- .../no-version-yes-tabs.test.ts.snap | 26 +++--- .../yes-version-no-tabs.test.ts.snap | 42 +++++----- .../yes-version-yes-tabs.test.ts.snap | 84 +++++++++---------- packages/commons/fdr-utils/src/buildUrl.ts | 10 +-- .../fdr-utils/src/getAllUrlsFromDocsConfig.ts | 4 +- packages/ui/app/package.json | 2 + .../api-page/examples/HttpRequestExample.ts | 6 +- .../stringifyHttpRequestExampleToCurl.test.ts | 2 +- pnpm-lock.yaml | 12 +++ 11 files changed, 113 insertions(+), 99 deletions(-) diff --git a/packages/commons/fdr-utils/package.json b/packages/commons/fdr-utils/package.json index 502bccce46..93925807a2 100644 --- a/packages/commons/fdr-utils/package.json +++ b/packages/commons/fdr-utils/package.json @@ -30,18 +30,20 @@ }, "dependencies": { "@fern-api/fdr-sdk": "workspace:*", - "@fern-ui/core-utils": "workspace:*" + "@fern-ui/core-utils": "workspace:*", + "url-join": "4.0.1" }, "devDependencies": { "@fern-platform/configs": "workspace:*", "@types/node": "^18.7.18", + "@types/url-join": "4.0.1", "depcheck": "^1.4.3", "eslint": "^8.56.0", - "vitest": "^1.5.0", "next-mdx-remote": "^4.4.1", "organize-imports-cli": "^0.10.0", "prettier": "^3.2.4", "stylelint": "^16.1.0", - "typescript": "5.4.3" + "typescript": "5.4.3", + "vitest": "^1.5.0" } } diff --git a/packages/commons/fdr-utils/src/__test__/__snapshots__/no-version-no-tabs.test.ts.snap b/packages/commons/fdr-utils/src/__test__/__snapshots__/no-version-no-tabs.test.ts.snap index 8076be8a77..e1478a77af 100644 --- a/packages/commons/fdr-utils/src/__test__/__snapshots__/no-version-no-tabs.test.ts.snap +++ b/packages/commons/fdr-utils/src/__test__/__snapshots__/no-version-no-tabs.test.ts.snap @@ -2,14 +2,14 @@ exports[`getAllUrlsFromDocsConfig > no-version-no-tabs > gets all urls from docs config 1`] = ` [ - "/docs/api", - "/docs/api/section-1", - "/docs/api/section-1/page-1", - "/docs/api/page-2", - "/docs/api/section-2", - "/docs/api/section-2/page-4", - "/docs/api/page-5", - "/docs/api/page-6", + "docs/api", + "docs/api/section-1", + "docs/api/section-1/page-1", + "docs/api/page-2", + "docs/api/section-2", + "docs/api/section-2/page-4", + "docs/api/page-5", + "docs/api/page-6", ] `; diff --git a/packages/commons/fdr-utils/src/__test__/__snapshots__/no-version-yes-tabs.test.ts.snap b/packages/commons/fdr-utils/src/__test__/__snapshots__/no-version-yes-tabs.test.ts.snap index cf30e5a8b7..03d69e5df9 100644 --- a/packages/commons/fdr-utils/src/__test__/__snapshots__/no-version-yes-tabs.test.ts.snap +++ b/packages/commons/fdr-utils/src/__test__/__snapshots__/no-version-yes-tabs.test.ts.snap @@ -2,19 +2,19 @@ exports[`getAllUrlsFromDocsConfig > no-version-yes-tabs > gets all urls from docs config 1`] = ` [ - "/docs/api", - "/docs/api/tab-1", - "/docs/api/tab-1/section-1", - "/docs/api/tab-1/section-1/page-1", - "/docs/api/page-2", - "/docs/api/tab-1/section-2", - "/docs/api/section-2", - "/docs/api/tab-1/section-2/page-4", - "/docs/api/tab-1/page-5", - "/docs/api/tab-2", - "/docs/api/tab-2/page-7", - "/docs/api/tab-2/page-8", - "/docs/api/tab-2/section-5", + "docs/api", + "docs/api/tab-1", + "docs/api/tab-1/section-1", + "docs/api/tab-1/section-1/page-1", + "docs/api/page-2", + "docs/api/tab-1/section-2", + "docs/api/section-2", + "docs/api/tab-1/section-2/page-4", + "docs/api/tab-1/page-5", + "docs/api/tab-2", + "docs/api/tab-2/page-7", + "docs/api/tab-2/page-8", + "docs/api/tab-2/section-5", ] `; diff --git a/packages/commons/fdr-utils/src/__test__/__snapshots__/yes-version-no-tabs.test.ts.snap b/packages/commons/fdr-utils/src/__test__/__snapshots__/yes-version-no-tabs.test.ts.snap index 03b5ef7ea5..8e4e39e4e7 100644 --- a/packages/commons/fdr-utils/src/__test__/__snapshots__/yes-version-no-tabs.test.ts.snap +++ b/packages/commons/fdr-utils/src/__test__/__snapshots__/yes-version-no-tabs.test.ts.snap @@ -2,27 +2,27 @@ exports[`getAllUrlsFromDocsConfig > yes-version-no-tabs > gets all urls from docs config 1`] = ` [ - "/docs/api", - "/docs/api/section-1", - "/docs/api/section-1/page-1", - "/docs/api/page-2", - "/docs/api/section-2", - "/docs/api/section-2/page-4", - "/docs/api/page-5", - "/docs/api/version-1", - "/docs/api/version-1/section-1", - "/docs/api/version-1/section-1/page-1", - "/docs/api/version-1/page-2", - "/docs/api/version-1/section-2", - "/docs/api/version-1/section-2/page-4", - "/docs/api/version-1/page-5", - "/docs/api/version-1/version-1", - "/docs/api/version-2", - "/docs/api/version-2/page-7", - "/docs/api/version-2/page-8", - "/docs/api/version-2/section-5", - "/docs/api/version-2/version-1", - "/docs/api/version-2/section-5/page-10", + "docs/api", + "docs/api/section-1", + "docs/api/section-1/page-1", + "docs/api/page-2", + "docs/api/section-2", + "docs/api/section-2/page-4", + "docs/api/page-5", + "docs/api/version-1", + "docs/api/version-1/section-1", + "docs/api/version-1/section-1/page-1", + "docs/api/version-1/page-2", + "docs/api/version-1/section-2", + "docs/api/version-1/section-2/page-4", + "docs/api/version-1/page-5", + "docs/api/version-1/version-1", + "docs/api/version-2", + "docs/api/version-2/page-7", + "docs/api/version-2/page-8", + "docs/api/version-2/section-5", + "docs/api/version-2/version-1", + "docs/api/version-2/section-5/page-10", ] `; diff --git a/packages/commons/fdr-utils/src/__test__/__snapshots__/yes-version-yes-tabs.test.ts.snap b/packages/commons/fdr-utils/src/__test__/__snapshots__/yes-version-yes-tabs.test.ts.snap index 2fa8b41d6c..6b0ef687b3 100644 --- a/packages/commons/fdr-utils/src/__test__/__snapshots__/yes-version-yes-tabs.test.ts.snap +++ b/packages/commons/fdr-utils/src/__test__/__snapshots__/yes-version-yes-tabs.test.ts.snap @@ -2,48 +2,48 @@ exports[`getAllUrlsFromDocsConfig > yes-version-yes-tabs > gets all urls from docs config 1`] = ` [ - "/docs/api", - "/docs/api/tab-1", - "/docs/api/tab-1/section-1", - "/docs/api/tab-1/section-1/page-1", - "/docs/api/page-2", - "/docs/api/tab-1/section-2", - "/docs/api/section-2", - "/docs/api/tab-1/section-2/page-4", - "/docs/api/tab-1/page-5", - "/docs/api/tab-2", - "/docs/api/tab-2/page-7", - "/docs/api/tab-2/page-8", - "/docs/api/tab-2/section-5", - "/docs/api/tab-2/section-5/page-10", - "/docs/api/version-1", - "/docs/api/version-1/tab-1", - "/docs/api/version-1/tab-1/section-1", - "/docs/api/version-1/tab-1/section-1/page-1", - "/docs/api/version-1/page-2", - "/docs/api/version-1/tab-1/section-2", - "/docs/api/version-1/section-2", - "/docs/api/version-1/tab-1/section-2/page-4", - "/docs/api/version-1/tab-1/page-5", - "/docs/api/version-1/tab-2", - "/docs/api/version-1/tab-2/page-7", - "/docs/api/version-1/tab-2/page-8", - "/docs/api/version-1/tab-2/section-5", - "/docs/api/version-1/tab-2/section-5/page-10", - "/docs/api/version-2", - "/docs/api/version-2/tab-1", - "/docs/api/version-2/tab-1/section-1", - "/docs/api/version-2/tab-1/section-1/page-1", - "/docs/api/version-2/page-2", - "/docs/api/version-2/tab-1/section-2", - "/docs/api/version-2/section-2", - "/docs/api/version-2/tab-1/section-2/page-4", - "/docs/api/version-2/tab-1/page-5", - "/docs/api/version-2/tab-2", - "/docs/api/version-2/tab-2/page-7", - "/docs/api/version-2/tab-2/page-8", - "/docs/api/version-2/tab-2/section-5", - "/docs/api/version-2/tab-2/section-5/page-10", + "docs/api", + "docs/api/tab-1", + "docs/api/tab-1/section-1", + "docs/api/tab-1/section-1/page-1", + "docs/api/page-2", + "docs/api/tab-1/section-2", + "docs/api/section-2", + "docs/api/tab-1/section-2/page-4", + "docs/api/tab-1/page-5", + "docs/api/tab-2", + "docs/api/tab-2/page-7", + "docs/api/tab-2/page-8", + "docs/api/tab-2/section-5", + "docs/api/tab-2/section-5/page-10", + "docs/api/version-1", + "docs/api/version-1/tab-1", + "docs/api/version-1/tab-1/section-1", + "docs/api/version-1/tab-1/section-1/page-1", + "docs/api/version-1/page-2", + "docs/api/version-1/tab-1/section-2", + "docs/api/version-1/section-2", + "docs/api/version-1/tab-1/section-2/page-4", + "docs/api/version-1/tab-1/page-5", + "docs/api/version-1/tab-2", + "docs/api/version-1/tab-2/page-7", + "docs/api/version-1/tab-2/page-8", + "docs/api/version-1/tab-2/section-5", + "docs/api/version-1/tab-2/section-5/page-10", + "docs/api/version-2", + "docs/api/version-2/tab-1", + "docs/api/version-2/tab-1/section-1", + "docs/api/version-2/tab-1/section-1/page-1", + "docs/api/version-2/page-2", + "docs/api/version-2/tab-1/section-2", + "docs/api/version-2/section-2", + "docs/api/version-2/tab-1/section-2/page-4", + "docs/api/version-2/tab-1/page-5", + "docs/api/version-2/tab-2", + "docs/api/version-2/tab-2/page-7", + "docs/api/version-2/tab-2/page-8", + "docs/api/version-2/tab-2/section-5", + "docs/api/version-2/tab-2/section-5/page-10", ] `; diff --git a/packages/commons/fdr-utils/src/buildUrl.ts b/packages/commons/fdr-utils/src/buildUrl.ts index ea1bceffa7..f5d11513db 100644 --- a/packages/commons/fdr-utils/src/buildUrl.ts +++ b/packages/commons/fdr-utils/src/buildUrl.ts @@ -1,8 +1,6 @@ +import urljoin from "url-join"; + export function buildUrl({ host, pathname }: { host: string; pathname: string }): string { - let hostWithoutTrailingSlash = host.endsWith("/") ? host.slice(0, -1) : host; - hostWithoutTrailingSlash = hostWithoutTrailingSlash.replace(".docs.staging.", ".docs."); - if (pathname.length === 0) { - return hostWithoutTrailingSlash; - } - return `${hostWithoutTrailingSlash}/${pathname}`; + host = host.replace(".docs.staging.", ".docs."); + return urljoin(host, pathname); } diff --git a/packages/commons/fdr-utils/src/getAllUrlsFromDocsConfig.ts b/packages/commons/fdr-utils/src/getAllUrlsFromDocsConfig.ts index 8dc793e80f..bad8bae41e 100644 --- a/packages/commons/fdr-utils/src/getAllUrlsFromDocsConfig.ts +++ b/packages/commons/fdr-utils/src/getAllUrlsFromDocsConfig.ts @@ -1,5 +1,5 @@ import type { APIV1Read, DocsV1Read } from "@fern-api/fdr-sdk"; -import { buildUrl } from "./buildUrl"; +import urljoin from "url-join"; import { resolveSidebarNodesRoot } from "./resolver"; import { visitSidebarNodeRaw } from "./visitSidebarNodeRaw"; @@ -17,5 +17,5 @@ export function getAllUrlsFromDocsConfig( visitedSlugs.push(node.slug.join("/")); }); - return Array.from(new Set(visitedSlugs.map((slug) => buildUrl({ host, pathname: slug })))); + return Array.from(new Set(visitedSlugs.map((slug) => urljoin(host, slug)))); } diff --git a/packages/ui/app/package.json b/packages/ui/app/package.json index 2109f212f3..7736851224 100644 --- a/packages/ui/app/package.json +++ b/packages/ui/app/package.json @@ -104,6 +104,7 @@ "tinycolor2": "^1.6.0", "unified": "^11.0.4", "unist-util-visit": "^5.0.0", + "url-join": "4.0.1", "vfile": "^6.0.1" }, "devDependencies": { @@ -131,6 +132,7 @@ "@types/react-dom": "^18.2.18", "@types/react-test-renderer": "^18.0.7", "@types/tinycolor2": "^1.4.6", + "@types/url-join": "4.0.1", "@vitejs/plugin-react": "^4.2.1", "autoprefixer": "^10.4.16", "chromatic": "^11.3.0", diff --git a/packages/ui/app/src/api-page/examples/HttpRequestExample.ts b/packages/ui/app/src/api-page/examples/HttpRequestExample.ts index 7122d9532a..0f2a141633 100644 --- a/packages/ui/app/src/api-page/examples/HttpRequestExample.ts +++ b/packages/ui/app/src/api-page/examples/HttpRequestExample.ts @@ -1,7 +1,7 @@ import { APIV1Read } from "@fern-api/fdr-sdk"; import { visitDiscriminatedUnion } from "@fern-ui/core-utils"; -import { noop } from "lodash-es"; -import { buildRequestUrl } from "../../api-playground/utils"; +import { compact, noop } from "lodash-es"; +import urljoin from "url-join"; import { ResolvedEndpointDefinition, ResolvedExampleEndpointRequest } from "../../resolver/types"; import { getEndpointEnvironmentUrl } from "../../util/endpoint"; @@ -23,7 +23,7 @@ export function convertEndpointExampleToHttpRequestExample( requestBody: ResolvedExampleEndpointRequest | null | undefined, ): HttpRequestExample { const environmentUrl = getEndpointEnvironmentUrl(endpoint); - const url = buildRequestUrl(environmentUrl, endpoint.path, example.pathParameters); + const url = urljoin(compact([environmentUrl, example.path])); const headers: Record = { ...example.headers }; diff --git a/packages/ui/app/src/api-page/examples/__test__/stringifyHttpRequestExampleToCurl.test.ts b/packages/ui/app/src/api-page/examples/__test__/stringifyHttpRequestExampleToCurl.test.ts index edfdd1009d..3f346141c0 100644 --- a/packages/ui/app/src/api-page/examples/__test__/stringifyHttpRequestExampleToCurl.test.ts +++ b/packages/ui/app/src/api-page/examples/__test__/stringifyHttpRequestExampleToCurl.test.ts @@ -36,7 +36,7 @@ const EMPTY_EXAMPLE = { pathParameters: {}, queryParameters: {}, headers: {}, - path: "", + path: "/api", responseStatusCode: 0, codeExamples: {}, codeSamples: [], diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ee9cec1c12..a393a00680 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -260,6 +260,9 @@ importers: '@fern-ui/core-utils': specifier: workspace:* version: link:../core-utils + url-join: + specifier: 4.0.1 + version: 4.0.1 devDependencies: '@fern-platform/configs': specifier: workspace:* @@ -267,6 +270,9 @@ importers: '@types/node': specifier: ^18.7.18 version: 18.19.26 + '@types/url-join': + specifier: 4.0.1 + version: 4.0.1 depcheck: specifier: ^1.4.3 version: 1.4.3 @@ -969,6 +975,9 @@ importers: unist-util-visit: specifier: ^5.0.0 version: 5.0.0 + url-join: + specifier: 4.0.1 + version: 4.0.1 vfile: specifier: ^6.0.1 version: 6.0.1 @@ -1045,6 +1054,9 @@ importers: '@types/tinycolor2': specifier: ^1.4.6 version: 1.4.6 + '@types/url-join': + specifier: 4.0.1 + version: 4.0.1 '@vitejs/plugin-react': specifier: ^4.2.1 version: 4.2.1(vite@5.2.6)