Skip to content

Commit

Permalink
feat: add useRequestURL to match upstream (#1110)
Browse files Browse the repository at this point in the history
  • Loading branch information
wattanx authored Feb 13, 2024
1 parent 8578aba commit 5503879
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/bridge/src/imports/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const granularAppPresets: InlinePreset[] = [
imports: ['useRequestHeaders', 'useRequestEvent'],
from: '#app/composables/ssr'
},
{
imports: ['useRequestURL'],
from: '#app/composables/url'
},
{
imports: ['useAsyncData', 'useFetch', 'useHydration'],
from: '#app/mocks'
Expand Down
9 changes: 9 additions & 0 deletions packages/bridge/src/runtime/composables/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getRequestURL } from 'h3'
import { useRequestEvent } from './ssr'

export function useRequestURL () {
if (process.server) {
return getRequestURL(useRequestEvent()!)
}
return new URL(window.location.href)
}
9 changes: 9 additions & 0 deletions playground/pages/url.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script setup>
const url = useRequestURL()
</script>

<template>
<div>
<div>path: {{ url.pathname }}</div>
</div>
</template>
4 changes: 4 additions & 0 deletions test/bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ await setup({
})

describe('nuxt composables', () => {
it('has useRequestURL()', async () => {
const html = await $fetch('/url')
expect(html).toContain('path: /url')
})
it('sets cookies correctly', async () => {
const res = await fetch('/cookies', {
headers: {
Expand Down

0 comments on commit 5503879

Please sign in to comment.