Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: base path support for openapi (#3780)
  • Loading branch information
kwasniew committed May 16, 2023
1 parent bd53193 commit d37bb6a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -92,7 +92,7 @@
]
},
"dependencies": {
"@unleash/express-openapi": "^0.2.2",
"@unleash/express-openapi": "^0.3.0",
"ajv": "^8.11.0",
"ajv-formats": "^2.1.1",
"async": "^3.2.4",
Expand Down
14 changes: 7 additions & 7 deletions src/lib/openapi/index.test.ts
Expand Up @@ -27,25 +27,25 @@ test('removeJsonSchemaProps', () => {
});

describe('createOpenApiSchema', () => {
test('createOpenApiSchema url', () => {
test('if no baseurl do not return servers', () => {
expect(
createOpenApiSchema({
unleashUrl: 'https://example.com',
baseUriPath: '',
}).servers![0].url,
).toEqual('https://example.com');
}).servers,
).toEqual([]);
});

test('if baseurl is set strips from serverUrl', () => {
test('if baseurl is set leave it in serverUrl', () => {
expect(
createOpenApiSchema({
unleashUrl: 'https://example.com/demo2',
baseUriPath: '/demo2',
}).servers![0].url,
).toEqual('https://example.com');
).toEqual('https://example.com/demo2');
});

test('if baseurl does not end with suffix, cowardly refuses to strip', () => {
test('if baseurl does not start with /, cowardly refuses to strip', () => {
expect(
createOpenApiSchema({
unleashUrl: 'https://example.com/demo2',
Expand All @@ -66,6 +66,6 @@ describe('createOpenApiSchema', () => {
unleashUrl: 'https://example.com/example/',
baseUriPath: '/example',
}).servers![0].url,
).toEqual('https://example.com');
).toEqual('https://example.com/example');
});
});
15 changes: 10 additions & 5 deletions src/lib/openapi/index.ts
Expand Up @@ -340,10 +340,14 @@ const findRootUrl: (unleashUrl: string, baseUriPath: string) => string = (
return unleashUrl;
}
const baseUrl = new URL(unleashUrl);
if (baseUrl.pathname.indexOf(baseUriPath) >= 0) {
return `${baseUrl.protocol}//${baseUrl.host}`;
}
return baseUrl.toString();
const url =
baseUrl.pathname.indexOf(baseUriPath) >= 0
? `${baseUrl.protocol}//${baseUrl.host}`
: baseUrl.toString();

return baseUriPath.startsWith('/')
? new URL(baseUriPath, url).toString()
: url;
};

export const createOpenApiSchema = ({
Expand All @@ -354,9 +358,10 @@ export const createOpenApiSchema = ({
'paths'
> => {
const url = findRootUrl(unleashUrl, baseUriPath);

return {
openapi: '3.0.3',
servers: url ? [{ url }] : [],
servers: baseUriPath ? [{ url }] : [],
info: {
title: 'Unleash API',
version: apiVersion,
Expand Down
Expand Up @@ -12935,11 +12935,7 @@ If the provided project does not exist, the list of events will be empty.",
"apiKey": [],
},
],
"servers": [
{
"url": "http://localhost:4242",
},
],
"servers": [],
"tags": [
{
"description": "Create, update, and delete [Unleash addons](https://docs.getunleash.io/addons).",
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Expand Up @@ -1528,10 +1528,10 @@
"@typescript-eslint/types" "5.59.2"
eslint-visitor-keys "^3.3.0"

"@unleash/express-openapi@^0.2.2":
version "0.2.2"
resolved "https://registry.yarnpkg.com/@unleash/express-openapi/-/express-openapi-0.2.2.tgz#b6e6a3d0fb93f8f52fc7d8f375e9bb52a3d3a46e"
integrity sha512-Evn1gVB5v7QMAs/mGjTc3NihX9wZnMdyBPvpd/JqMI8NDH9z/q46cYnh2t7bFPQj7FBghWwZlcJNm7PU0bxe7A==
"@unleash/express-openapi@^0.3.0":
version "0.3.0"
resolved "https://registry.yarnpkg.com/@unleash/express-openapi/-/express-openapi-0.3.0.tgz#3d65aeafc265732cf83c57b1d18452b5d0904856"
integrity sha512-rcbRNoL689knbemaTbL17FOb94hagr5T/vUUUZ1Fb107SGlBbOoQErf0tHlWpeCs6ffkM6uxo5BUnOAaf6CYDA==
dependencies:
ajv "^6.10.2"
http-errors "^1.7.3"
Expand Down

0 comments on commit d37bb6a

Please sign in to comment.