Skip to content

Commit

Permalink
experimental context function
Browse files Browse the repository at this point in the history
  • Loading branch information
lassejlv committed May 23, 2024
1 parent 5dd0fef commit 4f107d3
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 95 deletions.
17 changes: 17 additions & 0 deletions lib/helpers/context.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type ContextType = {
json: (json: object, status?: number) => Promise<Response>;
pretty: (json: object, status?: number) => Promise<Response>;
text: (text: string, status?: number) => Promise<Response>;
html: (html: string, status?: number) => Promise<Response>;
error: (message: string, status?: number) => Promise<Response>;
success: (message: string, status?: number) => Promise<Response>;
redirect: (url: string, status?: number) => Promise<Response>;
sendFile: (filePath: string, status?: number) => Promise<Response>;
readHtml: (filePath: string) => Promise<string>;
query: {
get: (key: string) => string | null;
};
req: Request;
};
declare function Context(request: Request): ContextType;
export { Context, type ContextType };
3 changes: 2 additions & 1 deletion lib/main/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SendJSON, Success, Failure, ServerFailure, Redirect, Html, SendFile } from "../helpers/helper.ts";
import { Context, type ContextType } from "../helpers/context.ts";
import { query } from "../helpers/query.ts";
import { param } from "../helpers/param.ts";
import MongoService from "../instances/mongodb.ts";
Expand All @@ -21,4 +22,4 @@ declare class ProBun {
definePreMiddleware(middleware: any): void;
definePostMiddleware(middleware: any): void;
}
export { ProBun, SendJSON, Success, Failure, ServerFailure, Redirect, Html, query, param, MongoService, PgService, SendFile, json };
export { ProBun, SendJSON, Success, Failure, ServerFailure, Redirect, Html, query, param, MongoService, PgService, SendFile, json, Context, type ContextType, };
2 changes: 1 addition & 1 deletion lib/main/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "probun",
"description": "Powerful file-based routing for Bun servers",
"module": "src/main/index.ts",
"version": "0.1.25",
"version": "0.1.26",
"main": "./lib/main/index.js",
"type": "module",
"homepage": "https://probun.dev",
Expand All @@ -25,4 +25,4 @@
"mongodb": "^6.5.0",
"pg": "^8.11.5"
}
}
}
115 changes: 115 additions & 0 deletions src/helpers/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import * as path from "path/posix";

type ContextType = {
json: (json: object, status?: number) => Promise<Response>;
pretty: (json: object, status?: number) => Promise<Response>;
text: (text: string, status?: number) => Promise<Response>;
html: (html: string, status?: number) => Promise<Response>;
error: (message: string, status?: number) => Promise<Response>;
success: (message: string, status?: number) => Promise<Response>;
redirect: (url: string, status?: number) => Promise<Response>;
sendFile: (filePath: string, status?: number) => Promise<Response>;
readHtml: (filePath: string) => Promise<string>;
query: {
get: (key: string) => string | null;
};
req: Request;
};

function Context(request: Request): ContextType {
const json = async (json: object, status: number = 200) => {
return new Response(JSON.stringify(json), {
status,
headers: {
"Content-Type": "application/json",
},
});
};

const pretty = async (json: object, status: number = 200) => {
return new Response(JSON.stringify(json, null, 2), {
status,
headers: {
"Content-Type": "application/json",
},
});
};

const text = async (text: string, status: number = 200) => {
return new Response(text, {
status,
headers: {
"Content-Type": "text/plain",
},
});
};

const html = async (html: string, status: number = 200) => {
return new Response(html, {
status,
headers: {
"Content-Type": "text/html",
},
});
};

const error = async (message: string, status: number = 500) => {
return json({ error: message }, status);
};

const success = async (message: string, status: number = 200) => {
return json({ message }, status);
};

const redirect = async (url: string, status: number = 302) => {
return new Response(null, {
status,
headers: {
Location: url,
},
});
};

const sendFile = async (filePath: string, status: number = 200) => {
const file = Bun.file(filePath);
let rawFileName = path.basename(filePath);
rawFileName = rawFileName.replace(/ /g, "_");
rawFileName = rawFileName.replace(/\\/g, "_");
rawFileName = rawFileName.split("_")[rawFileName.split("_").length - 1];
return new Response(file, {
headers: {
"Content-Type": "application/octet-stream",
"Content-Disposition": `attachment; filename="${rawFileName}"`,
},
status,
});
};

const query = {
get: (key: string) => {
return new URL(request.url).searchParams.get(key);
},
};

const readHtml = async (filePath: string) => {
if (!filePath.endsWith(".html")) throw new Error("File must be an HTML file");
const file = await Bun.file(filePath).text();
return file;
};

return {
json,
pretty,
text,
html,
error,
success,
redirect,
sendFile,
readHtml,
query,
req: request,
};
}

export { Context, type ContextType };
Loading

0 comments on commit 4f107d3

Please sign in to comment.