@toddledev/core
Advanced tools
Comparing version 0.0.3-alpha.18 to 0.0.3-alpha.19
@@ -25,7 +25,35 @@ "use strict"; | ||
const getUrl = (api, formulaContext, baseUrl) => { | ||
let urlPathname = ''; | ||
let urlQueryParams = new URLSearchParams(); | ||
let parsedUrl; | ||
const url = (0, formula_1.applyFormula)(api.url, formulaContext); | ||
const path = (0, exports.getRequestPath)(api.path, formulaContext); | ||
const queryParams = (0, exports.getRequestQueryParams)(api.queryParams, formulaContext); | ||
if (['string', 'number'].includes(typeof url)) { | ||
const urlInput = typeof url === 'number' ? String(url) : url; | ||
try { | ||
// Try to parse the URL to extract potential path and query parameters | ||
parsedUrl = new URL(urlInput, baseUrl); | ||
urlPathname = parsedUrl.pathname; | ||
urlQueryParams = parsedUrl.searchParams; | ||
// eslint-disable-next-line no-empty | ||
} | ||
catch { } | ||
} | ||
const pathParams = (0, exports.getRequestPath)(api.path, formulaContext); | ||
// Combine potential path parameters from the url declaration with the actual path parameters | ||
const path = `${urlPathname}${pathParams.length > 0 && !urlPathname.endsWith('/') ? '/' : ''}${pathParams}`; | ||
// Combine potential query parameters from the url declaration with the actual query parameters | ||
const queryParams = new URLSearchParams([ | ||
...urlQueryParams, | ||
...(0, exports.getRequestQueryParams)(api.queryParams, formulaContext), | ||
]); | ||
const queryString = [...queryParams.entries()].length > 0 ? `?${queryParams.toString()}` : ''; | ||
return new URL(`${url}${typeof url === 'string' && !url.endsWith('/') && path ? '/' : ''}${path}${queryString}`, baseUrl); | ||
if (parsedUrl) { | ||
const combinedUrl = new URL(parsedUrl.origin, baseUrl); | ||
combinedUrl.pathname = path; | ||
combinedUrl.search = queryParams.toString(); | ||
return combinedUrl; | ||
} | ||
else { | ||
return new URL(`${path}${queryString}`, baseUrl); | ||
} | ||
}; | ||
@@ -32,0 +60,0 @@ exports.getUrl = getUrl; |
@@ -16,3 +16,3 @@ { | ||
"main": "dist/index.js", | ||
"version": "0.0.3-alpha.18" | ||
"version": "0.0.3-alpha.19" | ||
} |
@@ -8,2 +8,3 @@ import { describe, expect, it, test } from '@jest/globals' | ||
getRequestQueryParams, | ||
getUrl, | ||
} from './api' | ||
@@ -81,2 +82,105 @@ import { ApiMethod, ApiRequest } from './apiTypes' | ||
}) | ||
describe('getUrl()', () => { | ||
test('it returns a valid url for null url', () => { | ||
const url = getUrl( | ||
{ | ||
url: valueFormula(null), | ||
path: { | ||
a: { formula: valueFormula('hello'), index: 0 }, | ||
b: { formula: valueFormula('world'), index: 1 }, | ||
}, | ||
queryParams: { | ||
q: { | ||
formula: valueFormula('test'), | ||
}, | ||
}, | ||
}, | ||
undefined as any, | ||
'https://example.com', | ||
) | ||
expect(url.href).toBe('https://example.com/hello/world?q=test') | ||
}) | ||
test('it returns a valid url for url with included path params', () => { | ||
const url = getUrl( | ||
{ | ||
url: valueFormula('https://example.com/test/path'), | ||
path: { | ||
a: { formula: valueFormula('hello'), index: 0 }, | ||
b: { formula: valueFormula('world'), index: 1 }, | ||
}, | ||
queryParams: { | ||
q: { | ||
formula: valueFormula('test'), | ||
}, | ||
}, | ||
}, | ||
undefined as any, | ||
'https://example.com', | ||
) | ||
expect(url.href).toBe('https://example.com/test/path/hello/world?q=test') | ||
}) | ||
test('it ignores trailing slashes in urls', () => { | ||
const url = getUrl( | ||
{ | ||
url: valueFormula('https://example.com/test/path/'), | ||
path: { | ||
a: { formula: valueFormula('hello'), index: 0 }, | ||
b: { formula: valueFormula('world'), index: 1 }, | ||
}, | ||
}, | ||
undefined as any, | ||
'https://example.com', | ||
) | ||
expect(url.href).toBe('https://example.com/test/path/hello/world') | ||
}) | ||
test('numbers are accepted as path parameters in url definition', () => { | ||
const url = getUrl( | ||
{ | ||
url: valueFormula(88), | ||
path: { | ||
a: { formula: valueFormula('hello'), index: 0 }, | ||
b: { formula: valueFormula('world'), index: 1 }, | ||
}, | ||
}, | ||
undefined as any, | ||
'https://example.com', | ||
) | ||
expect(url.href).toBe('https://example.com/88/hello/world') | ||
}) | ||
test('supports relative urls', () => { | ||
const url = getUrl( | ||
{ | ||
url: valueFormula('/test/path/'), | ||
path: { | ||
a: { formula: valueFormula('hello'), index: 0 }, | ||
b: { formula: valueFormula('world'), index: 1 }, | ||
}, | ||
}, | ||
undefined as any, | ||
'https://mysite.com', | ||
) | ||
expect(url.href).toBe('https://mysite.com/test/path/hello/world') | ||
}) | ||
}) | ||
test('supports query parameters in url declaration', () => { | ||
const url = getUrl( | ||
{ | ||
url: valueFormula('/test/path/?q=test&hello=world'), | ||
path: { | ||
a: { formula: valueFormula('hello'), index: 0 }, | ||
b: { formula: valueFormula('world'), index: 1 }, | ||
}, | ||
queryParams: { | ||
search: { | ||
formula: valueFormula('test'), | ||
}, | ||
}, | ||
}, | ||
undefined as any, | ||
'https://mysite.com', | ||
) | ||
expect(url.href).toBe( | ||
'https://mysite.com/test/path/hello/world?q=test&hello=world&search=test', | ||
) | ||
}) | ||
describe('getApiHeaders()', () => { | ||
@@ -83,0 +187,0 @@ test('it returns valid headers', () => { |
@@ -50,13 +50,34 @@ import { applyFormula, Formula, FormulaContext } from '../formula/formula' | ||
): URL => { | ||
let urlPathname = '' | ||
let urlQueryParams = new URLSearchParams() | ||
let parsedUrl: URL | undefined | ||
const url = applyFormula(api.url, formulaContext) | ||
const path = getRequestPath(api.path, formulaContext) | ||
const queryParams = getRequestQueryParams(api.queryParams, formulaContext) | ||
if (['string', 'number'].includes(typeof url)) { | ||
const urlInput = typeof url === 'number' ? String(url) : url | ||
try { | ||
// Try to parse the URL to extract potential path and query parameters | ||
parsedUrl = new URL(urlInput, baseUrl) | ||
urlPathname = parsedUrl.pathname | ||
urlQueryParams = parsedUrl.searchParams | ||
// eslint-disable-next-line no-empty | ||
} catch {} | ||
} | ||
const pathParams = getRequestPath(api.path, formulaContext) | ||
// Combine potential path parameters from the url declaration with the actual path parameters | ||
const path = `${urlPathname}${pathParams.length > 0 && !urlPathname.endsWith('/') ? '/' : ''}${pathParams}` | ||
// Combine potential query parameters from the url declaration with the actual query parameters | ||
const queryParams = new URLSearchParams([ | ||
...urlQueryParams, | ||
...getRequestQueryParams(api.queryParams, formulaContext), | ||
]) | ||
const queryString = | ||
[...queryParams.entries()].length > 0 ? `?${queryParams.toString()}` : '' | ||
return new URL( | ||
`${url}${ | ||
typeof url === 'string' && !url.endsWith('/') && path ? '/' : '' | ||
}${path}${queryString}`, | ||
baseUrl, | ||
) | ||
if (parsedUrl) { | ||
const combinedUrl = new URL(parsedUrl.origin, baseUrl) | ||
combinedUrl.pathname = path | ||
combinedUrl.search = queryParams.toString() | ||
return combinedUrl | ||
} else { | ||
return new URL(`${path}${queryString}`, baseUrl) | ||
} | ||
} | ||
@@ -63,0 +84,0 @@ |
Sorry, the diff of this file is not supported yet
379547
8671