Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CORS handling for web-components #2006

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
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 },
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the key needed change to modify how json-schema-ref-parser fetches remote refs.

},
};
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';
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this isn't a change I made....

Copy link

@gagarwa gagarwa Mar 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be automated...

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' },
});