Skip to content

Commit

Permalink
feat: replace proxyUrl boolean with string
Browse files Browse the repository at this point in the history
  • Loading branch information
llimllib committed Oct 17, 2024
1 parent 1f1c1af commit 02eb060
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
15 changes: 6 additions & 9 deletions packages/oas-to-har/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,13 @@ export default function oasToHar(
values: DataForHAR = {},
auth: AuthForHAR = {},
opts: oasToHarOptions = {
// If true, the operation URL will be rewritten and prefixed with
// a proxy
proxyUrl: false,
// 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: '',
},
) {
if (opts.proxyUrl && !opts.proxyAddress) {
// eslint-disable-next-line no-param-reassign
opts.proxyAddress = 'https://try.readme.io';
}

let operation: Operation;
if (!operationSchema || typeof operationSchema.getParameters !== 'function') {
/**
Expand Down Expand Up @@ -292,7 +289,7 @@ export default function oasToHar(

if (opts.proxyUrl) {
if (oas.getExtension(PROXY_ENABLED, operation)) {
har.url = `${opts.proxyAddress}/${har.url}`;
har.url = `${opts.proxyUrl}/${har.url}`;
}
}

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

export interface oasToHarOptions {
// The URL to use for the proxy; defaults to https://try.readme.io
proxyAddress?: string;

// 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;
}
13 changes: 4 additions & 9 deletions packages/oas-to-har/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,25 +204,20 @@ 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 });
expect(har.log.entries[0].request.url).toBe('https://try.readme.io/https://example.com/path');
});

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

0 comments on commit 02eb060

Please sign in to comment.