Skip to content

Commit

Permalink
feat: support params
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 12, 2020
1 parent 08f3a1e commit e6a56ff
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,22 @@ const { article } = await $fetch<Article>(`/api/article/${id}`)
// Auto complete working with article.id
```

## ✔️ Adding `baseURL`

## ✔️ Support baseURL

By setting `baseURL` option `$fetch` prepends it with respecting to trailing/leading slashes and query params for baseURL using [ufo](https://github.com/nuxt-contrib/ufo):
By using `baseURL` option, `$fetch` prepends it with respecting to trailing/leading slashes and query params for baseURL using [ufo](https://github.com/nuxt-contrib/ufo):

```js
await $fetch('/config', { baseURL })
```

## ✔️ Adding params

By using `params` option, `$fetch` adds params to URL by preserving params in request itself using [ufo](https://github.com/nuxt-contrib/ufo):

```js
await $fetch('/movie?lang=en', { params: { id: 123 } })
```

## 🍣 Access to Raw Response

If you need to access raw response (for headers, etc), can use `$fetch.raw`:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"devDependencies": {
"@nuxt/h2": "^0.0.14",
"@nuxt/ufo": "^0.0.4",
"@nuxt/ufo": "^0.1.2",
"@nuxtjs/eslint-config-typescript": "latest",
"@types/flat": "latest",
"@types/jest": "latest",
Expand Down
14 changes: 11 additions & 3 deletions src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import destr from 'destr'
import { joinURL } from '@nuxt/ufo'
import { joinURL, withParams } from '@nuxt/ufo'
import type { Fetch, RequestInfo, RequestInit, Response } from './types'
import { createFetchError } from './error'

export interface CreateFetchOptions { fetch: Fetch }

export type FetchRequest = RequestInfo

export interface SearchParams { [key: string]: any }

export interface FetchOptions extends RequestInit {
baseURL?: string
params?: SearchParams
response?: boolean
}

Expand All @@ -21,8 +24,13 @@ export interface $Fetch {

export function createFetch ({ fetch }: CreateFetchOptions): $Fetch {
const raw: $Fetch['raw'] = async function (request, opts) {
if (opts && opts.baseURL && typeof request === 'string') {
request = joinURL(opts.baseURL, request)
if (opts && typeof request === 'string') {
if (opts.baseURL) {
request = joinURL(opts.baseURL, request)
}
if (opts.params) {
request = withParams(request, opts.params)
}
}
const response: FetchResponse<any> = await fetch(request, opts)
const text = await response.text()
Expand Down
8 changes: 6 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { listen, Listener } from 'listhen'
import { getParams } from '@nuxt/ufo'
import { createApp } from '@nuxt/h2'
import { $fetch, FetchError } from '../src/node'

describe('ohmyfetch', () => {
let listener: Listener

it('setup', async () => {
const app = createApp().use('/api', () => ({ api: 1 }))
const app = createApp().use('/api', req => (getParams(req.url || '')))
listener = await listen(app)
})

Expand All @@ -15,7 +16,10 @@ describe('ohmyfetch', () => {
})

it('api', async () => {
expect(await $fetch('api', { baseURL: listener.url })).toMatchObject({ api: 1 })
expect(await $fetch('api', {
baseURL: listener.url,
params: { api: 1 }
})).toMatchObject({ api: '1' })
})

it('404', async () => {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,10 @@
resolved "https://registry.yarnpkg.com/@nuxt/ufo/-/ufo-0.0.3.tgz#7673a54b81c020e7aea3a9e01e09a58c494a1eca"
integrity sha512-LQkuVafVNB9+ggRF7443AX1V1rEWRs32Frk7F2qnRLf8j/SzRzxEZ99jiZqxVho72zU7NcWQ6Jy62m4fkZC6Wg==

"@nuxt/ufo@^0.0.4":
version "0.0.4"
resolved "https://registry.yarnpkg.com/@nuxt/ufo/-/ufo-0.0.4.tgz#a957b55684a27e743e8d4b716072cb9a278c9790"
integrity sha512-1K8kfmQATIAirHOnwhvRJ4z53W+c6fq3mg4Z+xW5X9I9sSjjc6qjX33+SJt4I3m3o+WIuSSv2S5MCF1EVhBH/g==
"@nuxt/ufo@^0.1.2":
version "0.1.2"
resolved "https://registry.yarnpkg.com/@nuxt/ufo/-/ufo-0.1.2.tgz#2db4c7124dcffc877743e5246edf07d5dd529898"
integrity sha512-NgiCWLBPaKRNNRzzLF00xyU5h91A3Op/ZOUZY93zXvvtjgb3yMDOBkWDqOaTnv23MnRZjeRwYo1+Z3rvixsbbA==

"@nuxtjs/eslint-config-typescript@latest":
version "5.0.0"
Expand Down

0 comments on commit e6a56ff

Please sign in to comment.