[ci] format
This commit is contained in:
parent
4c25a1c2ea
commit
279774c48e
17 changed files with 84 additions and 101 deletions
|
@ -5,13 +5,13 @@ export default defineConfig({
|
|||
vite: {
|
||||
server: {
|
||||
cors: {
|
||||
credentials: true
|
||||
credentials: true,
|
||||
},
|
||||
proxy: {
|
||||
'/api': {
|
||||
target: 'http://127.0.0.1:8085',
|
||||
changeOrigin: true,
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
|
@ -41,7 +41,7 @@ const routes = [
|
|||
match: /\/api\/cart/,
|
||||
async handle(req, res) {
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'application/json'
|
||||
'Content-Type': 'application/json',
|
||||
});
|
||||
let cookie = req.headers.cookie;
|
||||
let userId = cookie ? lightcookie.parse(cookie)['user-id'] : '1'; // default for testing
|
||||
|
@ -52,14 +52,14 @@ const routes = [
|
|||
let items = userCartItems.get(userId);
|
||||
let array = Array.from(items.values());
|
||||
res.end(JSON.stringify({ items: array }));
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
match: /\/api\/add-to-cart/,
|
||||
async handle(req, res) {
|
||||
let body = '';
|
||||
req.on('data', chunk => body += chunk);
|
||||
return new Promise(resolve => {
|
||||
req.on('data', (chunk) => (body += chunk));
|
||||
return new Promise((resolve) => {
|
||||
req.on('end', () => {
|
||||
let cookie = req.headers.cookie;
|
||||
let userId = lightcookie.parse(cookie)['user-id'];
|
||||
|
@ -82,8 +82,8 @@ const routes = [
|
|||
res.end(JSON.stringify({ ok: true }));
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
export async function apiHandler(req, res) {
|
||||
|
|
|
@ -22,7 +22,7 @@ const origin = MODE === 'development' ? `http://127.0.0.1:3000` : `http://127.0.
|
|||
|
||||
async function get<T>(endpoint: string, cb: (response: Response) => Promise<T>): Promise<T> {
|
||||
const response = await fetch(`${origin}${endpoint}`, {
|
||||
credentials: 'same-origin'
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
if (!response.ok) {
|
||||
// TODO make this better...
|
||||
|
@ -46,14 +46,14 @@ export async function getProduct(id: number): Promise<Product> {
|
|||
}
|
||||
|
||||
export async function getUser(): Promise<User> {
|
||||
return get<User>(`/api/user`, async response => {
|
||||
return get<User>(`/api/user`, async (response) => {
|
||||
const user: User = await response.json();
|
||||
return user;
|
||||
});
|
||||
}
|
||||
|
||||
export async function getCart(): Promise<Cart> {
|
||||
return get<Cart>(`/api/cart`, async response => {
|
||||
return get<Cart>(`/api/cart`, async (response) => {
|
||||
const cart: Cart = await response.json();
|
||||
return cart;
|
||||
});
|
||||
|
@ -66,11 +66,11 @@ export async function addToUserCart(id: number | string, name: string): Promise<
|
|||
mode: 'no-cors',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache': 'no-cache'
|
||||
Cache: 'no-cache',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
id,
|
||||
name
|
||||
})
|
||||
name,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import lightcookie from 'lightcookie';
|
||||
|
||||
|
||||
export function isLoggedIn(request: Request): boolean {
|
||||
const cookie = request.headers.get('cookie');
|
||||
const parsed = lightcookie.parse(cookie);
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
|
||||
export function post(params, request) {
|
||||
return new Response(null, {
|
||||
status: 301,
|
||||
headers: {
|
||||
'Location': '/',
|
||||
'Set-Cookie': 'user-id=1; Path=/; Max-Age=2592000'
|
||||
}
|
||||
Location: '/',
|
||||
'Set-Cookie': 'user-id=1; Path=/; Max-Age=2592000',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ export class App {
|
|||
if (!routeData) {
|
||||
return new Response(null, {
|
||||
status: 404,
|
||||
statusText: 'Not found'
|
||||
statusText: 'Not found',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ export class App {
|
|||
|
||||
let html = result.html;
|
||||
return new Response(html, {
|
||||
status: 200
|
||||
status: 200,
|
||||
});
|
||||
}
|
||||
async #loadRenderers(): Promise<Renderer[]> {
|
||||
|
|
|
@ -11,7 +11,7 @@ function createRequestFromNodeRequest(req: IncomingMessage): Request {
|
|||
const entries = Object.entries(req.headers as Record<string, any>);
|
||||
let request = new Request(url, {
|
||||
method: req.method || 'GET',
|
||||
headers: new Headers(entries)
|
||||
headers: new Headers(entries),
|
||||
});
|
||||
return request;
|
||||
}
|
||||
|
|
|
@ -402,16 +402,15 @@ async function generatePath(pathname: string, opts: StaticBuildOptions, gopts: G
|
|||
route: pageData.route,
|
||||
routeCache,
|
||||
site: astroConfig.buildOptions.site,
|
||||
ssr: opts.astroConfig.buildOptions.experimentalSsr
|
||||
}
|
||||
ssr: opts.astroConfig.buildOptions.experimentalSsr,
|
||||
};
|
||||
|
||||
let body: string;
|
||||
if (pageData.route.type === 'endpoint') {
|
||||
|
||||
const result = await callEndpoint(mod as unknown as EndpointHandler, options);
|
||||
|
||||
if (result.type === 'response') {
|
||||
throw new Error(`Returning a Response from an endpoint is not supported in SSG mode.`)
|
||||
throw new Error(`Returning a Response from an endpoint is not supported in SSG mode.`);
|
||||
}
|
||||
body = result.body;
|
||||
} else {
|
||||
|
|
|
@ -7,15 +7,13 @@ import { call as callEndpoint } from '../index.js';
|
|||
import { getParamsAndProps, GetParamsAndPropsError } from '../../render/core.js';
|
||||
import { createRequest } from '../../render/request.js';
|
||||
|
||||
|
||||
export async function call(ssrOpts: SSROptions) {
|
||||
try {
|
||||
const [, mod] = await preload(ssrOpts);
|
||||
return await callEndpoint(mod as unknown as EndpointHandler, {
|
||||
...ssrOpts,
|
||||
ssr: ssrOpts.astroConfig.buildOptions.experimentalSsr
|
||||
ssr: ssrOpts.astroConfig.buildOptions.experimentalSsr,
|
||||
});
|
||||
|
||||
} catch (e: unknown) {
|
||||
await errorHandler(e, { viteServer: ssrOpts.viteServer, filePath: ssrOpts.filePath });
|
||||
throw e;
|
||||
|
|
|
@ -4,48 +4,38 @@ import { renderEndpoint } from '../../runtime/server/index.js';
|
|||
import { getParamsAndProps, GetParamsAndPropsError } from '../render/core.js';
|
||||
import { createRequest } from '../render/request.js';
|
||||
|
||||
export type EndpointOptions = Pick<RenderOptions,
|
||||
'logging' |
|
||||
'headers' |
|
||||
'method' |
|
||||
'origin' |
|
||||
'route' |
|
||||
'routeCache' |
|
||||
'pathname' |
|
||||
'route' |
|
||||
'site' |
|
||||
'ssr'
|
||||
>;
|
||||
export type EndpointOptions = Pick<RenderOptions, 'logging' | 'headers' | 'method' | 'origin' | 'route' | 'routeCache' | 'pathname' | 'route' | 'site' | 'ssr'>;
|
||||
|
||||
type EndpointCallResult = {
|
||||
type: 'simple',
|
||||
body: string
|
||||
} | {
|
||||
type: 'response',
|
||||
response: Response
|
||||
type EndpointCallResult =
|
||||
| {
|
||||
type: 'simple';
|
||||
body: string;
|
||||
}
|
||||
| {
|
||||
type: 'response';
|
||||
response: Response;
|
||||
};
|
||||
|
||||
export async function call(mod: EndpointHandler, opts: EndpointOptions): Promise<EndpointCallResult> {
|
||||
const paramsAndPropsResp = await getParamsAndProps({ ...opts, mod: (mod as any) });
|
||||
const paramsAndPropsResp = await getParamsAndProps({ ...opts, mod: mod as any });
|
||||
|
||||
if (paramsAndPropsResp === GetParamsAndPropsError.NoMatchingStaticPath) {
|
||||
throw new Error(`[getStaticPath] route pattern matched, but no matching static path found. (${opts.pathname})`);
|
||||
}
|
||||
const [params] = paramsAndPropsResp;
|
||||
const request = createRequest(opts.method, opts.pathname, opts.headers, opts.origin,
|
||||
opts.site, opts.ssr);
|
||||
const request = createRequest(opts.method, opts.pathname, opts.headers, opts.origin, opts.site, opts.ssr);
|
||||
|
||||
const response = await renderEndpoint(mod, request, params);
|
||||
|
||||
if (response instanceof Response) {
|
||||
return {
|
||||
type: 'response',
|
||||
response
|
||||
response,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'simple',
|
||||
body: response.body
|
||||
body: response.body,
|
||||
};
|
||||
}
|
||||
|
|
|
@ -74,7 +74,7 @@ export interface RenderOptions {
|
|||
headers: Headers;
|
||||
}
|
||||
|
||||
export async function render(opts: RenderOptions): Promise<{ type: 'html', html: string } | { type: 'response', response: Response }> {
|
||||
export async function render(opts: RenderOptions): Promise<{ type: 'html'; html: string } | { type: 'response'; response: Response }> {
|
||||
const { headers, legacyBuild, links, logging, origin, markdownRender, method, mod, pathname, scripts, renderers, resolve, route, routeCache, site, ssr } = opts;
|
||||
|
||||
const paramsAndPropsRes = await getParamsAndProps({
|
||||
|
@ -109,7 +109,7 @@ export async function render(opts: RenderOptions): Promise<{ type: 'html', html:
|
|||
scripts,
|
||||
ssr,
|
||||
method,
|
||||
headers
|
||||
headers,
|
||||
});
|
||||
|
||||
let page = await renderPage(result, Component, pageProps, null);
|
||||
|
@ -133,6 +133,6 @@ export async function render(opts: RenderOptions): Promise<{ type: 'html', html:
|
|||
|
||||
return {
|
||||
type: 'html',
|
||||
html
|
||||
html,
|
||||
};
|
||||
}
|
||||
|
|
|
@ -41,9 +41,7 @@ export interface SSROptions {
|
|||
|
||||
export type ComponentPreload = [Renderer[], ComponentInstance];
|
||||
|
||||
export type RenderResponse =
|
||||
{ type: 'html', html: string } |
|
||||
{ type: 'response', response: Response };
|
||||
export type RenderResponse = { type: 'html'; html: string } | { type: 'response'; response: Response };
|
||||
|
||||
const svelteStylesRE = /svelte\?svelte&type=style/;
|
||||
|
||||
|
@ -186,7 +184,7 @@ export async function render(renderers: Renderer[], mod: ComponentInstance, ssrO
|
|||
|
||||
return {
|
||||
type: 'html',
|
||||
html
|
||||
html,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -18,10 +18,9 @@ export interface AstroRequest {
|
|||
method: string;
|
||||
}
|
||||
|
||||
export type AstroRequestSSR = AstroRequest
|
||||
export type AstroRequestSSR = AstroRequest;
|
||||
|
||||
export function createRequest(method: string, pathname: string, headers: Headers,
|
||||
origin: string, site: Site, ssr: boolean): AstroRequest {
|
||||
export function createRequest(method: string, pathname: string, headers: Headers, origin: string, site: Site, ssr: boolean): AstroRequest {
|
||||
const url = new URL('.' + pathname, new URL(origin));
|
||||
|
||||
const canonicalURL = utilCanonicalURL('.' + pathname, site ?? url.origin);
|
||||
|
@ -31,7 +30,7 @@ export function createRequest(method: string, pathname: string, headers: Headers
|
|||
canonicalURL,
|
||||
params: {},
|
||||
headers,
|
||||
method
|
||||
method,
|
||||
};
|
||||
|
||||
if (!ssr) {
|
||||
|
|
|
@ -11,7 +11,7 @@ import { warn, LogOptions } from '../logger.js';
|
|||
function onlyAvailableInSSR(name: string) {
|
||||
return function () {
|
||||
// TODO add more guidance when we have docs and adapters.
|
||||
throw new Error(`Oops, you are trying to use ${name}, which is only available with SSR.`)
|
||||
throw new Error(`Oops, you are trying to use ${name}, which is only available with SSR.`);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -94,14 +94,16 @@ export function createResult(args: CreateResultArgs): SSRResult {
|
|||
__proto__: astroGlobal,
|
||||
props,
|
||||
request,
|
||||
redirect: args.ssr ? (path: string) => {
|
||||
redirect: args.ssr
|
||||
? (path: string) => {
|
||||
return new Response(null, {
|
||||
status: 301,
|
||||
headers: {
|
||||
Location: path
|
||||
}
|
||||
Location: path,
|
||||
},
|
||||
});
|
||||
} : onlyAvailableInSSR('Astro.redirect'),
|
||||
}
|
||||
: onlyAvailableInSSR('Astro.redirect'),
|
||||
resolve(path: string) {
|
||||
if (!legacyBuild) {
|
||||
let extra = `This can be replaced with a dynamic import like so: await import("${path}")`;
|
||||
|
|
|
@ -434,8 +434,7 @@ async function replaceHeadInjection(result: SSRResult, html: string): Promise<st
|
|||
}
|
||||
|
||||
// Calls a component and renders it into a string of HTML
|
||||
export async function renderToString(result: SSRResult, componentFactory: AstroComponentFactory,
|
||||
props: any, children: any): Promise<string> {
|
||||
export async function renderToString(result: SSRResult, componentFactory: AstroComponentFactory, props: any, children: any): Promise<string> {
|
||||
const Component = await componentFactory(result, props, children);
|
||||
if (!isAstroComponent(Component)) {
|
||||
throw new Error('Cannot return a Response from a nested component.');
|
||||
|
@ -450,7 +449,7 @@ export async function renderPage(
|
|||
componentFactory: AstroComponentFactory,
|
||||
props: any,
|
||||
children: any
|
||||
): Promise<{ type: 'html', html: string } | { type: 'response', response: Response }> {
|
||||
): Promise<{ type: 'html'; html: string } | { type: 'response'; response: Response }> {
|
||||
const response = await componentFactory(result, props, children);
|
||||
|
||||
if (isAstroComponent(response)) {
|
||||
|
@ -458,12 +457,12 @@ export async function renderPage(
|
|||
const html = await replaceHeadInjection(result, template);
|
||||
return {
|
||||
type: 'html',
|
||||
html
|
||||
html,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
type: 'response',
|
||||
response
|
||||
response,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue