Skip to content

Commit

Permalink
Add parameter to configure CORS withCredentials handling when resolvi…
Browse files Browse the repository at this point in the history
…ng refs in HTTP served schemas.
  • Loading branch information
danpat committed Jan 28, 2022
1 parent efb3474 commit 2eead18
Show file tree
Hide file tree
Showing 4 changed files with 22 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: $RefParser.Options = {
continueOnError: true,
resolve: {
http: <$RefParser.HTTPResolverOptions>{ withCredentials },
},
};
if (!baseUrl) {
return $RefParser.bundle(data, commonBundleOptions);
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/elements-dev-portal/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// auto-updated during build
export const appVersion = '1.6.7';
export const appVersion = '1.6.9';
10 changes: 9 additions & 1 deletion packages/elements/src/containers/API.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ export interface CommonAPIProps extends RoutingProps {
* @default false
*/
tryItCorsProxy?: string;

/**
* 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 @@ -105,6 +112,7 @@ export const APIImpl: React.FC<APIProps> = props => {
hideExport,
tryItCredentialsPolicy,
tryItCorsProxy,
withCredentials,
} = props;
const apiDescriptionDocument = propsAreWithDocument(props) ? props.apiDescriptionDocument : undefined;

Expand All @@ -124,7 +132,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 @@ -16,4 +16,5 @@ export const ApiElement = createElementClass(API, {
logo: { type: 'string' },
tryItCredentialsPolicy: { type: 'string' },
tryItCorsProxy: { type: 'string' },
withCredentials: { type: 'boolean' },
});

0 comments on commit 2eead18

Please sign in to comment.