Skip to content

Commit 9ca5d0b

Browse files
ellyx-csglaurie-dgit
authored andcommitted
send credential when bundling $ref
based on this PR stoplightio#2006
1 parent b6d8236 commit 9ca5d0b

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

Diff for: packages/elements-core/src/hooks/useBundleRefsIntoDocument.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as React from 'react';
99

1010
interface Options {
1111
baseUrl?: string;
12+
withCredentials?: boolean;
1213
}
1314

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

2021
const baseUrl = options?.baseUrl;
22+
const withCredentials = options?.withCredentials;
2123

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

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

5052
return bundledData;
5153
}
5254

53-
const commonBundleOptions = { continueOnError: true };
54-
const doBundle = (data: object, baseUrl?: string) => {
55+
const doBundle = (data: object, baseUrl?: string, withCredentials?: boolean) => {
56+
const commonBundleOptions = {
57+
continueOnError: true,
58+
resolve: {
59+
http: <$RefParser.HTTPResolverOptions>{ withCredentials },
60+
},
61+
};
5562
if (!baseUrl) {
5663
return $RefParser.bundle(data, commonBundleOptions);
5764
} else {

Diff for: packages/elements/src/containers/API.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ export interface CommonAPIProps extends RoutingProps {
103103
* Allows to define renderers for vendor extensions
104104
*/
105105
renderExtensionAddon?: ExtensionAddonRenderer;
106+
107+
/**
108+
* Whether to include CORS credentials (cookies, authorization headers, TLS client certificates)
109+
* in remote ref requests
110+
* @default: false
111+
*/
112+
withCredentials?: boolean;
113+
106114
}
107115

108116
const propsAreWithDocument = (props: APIProps): props is APIPropsWithDocument => {
@@ -122,6 +130,7 @@ export const APIImpl: React.FC<APIProps> = props => {
122130
tryItCorsProxy,
123131
maxRefDepth,
124132
renderExtensionAddon,
133+
withCredentials,
125134
} = props;
126135
const location = useLocation();
127136
const apiDescriptionDocument = propsAreWithDocument(props) ? props.apiDescriptionDocument : undefined;
@@ -143,7 +152,7 @@ export const APIImpl: React.FC<APIProps> = props => {
143152

144153
const document = apiDescriptionDocument || fetchedDocument || '';
145154
const parsedDocument = useParsedValue(document);
146-
const bundledDocument = useBundleRefsIntoDocument(parsedDocument, { baseUrl: apiDescriptionUrl });
155+
const bundledDocument = useBundleRefsIntoDocument(parsedDocument, { baseUrl: apiDescriptionUrl, withCredentials });
147156
const serviceNode = React.useMemo(() => transformOasToServiceNode(bundledDocument), [bundledDocument]);
148157
const exportProps = useExportDocumentProps({ originalDocument: document, bundledDocument });
149158

Diff for: packages/elements/src/web-components/components.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ export const ApiElement = createElementClass(API, {
1818
tryItCorsProxy: { type: 'string' },
1919
maxRefDepth: { type: 'number' },
2020
renderExtensionAddon: { type: 'function' },
21+
withCredentials: { type: 'boolean' },
2122
});

0 commit comments

Comments
 (0)