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 authored and williamsuryadgit committed Aug 8, 2024
1 parent 48f777a commit 14805b6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 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
11 changes: 9 additions & 2 deletions packages/elements/src/containers/API.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,13 @@ export interface CommonAPIProps extends RoutingProps {
tryItCorsProxy?: string;

/**
* The amount of references deep should be presented.
* 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
*/
maxRefDepth?: number;
Expand Down Expand Up @@ -143,6 +149,7 @@ export const APIImpl: React.FC<APIProps> = props => {
hideExport,
tryItCredentialsPolicy,
tryItCorsProxy,
withCredentials,
maxRefDepth,
renderExtensionAddon,
} = props;
Expand All @@ -166,7 +173,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' },
});

0 comments on commit 14805b6

Please sign in to comment.