-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
176 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { oc } from '@orpc/contract' | ||
import { router } from '../tests/shared' | ||
import { deepSetLazyRouterPrefix, getLazyRouterPrefix, getRouterContract, setRouterContract } from './hidden' | ||
import { lazy, unlazy } from './lazy' | ||
import { createAccessibleLazyRouter } from './router-accessible-lazy' | ||
|
||
describe('setRouterContract', () => { | ||
const ping = oc.route({}) | ||
const baseContract = { ping } | ||
const nestedContract = { ping, nested: { ping } } | ||
|
||
it('sets contract on empty object', () => { | ||
const obj = {} | ||
const router = setRouterContract(obj, baseContract) | ||
expect(getRouterContract(router)).toBe(baseContract) | ||
}) | ||
|
||
it('preserves original object properties', () => { | ||
const obj = { existingProp: 'value' } as any | ||
const router = setRouterContract(obj, baseContract) | ||
expect(router.existingProp).toBe('value') | ||
expect(getRouterContract(router)).toBe(baseContract) | ||
}) | ||
|
||
it('handles nested contracts', () => { | ||
const obj = { nested: { value: 42 } } as any | ||
const router = setRouterContract(obj, nestedContract) | ||
expect(getRouterContract(router)).toBe(nestedContract) | ||
expect(router.nested.value).toBe(42) | ||
expect(getRouterContract(router.nested)).toBeUndefined() | ||
}) | ||
|
||
it('allows contract overwriting', () => { | ||
const obj = {} | ||
const router1 = setRouterContract(obj, baseContract) | ||
const router2 = setRouterContract(router1, nestedContract) | ||
expect(getRouterContract(router2)).toBe(nestedContract) | ||
}) | ||
}) | ||
|
||
describe('deepSetLazyRouterPrefix', () => { | ||
it('prefix on root and nested lazy', async () => { | ||
const obj = createAccessibleLazyRouter(lazy(() => Promise.resolve({ | ||
default: { | ||
l1: { | ||
l2: { | ||
l3: { value: router.ping }, | ||
}, | ||
}, | ||
}, | ||
}))) | ||
const prefixed = deepSetLazyRouterPrefix(obj, '/api') | ||
expect(getLazyRouterPrefix(prefixed)).toBe('/api') | ||
expect(getLazyRouterPrefix(prefixed.l1)).toBe('/api') | ||
expect(getLazyRouterPrefix(prefixed.l1.l2)).toBe('/api') | ||
expect(getLazyRouterPrefix(prefixed.l1.l2.l3)).toBe('/api') | ||
expect(getLazyRouterPrefix(prefixed.l1.l2.l3.value)).toBe('/api') | ||
expect(await unlazy(prefixed.l1.l2.l3.value)).toEqual(await unlazy(router.ping)) | ||
}) | ||
|
||
it('can override old prefix', async () => { | ||
const obj = createAccessibleLazyRouter(lazy(() => Promise.resolve({ | ||
default: { | ||
l1: { | ||
l2: { | ||
l3: { value: router.ping }, | ||
}, | ||
}, | ||
}, | ||
}))) | ||
|
||
const prefixed1 = deepSetLazyRouterPrefix(obj, '/api') | ||
const prefixed2 = deepSetLazyRouterPrefix(prefixed1, '/v2') | ||
|
||
expect(getLazyRouterPrefix(prefixed1)).toBe('/api') | ||
expect(getLazyRouterPrefix(prefixed1.l1.l2.l3.value)).toBe('/api') | ||
expect(getLazyRouterPrefix(prefixed2)).toBe('/v2') | ||
expect(getLazyRouterPrefix(prefixed2.l1.l2.l3.value)).toBe('/v2') | ||
|
||
expect(await unlazy(obj)).toEqual(await unlazy(obj)) | ||
}) | ||
|
||
it('not prefix on non-lazy', () => { | ||
const obj = { | ||
l1: { | ||
l2: { | ||
l3: { value: router.ping }, | ||
value: 1, // not lazy | ||
}, | ||
}, | ||
} as any | ||
|
||
const prefixed = deepSetLazyRouterPrefix(obj, '/api') as any | ||
|
||
expect(getLazyRouterPrefix(prefixed)).toBe('/api') | ||
expect(getLazyRouterPrefix(prefixed.l1)).toBe(undefined) | ||
expect(getLazyRouterPrefix(prefixed.l1.l2.value)).toBe(undefined) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { AnyContractRouter, ContractRouter, HTTPPath } from '@orpc/contract' | ||
import type { Lazy } from './lazy' | ||
import type { AnyRouter } from './router' | ||
import { isLazy } from './lazy' | ||
|
||
const ROUTER_CONTRACT_SYMBOL = Symbol('ORPC_ROUTER_CONTRACT') | ||
|
||
export function setRouterContract<T extends AnyRouter>(obj: T, contract: AnyContractRouter): T { | ||
return new Proxy(obj, { | ||
get(target, key) { | ||
if (key === ROUTER_CONTRACT_SYMBOL) { | ||
return contract | ||
} | ||
|
||
return Reflect.get(target, key) | ||
}, | ||
}) | ||
} | ||
|
||
export function getRouterContract(obj: object): ContractRouter<any> | undefined { | ||
return (obj as any)[ROUTER_CONTRACT_SYMBOL] | ||
} | ||
|
||
const LAZY_ROUTER_PREFIX_SYMBOL = Symbol('ORPC_LAZY_ROUTER_PREFIX') | ||
|
||
export function deepSetLazyRouterPrefix<T extends Lazy<any>>(router: T, prefix: HTTPPath): T { | ||
return new Proxy(router, { | ||
get(target, key) { | ||
if (key !== LAZY_ROUTER_PREFIX_SYMBOL) { | ||
const val = Reflect.get(target, key) | ||
if (isLazy(val)) { | ||
return deepSetLazyRouterPrefix(val, prefix) | ||
} | ||
|
||
return val | ||
} | ||
|
||
return prefix | ||
}, | ||
}) | ||
} | ||
|
||
export function getLazyRouterPrefix(obj: Lazy<any>): HTTPPath | undefined { | ||
return (obj as any)[LAZY_ROUTER_PREFIX_SYMBOL] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters