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:
Matthew Phillips 2023-08-17 09:28:42 -04:00 committed by GitHub
parent 4843bff0d2
commit ea7ff5177d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 55 additions and 20 deletions

View 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.

View file

@ -1,4 +1,4 @@
import { defineMiddleware, sequence } from 'astro/middleware';
import { defineMiddleware, sequence } from 'astro:middleware';
import htmlMinifier from 'html-minifier';
const limit = 50;

View file

@ -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'];

View file

@ -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": {

View file

@ -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!";

View file

@ -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

View file

@ -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();

View file

@ -0,0 +1,4 @@
export {
defineMiddleware,
sequence,
} from './index.js';

View file

@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Code component</title></head>

View file

@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Code component</title></head>

View file

@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Code component</title></head>

View file

@ -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'

View file

@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Code component</title></head>

View file

@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Code component</title></head>

View file

@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Code component</title></head>

View file

@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Code component</title></head>

View file

@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Code component</title></head>

View file

@ -1,5 +1,5 @@
---
import { Code } from 'astro/components';
import { Code } from 'astro:components';
---
<html>
<head>

View file

@ -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')) {

View file

@ -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')) {

View file

@ -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/') {

View file

@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Testing</title></head>

View file

@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Testing</title></head>