Skip to content

Commit

Permalink
wip, fix: add type safety to resolveEnvironment (#1202)
Browse files Browse the repository at this point in the history
  • Loading branch information
RohinBhargava authored Jul 26, 2024
1 parent 2070cfe commit c9b055a
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/ui/app/src/api-page/endpoints/EndpointUrl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const EndpointUrl = React.forwardRef<HTMLDivElement, PropsWithChildren<En
const renderPathParts = (parts: EndpointPathPart[]) => {
const elements: (ReactElement | null)[] = [];
if (selectedEnvironment != null) {
const url = parse(selectedEnvironment.baseUrl);
const url = parse(selectedEnvironment?.baseUrl);
if (showEnvironment) {
if (allEnvironmentIds.length < 2) {
elements.push(
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/app/src/api-page/web-socket/WebSocket.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ const WebhookContent: FC<WebSocket.Props> = ({ websocket, isLastInApi, types })
<CopyToClipboardButton
className="-mr-1"
content={() =>
`${resolveEnvironment(websocket, selectedEnvironmentId).baseUrl}${websocket.path.map((path) => (path.type === "literal" ? path.value : `:${path.key}`)).join("/")}`
`${resolveEnvironment(websocket, selectedEnvironmentId)?.baseUrl}${websocket.path.map((path) => (path.type === "literal" ? path.value : `:${path.key}`)).join("/")}`
}
/>
</div>
Expand Down Expand Up @@ -320,7 +320,7 @@ const WebhookContent: FC<WebSocket.Props> = ({ websocket, isLastInApi, types })
<tr>
<td className="text-left align-top">URL</td>
<td className="text-left align-top">
{`${resolveEnvironment(websocket, selectedEnvironmentId).baseUrl ?? ""}${example?.path ?? stringifyResolvedEndpointPathParts(websocket.path)}`}
{`${resolveEnvironment(websocket, selectedEnvironmentId)?.baseUrl ?? ""}${example?.path ?? stringifyResolvedEndpointPathParts(websocket.path)}`}
</td>
</tr>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/app/src/api-playground/PlaygroundWebSocket.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const PlaygroundWebSocket: FC<PlaygroundWebSocketProps> = ({ websocket, t
useEffect(() => () => socket.current?.close(), []);

const selectedEnvironmentId = useSelectedEnvironmentId();
const baseUrl = resolveEnvironment(websocket, selectedEnvironmentId).baseUrl;
const baseUrl = resolveEnvironment(websocket, selectedEnvironmentId)?.baseUrl;

const startSession = useCallback(async () => {
return new Promise<boolean>((resolve) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/app/src/api-playground/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function buildEndpointUrl(
formState: PlaygroundRequestFormState | undefined,
): string {
return buildRequestUrl(
endpoint && resolveEnvironment(endpoint).baseUrl,
endpoint && resolveEnvironment(endpoint)?.baseUrl,
endpoint?.path,
formState?.pathParameters,
formState?.queryParameters,
Expand Down Expand Up @@ -450,7 +450,7 @@ export function stringifyCurl({
return stringifyHttpRequestExampleToCurl({
method: endpoint.method,
// TODO: wire through the hook based environment
url: buildRequestUrl(resolveEnvironment(endpoint).baseUrl, endpoint?.path, formState?.pathParameters),
url: buildRequestUrl(resolveEnvironment(endpoint)?.baseUrl, endpoint?.path, formState?.pathParameters),
urlQueries: formState.queryParameters,
headers,
body:
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/app/src/resolver/resolveCodeSnippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function getHarRequest(
cookies: [],
bodySize: -1,
};
request.url = buildRequestUrl(resolveEnvironment(endpoint).baseUrl, endpoint.path, example.pathParameters);
request.url = buildRequestUrl(resolveEnvironment(endpoint)?.baseUrl, endpoint.path, example.pathParameters);
request.method = endpoint.method;
request.queryString = Object.entries(example.queryParameters).map(([name, value]) => ({
name,
Expand Down
7 changes: 4 additions & 3 deletions packages/ui/app/src/resolver/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ export function getParameterDescription(
export const resolveEnvironment = (
endpoint: ResolvedWebSocketChannel | ResolvedEndpointDefinition,
selectedEnvironmentId?: string,
): APIV1Read.Environment => {
): APIV1Read.Environment | undefined => {
if (!selectedEnvironmentId && typeof window !== "undefined") {
// TODO: replace this, this is a workaround for now, for functions that need to resolve in the playground,
// but do not have access to hooks
Expand All @@ -768,8 +768,9 @@ export const resolveEnvironmentUrlInCodeSnippet = (
selectedEnvironmentId?: string,
): string => {
const urlToReplace = endpoint.environments.find((env) => requestCodeSnippet.includes(env.baseUrl))?.baseUrl;
return urlToReplace
? requestCodeSnippet.replace(urlToReplace, resolveEnvironment(endpoint, selectedEnvironmentId).baseUrl)
const resolvedEnvironment = resolveEnvironment(endpoint, selectedEnvironmentId);
return urlToReplace && resolvedEnvironment
? requestCodeSnippet.replace(urlToReplace, resolvedEnvironment.baseUrl)
: requestCodeSnippet;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/ui/app/src/util/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function divideEndpointPathToParts(path: ResolvedEndpointPathParts[]): En
}

export function getEndpointEnvironmentUrl(endpoint: ResolvedEndpointDefinition): string | undefined {
return resolveEnvironment(endpoint).baseUrl;
return resolveEnvironment(endpoint)?.baseUrl;
}

export function getEndpointTitleAsString(endpoint: APIV1Read.EndpointDefinition): string {
Expand Down

0 comments on commit c9b055a

Please sign in to comment.