Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
refactor: update unjs dependencies to stable v1 (#9011)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored and danielroe committed Jan 21, 2023
1 parent 2d347f5 commit 9eb3325
Show file tree
Hide file tree
Showing 33 changed files with 641 additions and 295 deletions.
2 changes: 1 addition & 1 deletion docs/content/1.getting-started/6.data-fetching.md
Expand Up @@ -352,7 +352,7 @@ export default defineComponent({

## Directly Calling an API Endpoint

There are instances where you may need to directly call the API. Nuxt 3 provides a globally available `$fetch` method using [unjs/ohmyfetch](https://github.com/unjs/ohmyfetch) (in addition to `fetch`)
There are instances where you may need to directly call the API. Nuxt 3 provides a globally available `$fetch` method using [unjs/ofetch](https://github.com/unjs/ofetch) (in addition to `fetch`)
with the same API as the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch).

Using `$fetch` has a number of benefits, including:
Expand Down
4 changes: 2 additions & 2 deletions docs/content/2.guide/1.concepts/4.server-engine.md
Expand Up @@ -32,12 +32,12 @@ Learn more about the API layer in the [`server/`](/guide/directory-structure/ser

Nitro allows 'direct' calling of routes via the globally-available `$fetch` helper. This will make an API call to the server if run on the browser, but will directly call the relevant function if run on the server, **saving an additional API call**.

`$fetch` API is using [ohmyfetch](https://github.com/unjs/ohmyfetch), with key features including:
`$fetch` API is using [ofetch](https://github.com/unjs/ofetch), with key features including:

- Automatic parsing of JSON responses (with access to raw response if needed)
- Request body and params are automatically handled, with correct `Content-Type` headers

For more information on `$fetch` features, check out [ohmyfetch](https://github.com/unjs/ohmyfetch).
For more information on `$fetch` features, check out [ofetch](https://github.com/unjs/ofetch).

## Typed API Routes

Expand Down
4 changes: 2 additions & 2 deletions docs/content/2.guide/2.directory-structure/1.server.md
Expand Up @@ -11,7 +11,7 @@ Nuxt automatically scans files inside the `~/server/api`, `~/server/routes`, and

Each file should export a default function defined with `defineEventHandler()`.

The handler can directly return JSON data, a `Promise` or use `event.res.end()` to send response.
The handler can directly return JSON data, a `Promise` or use `event.node.res.end()` to send response.

::ReadMore{link="https://nitro.unjs.io/guide/introduction/routing" title="Nitro Route Handling Docs"}
::
Expand Down Expand Up @@ -57,7 +57,7 @@ Middleware handlers should not return anything (nor close or respond to the requ

```ts [server/middleware/log.ts]
export default defineEventHandler((event) => {
console.log('New request: ' + event.req.url)
console.log('New request: ' + event.node.req.url)
})
```

Expand Down
6 changes: 3 additions & 3 deletions docs/content/3.api/1.composables/use-fetch.md
Expand Up @@ -41,7 +41,7 @@ type AsyncData<DataT> = {
## Params
* **Url**: The URL to fetch.
* **Options (extends [unjs/ohmyfetch](https://github.com/unjs/ohmyfetch) options & [AsyncDataOptions](/api/composables/use-async-data#params))**:
* **Options (extends [unjs/ofetch](https://github.com/unjs/ofetch) options & [AsyncDataOptions](/api/composables/use-async-data#params))**:
* `method`: Request method.
* `query`: Adds query search params to URL using [ufo](https://github.com/unjs/ufo)
* `params`: Alias for `query`
Expand Down Expand Up @@ -89,7 +89,7 @@ const { data, pending, error, refresh } = await useFetch('https://api.nuxtjs.dev

Adding Query Search Params:

Using the `query` option, you can add search parameters to your query. This option is extended from [unjs/ohmyfetch](https://github.com/unjs/ohmyfetch) and is using [ufo](https://github.com/unjs/ufo) to create the URL. Objects are automatically stringified.
Using the `query` option, you can add search parameters to your query. This option is extended from [unjs/ofetch](https://github.com/unjs/ofetch) and is using [unjs/ufo](https://github.com/unjs/ufo) to create the URL. Objects are automatically stringified.

```ts
const param1 = ref('value1')
Expand All @@ -100,7 +100,7 @@ const { data, pending, error, refresh } = await useFetch('https://api.nuxtjs.dev

Results in `https://api.nuxtjs.dev/mountains?param1=value1&param2=value2`

Using [interceptors](https://github.com/unjs/ohmyfetch#%EF%B8%8F-interceptors):
Using [interceptors](https://github.com/unjs/ofetch#%EF%B8%8F-interceptors):

```ts
const { data, pending, error, refresh } = await useFetch('/api/auth/login', {
Expand Down
2 changes: 1 addition & 1 deletion docs/content/3.api/1.composables/use-request-event.md
Expand Up @@ -12,7 +12,7 @@ Within your pages, components, and plugins you can use `useRequestEvent` to acce
const event = useRequestEvent()

// Get the URL
const url = event.req.url
const url = event.node.req.url
```

::alert{icon=👉}
Expand Down
4 changes: 2 additions & 2 deletions docs/content/3.api/3.utils/$fetch.md
@@ -1,11 +1,11 @@
---
title: "$fetch"
description: Nuxt uses ohmyfetch to expose globally the $fetch helper for making HTTP requests.
description: Nuxt uses ofetch to expose globally the $fetch helper for making HTTP requests.
---

# `$fetch`

Nuxt uses [ohmyfetch](https://github.com/unjs/ohmyfetch) to expose globally the `$fetch` helper for making HTTP requests within your Vue app or API routes.
Nuxt uses [ofetch](https://github.com/unjs/ofetch) to expose globally the `$fetch` helper for making HTTP requests within your Vue app or API routes.

::ReadMore{link="/getting-started/data-fetching"}
::
Expand Down
2 changes: 1 addition & 1 deletion docs/content/7.migration/7.component-options.md
Expand Up @@ -11,7 +11,7 @@ Nuxt 3 provides new options for [fetching data from an API](/getting-started/dat

In Nuxt 2 you might use `@nuxtjs/axios` or `@nuxt/http` to fetch your data - or just the polyfilled global `fetch`.

In Nuxt 3 you can use a globally available `fetch` method that has the same API as [the Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) or `$fetch` method which is using [unjs/ohmyfetch](https://github.com/unjs/ohmyfetch). It has a number of benefits, including:
In Nuxt 3 you can use a globally available `fetch` method that has the same API as [the Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) or `$fetch` method which is using [unjs/ofetch](https://github.com/unjs/ofetch). It has a number of benefits, including:

1. It will handle 'smartly' making [direct API calls](/guide/concepts/server-engine#direct-api-calls) if it's running on the server, or making a client-side call to your API if it's running on the client. (It can also handle calling third-party APIs.)

Expand Down
4 changes: 2 additions & 2 deletions docs/package.json
Expand Up @@ -12,8 +12,8 @@
"@nuxt-themes/website": "0.1.9",
"jiti": "^1.16.0",
"nuxt": "^3.0.0-rc.12",
"pathe": "^0.3.9",
"scule": "^0.3.2",
"pathe": "^1.0.0",
"scule": "^1.0.0",
"untyped": "^0.5.0"
},
"packageManager": "yarn@3.2.4"
Expand Down
@@ -1 +1 @@
export default defineEventHandler(event => `Hello world (${event.req.url.substr(1)}) (Generated at ${new Date().toUTCString()})`)
export default defineEventHandler(event => `Hello world (${event.node.req.url.substr(1)}) (Generated at ${new Date().toUTCString()})`)
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -64,13 +64,13 @@
"markdownlint-cli": "^0.32.2",
"nuxi": "workspace:*",
"nuxt": "workspace:*",
"ohmyfetch": "^0.4.21",
"pathe": "^0.3.9",
"ofetch": "^1.0.0",
"pathe": "^1.0.0",
"rimraf": "^3.0.2",
"semver": "^7.3.8",
"std-env": "^3.3.0",
"typescript": "^4.8.4",
"ufo": "^0.8.6",
"ufo": "^1.0.0",
"unbuild": "^0.9.4",
"vite": "^3.2.3",
"vitest": "^0.25.1",
Expand Down
14 changes: 7 additions & 7 deletions packages/kit/package.json
Expand Up @@ -14,22 +14,22 @@
},
"dependencies": {
"@nuxt/schema": "3.0.0-rc.13",
"c12": "^0.2.13",
"c12": "^1.0.1",
"consola": "^2.15.3",
"defu": "^6.1.0",
"globby": "^13.1.2",
"hash-sum": "^2.0.0",
"ignore": "^5.2.0",
"jiti": "^1.16.0",
"knitwork": "^0.1.2",
"knitwork": "^1.0.0",
"lodash.template": "^4.5.0",
"mlly": "^0.5.16",
"pathe": "^0.3.9",
"pkg-types": "^0.3.6",
"scule": "^0.3.2",
"mlly": "^1.0.0",
"pathe": "^1.0.0",
"pkg-types": "^1.0.1",
"scule": "^1.0.0",
"semver": "^7.3.8",
"unctx": "^2.0.2",
"unimport": "^0.7.0",
"unimport": "^1.0.0",
"untyped": "^0.5.0"
},
"devDependencies": {
Expand Down
12 changes: 6 additions & 6 deletions packages/nuxi/package.json
Expand Up @@ -24,7 +24,7 @@
"@types/flat": "^5.0.2",
"@types/mri": "^1.1.1",
"@types/semver": "^7",
"c12": "^0.2.13",
"c12": "^1.0.1",
"chokidar": "^3.5.3",
"clear": "^0.1.0",
"clipboardy": "^3.0.0",
Expand All @@ -35,15 +35,15 @@
"execa": "^6.1.0",
"flat": "^5.0.2",
"giget": "^0.1.7",
"h3": "^0.8.6",
"h3": "^1.0.1",
"jiti": "^1.16.0",
"listhen": "^0.3.5",
"mlly": "^0.5.16",
"mlly": "^1.0.0",
"mri": "^1.2.0",
"pathe": "^0.3.9",
"pathe": "^1.0.0",
"perfect-debounce": "^0.1.3",
"pkg-types": "^0.3.6",
"scule": "^0.3.2",
"pkg-types": "^1.0.1",
"scule": "^1.0.0",
"semver": "^7.3.8",
"unbuild": "latest"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxi/src/commands/analyze.ts
Expand Up @@ -39,7 +39,7 @@ export default defineNuxtCommand({

const serveFile = (filePath: string) => lazyEventHandler(async () => {
const contents = await fsp.readFile(filePath, 'utf-8')
return eventHandler((event) => { event.res.end(contents) })
return eventHandler((event) => { event.node.res.end(contents) })
})

console.warn('Do not deploy analyze results! Use `nuxi build` before deploying.')
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/build.config.ts
Expand Up @@ -17,7 +17,7 @@ export default defineBuildConfig({
dependencies: [
'nuxi',
'vue-router',
'ohmyfetch'
'ofetch'
],
externals: [
'@vue/reactivity',
Expand Down
22 changes: 11 additions & 11 deletions packages/nuxt/package.json
Expand Up @@ -53,25 +53,25 @@
"estree-walker": "^3.0.1",
"fs-extra": "^10.1.0",
"globby": "^13.1.2",
"h3": "^0.8.6",
"h3": "^1.0.1",
"hash-sum": "^2.0.0",
"hookable": "^5.4.1",
"knitwork": "^0.1.2",
"knitwork": "^1.0.0",
"magic-string": "^0.26.7",
"mlly": "^0.5.16",
"nitropack": "^0.6.1",
"mlly": "^1.0.0",
"nitropack": "^1.0.0-0",
"nuxi": "3.0.0-rc.13",
"ohash": "^0.1.5",
"ohmyfetch": "^0.4.21",
"pathe": "^0.3.9",
"ohash": "^1.0.0",
"ofetch": "^1.0.0",
"pathe": "^1.0.0",
"perfect-debounce": "^0.1.3",
"scule": "^0.3.2",
"strip-literal": "^0.4.2",
"ufo": "^0.8.6",
"scule": "^1.0.0",
"strip-literal": "^1.0.0",
"ufo": "^1.0.0",
"ultrahtml": "^1.0.0",
"unctx": "^2.0.2",
"unenv": "^0.6.2",
"unimport": "^0.7.0",
"unimport": "^1.0.0",
"unplugin": "^0.10.2",
"untyped": "^0.5.0",
"vue": "^3.2.45",
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/app/composables/fetch.ts
@@ -1,4 +1,4 @@
import type { FetchError, FetchOptions } from 'ohmyfetch'
import type { FetchError, FetchOptions } from 'ofetch'
import type { TypedInternalResponse, NitroFetchRequest } from 'nitropack'
import { computed, unref, Ref, reactive } from 'vue'
import type { AsyncDataOptions, _Transform, KeyOfRes, AsyncData, PickFrom } from './asyncData'
Expand Down
6 changes: 3 additions & 3 deletions packages/nuxt/src/app/composables/ssr.ts
Expand Up @@ -6,7 +6,7 @@ export function useRequestHeaders<K extends string = string> (include: K[]): Rec
export function useRequestHeaders (): Readonly<Record<string, string | undefined>>
export function useRequestHeaders (include?: any[]) {
if (process.client) { return {} }
const headers = useNuxtApp().ssrContext?.event.req.headers ?? {}
const headers = useNuxtApp().ssrContext?.event.node.req.headers ?? {}
if (!include) { return headers }
return Object.fromEntries(include.map(key => key.toLowerCase()).filter(key => headers[key]).map(key => [key, headers[key]]))
}
Expand All @@ -18,9 +18,9 @@ export function useRequestEvent (nuxtApp: NuxtApp = useNuxtApp()): H3Event {
export function setResponseStatus (code: number, message?: string) {
const event = process.server && useRequestEvent()
if (event) {
event.res.statusCode = code
event.node.res.statusCode = code
if (message) {
event.res.statusMessage = message
event.node.res.statusMessage = message
}
}
}
2 changes: 1 addition & 1 deletion packages/nuxt/src/app/entry.ts
@@ -1,6 +1,6 @@
// We set __webpack_public_path via this import with webpack builder
import { createSSRApp, createApp, nextTick } from 'vue'
import { $fetch } from 'ohmyfetch'
import { $fetch } from 'ofetch'
// @ts-ignore
import { baseURL } from '#build/paths.mjs'
import { createNuxtApp, applyPlugins, normalizePlugins, CreateOptions } from '#app'
Expand Down
22 changes: 11 additions & 11 deletions packages/nuxt/src/core/runtime/nitro/error.ts
Expand Up @@ -10,7 +10,7 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,

// Create an error object
const errorObject = {
url: event.req.url,
url: event.node.req.url,
statusCode,
statusMessage,
message,
Expand All @@ -21,9 +21,9 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
}

// Set response code and message
event.res.statusCode = (errorObject.statusCode !== 200 && errorObject.statusCode) as any as number || 500
event.node.res.statusCode = (errorObject.statusCode !== 200 && errorObject.statusCode) as any as number || 500
if (errorObject.statusMessage) {
event.res.statusMessage = errorObject.statusMessage
event.node.res.statusMessage = errorObject.statusMessage
}
// Console output
if (error.unhandled || error.fatal) {
Expand All @@ -39,13 +39,13 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,

// JSON response
if (isJsonRequest(event)) {
event.res.setHeader('Content-Type', 'application/json')
event.res.end(JSON.stringify(errorObject))
event.node.res.setHeader('Content-Type', 'application/json')
event.node.res.end(JSON.stringify(errorObject))
return
}

// HTML response (via SSR)
const isErrorPage = event.req.url?.startsWith('/__nuxt_error')
const isErrorPage = event.node.req.url?.startsWith('/__nuxt_error')
const res = !isErrorPage
? await useNitroApp().localFetch(withQuery(joinURL(useRuntimeConfig().app.baseURL, '/__nuxt_error'), errorObject), {
headers: getRequestHeaders(event) as Record<string, string>,
Expand All @@ -64,8 +64,8 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
// TODO: Support `message` in template
(errorObject as any).description = errorObject.message
}
event.res.setHeader('Content-Type', 'text/html;charset=UTF-8')
event.res.end(template(errorObject))
event.node.res.setHeader('Content-Type', 'text/html;charset=UTF-8')
event.node.res.end(template(errorObject))
return
}

Expand All @@ -74,12 +74,12 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
}

if (res.status && res.status !== 200) {
event.res.statusCode = res.status
event.node.res.statusCode = res.status
}

if (res.statusText) {
event.res.statusMessage = res.statusText
event.node.res.statusMessage = res.statusText
}

event.res.end(await res.text())
event.node.res.end(await res.text())
}

0 comments on commit 9eb3325

Please sign in to comment.