0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-20 22:12:38 -05:00

chore: delete core/endpoint/index.ts (#10324)

This commit is contained in:
Arsh 2024-03-05 12:44:13 +05:30 committed by GitHub
parent d1700cf731
commit 3c0b854ac6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,132 +0,0 @@
import type { APIContext, Locales, Params } from '../../@types/astro.js';
import {
type RoutingStrategies,
computeCurrentLocale,
computePreferredLocale,
computePreferredLocaleList,
} from '../../i18n/utils.js';
import { ASTRO_VERSION, clientAddressSymbol, clientLocalsSymbol } from '../constants.js';
import type { AstroCookies } from '../cookies/index.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
type CreateAPIContext = {
request: Request;
params: Params;
site?: string;
props: Record<string, any>;
adapterName?: string;
locales: Locales | undefined;
routingStrategy: RoutingStrategies | undefined;
defaultLocale: string | undefined;
route: string;
cookies: AstroCookies;
};
/**
* Creates a context that holds all the information needed to handle an Astro endpoint.
*
* @param {CreateAPIContext} payload
*/
export function createAPIContext({
request,
params,
site,
props,
adapterName,
locales,
routingStrategy,
defaultLocale,
route,
cookies,
}: CreateAPIContext): APIContext {
let preferredLocale: string | undefined = undefined;
let preferredLocaleList: string[] | undefined = undefined;
let currentLocale: string | undefined = undefined;
const context = {
cookies,
request,
params,
site: site ? new URL(site) : undefined,
generator: `Astro v${ASTRO_VERSION}`,
props,
redirect(path, status) {
return new Response(null, {
status: status || 302,
headers: {
Location: path,
},
});
},
get preferredLocale(): string | undefined {
if (preferredLocale) {
return preferredLocale;
}
if (locales) {
preferredLocale = computePreferredLocale(request, locales);
return preferredLocale;
}
return undefined;
},
get preferredLocaleList(): string[] | undefined {
if (preferredLocaleList) {
return preferredLocaleList;
}
if (locales) {
preferredLocaleList = computePreferredLocaleList(request, locales);
return preferredLocaleList;
}
return undefined;
},
get currentLocale(): string | undefined {
if (currentLocale) {
return currentLocale;
}
if (locales) {
currentLocale = computeCurrentLocale(route, locales, routingStrategy, defaultLocale);
}
return currentLocale;
},
url: new URL(request.url),
get clientAddress() {
if (clientAddressSymbol in request) {
return Reflect.get(request, clientAddressSymbol) as string;
}
if (adapterName) {
throw new AstroError({
...AstroErrorData.ClientAddressNotAvailable,
message: AstroErrorData.ClientAddressNotAvailable.message(adapterName),
});
} else {
throw new AstroError(AstroErrorData.StaticClientAddressNotAvailable);
}
},
get locals() {
let locals = Reflect.get(request, clientLocalsSymbol);
if (locals === undefined) {
locals = {};
Reflect.set(request, clientLocalsSymbol, locals);
}
if (typeof locals !== 'object') {
throw new AstroError(AstroErrorData.LocalsNotAnObject);
}
return locals;
},
// We define a custom property, so we can check the value passed to locals
set locals(val) {
if (typeof val !== 'object') {
throw new AstroError(AstroErrorData.LocalsNotAnObject);
} else {
Reflect.set(request, clientLocalsSymbol, val);
}
},
} satisfies APIContext;
return context;
}