From b2b469bd3dbe01f9b44146e1b95c058c2ec6c7ff Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Fri, 21 Jun 2024 11:07:45 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=90=20fix(actions):=20Correct=20URL=20?= =?UTF-8?q?Formation=20for=20Subdomains=20in=20`createURL`=20(#3149)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/data-provider/specs/actions.spec.ts | 43 ++++++++++++++++++++ packages/data-provider/src/actions.ts | 8 ++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/packages/data-provider/specs/actions.spec.ts b/packages/data-provider/specs/actions.spec.ts index 09ddf857b1..9e18a48ad8 100644 --- a/packages/data-provider/specs/actions.spec.ts +++ b/packages/data-provider/specs/actions.spec.ts @@ -1,6 +1,7 @@ import axios from 'axios'; import { OpenAPIV3 } from 'openapi-types'; import { + createURL, resolveRef, ActionRequest, openapiToFunction, @@ -506,3 +507,45 @@ describe('validateAndParseOpenAPISpec', () => { expect(requestBuilders).toHaveProperty('saveCitation'); }); }); + +describe('createURL', () => { + it('correctly combines domain and path', () => { + expect(createURL('https://example.com', '/api/v1/users')).toBe( + 'https://example.com/api/v1/users', + ); + }); + + it('handles domain with trailing slash', () => { + expect(createURL('https://example.com/', '/api/v1/users')).toBe( + 'https://example.com/api/v1/users', + ); + }); + + it('handles path with leading slash', () => { + expect(createURL('https://example.com', 'api/v1/users')).toBe( + 'https://example.com/api/v1/users', + ); + }); + + it('handles domain with trailing slash and path with leading slash', () => { + expect(createURL('https://example.com/', '/api/v1/users')).toBe( + 'https://example.com/api/v1/users', + ); + }); + + it('handles domain without trailing slash and path without leading slash', () => { + expect(createURL('https://example.com', 'api/v1/users')).toBe( + 'https://example.com/api/v1/users', + ); + }); + + it('handles empty path', () => { + expect(createURL('https://example.com', '')).toBe('https://example.com/'); + }); + + it('handles domain with subdirectory', () => { + expect(createURL('https://example.com/subdirectory', '/api/v1/users')).toBe( + 'https://example.com/subdirectory/api/v1/users', + ); + }); +}); diff --git a/packages/data-provider/src/actions.ts b/packages/data-provider/src/actions.ts index 120ab3d7b4..0e1260d5db 100644 --- a/packages/data-provider/src/actions.ts +++ b/packages/data-provider/src/actions.ts @@ -32,8 +32,10 @@ export function sha1(input: string) { } export function createURL(domain: string, path: string) { - const myURL = new URL(path, domain); - return myURL.toString(); + const cleanDomain = domain.replace(/\/$/, ''); + const cleanPath = path.replace(/^\//, ''); + const fullURL = `${cleanDomain}/${cleanPath}`; + return new URL(fullURL).toString(); } export class FunctionSignature { @@ -92,7 +94,7 @@ export class ActionRequest { private authHeaders: Record = {}; private authToken?: string; - async setParams(params: object) { + setParams(params: object) { this.operationHash = sha1(JSON.stringify(params)); this.params = params;