Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: $RefParser.Options = {
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 @@ -112,6 +112,13 @@ export interface CommonAPIProps extends RoutingProps {
*/
tryItCorsProxy?: string;

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

/**
* The amount of references deep should be presented.
* @default undefined
Expand Down Expand Up @@ -145,6 +152,7 @@ export const APIImpl: React.FC<APIProps> = props => {
hideExport,
tryItCredentialsPolicy,
tryItCorsProxy,
withCredentials,
maxRefDepth,
renderExtensionAddon,
basePath,
Expand All @@ -170,7 +178,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 @@ -20,6 +20,7 @@ export const ApiElement = createElementClass(API, {
logo: { type: 'string' },
tryItCredentialsPolicy: { type: 'string' },
tryItCorsProxy: { type: 'string' },
withCredentials: { type: 'boolean' },
maxRefDepth: { type: 'number' },
renderExtensionAddon: { type: 'function' },
outerRouter: { type: 'boolean' },
Expand Down