@glass-cannon/router 0.2.0 → 0.5.0
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/dist/index.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ export interface RouterOptions<Context = unknown> {
|
|
|
19
19
|
fallback?: Handler;
|
|
20
20
|
middleware?: Middleware<Context>;
|
|
21
21
|
}
|
|
22
|
-
export interface RouteOptions<Context, NewContext =
|
|
22
|
+
export interface RouteOptions<Context, NewContext = unknown> {
|
|
23
23
|
method?: StringWithSuggestions<'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS'>;
|
|
24
24
|
path: string;
|
|
25
25
|
middleware?: Middleware<NewContext>;
|
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
import { type Response } from '@glass-cannon/server';
|
|
1
|
+
import { type RequestBody, type Response } from '@glass-cannon/server';
|
|
2
2
|
import type { Middleware } from './index.js';
|
|
3
3
|
import type { RouteContext } from '../index.js';
|
|
4
4
|
export interface ContentTypeOptions {
|
|
5
|
-
|
|
5
|
+
allowNoContent?: boolean;
|
|
6
6
|
onInvalidContentType?: (context: RouteContext) => Promise<Response> | Response;
|
|
7
7
|
}
|
|
8
|
+
export declare function contentType<T>({ contentType, deserializeRequest, allowNoContent, onInvalidContentType, }: {
|
|
9
|
+
contentType: string;
|
|
10
|
+
deserializeRequest: (body?: RequestBody) => Promise<T> | T;
|
|
11
|
+
} & ContentTypeOptions): Middleware<{
|
|
12
|
+
body: T;
|
|
13
|
+
}>;
|
|
8
14
|
export declare function textBody(options?: ContentTypeOptions): Middleware<{
|
|
9
15
|
body: string;
|
|
10
16
|
}>;
|
|
@@ -1,24 +1,21 @@
|
|
|
1
1
|
import { readJson, readText } from '@glass-cannon/server';
|
|
2
|
-
export function
|
|
3
|
-
const allowNoContent = options?.allowNoConent ?? true;
|
|
4
|
-
const onInvalidContentType = options?.onInvalidContentType ?? (() => ({ status: 415 }));
|
|
2
|
+
export function contentType({ contentType, deserializeRequest, allowNoContent = true, onInvalidContentType = () => ({ status: 415 }), }) {
|
|
5
3
|
return async (handler, context) => {
|
|
6
|
-
const
|
|
7
|
-
if (!
|
|
4
|
+
const contentTypeHeader = context.headers.get('Content-Type');
|
|
5
|
+
if (!contentTypeHeader) {
|
|
8
6
|
if (allowNoContent) {
|
|
9
|
-
|
|
7
|
+
const body = await deserializeRequest();
|
|
8
|
+
return await handler({ body });
|
|
10
9
|
}
|
|
11
10
|
else {
|
|
12
11
|
return await onInvalidContentType(context);
|
|
13
12
|
}
|
|
14
13
|
}
|
|
15
|
-
if (!
|
|
14
|
+
if (!contentTypeHeader.startsWith(contentType))
|
|
16
15
|
return await onInvalidContentType(context);
|
|
17
|
-
const charsetMatch = /charset=([^;]*)/i.exec(contentType);
|
|
18
|
-
const encoding = (charsetMatch?.[1]?.trim().toLowerCase() ?? 'utf-8');
|
|
19
16
|
let body;
|
|
20
17
|
try {
|
|
21
|
-
body = await
|
|
18
|
+
body = await deserializeRequest(context.stream);
|
|
22
19
|
}
|
|
23
20
|
catch {
|
|
24
21
|
return await onInvalidContentType(context);
|
|
@@ -26,29 +23,18 @@ export function textBody(options) {
|
|
|
26
23
|
return await handler({ body });
|
|
27
24
|
};
|
|
28
25
|
}
|
|
26
|
+
export function textBody(options) {
|
|
27
|
+
return contentType({
|
|
28
|
+
contentType: 'text/plain',
|
|
29
|
+
deserializeRequest: (body) => (body ? readText(body, 'utf-8') : ''),
|
|
30
|
+
...options,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
29
33
|
export function jsonBody(options) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (allowNoContent) {
|
|
36
|
-
return await handler({ body: undefined });
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
return await onInvalidContentType(context);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
if (!contentType.startsWith('application/json'))
|
|
43
|
-
return await onInvalidContentType(context);
|
|
44
|
-
let body;
|
|
45
|
-
try {
|
|
46
|
-
body = await readJson(context.stream, 'utf-8');
|
|
47
|
-
}
|
|
48
|
-
catch {
|
|
49
|
-
return await onInvalidContentType(context);
|
|
50
|
-
}
|
|
51
|
-
return await handler({ body });
|
|
52
|
-
};
|
|
34
|
+
return contentType({
|
|
35
|
+
contentType: 'application/json',
|
|
36
|
+
deserializeRequest: (body) => body && readJson(body),
|
|
37
|
+
...options,
|
|
38
|
+
});
|
|
53
39
|
}
|
|
54
40
|
//# sourceMappingURL=contentType.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contentType.js","sourceRoot":"","sources":["../../src/middleware/contentType.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,QAAQ,
|
|
1
|
+
{"version":3,"file":"contentType.js","sourceRoot":"","sources":["../../src/middleware/contentType.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAmC,MAAM,sBAAsB,CAAC;AAS3F,MAAM,UAAU,WAAW,CAAI,EAC7B,WAAW,EACX,kBAAkB,EAClB,cAAc,GAAG,IAAI,EACrB,oBAAoB,GAAG,GAAG,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,GAI1B;IACpB,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE;QAChC,MAAM,iBAAiB,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;QAC9D,IAAI,CAAC,iBAAiB,EAAE,CAAC;YACvB,IAAI,cAAc,EAAE,CAAC;gBACnB,MAAM,IAAI,GAAG,MAAM,kBAAkB,EAAE,CAAC;gBACxC,OAAO,MAAM,OAAO,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;YACjC,CAAC;iBAAM,CAAC;gBACN,OAAO,MAAM,oBAAoB,CAAC,OAAO,CAAC,CAAC;YAC7C,CAAC;QACH,CAAC;QAED,IAAI,CAAC,iBAAiB,CAAC,UAAU,CAAC,WAAW,CAAC;YAAE,OAAO,MAAM,oBAAoB,CAAC,OAAO,CAAC,CAAC;QAC3F,IAAI,IAAO,CAAC;QACZ,IAAI,CAAC;YACH,IAAI,GAAG,MAAM,kBAAkB,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAClD,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,MAAM,oBAAoB,CAAC,OAAO,CAAC,CAAC;QAC7C,CAAC;QACD,OAAO,MAAM,OAAO,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;IACjC,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,OAA4B;IACnD,OAAO,WAAW,CAAC;QACjB,WAAW,EAAE,YAAY;QACzB,kBAAkB,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACnE,GAAG,OAAO;KACX,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,OAA4B;IACnD,OAAO,WAAW,CAAC;QACjB,WAAW,EAAE,kBAAkB;QAC/B,kBAAkB,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,IAAI,QAAQ,CAAC,IAAI,CAAC;QACpD,GAAG,OAAO;KACX,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@glass-cannon/router",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.5.0",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
7
7
|
"import": "./dist/index.js",
|
|
@@ -12,9 +12,11 @@
|
|
|
12
12
|
"types": "./dist/middleware/index.d.ts"
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
|
-
"files": [
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
16
18
|
"dependencies": {
|
|
17
|
-
"@glass-cannon/server": "
|
|
19
|
+
"@glass-cannon/server": "0.5.0"
|
|
18
20
|
},
|
|
19
21
|
"devDependencies": {
|
|
20
22
|
"@types/bun": "latest"
|
|
@@ -25,4 +27,4 @@
|
|
|
25
27
|
"scripts": {
|
|
26
28
|
"build": "tsc && tsc-alias"
|
|
27
29
|
}
|
|
28
|
-
}
|
|
30
|
+
}
|