Skip to content

Commit

Permalink
astro: namespace for middleware and components (#8101)
Browse files Browse the repository at this point in the history
* `astro:` namespace for middleware and components

* Update errors to use namespace

* Create a namespace module just for the astro: stuff
  • Loading branch information
matthewp committed Aug 17, 2023
1 parent 4843bff commit ea7ff51
Show file tree
Hide file tree
Showing 23 changed files with 55 additions and 20 deletions.
8 changes: 8 additions & 0 deletions .changeset/neat-mugs-end.md
@@ -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.
2 changes: 1 addition & 1 deletion examples/middleware/src/middleware.ts
@@ -1,4 +1,4 @@
import { defineMiddleware, sequence } from 'astro/middleware';
import { defineMiddleware, sequence } from 'astro:middleware';
import htmlMinifier from 'html-minifier';

const limit = 50;
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/client.d.ts
Expand Up @@ -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'];
Expand Down
7 changes: 7 additions & 0 deletions packages/astro/package.json
Expand Up @@ -23,6 +23,9 @@
],
"middleware": [
"./dist/core/middleware/index.d.ts"
],
"middleware/namespace": [
"./dist/core/middleware/namespace.d.ts"
]
}
},
Expand Down Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/@types/astro.ts
Expand Up @@ -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!";
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/src/core/create-vite.ts
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/src/core/errors/errors-data.ts
Expand Up @@ -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;
Expand All @@ -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"
* });
Expand All @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions packages/astro/src/core/middleware/namespace.ts
@@ -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>
Expand Down
@@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Code component</title></head>
Expand Down
@@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Code component</title></head>
Expand Down
@@ -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'
Expand Down
@@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Code component</title></head>
Expand Down
@@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Code component</title></head>
Expand Down
@@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Code component</title></head>
Expand Down
@@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Code component</title></head>
Expand Down
@@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Code component</title></head>
Expand Down
@@ -1,5 +1,5 @@
---
import { Code } from 'astro/components';
import { Code } from 'astro:components';
---
<html>
<head>
Expand Down
@@ -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')) {
Expand Down
@@ -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')) {
Expand Down
@@ -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/') {
Expand Down
@@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Testing</title></head>
Expand Down
@@ -1,5 +1,5 @@
---
import {Code} from 'astro/components';
import {Code} from 'astro:components';
---
<html>
<head><title>Testing</title></head>
Expand Down

0 comments on commit ea7ff51

Please sign in to comment.