Skip to content

Commit

Permalink
delete callEndpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy committed Jan 31, 2024
1 parent a6fd6b5 commit 7f7be82
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 52 deletions.
32 changes: 1 addition & 31 deletions packages/astro/src/core/endpoint/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import type {
APIContext,
EndpointHandler,
Locales,
MiddlewareHandler,
Params,
} from '../../@types/astro.js';
import { renderEndpoint } from '../../runtime/server/index.js';
import { ASTRO_VERSION } from '../constants.js';
import { AstroCookies, attachCookiesToResponse } from '../cookies/index.js';
import { AstroCookies } from '../cookies/index.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import { callMiddleware } from '../middleware/callMiddleware.js';
import {
computeCurrentLocale,
computePreferredLocale,
computePreferredLocaleList,
} from '../render/context.js';
import { type Environment, type RenderContext } from '../render/index.js';
import type { RoutingStrategies } from '../config/schema.js';

const clientAddressSymbol = Symbol.for('astro.clientAddress');
Expand Down Expand Up @@ -138,28 +133,3 @@ export function createAPIContext({

return context;
}

export async function callEndpoint(
mod: EndpointHandler,
env: Environment,
ctx: RenderContext,
onRequest: MiddlewareHandler
): Promise<Response> {
const context = createAPIContext({
request: ctx.request,
params: ctx.params,
props: ctx.props,
site: env.site,
adapterName: env.adapterName,
routingStrategy: ctx.routing,
defaultLocale: ctx.defaultLocale,
locales: ctx.locales,
});

const response = await callMiddleware(onRequest, context, async () => {
return await renderEndpoint(mod, context, env.serverLike, env.logger);
});

attachCookiesToResponse(response, context.cookies);
return response;
}
32 changes: 11 additions & 21 deletions packages/astro/src/core/pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ComponentInstance, EndpointHandler, MiddlewareHandler } from '../@types/astro.js';
import { callEndpoint, createAPIContext } from './endpoint/index.js';
import type { ComponentInstance, EndpointHandler } from '../@types/astro.js';
import { renderEndpoint } from '../runtime/server/endpoint.js';
import { attachCookiesToResponse } from './cookies/response.js';
import { createAPIContext } from './endpoint/index.js';
import { callMiddleware } from './middleware/callMiddleware.js';
import { renderPage } from './render/core.js';
import { type Environment, type RenderContext } from './render/index.js';
Expand Down Expand Up @@ -36,25 +38,13 @@ export class Pipeline {
Reflect.set(this.renderContext.request, Symbol.for('astro.routeData'), this.renderContext.route)
const { renderContext, environment } = this;
const { defaultLocale, locales, params, props, request, routing: routingStrategy } = renderContext;
const { adapterName, site } = environment;
const { adapterName, logger, site, serverLike } = environment;
const apiContext = createAPIContext({ adapterName, defaultLocale, locales, params, props, request, routingStrategy, site });

switch (renderContext.route.type) {
case 'page':
case 'fallback':
case 'redirect': {
return await callMiddleware(this.middleware, apiContext, () => renderPage({
mod: componentInstance,
renderContext,
env: environment,
cookies: apiContext.cookies,
}))
}
case 'endpoint': {
return await callEndpoint(componentInstance as any as EndpointHandler, environment, renderContext, this.middleware);
}
default:
throw new Error(`Couldn't find route of type [${renderContext.route.type}]`);
}
const terminalNext = renderContext.route.type === 'endpoint'
? () => renderEndpoint(componentInstance as any as EndpointHandler, apiContext, serverLike, logger)
: () => renderPage({ mod: componentInstance, renderContext, env: environment, cookies: apiContext.cookies });
const response = await callMiddleware(this.middleware, apiContext, terminalNext);
attachCookiesToResponse(response, apiContext.cookies);
return response;
}
}

0 comments on commit 7f7be82

Please sign in to comment.