spooder 4.0.0 → 4.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -0
- package/package.json +1 -1
- package/src/api.d.ts +5 -0
- package/src/api.ts +9 -0
package/README.md
CHANGED
|
@@ -404,6 +404,7 @@ In addition to the information provided by the developer, `spooder` also include
|
|
|
404
404
|
- [`panic(err_message_or_obj: string | object, ...err: object[]): Promise<void>`](#api-error-handling-panic)
|
|
405
405
|
- [API > Content](#api-content)
|
|
406
406
|
- [`template_sub(template: string, replacements: Record<string, string>): string`](#api-content-template-sub)
|
|
407
|
+
- [`template_sub_file(template_file: string, replacements: Record<string, string>): Promise<string>`](#api-content-template-sub-file)
|
|
407
408
|
- [`generate_hash_subs(length: number, prefix: string): Promise<Record<string, string>>`](#api-content-generate-hash-subs)
|
|
408
409
|
- [`apply_range(file: BunFile, request: Request): HandlerReturnType`](#api-content-apply-range)
|
|
409
410
|
- [API > State Management](#api-state-management)
|
|
@@ -457,6 +458,20 @@ server.route('/test/route', (req, url) => {
|
|
|
457
458
|
server.route('/redirect', () => Response.redirect('/redirected', 301));
|
|
458
459
|
```
|
|
459
460
|
|
|
461
|
+
### Status Code Text
|
|
462
|
+
|
|
463
|
+
`spooder` exposes `HTTP_STATUS_CODE` to convieniently access status code text.
|
|
464
|
+
|
|
465
|
+
```ts
|
|
466
|
+
import { HTTP_STATUS_CODE } from 'spooder';
|
|
467
|
+
|
|
468
|
+
server.default((req, status_code) => {
|
|
469
|
+
// status_code: 404
|
|
470
|
+
// Body: Not Found
|
|
471
|
+
return new Response(HTTP_STATUS_CODE[status_code], { status: status_code });
|
|
472
|
+
});
|
|
473
|
+
```
|
|
474
|
+
|
|
460
475
|
<a id="api-routing-request-handler"></a>
|
|
461
476
|
## API > Routing > RequestHandler
|
|
462
477
|
|
|
@@ -876,6 +891,24 @@ const html = template_sub(template, replacements);
|
|
|
876
891
|
</html>
|
|
877
892
|
```
|
|
878
893
|
|
|
894
|
+
<a id="api-content-template-sub-file"></a>
|
|
895
|
+
### 🔧 `template_sub_file(template_file: string, replacements: Record<string, string>): Promise<string>`
|
|
896
|
+
|
|
897
|
+
Replace placeholders in a template file with values from a replacement object.
|
|
898
|
+
|
|
899
|
+
> [!NOTE]
|
|
900
|
+
> This function is a convenience wrapper around `template_sub` and `Bun.file().text()` to reduce boilerplate. See `template_sub` for more information.
|
|
901
|
+
|
|
902
|
+
```ts
|
|
903
|
+
const html = await template_sub_file('./template.html', replacements);
|
|
904
|
+
|
|
905
|
+
// Is equivalent to:
|
|
906
|
+
const file = Bun.file('./template.html');
|
|
907
|
+
const file_contents = await file.text();
|
|
908
|
+
const html = await template_sub(file_contents, replacements);
|
|
909
|
+
```
|
|
910
|
+
|
|
911
|
+
|
|
879
912
|
<a id="api-content-generate-hash-subs"></a>
|
|
880
913
|
### 🔧 `generate_hash_subs(prefix: string): Promise<Record<string, string>>`
|
|
881
914
|
|
package/package.json
CHANGED
package/src/api.d.ts
CHANGED
|
@@ -4,6 +4,10 @@
|
|
|
4
4
|
/// <reference types="node" />
|
|
5
5
|
import fs from 'node:fs/promises';
|
|
6
6
|
import { Blob } from 'node:buffer';
|
|
7
|
+
export declare const HTTP_STATUS_CODE: {
|
|
8
|
+
[errorCode: number]: string | undefined;
|
|
9
|
+
[errorCode: string]: string | undefined;
|
|
10
|
+
};
|
|
7
11
|
export declare class ErrorWithMetadata extends Error {
|
|
8
12
|
metadata: Record<string, unknown>;
|
|
9
13
|
constructor(message: string, metadata: Record<string, unknown>);
|
|
@@ -11,6 +15,7 @@ export declare class ErrorWithMetadata extends Error {
|
|
|
11
15
|
}
|
|
12
16
|
export declare function panic(err_message_or_obj: string | object, ...err: object[]): Promise<void>;
|
|
13
17
|
export declare function caution(err_message_or_obj: string | object, ...err: object[]): Promise<void>;
|
|
18
|
+
export declare function template_sub_file(template_file: string, replacements: Record<string, string>): Promise<string>;
|
|
14
19
|
export declare function template_sub(template: string, replacements: Record<string, string>): string;
|
|
15
20
|
export declare function generate_hash_subs(length?: number, prefix?: string): Promise<Record<string, string>>;
|
|
16
21
|
type CookieOptions = {
|
package/src/api.ts
CHANGED
|
@@ -7,6 +7,8 @@ import { log } from './utils';
|
|
|
7
7
|
import crypto from 'crypto';
|
|
8
8
|
import { Blob } from 'node:buffer';
|
|
9
9
|
|
|
10
|
+
export const HTTP_STATUS_CODE = http.STATUS_CODES;
|
|
11
|
+
|
|
10
12
|
export class ErrorWithMetadata extends Error {
|
|
11
13
|
constructor(message: string, public metadata: Record<string, unknown>) {
|
|
12
14
|
super(message);
|
|
@@ -82,6 +84,13 @@ export async function caution(err_message_or_obj: string | object, ...err: objec
|
|
|
82
84
|
await handle_error('caution: ', err_message_or_obj, ...err);
|
|
83
85
|
}
|
|
84
86
|
|
|
87
|
+
export async function template_sub_file(template_file: string, replacements: Record<string, string>): Promise<string> {
|
|
88
|
+
const file = Bun.file(template_file);
|
|
89
|
+
const file_contents = await file.text();
|
|
90
|
+
|
|
91
|
+
return template_sub(file_contents, replacements);
|
|
92
|
+
}
|
|
93
|
+
|
|
85
94
|
export function template_sub(template: string, replacements: Record<string, string>): string {
|
|
86
95
|
let result = '';
|
|
87
96
|
let buffer = '';
|