0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00

[ci] format

This commit is contained in:
matthewp 2022-09-14 19:49:26 +00:00 committed by fredkbot
parent 005d5bacd9
commit a74f246333
4 changed files with 20 additions and 21 deletions

View file

@ -202,10 +202,10 @@ export class App {
}); });
if (result.type === 'response') { if (result.type === 'response') {
if(result.response.headers.get('X-Astro-Response') === 'Not-Found') { if (result.response.headers.get('X-Astro-Response') === 'Not-Found') {
const fourOhFourRequest = new Request(new URL('/404', request.url)); const fourOhFourRequest = new Request(new URL('/404', request.url));
const fourOhFourRouteData = this.match(fourOhFourRequest); const fourOhFourRouteData = this.match(fourOhFourRequest);
if(fourOhFourRouteData) { if (fourOhFourRouteData) {
return this.render(fourOhFourRequest, fourOhFourRouteData); return this.render(fourOhFourRequest, fourOhFourRouteData);
} }
} }

View file

@ -18,11 +18,7 @@ function getHandlerFromModule(mod: EndpointHandler, method: string) {
} }
/** Renders an endpoint request to completion, returning the body. */ /** Renders an endpoint request to completion, returning the body. */
export async function renderEndpoint( export async function renderEndpoint(mod: EndpointHandler, request: Request, params: Params) {
mod: EndpointHandler,
request: Request,
params: Params
) {
const chosenMethod = request.method?.toLowerCase(); const chosenMethod = request.method?.toLowerCase();
const handler = getHandlerFromModule(mod, chosenMethod); const handler = getHandlerFromModule(mod, chosenMethod);
if (!handler || typeof handler !== 'function') { if (!handler || typeof handler !== 'function') {
@ -32,7 +28,7 @@ export async function renderEndpoint(
let response = new Response(null, { let response = new Response(null, {
status: 404, status: 404,
headers: { headers: {
'X-Astro-Response': 'Not-Found' 'X-Astro-Response': 'Not-Found',
}, },
}); });
return response; return response;

View file

@ -1,7 +1,7 @@
import type http from 'http'; import type http from 'http';
import mime from 'mime'; import mime from 'mime';
import type * as vite from 'vite'; import type * as vite from 'vite';
import type { AstroConfig, ManifestData, SSRManifest } from '../@types/astro'; import type { AstroConfig, ManifestData } from '../@types/astro';
import type { SSROptions } from '../core/render/dev/index'; import type { SSROptions } from '../core/render/dev/index';
import { Readable } from 'stream'; import { Readable } from 'stream';
@ -28,8 +28,11 @@ interface AstroPluginOptions {
logging: LogOptions; logging: LogOptions;
} }
type AsyncReturnType<T extends (...args: any) => Promise<any>> = type AsyncReturnType<T extends (...args: any) => Promise<any>> = T extends (
T extends (...args: any) => Promise<infer R> ? R : any ...args: any
) => Promise<infer R>
? R
: any;
function writeHtmlResponse(res: http.ServerResponse, statusCode: number, html: string) { function writeHtmlResponse(res: http.ServerResponse, statusCode: number, html: string) {
res.writeHead(statusCode, { res.writeHead(statusCode, {
@ -181,7 +184,7 @@ async function matchRoute(
viteServer: vite.ViteDevServer, viteServer: vite.ViteDevServer,
logging: LogOptions, logging: LogOptions,
manifest: ManifestData, manifest: ManifestData,
config: AstroConfig, config: AstroConfig
) { ) {
const matches = matchAllRoutes(pathname, manifest); const matches = matchAllRoutes(pathname, manifest);
@ -273,7 +276,7 @@ async function handleRequest(
if (!(req.method === 'GET' || req.method === 'HEAD')) { if (!(req.method === 'GET' || req.method === 'HEAD')) {
let bytes: Uint8Array[] = []; let bytes: Uint8Array[] = [];
await new Promise((resolve) => { await new Promise((resolve) => {
req.on('data', part => { req.on('data', (part) => {
bytes.push(part); bytes.push(part);
}); });
req.on('end', resolve); req.on('end', resolve);
@ -307,7 +310,7 @@ async function handleRequest(
req, req,
res res
); );
} catch(_err) { } catch (_err) {
const err = fixViteErrorMessage(_err, viteServer, filePath); const err = fixViteErrorMessage(_err, viteServer, filePath);
const errorWithMetadata = collectErrorMetadata(err); const errorWithMetadata = collectErrorMetadata(err);
error(logging, null, msg.formatErrorMessage(errorWithMetadata)); error(logging, null, msg.formatErrorMessage(errorWithMetadata));
@ -376,7 +379,7 @@ async function handleRoute(
if (route.type === 'endpoint') { if (route.type === 'endpoint') {
const result = await callEndpoint(options); const result = await callEndpoint(options);
if (result.type === 'response') { if (result.type === 'response') {
if(result.response.headers.get('X-Astro-Response') === 'Not-Found') { if (result.response.headers.get('X-Astro-Response') === 'Not-Found') {
const fourOhFourRoute = await matchRoute( const fourOhFourRoute = await matchRoute(
'/404', '/404',
routeCache, routeCache,

View file

@ -28,7 +28,7 @@ describe('404 and 500 pages', () => {
it('Returns 404 when hitting an API route with the wrong method', async () => { it('Returns 404 when hitting an API route with the wrong method', async () => {
let res = await fixture.fetch('/api/route', { let res = await fixture.fetch('/api/route', {
method: 'PUT' method: 'PUT',
}); });
let html = await res.text(); let html = await res.text();
let $ = cheerio.load(html); let $ = cheerio.load(html);
@ -75,7 +75,7 @@ describe('404 and 500 pages', () => {
it('Returns 404 when hitting an API route with the wrong method', async () => { it('Returns 404 when hitting an API route with the wrong method', async () => {
const app = await fixture.loadTestAdapterApp(); const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/api/route', { const request = new Request('http://example.com/api/route', {
method: 'PUT' method: 'PUT',
}); });
const response = await app.render(request); const response = await app.render(request);
expect(response.status).to.equal(404); expect(response.status).to.equal(404);