Skip to content

Commit

Permalink
send credential when bundling $ref
Browse files Browse the repository at this point in the history
based on this PR stoplightio#2006
  • Loading branch information
ellyx-csg committed Sep 16, 2022
1 parent 0a40509 commit 5599d9a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
15 changes: 11 additions & 4 deletions packages/elements-core/src/hooks/useBundleRefsIntoDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as React from 'react';

interface Options {
baseUrl?: string;
withCredentials?: boolean;
}

/**
Expand All @@ -18,6 +19,7 @@ export function useBundleRefsIntoDocument(document: unknown, options?: Options)
const [bundledData, setBundledData] = React.useState(document);

const baseUrl = options?.baseUrl;
const withCredentials = options?.withCredentials;

React.useEffect(() => {
if (!isObject(document)) {
Expand All @@ -26,7 +28,7 @@ export function useBundleRefsIntoDocument(document: unknown, options?: Options)
}

let isMounted = true;
doBundle(document, baseUrl)
doBundle(document, baseUrl, withCredentials)
.then(res => {
if (isMounted) {
setBundledData({ ...res }); // this hmm....library mutates document so a shallow copy is required to force a rerender in all cases
Expand All @@ -45,13 +47,18 @@ export function useBundleRefsIntoDocument(document: unknown, options?: Options)
return () => {
isMounted = false;
};
}, [document, baseUrl]);
}, [document, baseUrl, withCredentials]);

return bundledData;
}

const commonBundleOptions = { continueOnError: true };
const doBundle = (data: object, baseUrl?: string) => {
const doBundle = (data: object, baseUrl?: string, withCredentials?: boolean) => {
const commonBundleOptions = {
continueOnError: true,
resolve: {
http: <$RefParser.HTTPResolverOptions>{ withCredentials },
},
};
if (!baseUrl) {
return $RefParser.bundle(data, commonBundleOptions);
} else {
Expand Down
10 changes: 9 additions & 1 deletion packages/elements/src/containers/API.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ export interface CommonAPIProps extends RoutingProps {
* When set true use Redoc Schema component to render the schema
*/
redocSchema?: boolean;

/**
* Whether to include CORS credentials (cookies, authorization headers, TLS client certificates)
* in remote ref requests
* @default: false
*/
withCredentials?: boolean;
}

const propsAreWithDocument = (props: APIProps): props is APIPropsWithDocument => {
Expand All @@ -112,6 +119,7 @@ export const APIImpl: React.FC<APIProps> = props => {
tryItCredentialsPolicy,
tryItCorsProxy,
redocSchema,
withCredentials,
} = props;
const apiDescriptionDocument = propsAreWithDocument(props) ? props.apiDescriptionDocument : undefined;

Expand All @@ -131,7 +139,7 @@ export const APIImpl: React.FC<APIProps> = props => {

const document = apiDescriptionDocument || fetchedDocument || '';
const parsedDocument = useParsedValue(document);
const bundledDocument = useBundleRefsIntoDocument(parsedDocument, { baseUrl: apiDescriptionUrl });
const bundledDocument = useBundleRefsIntoDocument(parsedDocument, { baseUrl: apiDescriptionUrl, withCredentials });
const serviceNode = React.useMemo(() => transformOasToServiceNode(bundledDocument), [bundledDocument]);
const exportProps = useExportDocumentProps({ originalDocument: document, bundledDocument });

Expand Down
1 change: 1 addition & 0 deletions packages/elements/src/web-components/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export const ApiElement = createElementClass(API, {
tryItCredentialsPolicy: { type: 'string' },
tryItCorsProxy: { type: 'string' },
redocSchema: { type: 'boolean' },
withCredentials: { type: 'boolean' },
});

0 comments on commit 5599d9a

Please sign in to comment.