astro:
namespace for middleware and components (#8101)
* `astro:` namespace for middleware and components * Update errors to use namespace * Create a namespace module just for the astro: stuff
This commit is contained in:
parent
4843bff0d2
commit
ea7ff5177d
23 changed files with 55 additions and 20 deletions
8
.changeset/neat-mugs-end.md
Normal file
8
.changeset/neat-mugs-end.md
Normal file
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
'astro': minor
|
||||
---
|
||||
|
||||
|
||||
`astro:`namespace aliases for middleware and components
|
||||
|
||||
This adds aliases of `astro:middleware` and `astro:components` for the middleware and components modules. This is to make our documentation consistent between are various modules, where some are virtual modules and others are not. Going forward new built-in modules will use this namespace.
|
|
@ -1,4 +1,4 @@
|
|||
import { defineMiddleware, sequence } from 'astro/middleware';
|
||||
import { defineMiddleware, sequence } from 'astro:middleware';
|
||||
import htmlMinifier from 'html-minifier';
|
||||
|
||||
const limit = 50;
|
||||
|
|
8
packages/astro/client.d.ts
vendored
8
packages/astro/client.d.ts
vendored
|
@ -119,6 +119,14 @@ declare module 'astro:transitions' {
|
|||
export const ViewTransitions: ViewTransitionsModule['default'];
|
||||
}
|
||||
|
||||
declare module 'astro:middleware' {
|
||||
export * from 'astro/middleware/namespace';
|
||||
}
|
||||
|
||||
declare module 'astro:components' {
|
||||
export * from 'astro/components';
|
||||
}
|
||||
|
||||
type MD = import('./dist/@types/astro').MarkdownInstance<Record<string, any>>;
|
||||
interface ExportedMarkdownModuleEntities {
|
||||
frontmatter: MD['frontmatter'];
|
||||
|
|
|
@ -23,6 +23,9 @@
|
|||
],
|
||||
"middleware": [
|
||||
"./dist/core/middleware/index.d.ts"
|
||||
],
|
||||
"middleware/namespace": [
|
||||
"./dist/core/middleware/namespace.d.ts"
|
||||
]
|
||||
}
|
||||
},
|
||||
|
@ -70,6 +73,10 @@
|
|||
"types": "./dist/core/middleware/index.d.ts",
|
||||
"default": "./dist/core/middleware/index.js"
|
||||
},
|
||||
"./middleware/namespace": {
|
||||
"types": "./dist/core/middleware/namespace.d.ts",
|
||||
"default": "./dist/core/middleware/namespace.js"
|
||||
},
|
||||
"./transitions": "./dist/transitions/index.js"
|
||||
},
|
||||
"imports": {
|
||||
|
|
|
@ -1920,7 +1920,7 @@ export interface APIContext<Props extends Record<string, any> = Record<string, a
|
|||
*
|
||||
* ```ts
|
||||
* // src/middleware.ts
|
||||
* import {defineMiddleware} from "astro/middleware";
|
||||
* import {defineMiddleware} from "astro:middleware";
|
||||
*
|
||||
* export const onRequest = defineMiddleware((context, next) => {
|
||||
* context.locals.greeting = "Hello!";
|
||||
|
|
|
@ -170,6 +170,14 @@ export async function createVite(
|
|||
find: /^astro$/,
|
||||
replacement: fileURLToPath(new URL('../@types/astro', import.meta.url)),
|
||||
},
|
||||
{
|
||||
find: 'astro:middleware',
|
||||
replacement: 'astro/middleware/namespace',
|
||||
},
|
||||
{
|
||||
find: 'astro:components',
|
||||
replacement: 'astro/components',
|
||||
},
|
||||
],
|
||||
conditions: ['astro'],
|
||||
// Astro imports in third-party packages should use the same version as root
|
||||
|
|
|
@ -658,7 +658,7 @@ export const ResponseSentError = {
|
|||
*
|
||||
* For example:
|
||||
* ```ts
|
||||
* import {defineMiddleware} from "astro/middleware";
|
||||
* import {defineMiddleware} from "astro:middleware";
|
||||
* export const onRequest = defineMiddleware((context, _) => {
|
||||
* // doesn't return anything or call `next`
|
||||
* context.locals.someData = false;
|
||||
|
@ -678,7 +678,7 @@ export const MiddlewareNoDataOrNextCalled = {
|
|||
*
|
||||
* For example:
|
||||
* ```ts
|
||||
* import {defineMiddleware} from "astro/middleware";
|
||||
* import {defineMiddleware} from "astro:middleware";
|
||||
* export const onRequest = defineMiddleware(() => {
|
||||
* return "string"
|
||||
* });
|
||||
|
@ -698,7 +698,7 @@ export const MiddlewareNotAResponse = {
|
|||
*
|
||||
* For example:
|
||||
* ```ts
|
||||
* import {defineMiddleware} from "astro/middleware";
|
||||
* import {defineMiddleware} from "astro:middleware";
|
||||
* export const onRequest = defineMiddleware((context, next) => {
|
||||
* context.locals = 1541;
|
||||
* return next();
|
||||
|
|
4
packages/astro/src/core/middleware/namespace.ts
Normal file
4
packages/astro/src/core/middleware/namespace.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
export {
|
||||
defineMiddleware,
|
||||
sequence,
|
||||
} from './index.js';
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
import {Code} from 'astro/components';
|
||||
import {Code} from 'astro:components';
|
||||
---
|
||||
<html>
|
||||
<head><title>Code component</title></head>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
import {Code} from 'astro/components';
|
||||
import {Code} from 'astro:components';
|
||||
---
|
||||
<html>
|
||||
<head><title>Code component</title></head>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
import {Code} from 'astro/components';
|
||||
import {Code} from 'astro:components';
|
||||
---
|
||||
<html>
|
||||
<head><title>Code component</title></head>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
import {Code} from 'astro/components';
|
||||
import {Code} from 'astro:components';
|
||||
import riGrammar from '../assets/ri.tmLanguage.json'
|
||||
import serendipity from '../assets/serendipity-morning.json'
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
import {Code} from 'astro/components';
|
||||
import {Code} from 'astro:components';
|
||||
---
|
||||
<html>
|
||||
<head><title>Code component</title></head>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
import {Code} from 'astro/components';
|
||||
import {Code} from 'astro:components';
|
||||
---
|
||||
<html>
|
||||
<head><title>Code component</title></head>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
import {Code} from 'astro/components';
|
||||
import {Code} from 'astro:components';
|
||||
---
|
||||
<html>
|
||||
<head><title>Code component</title></head>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
import {Code} from 'astro/components';
|
||||
import {Code} from 'astro:components';
|
||||
---
|
||||
<html>
|
||||
<head><title>Code component</title></head>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
import {Code} from 'astro/components';
|
||||
import {Code} from 'astro:components';
|
||||
---
|
||||
<html>
|
||||
<head><title>Code component</title></head>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
import { Code } from 'astro/components';
|
||||
import { Code } from 'astro:components';
|
||||
---
|
||||
<html>
|
||||
<head>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { sequence, defineMiddleware } from 'astro/middleware';
|
||||
import { sequence, defineMiddleware } from 'astro:middleware';
|
||||
|
||||
const first = defineMiddleware(async (context, next) => {
|
||||
if (context.request.url.includes('/lorem')) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { sequence, defineMiddleware } from 'astro/middleware';
|
||||
import { sequence, defineMiddleware } from 'astro:middleware';
|
||||
|
||||
const first = defineMiddleware(async (context, next) => {
|
||||
if (context.request.url.includes('/second')) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { defineMiddleware } from 'astro/middleware';
|
||||
import { defineMiddleware } from 'astro:middleware';
|
||||
|
||||
export const onRequest = defineMiddleware(({ request }, next) => {
|
||||
if(new URL(request.url).pathname === '/middleware-redirect/') {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
import {Code} from 'astro/components';
|
||||
import {Code} from 'astro:components';
|
||||
---
|
||||
<html>
|
||||
<head><title>Testing</title></head>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
import {Code} from 'astro/components';
|
||||
import {Code} from 'astro:components';
|
||||
---
|
||||
<html>
|
||||
<head><title>Testing</title></head>
|
||||
|
|
Loading…
Reference in a new issue