Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
briward committed Nov 8, 2024
2 parents d8e86ae + b35b1c6 commit 0a3a4a1
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 60 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The following middleware response will be automatically detected as `application

```ts
app.add(() => ({
name: 'Ian Malcolm',
name: 'Dr Ian Malcolm',
}));
```

Expand All @@ -83,9 +83,7 @@ app.add((context: Context) => {
context.response.headers.set('content-type', 'application/hal+json');

return {
data: {
name: 'Ian Malcolm'
}
name: 'Dr Ian Malcolm'
}
});
```
Expand Down Expand Up @@ -114,7 +112,7 @@ app.add((_context: Context, next: CallableFunction) => {
});

app.add(() => ({
name: 'Ellie Sattler'
name: 'Dr Ellie Sattler'
}));
```

Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@raptor/framework",
"version": "0.4.0",
"version": "0.4.1",
"exports": "./mod.ts",
"publish": {
"exclude": [
Expand Down
2 changes: 1 addition & 1 deletion src/http/context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* The context definition.
*/
export default class Context implements Context {
export default class Context {
/**
* Initialise an HTTP context.
*
Expand Down
58 changes: 58 additions & 0 deletions src/http/processor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// deno-lint-ignore-file no-explicit-any

import type Context from "./context.ts";

export default class Processor {
/**
* Initialise the HTTP processor.
*
* @param context The current HTTP context.
* @constructor
*/
constructor(private context: Context) {}

/**
* Process a body into a valid Response object.
*
* @param body A body value to process.
* @returns A valid HTTP response object.
*/
public process(body: any): Response {
// If the middleware provides a Response object, use it.
if (body instanceof Response) {
return body;
}

const hasContentType = this.context.response.headers.get("content-type");

// If the middleware returns an object, process it as JSON.
if (typeof body === "object") {
if (!hasContentType) {
this.context.response.headers.set("content-type", "application/json");
}

return new Response(JSON.stringify(body), {
headers: this.context.response.headers,
});
}

// If the middleware returns a string, process plain text or HTML.
if (typeof body === "string") {
const isHtml = (new RegExp(/<[a-z/][\s\S]*>/i)).test(body);

if (!hasContentType) {
this.context.response.headers.set("content-type", "text/plain");
}

if (isHtml && !hasContentType) {
this.context.response.headers.set("content-type", "text/html");
}

return new Response(body as string, {
headers: this.context.response.headers,
});
}

return new Response(body as string);
}
}
62 changes: 9 additions & 53 deletions src/kernel.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// deno-lint-ignore-file no-explicit-any

import Context from "./http/context.ts";
import Processor from "./http/processor.ts";

import type { Error } from "./error/interfaces/error.ts";

/**
* The root initialiser for the framework.
*/
export default class Kernel {
/**
* The response processor for the kernel.
*/
private processor: Processor;

/**
* The current HTTP context.
*/
Expand All @@ -18,11 +22,6 @@ export default class Kernel {
*/
private middleware: CallableFunction[] = [];

/**
* The current middleware index.
*/
private currentIndex: number = 0;

/**
* Initialise the kernel.
*
Expand All @@ -33,6 +32,8 @@ export default class Kernel {
new Request(Deno.env.get("APP_URL") as string),
new Response(null),
);

this.processor = new Processor(this.context);
}

/**
Expand Down Expand Up @@ -92,7 +93,7 @@ export default class Kernel {
);

if (!called) {
this.context.response = this.process(body);
this.context.response = this.processor.process(body);
}
}
};
Expand All @@ -101,51 +102,6 @@ export default class Kernel {
await execute(0);
}

/**
* Process middleware into an HTTP response.
*
* @param body The response body.
* @returns
*/
private process(body: any): Response {
// If the middleware provides a Response object, use it.
if (body instanceof Response) {
return body;
}

const hasContentType = this.context.response.headers.get("content-type");

// If the middleware returns an object, process it as JSON.
if (typeof body === "object") {
if (!hasContentType) {
this.context.response.headers.set("content-type", "application/json");
}

return new Response(JSON.stringify(body), {
headers: this.context.response.headers,
});
}

// If the middleware returns a string, process plain text or HTML.
if (typeof body === "string") {
const isHtml = (new RegExp(/<[a-z/][\s\S]*>/i)).test(body);

if (!hasContentType) {
this.context.response.headers.set("content-type", "text/plain");
}

if (isHtml && !hasContentType) {
this.context.response.headers.set("content-type", "text/html");
}

return new Response(body as string, {
headers: this.context.response.headers,
});
}

return new Response(body as string);
}

/**
* Handles an error and returns a response.
*
Expand Down

0 comments on commit 0a3a4a1

Please sign in to comment.