Skip to content

Commit

Permalink
fix: use urljoin and fix awkward uri encoding (#792)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored May 3, 2024
1 parent 4ec4395 commit 82661a8
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 99 deletions.
8 changes: 5 additions & 3 deletions packages/commons/fdr-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
`;

Expand Down
10 changes: 4 additions & 6 deletions packages/commons/fdr-utils/src/buildUrl.ts
Original file line number Diff line number Diff line change
@@ -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);
}
4 changes: 2 additions & 2 deletions packages/commons/fdr-utils/src/getAllUrlsFromDocsConfig.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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))));
}
2 changes: 2 additions & 0 deletions packages/ui/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions packages/ui/app/src/api-page/examples/HttpRequestExample.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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<string, unknown> = { ...example.headers };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const EMPTY_EXAMPLE = {
pathParameters: {},
queryParameters: {},
headers: {},
path: "",
path: "/api",
responseStatusCode: 0,
codeExamples: {},
codeSamples: [],
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 82661a8

Please sign in to comment.