Skip to content

Commit

Permalink
feat: make proxy address customizable
Browse files Browse the repository at this point in the history
Converting proxyUrl into a string would be possibly a breaking change,
so instead I added a proxyAddress option which is a URL to use instead
of try.readme.io

The reason to make this change is so that I can test a beta version of
try.readme.io safely in staging

Bumped oas-to-har version to 23.3.0
  • Loading branch information
llimllib committed Oct 17, 2024
1 parent 991b659 commit 9c9a488
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
13 changes: 4 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/oas-to-har/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@readme/oas-to-har",
"description": "Utility to transform an OAS operation into a HAR representation",
"version": "23.2.27",
"version": "23.3.0",
"author": "Jon Ursenbach <jon@ursenba.ch>",
"license": "ISC",
"sideEffects": false,
Expand Down
11 changes: 8 additions & 3 deletions packages/oas-to-har/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,16 @@ export default function oasToHar(
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.
// If true, the operation URL will be rewritten and prefixed with
// a proxy
proxyUrl: false,
},
) {
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 @@ -287,7 +292,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.proxyAddress}/${har.url}`;
}
}

Expand Down
3 changes: 3 additions & 0 deletions packages/oas-to-har/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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;
Expand Down
11 changes: 11 additions & 0 deletions packages/oas-to-har/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ describe('oas-to-har', () => {
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', () => {
const har = oasToHar(
proxyOas,
proxyOas.operation('/path', 'get'),
{},
{},
{ proxyUrl: true, proxyAddress: 'https://beta.try.readme.io' },
);
expect(har.log.entries[0].request.url).toBe('https://beta.try.readme.io/https://example.com/path');
});
});
});

Expand Down

0 comments on commit 9c9a488

Please sign in to comment.