Skip to content

Commit

Permalink
Merge pull request #7 from momentumframework/v0.8.1
Browse files Browse the repository at this point in the history
V0.8.1
  • Loading branch information
lennykean committed Mar 7, 2021
2 parents a48909e + 350cd59 commit b3c0513
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 18 deletions.
47 changes: 45 additions & 2 deletions core/context-accessor.ts
Expand Up @@ -91,11 +91,35 @@ export class ContextAccessor {
)) as string;
}

/**
* Get a state item from the context
*/
async getState(name: string) {
return await this.#platform.getContextItem("state", this.#context, name);
}

/**
* Get a request state item from the context
*/
async getRequestState(name: string) {
return await this.#platform.getContextItem(
"requestState",
this.#context,
name,
);
}

/**
* Set a cookie on the response
*/
async setCookie(name: string, value: string) {
await this.#platform.setContextItem("cookie", this.#context, value, name);
async setCookie(name: string, value: string, options?: unknown) {
await this.#platform.setContextItem(
"cookie",
this.#context,
value,
name,
options,
);
}

/**
Expand Down Expand Up @@ -123,6 +147,25 @@ export class ContextAccessor {
await this.#platform.setContextItem("status", this.#context, status);
}

/**
* Set a state item on the context
*/
async setState(name: string, value: unknown) {
await this.#platform.setContextItem("state", this.#context, value, name);
}

/**
* Set a request state item on the context
*/
async setRequestState(name: string, value: unknown) {
await this.#platform.setContextItem(
"requestState",
this.#context,
value,
name,
);
}

/**
* Set a file as a response
*/
Expand Down
1 change: 1 addition & 0 deletions core/decorators/body.ts
Expand Up @@ -15,5 +15,6 @@ export function Body(name: string): any;
export function Body(name?: string) {
return createParameterDecorator(
async (contextAccessor) => await contextAccessor.getBody(name),
name ?? "body",
);
}
1 change: 1 addition & 0 deletions core/decorators/cookie.ts
Expand Up @@ -8,5 +8,6 @@ import { createParameterDecorator } from "./create-parameter-decorator.ts";
export function Cookie(name: string) {
return createParameterDecorator(
async (contextAccessor) => await contextAccessor.getCookie(name),
name,
);
}
3 changes: 2 additions & 1 deletion core/decorators/create-parameter-decorator.ts
Expand Up @@ -11,6 +11,7 @@ import { ValueProvider } from "../value-provider.ts";
*/
export function createParameterDecorator(
valueProvider?: ValueProvider,
name?: string,
): ParameterDecorator {
return function (
// deno-lint-ignore ban-types
Expand All @@ -28,7 +29,7 @@ export function createParameterDecorator(
propertyKey.toString(),
{
index: parameterIndex,
name: propertyKey.toString(),
name,
type: parameterType,
},
);
Expand Down
1 change: 1 addition & 0 deletions core/decorators/ctx.ts
Expand Up @@ -6,5 +6,6 @@ import { createParameterDecorator } from "./create-parameter-decorator.ts";
export function Ctx() {
return createParameterDecorator(
async (contextAccessor) => await contextAccessor.getContext(),
"ctx",
);
}
1 change: 1 addition & 0 deletions core/decorators/header.ts
Expand Up @@ -8,5 +8,6 @@ import { createParameterDecorator } from "./create-parameter-decorator.ts";
export function Header(name: string) {
return createParameterDecorator(
async (contextAccessor) => await contextAccessor.getHeader(name),
name,
);
}
1 change: 1 addition & 0 deletions core/decorators/param.ts
Expand Up @@ -8,5 +8,6 @@ import { createParameterDecorator } from "./create-parameter-decorator.ts";
export function Param(name: string) {
return createParameterDecorator(
async (contextAccessor) => await contextAccessor.getParameter(name),
name,
);
}
1 change: 1 addition & 0 deletions core/decorators/query.ts
Expand Up @@ -8,5 +8,6 @@ import { createParameterDecorator } from "./create-parameter-decorator.ts";
export function Query(name: string) {
return createParameterDecorator(
async (contextAccessor) => await contextAccessor.getQuery(name),
name,
);
}
1 change: 1 addition & 0 deletions core/decorators/req.ts
Expand Up @@ -6,5 +6,6 @@ import { createParameterDecorator } from "./create-parameter-decorator.ts";
export function Req() {
return createParameterDecorator(
async (contextAccessor) => await contextAccessor.getRequest(),
"req",
);
}
1 change: 1 addition & 0 deletions core/decorators/res.ts
Expand Up @@ -6,5 +6,6 @@ import { createParameterDecorator } from "./create-parameter-decorator.ts";
export function Res() {
return createParameterDecorator(
async (contextAccessor) => await contextAccessor.getResponse(),
"res",
);
}
7 changes: 6 additions & 1 deletion core/mv-middleware.ts
@@ -1,3 +1,5 @@
import { ContextAccessor } from "./context-accessor.ts";

export type NextMiddlewareFunction = () => Promise<void>;

/**
Expand All @@ -7,5 +9,8 @@ export type NextMiddlewareFunction = () => Promise<void>;
* Middleware will be executed immediately before a request is processed.
*/
export interface MvMiddleware {
execute(context: unknown, next: NextMiddlewareFunction): Promise<void>;
execute(
context: ContextAccessor,
next: NextMiddlewareFunction,
): Promise<void>;
}
12 changes: 11 additions & 1 deletion core/platform.ts
Expand Up @@ -220,17 +220,27 @@ export abstract class ServerPlatform extends Platform {
| "body"
| "cookie"
| "header"
| "state"
| "requestState"
| "request"
| "response",
context: unknown,
identifier?: unknown,
): Promise<unknown>;

abstract setContextItem(
kind: "body" | "status" | "cookie" | "header" | "status",
kind:
| "body"
| "status"
| "cookie"
| "header"
| "status"
| "state"
| "requestState",
context: unknown,
value: unknown,
identifier?: unknown,
options?: unknown,
): void | Promise<void>;

abstract sendFile(context: unknown, path: string): void | Promise<void>;
Expand Down
18 changes: 9 additions & 9 deletions core/server-controller.ts
Expand Up @@ -256,15 +256,6 @@ export class ServerController {

private async handleError(err: unknown, contextAccessor: ContextAccessor) {
this.#logger.error(err, "An unhandled exception occurred");
for (const handler of this.getGlobalErrorHandlers()) {
try {
const result = await handler(err, contextAccessor);
if (result && result.handled) {
return;
}
// deno-lint-ignore no-empty
} catch {}
}
if (err instanceof ServerException) {
try {
let content: string;
Expand All @@ -284,6 +275,15 @@ export class ServerController {
// deno-lint-ignore no-empty
} catch {}
}
for (const handler of this.getGlobalErrorHandlers()) {
try {
const result = await handler(err, contextAccessor);
if (result && result.handled) {
return;
}
// deno-lint-ignore no-empty
} catch {}
}
contextAccessor.setBody("An unknown error occurred");
contextAccessor.setHeader("Content-Type", "text/plain");
contextAccessor.setStatus(500);
Expand Down
4 changes: 4 additions & 0 deletions mvc/mvc-filter.ts
@@ -1,5 +1,6 @@
import {
ActionMetadata,
ActionResult,
ContextAccessor,
ControllerMetadata,
Injectable,
Expand All @@ -20,6 +21,9 @@ export class MvcFilter implements MvFilter {
actionMetadata?: ActionMetadata,
): Promise<unknown> {
const model = await next();
if (model instanceof ActionResult) {
return model;
}
if (!controllerMetadata || !actionMetadata) {
return model;
}
Expand Down
36 changes: 33 additions & 3 deletions platform-oak/platform-oak.ts
Expand Up @@ -74,7 +74,6 @@ export class OakPlatform extends ServerPlatform {
}

async postBootstrap() {
this.#app.use(this.#router.routes());
this.#app.addEventListener("listen", ({ hostname, port, secure }) => {
this.logger.log([
`PlatformOak listening on `,
Expand All @@ -84,6 +83,7 @@ export class OakPlatform extends ServerPlatform {
].join(""));
});
await super.postBootstrap();
this.#app.use(this.#router.routes());
}

addRouteHandler(
Expand Down Expand Up @@ -139,6 +139,8 @@ export class OakPlatform extends ServerPlatform {
| "body"
| "cookie"
| "header"
| "state"
| "requestState"
| "request"
| "response",
context: RouterContext,
Expand Down Expand Up @@ -173,6 +175,18 @@ export class OakPlatform extends ServerPlatform {
return context.cookies.get(identifier);
case "header":
return context.request.headers.get(identifier);
case "state":
return context.state[identifier];
case "requestState": {
const request = context.request as unknown as {
__state: Record<string, unknown>;
};
let state = request.__state;
if (!state) {
return;
}
return state[identifier];
}
case "request":
return context.request;
case "response":
Expand All @@ -183,12 +197,14 @@ export class OakPlatform extends ServerPlatform {
}

setContextItem(
kind: "body" | "status" | "cookie" | "header",
kind: "body" | "status" | "cookie" | "header" | "state" | "requestState",
context: RouterContext,
// deno-lint-ignore no-explicit-any
value: any,
// deno-lint-ignore no-explicit-any
identifier?: any,
// deno-lint-ignore no-explicit-any
options?: any,
) {
switch (kind) {
case "body":
Expand All @@ -198,11 +214,25 @@ export class OakPlatform extends ServerPlatform {
context.response.status = value;
break;
case "cookie":
context.cookies.set(identifier, value);
context.cookies.set(identifier, value, options);
break;
case "header":
context.response.headers.set(identifier, value);
break;
case "state":
context.state[identifier] = value;
break;
case "requestState": {
const request = context.request as unknown as {
__state: Record<string, unknown>;
};
let state = request.__state;
if (!state) {
state = {};
request.__state = state;
}
state[identifier] = value;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion static-files/static-file.module.ts
Expand Up @@ -16,7 +16,7 @@ import { StaticFilesConfig } from "./static-files-config.ts";
providers: [StaticFileMiddleware],
})
export class StaticFileModule {
static register(config: StaticFilesConfig): DynamicModule {
static register(config: Partial<StaticFilesConfig>): DynamicModule {
return {
type: StaticFileModule,
providers: [
Expand Down

0 comments on commit b3c0513

Please sign in to comment.