Skip to content

Commit

Permalink
feat: make proxy address customizable (#909)
Browse files Browse the repository at this point in the history
Convert proxyUrl from a boolean to a string, so now it represents the URL of the proxy server instead of an option about whether to use a proxy server or not.

This is a breaking change and oas-to-har should get a version major bump when it's released

The reason to make this change is so that I can test a beta version of try.readme.io safely in staging
  • Loading branch information
llimllib authored Oct 17, 2024
1 parent fbea635 commit 8cff2b9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
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 @@ -205,13 +205,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

0 comments on commit 8cff2b9

Please sign in to comment.