Skip to content

Commit

Permalink
Extend interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudio Benedetti committed Jan 15, 2024
1 parent 8aee405 commit f5efd1a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/utils/__tests__/http-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -890,12 +890,12 @@ describe('Rerouting', () => {
reroutingRules: [
{from: '^/unk$', to: '/reroute-unk'},
{from: '^/$', to: '/reroute'},
{from: '^/orig$', to: '/reroute-1'},
{from: new RegExp('^/orig$'), to: '/reroute-1'},
{from: {method: 'GET', url: new RegExp('^/orig-2$')}, to: '/path/reroute-2'},
{from: {method: 'GET', url: '^/path/orig$'}, to: '/reroute-3'},
{from : '^(?<base>.*)/add/(.*)', to: '$base/add/orders/$1'}
]
}
}
const client = createFetchHttpClient.bind<() => HttpClientInstance>(customSupport)()

const {origin} = window.location
Expand Down Expand Up @@ -1109,7 +1109,7 @@ describe('Rerouting', () => {
expect(fetchMock).toHaveBeenNthCalledWith(6, `${origin}/`, expect.any(Object))
})

it('should fetch a get method', async () => {
it('should fetch a get method with URL input', async () => {
const customSupport = {
...support,
reroutingRules: [
Expand Down
17 changes: 10 additions & 7 deletions src/utils/http-client/with-rerouting.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {HttpClientSupport, HttpMethods} from './http-client'

export type RerouteRule = {
from: string | {url: string | RegExp, method: HttpMethods}
from: string | RegExp | {url: string | RegExp, method: HttpMethods}
to: string
}
type ValidRerouteRule = {
from: {method: HttpMethods, url: RegExp}
from: {url: RegExp, method: HttpMethods}
to: string
}

Expand Down Expand Up @@ -64,16 +64,20 @@ const completeRules = (rules: RerouteRule[]): ValidRerouteRule[] => {
if (isValidRerouteRule(rule)) {
acc.push(rule)
}
else if (typeof rule.from === 'string' && typeof rule.to === 'string') {
const url = new RegExp(rule.from)
acc.push(...methods.map(method => ({from: {method, url}, to: rule.to})))
}
else if (isValidObject(rule.from) && typeof rule.from.url === 'string') {
rule.from.url = new RegExp(rule.from.url)
if (isValidRerouteRule(rule)) {
acc.push(rule)
}
}
else if (typeof rule.from === 'string' || rule.from instanceof RegExp) {
const url = typeof rule.from === 'string' ? new RegExp(rule.from) : rule.from
acc.push(
...methods
.map(method => ({from: {method, url}, to: rule.to}))
.filter(isValidRerouteRule)
)
}
return acc
}, [])
}
Expand All @@ -82,7 +86,6 @@ export function withRerouting (this: HttpClientSupport): void | (() => void) {
type Fetch = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>

const {proxyWindow = window, reroutingRules} = this

const {fetch} = proxyWindow
if (!Array.isArray(reroutingRules) || reroutingRules.length === 0) {
return
Expand Down

0 comments on commit f5efd1a

Please sign in to comment.