1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-20 00:08:02 +01:00

feat: base path support for openapi (#3780)

This commit is contained in:
Mateusz Kwasniewski 2023-05-16 12:01:47 +02:00 committed by GitHub
parent bd53193dc9
commit d37bb6a790
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 22 deletions

View File

@ -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",

View File

@ -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',
@ -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');
});
});

View File

@ -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 = ({
@ -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,

View File

@ -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).",

View File

@ -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"