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

feat: make proxy address customizable #909

Merged
merged 5 commits into from
Oct 17, 2024
Merged
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
8 changes: 2 additions & 6 deletions packages/oas-to-har/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,7 @@ export default function oasToHar(
operationSchema?: Operation,
values: DataForHAR = {},
auth: AuthForHAR = {},
opts: oasToHarOptions = {
// If true, the operation URL will be rewritten and prefixed with https://try.readme.io/ in
// order to funnel requests through our CORS-friendly proxy.
proxyUrl: false,
},
opts: oasToHarOptions = { proxyUrl: '' },
) {
let operation: Operation;
if (!operationSchema || typeof operationSchema.getParameters !== 'function') {
Expand Down Expand Up @@ -287,7 +283,7 @@ export default function oasToHar(

if (opts.proxyUrl) {
if (oas.getExtension(PROXY_ENABLED, operation)) {
har.url = `https://try.readme.io/${har.url}`;
har.url = `${opts.proxyUrl}/${har.url}`;
}
}

Expand Down
9 changes: 6 additions & 3 deletions packages/oas-to-har/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ export type AuthForHAR = Record<string, number | string | { pass?: string; user?
export { DataForHAR } from 'oas/types';

export interface oasToHarOptions {
// If true, the operation URL will be rewritten and prefixed with https://try.readme.io/ in
// order to funnel requests through our CORS-friendly proxy.
proxyUrl: boolean;
/* the URL of a proxy to use. Requests will be preefixed with its value; for
* example if you use "https://try.readme.io", a request to
* "https://example.com/some/api" will be sent to
* "https://try.readme.io/https://example.com/some/api"
*/
proxyUrl: string;
}
12 changes: 9 additions & 3 deletions packages/oas-to-har/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,19 @@ describe('oas-to-har', () => {
});
});

it('should not be prefixed with without option', () => {
it('should not be prefixed without proxyUrl', () => {
const har = oasToHar(proxyOas, proxyOas.operation('/path', 'get'));
expect(har.log.entries[0].request.url).toBe('https://example.com/path');
});

it('should be prefixed with try.readme.io with option', () => {
const har = oasToHar(proxyOas, proxyOas.operation('/path', 'get'), {}, {}, { proxyUrl: true });
it('should be prefixed with proxyUrl', () => {
const har = oasToHar(
proxyOas,
proxyOas.operation('/path', 'get'),
{},
{},
{ proxyUrl: 'https://try.readme.io' },
);
expect(har.log.entries[0].request.url).toBe('https://try.readme.io/https://example.com/path');
});
});
Expand Down