docs: update canary docs for Fresh 2 (#3108)
There are still some rough edges here and there, but it's in a much better shape than what we have today.
This commit is contained in:
+38
@@ -166,6 +166,10 @@ export let getBuildCache: (app: App<any>) => BuildCache | null;
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export let setBuildCache: (app: App<any>, cache: BuildCache | null) => void;
|
||||
|
||||
/**
|
||||
* Create an application instance that passes the incoming `Request`
|
||||
* instance through middlewares and routes.
|
||||
*/
|
||||
export class App<State> {
|
||||
#router: Router<MiddlewareFn<State>> = new UrlPatternRouter<
|
||||
MiddlewareFn<State>
|
||||
@@ -221,6 +225,9 @@ export class App<State> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add one or more middlewares at the top or the specified path.
|
||||
*/
|
||||
use(...middleware: MiddlewareFn<State>[]): this;
|
||||
use(path: string, ...middleware: MiddlewareFn<State>[]): this;
|
||||
use(
|
||||
@@ -244,6 +251,9 @@ export class App<State> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the app's 404 error handler. Can be a {@linkcode Route} or a {@linkcode MiddlewareFn}.
|
||||
*/
|
||||
notFound(routeOrMiddleware: Route<State> | MiddlewareFn<State>): this {
|
||||
const route = typeof routeOrMiddleware === "function"
|
||||
? { handler: routeOrMiddleware }
|
||||
@@ -304,31 +314,52 @@ export class App<State> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add middlewares for GET requests at the specified path.
|
||||
*/
|
||||
get(path: string, ...middlewares: MiddlewareFn<State>[]): this {
|
||||
this.#addMiddleware("GET", path, middlewares);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Add middlewares for POST requests at the specified path.
|
||||
*/
|
||||
post(path: string, ...middlewares: MiddlewareFn<State>[]): this {
|
||||
this.#addMiddleware("POST", path, middlewares);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Add middlewares for PATCH requests at the specified path.
|
||||
*/
|
||||
patch(path: string, ...middlewares: MiddlewareFn<State>[]): this {
|
||||
this.#addMiddleware("PATCH", path, middlewares);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Add middlewares for PUT requests at the specified path.
|
||||
*/
|
||||
put(path: string, ...middlewares: MiddlewareFn<State>[]): this {
|
||||
this.#addMiddleware("PUT", path, middlewares);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Add middlewares for DELETE requests at the specified path.
|
||||
*/
|
||||
delete(path: string, ...middlewares: MiddlewareFn<State>[]): this {
|
||||
this.#addMiddleware("DELETE", path, middlewares);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Add middlewares for HEAD requests at the specified path.
|
||||
*/
|
||||
head(path: string, ...middlewares: MiddlewareFn<State>[]): this {
|
||||
this.#addMiddleware("HEAD", path, middlewares);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add middlewares for all HTTP verbs at the specified path.
|
||||
*/
|
||||
all(path: string, ...middlewares: MiddlewareFn<State>[]): this {
|
||||
this.#addMiddleware("ALL", path, middlewares);
|
||||
return this;
|
||||
@@ -356,6 +387,10 @@ export class App<State> {
|
||||
this.#routeDefs.push({ method, pattern: path, fns });
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge another {@linkcode App} instance into this app at the
|
||||
* specified path.
|
||||
*/
|
||||
mountApp(path: string, app: App<State>): this {
|
||||
const segmentPath = mergePath("", path);
|
||||
const segment = getOrCreateSegment(this.#root, segmentPath, true);
|
||||
@@ -484,6 +519,9 @@ export class App<State> {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Spawn a server for this app.
|
||||
*/
|
||||
async listen(options: ListenOptions = {}): Promise<void> {
|
||||
if (!options.onListen) {
|
||||
options.onListen = createOnListen(this.config.basePath, options);
|
||||
|
||||
@@ -43,6 +43,10 @@ export interface ResolvedFreshConfig {
|
||||
build: {
|
||||
outDir: string;
|
||||
};
|
||||
/**
|
||||
* Serve fresh from a base path instead of from the root.
|
||||
* "/foo/bar" -> http://localhost:8000/foo/bar
|
||||
*/
|
||||
basePath: string;
|
||||
staticDir: string;
|
||||
/**
|
||||
|
||||
+10
-1
@@ -62,11 +62,14 @@ export class Context<State> {
|
||||
* certain search parameters are set.
|
||||
*/
|
||||
readonly url: URL;
|
||||
/** The original incoming `Request` object` */
|
||||
/** The original incoming {@linkcode Request} object. */
|
||||
readonly req: Request;
|
||||
/** The url parameters of the matched route pattern. */
|
||||
readonly params: Record<string, string>;
|
||||
/** State object that is shared with all middlewares. */
|
||||
readonly state: State = {} as State;
|
||||
data: unknown = undefined;
|
||||
/** Error value if an error was caught (Default: null) */
|
||||
error: unknown | null = null;
|
||||
readonly info: Deno.ServeHandlerInfo | Deno.ServeHandlerInfo;
|
||||
/**
|
||||
@@ -174,6 +177,12 @@ export class Context<State> {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Render JSX and return an HTML `Response` instance.
|
||||
* ```tsx
|
||||
* ctx.render(<h1>hello world</h1>);
|
||||
* ```
|
||||
*/
|
||||
async render(
|
||||
// deno-lint-ignore no-explicit-any
|
||||
vnode: VNode<any> | null,
|
||||
|
||||
+21
-14
@@ -1,33 +1,40 @@
|
||||
import type { FreshContext } from "../context.ts";
|
||||
import type { Context } from "../context.ts";
|
||||
import type { MiddlewareFn } from "./mod.ts";
|
||||
|
||||
export type CORSOptions<State> = {
|
||||
/**
|
||||
* The value of [`Access-Control-Allow-Origin`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Access-Control-Allow-Origin) CORS header
|
||||
*/
|
||||
origin:
|
||||
| string
|
||||
| string[]
|
||||
| ((
|
||||
requestOrigin: string,
|
||||
ctx: FreshContext<State>,
|
||||
) => string | undefined | null);
|
||||
| ((origin: string, ctx: Context<State>) => string | undefined | null);
|
||||
/**
|
||||
* Sets the permitted methods for the [`Access-Control-Allow-Methods`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Access-Control-Allow-Methods) CORS header.
|
||||
*/
|
||||
allowMethods?: string[];
|
||||
/**
|
||||
* Indicate the HTTP headers that can be used during the actual request. Sets the [`Access-Control-Allow-Headers`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Access-Control-Allow-Headers) header
|
||||
*/
|
||||
allowHeaders?: string[];
|
||||
/**
|
||||
* The value of [`Access-Control-Max-Age`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Access-Control-Max-Age) CORS header
|
||||
*/
|
||||
maxAge?: number;
|
||||
/**
|
||||
* The value of [`Access-Control-Allow-Credentials`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Access-Control-Allow-Credentials) CORS header
|
||||
*/
|
||||
credentials?: boolean;
|
||||
/**
|
||||
* The value of [`Access-Control-Expose-Headers`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Access-Control-Expose-Headers) CORS header
|
||||
*/
|
||||
exposeHeaders?: string[];
|
||||
};
|
||||
|
||||
/**
|
||||
* CORS Middleware for Fresh.
|
||||
* Adapted from Hono's CORS middleware.
|
||||
* Would like to express my gratitude to all the developers of Hono.
|
||||
* CORS Middleware to set [`Cross-Origin-Resource-Sharing`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS) headers.
|
||||
*
|
||||
* @param {CORSOptions} [options] - The options for the CORS middleware.
|
||||
* @param {string | string[] | ((requestOrigin: string, ctx: FreshContext) => string | undefined | null)} [options.origin='*'] - The value of "Access-Control-Allow-Origin" CORS header.
|
||||
* @param {string[]} [options.allowMethods=['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH']] - The value of "Access-Control-Allow-Methods" CORS header.
|
||||
* @param {string[]} [options.allowHeaders=[]] - The value of "Access-Control-Allow-Headers" CORS header.
|
||||
* @param {number} [options.maxAge] - The value of "Access-Control-Max-Age" CORS header.
|
||||
* @param {boolean} [options.credentials] - The value of "Access-Control-Allow-Credentials" CORS header.
|
||||
* @param {string[]} [options.exposeHeaders=[]] - The value of "Access-Control-Expose-Headers" CORS header.
|
||||
* @returns {(req: Request, ctx: FreshContext) => Promise<Response>} The Fresh middleware handler function.
|
||||
*
|
||||
* @example
|
||||
|
||||
Reference in New Issue
Block a user