@bepalo/spine 3.12.27 → 3.15.30
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 +793 -1048
- package/dist/auth-middlewares.d.ts +7 -7
- package/dist/auth-middlewares.d.ts.map +1 -1
- package/dist/auth-middlewares.js.map +1 -1
- package/dist/cjs/auth-middlewares.d.ts +7 -7
- package/dist/cjs/auth-middlewares.d.ts.map +1 -1
- package/dist/cjs/auth-middlewares.js.map +1 -1
- package/dist/cjs/helpers.d.ts +17 -13
- package/dist/cjs/helpers.d.ts.map +1 -1
- package/dist/cjs/helpers.js +51 -46
- package/dist/cjs/helpers.js.map +1 -1
- package/dist/cjs/middlewares.d.ts +71 -5
- package/dist/cjs/middlewares.d.ts.map +1 -1
- package/dist/cjs/middlewares.js +100 -26
- package/dist/cjs/middlewares.js.map +1 -1
- package/dist/cjs/parsers.d.ts +4 -1
- package/dist/cjs/parsers.d.ts.map +1 -1
- package/dist/cjs/parsers.js +50 -18
- package/dist/cjs/parsers.js.map +1 -1
- package/dist/cjs/router.d.ts +47 -0
- package/dist/cjs/router.d.ts.map +1 -1
- package/dist/cjs/router.js +433 -3
- package/dist/cjs/router.js.map +1 -1
- package/dist/cjs/types.d.ts +8 -1
- package/dist/cjs/types.d.ts.map +1 -1
- package/dist/cjs/types.js.map +1 -1
- package/dist/helpers.d.ts +17 -13
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js +51 -46
- package/dist/helpers.js.map +1 -1
- package/dist/middlewares.d.ts +71 -5
- package/dist/middlewares.d.ts.map +1 -1
- package/dist/middlewares.js +100 -26
- package/dist/middlewares.js.map +1 -1
- package/dist/parsers.d.ts +4 -1
- package/dist/parsers.d.ts.map +1 -1
- package/dist/parsers.js +50 -18
- package/dist/parsers.js.map +1 -1
- package/dist/router.d.ts +47 -0
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +433 -3
- package/dist/router.js.map +1 -1
- package/dist/types.d.ts +8 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { type Context, type Handler } from "./types.ts";
|
|
1
|
+
import { EmptyRecord, type Context, type Handler } from "./types.ts";
|
|
2
2
|
/**
|
|
3
3
|
* Represents an authenticated user.
|
|
4
4
|
*
|
|
5
5
|
* @template {Record<string, unknown>} ExtendAuth - Extend Auth Context
|
|
6
6
|
*/
|
|
7
|
-
export type Auth<ExtendAuth extends Record<string, unknown> =
|
|
7
|
+
export type Auth<ExtendAuth extends Record<string, unknown> = EmptyRecord> = {
|
|
8
8
|
/** Role assigned to the user (e.g., "admin", "user") */
|
|
9
9
|
role: string;
|
|
10
10
|
} & ExtendAuth;
|
|
@@ -13,7 +13,7 @@ export type Auth<ExtendAuth extends Record<string, unknown> = Record<string, nev
|
|
|
13
13
|
*
|
|
14
14
|
* @template {Record<string, unknown>} ExtendAuth - Extend Auth Context
|
|
15
15
|
*/
|
|
16
|
-
export type CTAuth<ExtendAuth extends Record<string, unknown> =
|
|
16
|
+
export type CTAuth<ExtendAuth extends Record<string, unknown> = EmptyRecord> = {
|
|
17
17
|
/** Authenticated user details */
|
|
18
18
|
auth?: Auth<ExtendAuth>;
|
|
19
19
|
};
|
|
@@ -23,7 +23,7 @@ export type CTAuth<ExtendAuth extends Record<string, unknown> = Record<string, n
|
|
|
23
23
|
* @template {Record<string, unknown>} ExtendAuth - Extend Auth Context
|
|
24
24
|
* @template {Record<string, unknown>} ExtendContext - Extend Router Context
|
|
25
25
|
*/
|
|
26
|
-
export type ParseAuthFn<ExtendAuth extends Record<string, unknown> =
|
|
26
|
+
export type ParseAuthFn<ExtendAuth extends Record<string, unknown> = EmptyRecord, ExtendContext extends Record<string, unknown> = EmptyRecord> = (ctx: Context<CTAuth<ExtendAuth> & ExtendContext>) => Promise<Auth<ExtendAuth> | Response | null | undefined> | Auth<ExtendAuth> | Response | null | undefined;
|
|
27
27
|
/**
|
|
28
28
|
* Middleware to authenticate a request.
|
|
29
29
|
*
|
|
@@ -39,7 +39,7 @@ export type ParseAuthFn<ExtendAuth extends Record<string, unknown> = Record<stri
|
|
|
39
39
|
* @returns {Handler<CTAuth<ExtendAuth> & ExtendContext>} A handler that sets `ctx.auth` if authentication succeeds,
|
|
40
40
|
* otherwise returns a `401 Unauthorized` or with error message if available response (unless `checkOnly` is true).
|
|
41
41
|
*/
|
|
42
|
-
export declare const authenticate: <ExtendAuth extends Record<string, unknown> =
|
|
42
|
+
export declare const authenticate: <ExtendAuth extends Record<string, unknown> = EmptyRecord, ExtendContext extends Record<string, unknown> = EmptyRecord>({ parseAuth, breakPipeline, checkOnly, responseType, }: {
|
|
43
43
|
parseAuth: ParseAuthFn<ExtendAuth, ExtendContext>;
|
|
44
44
|
breakPipeline?: boolean;
|
|
45
45
|
checkOnly?: boolean;
|
|
@@ -64,7 +64,7 @@ export declare const authenticate: <ExtendAuth extends Record<string, unknown> =
|
|
|
64
64
|
* Throws an error if `permissions` is set without `hasPermission`.
|
|
65
65
|
*
|
|
66
66
|
*/
|
|
67
|
-
export declare const authorize: <ExtendAuth extends Record<string, unknown> =
|
|
67
|
+
export declare const authorize: <ExtendAuth extends Record<string, unknown> = EmptyRecord, ExtendContext extends Record<string, unknown> = EmptyRecord>({ allowRole, forbidRole, permissions, hasPermission, responseType, breakPipeline, }: {
|
|
68
68
|
allowRole?: (role: string) => boolean;
|
|
69
69
|
forbidRole?: (role: string) => boolean;
|
|
70
70
|
permissions?: string[];
|
|
@@ -110,7 +110,7 @@ export declare const basicAuthParser: <ExtendAuth extends {
|
|
|
110
110
|
username: string;
|
|
111
111
|
} & Record<string, unknown> = {
|
|
112
112
|
username: string;
|
|
113
|
-
}, ExtendContext extends Record<string, unknown> =
|
|
113
|
+
}, ExtendContext extends Record<string, unknown> = EmptyRecord>({ validateCredentials, type, separator, realm, defaultRole, }: {
|
|
114
114
|
validateCredentials: (credentials: {
|
|
115
115
|
username: string;
|
|
116
116
|
password: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-middlewares.d.ts","sourceRoot":"","sources":["../src/auth-middlewares.ts"],"names":[],"mappings":"AAIA,OAAO,
|
|
1
|
+
{"version":3,"file":"auth-middlewares.d.ts","sourceRoot":"","sources":["../src/auth-middlewares.ts"],"names":[],"mappings":"AAIA,OAAO,EAEL,WAAW,EAEX,KAAK,OAAO,EACZ,KAAK,OAAO,EACb,MAAM,YAAY,CAAC;AAEpB;;;;GAIG;AACH,MAAM,MAAM,IAAI,CACd,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,WAAW,IACtD;IACF,wDAAwD;IACxD,IAAI,EAAE,MAAM,CAAC;CACd,GAAG,UAAU,CAAC;AAEf;;;;GAIG;AACH,MAAM,MAAM,MAAM,CAChB,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,WAAW,IACtD;IACF,iCAAiC;IACjC,IAAI,CAAC,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;CACzB,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,WAAW,CACrB,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,WAAW,EACxD,aAAa,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,WAAW,IACzD,CACF,GAAG,EAAE,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,aAAa,CAAC,KAE9C,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,QAAQ,GAAG,IAAI,GAAG,SAAS,CAAC,GACvD,IAAI,CAAC,UAAU,CAAC,GAChB,QAAQ,GACR,IAAI,GACJ,SAAS,CAAC;AAEd;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,YAAY,GACvB,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,WAAW,EACxD,aAAa,SAAS,MAAM,CAAC,MAAM,EA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AO,CAAC,GAAG,WAAW,EAC3D,+DAMC;IACD,mBAAmB,EAAE,CAAC,WAAW,EAAE;QACjC,QAAQ,EAAE,MAAM,CAAC;QACjB,QAAQ,EAAE,MAAM,CAAC;KAClB,KACG,OAAO,CAAC,OAAO,GAAG,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC,GAC9C,OAAO,GACP,QAAQ,GACR,IAAI,CAAC,UAAU,CAAC,CAAC;IACrB,IAAI,CAAC,EAAE,KAAK,GAAG,QAAQ,CAAC;IACxB,SAAS,CAAC,EAAE,GAAG,GAAG,GAAG,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,KAAG,WAAW,CAAC,UAAU,EAAE,aAAa,CAmCxC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-middlewares.js","sourceRoot":"","sources":["../src/auth-middlewares.ts"],"names":[],"mappings":";AAAA,0BAA0B;;;;;;;;;;;;AAE1B,6CAAkD;AAClD,2CAAqC;AACrC,
|
|
1
|
+
{"version":3,"file":"auth-middlewares.js","sourceRoot":"","sources":["../src/auth-middlewares.ts"],"names":[],"mappings":";AAAA,0BAA0B;;;;;;;;;;;;AAE1B,6CAAkD;AAClD,2CAAqC;AACrC,yCAMoB;AA4CpB;;;;;;;;;;;;;;GAcG;AACI,MAAM,YAAY,GAAG,CAG1B,EACA,SAAS,EACT,aAAa,GAAG,KAAK,EACrB,SAAS,GAAG,KAAK,EACjB,YAAY,GAAG,MAAM,GAMtB,EAA+C,EAAE;IAChD,MAAM,OAAO,GACX,YAAY,KAAK,MAAM;QACrB,CAAC,CAAC,CACE,IAAY,EACZ,OAAe,EACf,GAAW,EACX,GAAW,EACX,IAAmB,EACnB,EAAE,CACF,IAAA,iBAAI,EAAC,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,GAAG,KAAK,OAAO,EAAE,EAAE,GAAG,EAAE,kCAAO,IAAI,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BAAM,EAAC,GAAG,CAAC,CAAC;QACrB,IAAI,MAAM,GAAG,KAAK,CAAC;QACnB,IAAI,IAAI,KAAK,QAAQ,EAAE,CAAC;YACtB,IAAI,CAAC;gBACH,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;YACvB,CAAC;YAAC,WAAM,CAAC;gBACP,OAAO,IAAA,mBAAM,EAAC,GAAG,CAAC,CAAC;YACrB,CAAC;QACH,CAAC;QACD,MAAM,CAAC,QAAQ,EAAE,QAAQ,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;QACxD,IAAI,CAAC,QAAQ,IAAI,CAAC,QAAQ;YAAE,OAAO,IAAA,mBAAM,EAAC,GAAG,CAAC,CAAC;QAC/C,MAAM,WAAW,GAAG;YAClB,QAAQ;YACR,QAAQ;SACT,CAAC;QACF,MAAM,qBAAqB,GAAG,MAAM,mBAAmB,CAAC,WAAW,CAAC,CAAC;QACrE,IAAI,qBAAqB,YAAY,QAAQ,EAAE,CAAC;YAC9C,OAAO,qBAAqB,CAAC;QAC/B,CAAC;QACD,IAAI,CAAC,qBAAqB;YAAE,OAAO,IAAA,mBAAM,EAAC,GAAG,CAAC,CAAC;QAC/C,OAAO,qBAAqB,KAAK,IAAI;YACnC,CAAC,CAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,WAAW,EAAuB;YACvD,CAAC,CAAC,qBAAqB,CAAC;IAC5B,CAAC,CAAA,CAAC;AACJ,CAAC,CAAC;AA3DW,QAAA,eAAe,mBA2D1B"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { type Context, type Handler } from "./types.ts";
|
|
1
|
+
import { EmptyRecord, type Context, type Handler } from "./types.ts";
|
|
2
2
|
/**
|
|
3
3
|
* Represents an authenticated user.
|
|
4
4
|
*
|
|
5
5
|
* @template {Record<string, unknown>} ExtendAuth - Extend Auth Context
|
|
6
6
|
*/
|
|
7
|
-
export type Auth<ExtendAuth extends Record<string, unknown> =
|
|
7
|
+
export type Auth<ExtendAuth extends Record<string, unknown> = EmptyRecord> = {
|
|
8
8
|
/** Role assigned to the user (e.g., "admin", "user") */
|
|
9
9
|
role: string;
|
|
10
10
|
} & ExtendAuth;
|
|
@@ -13,7 +13,7 @@ export type Auth<ExtendAuth extends Record<string, unknown> = Record<string, nev
|
|
|
13
13
|
*
|
|
14
14
|
* @template {Record<string, unknown>} ExtendAuth - Extend Auth Context
|
|
15
15
|
*/
|
|
16
|
-
export type CTAuth<ExtendAuth extends Record<string, unknown> =
|
|
16
|
+
export type CTAuth<ExtendAuth extends Record<string, unknown> = EmptyRecord> = {
|
|
17
17
|
/** Authenticated user details */
|
|
18
18
|
auth?: Auth<ExtendAuth>;
|
|
19
19
|
};
|
|
@@ -23,7 +23,7 @@ export type CTAuth<ExtendAuth extends Record<string, unknown> = Record<string, n
|
|
|
23
23
|
* @template {Record<string, unknown>} ExtendAuth - Extend Auth Context
|
|
24
24
|
* @template {Record<string, unknown>} ExtendContext - Extend Router Context
|
|
25
25
|
*/
|
|
26
|
-
export type ParseAuthFn<ExtendAuth extends Record<string, unknown> =
|
|
26
|
+
export type ParseAuthFn<ExtendAuth extends Record<string, unknown> = EmptyRecord, ExtendContext extends Record<string, unknown> = EmptyRecord> = (ctx: Context<CTAuth<ExtendAuth> & ExtendContext>) => Promise<Auth<ExtendAuth> | Response | null | undefined> | Auth<ExtendAuth> | Response | null | undefined;
|
|
27
27
|
/**
|
|
28
28
|
* Middleware to authenticate a request.
|
|
29
29
|
*
|
|
@@ -39,7 +39,7 @@ export type ParseAuthFn<ExtendAuth extends Record<string, unknown> = Record<stri
|
|
|
39
39
|
* @returns {Handler<CTAuth<ExtendAuth> & ExtendContext>} A handler that sets `ctx.auth` if authentication succeeds,
|
|
40
40
|
* otherwise returns a `401 Unauthorized` or with error message if available response (unless `checkOnly` is true).
|
|
41
41
|
*/
|
|
42
|
-
export declare const authenticate: <ExtendAuth extends Record<string, unknown> =
|
|
42
|
+
export declare const authenticate: <ExtendAuth extends Record<string, unknown> = EmptyRecord, ExtendContext extends Record<string, unknown> = EmptyRecord>({ parseAuth, breakPipeline, checkOnly, responseType, }: {
|
|
43
43
|
parseAuth: ParseAuthFn<ExtendAuth, ExtendContext>;
|
|
44
44
|
breakPipeline?: boolean;
|
|
45
45
|
checkOnly?: boolean;
|
|
@@ -64,7 +64,7 @@ export declare const authenticate: <ExtendAuth extends Record<string, unknown> =
|
|
|
64
64
|
* Throws an error if `permissions` is set without `hasPermission`.
|
|
65
65
|
*
|
|
66
66
|
*/
|
|
67
|
-
export declare const authorize: <ExtendAuth extends Record<string, unknown> =
|
|
67
|
+
export declare const authorize: <ExtendAuth extends Record<string, unknown> = EmptyRecord, ExtendContext extends Record<string, unknown> = EmptyRecord>({ allowRole, forbidRole, permissions, hasPermission, responseType, breakPipeline, }: {
|
|
68
68
|
allowRole?: (role: string) => boolean;
|
|
69
69
|
forbidRole?: (role: string) => boolean;
|
|
70
70
|
permissions?: string[];
|
|
@@ -110,7 +110,7 @@ export declare const basicAuthParser: <ExtendAuth extends {
|
|
|
110
110
|
username: string;
|
|
111
111
|
} & Record<string, unknown> = {
|
|
112
112
|
username: string;
|
|
113
|
-
}, ExtendContext extends Record<string, unknown> =
|
|
113
|
+
}, ExtendContext extends Record<string, unknown> = EmptyRecord>({ validateCredentials, type, separator, realm, defaultRole, }: {
|
|
114
114
|
validateCredentials: (credentials: {
|
|
115
115
|
username: string;
|
|
116
116
|
password: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-middlewares.d.ts","sourceRoot":"","sources":["../../src/auth-middlewares.ts"],"names":[],"mappings":"AAIA,OAAO,
|
|
1
|
+
{"version":3,"file":"auth-middlewares.d.ts","sourceRoot":"","sources":["../../src/auth-middlewares.ts"],"names":[],"mappings":"AAIA,OAAO,EAEL,WAAW,EAEX,KAAK,OAAO,EACZ,KAAK,OAAO,EACb,MAAM,YAAY,CAAC;AAEpB;;;;GAIG;AACH,MA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xB,SAAS,CAAC,EAAE,GAAG,GAAG,GAAG,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,KAAG,WAAW,CAAC,UAAU,EAAE,aAAa,CAmCxC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-middlewares.js","sourceRoot":"","sources":["../../src/auth-middlewares.ts"],"names":[],"mappings":";AAAA,0BAA0B;;;;;;;;;;;;AAE1B,6CAAkD;AAClD,2CAAqC;AACrC,
|
|
1
|
+
{"version":3,"file":"auth-middlewares.js","sourceRoot":"","sources":["../../src/auth-middlewares.ts"],"names":[],"mappings":";AAAA,0BAA0B;;;;;;;;;;;;AAE1B,6CAAkD;AAClD,2CAAqC;AACrC,yCAMoB;AA4CpB;;;;;;;;;;;;;;GAcG;AACI,MAAM,YAAY,GAAG,CAG1B,EACA,SAAS,EACT,aAAa,GAAG,KAAK,EACrB,SAAS,GAAG,KAAK,EACjB,YAAY,GAAG,MAAM,GAMtB,EAA+C,EAAE;IAChD,MAAM,OAAO,GACX,YAAY,KAAK,MAAM;QACrB,CAAC,CAAC,CACE,IAAY,EACZ,OAAe,EACf,GAAW,EACX,GAAW,EACX,IAAmB,EACnB,EAAE,CACF,IAAA,iBAAI,EAAC,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,GAAG,KAAK,OAAO,EAAE,EAAE,GAAG,EAAE,kCAAO,IAAI,KAAE,MAAM,EAAE,IAAI,IAAG;QACzE,CAAC,CAAC,YAAY,KAAK,MAAM;YACvB,CAAC,CAAC,CACE,IAAY,EACZ,OAAe,EACf,GAAW,EACX,GAAW,EACX,IAAmB,EACnB,EAAE,CAAC,IAAA,iBAAI,EAAC,IAAI,GAAG,KAAK,GAAG,KAAK,OAAO,EAAE,kCAAO,IAAI,KAAE,MAAM,EAAE,IAAI,IAAG;YACrE,CAAC,CAAC,CACE,IAAY,EACZ,QAAmC,EACnC,IAAY,EACZ,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AG,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;QACxD,IAAI,CAAC,QAAQ,IAAI,CAAC,QAAQ;YAAE,OAAO,IAAA,mBAAM,EAAC,GAAG,CAAC,CAAC;QAC/C,MAAM,WAAW,GAAG;YAClB,QAAQ;YACR,QAAQ;SACT,CAAC;QACF,MAAM,qBAAqB,GAAG,MAAM,mBAAmB,CAAC,WAAW,CAAC,CAAC;QACrE,IAAI,qBAAqB,YAAY,QAAQ,EAAE,CAAC;YAC9C,OAAO,qBAAqB,CAAC;QAC/B,CAAC;QACD,IAAI,CAAC,qBAAqB;YAAE,OAAO,IAAA,mBAAM,EAAC,GAAG,CAAC,CAAC;QAC/C,OAAO,qBAAqB,KAAK,IAAI;YACnC,CAAC,CAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,WAAW,EAAuB;YACvD,CAAC,CAAC,qBAAqB,CAAC;IAC5B,CAAC,CAAA,CAAC;AACJ,CAAC,CAAC;AA3DW,QAAA,eAAe,mBA2D1B"}
|
package/dist/cjs/helpers.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ export declare const buildStrictTransportSecurity: (strictTransportSecurity: Str
|
|
|
19
19
|
* Defaults to 'text/plain; charset=utf-8' content-type if not provided in init.headers.
|
|
20
20
|
* @param {number} status - The HTTP status code
|
|
21
21
|
* @param {string|null} [content] - The response body content
|
|
22
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
22
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
23
23
|
* @returns {Response} A Response object
|
|
24
24
|
* @example
|
|
25
25
|
* status(200, "Success");
|
|
@@ -31,7 +31,7 @@ export declare const status: (status: number, content?: string | null, init?: Re
|
|
|
31
31
|
* Creates a redirect Response.
|
|
32
32
|
* Defaults to 302 Found unless another status is provided.
|
|
33
33
|
* @param {string} location - The URL to redirect to
|
|
34
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
34
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
35
35
|
* @returns {Response} A Response object with Location header
|
|
36
36
|
*/
|
|
37
37
|
export declare const redirect: (location: string, init?: ResponseInit) => Response;
|
|
@@ -87,7 +87,7 @@ export declare const forward: <ExtendContext extends Record<string, unknown> = R
|
|
|
87
87
|
* Creates a text/plain Response.
|
|
88
88
|
* Defaults to status 200 and 'text/plain; charset=utf-8' content-type if not specified.
|
|
89
89
|
* @param {string} content - The text content to return
|
|
90
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
90
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
91
91
|
* @returns {Response} A Response object with text/plain content-type
|
|
92
92
|
* @example
|
|
93
93
|
* text("Hello, world!");
|
|
@@ -98,7 +98,7 @@ export declare const text: (content: string, init?: ResponseInit) => Response;
|
|
|
98
98
|
* Creates an HTML Response.
|
|
99
99
|
* Defaults to status 200 and text/html content-type if not specified.
|
|
100
100
|
* @param {string} content - The HTML content to return
|
|
101
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
101
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
102
102
|
* @returns {Response} A Response object with text/html content-type
|
|
103
103
|
* @example
|
|
104
104
|
* html("<h1>Hello</h1>");
|
|
@@ -110,31 +110,33 @@ export declare const html: (content: string, init?: ResponseInit) => Response;
|
|
|
110
110
|
* Defaults to status 200 and 'application/json; charset=utf-8' content-type if not specified.
|
|
111
111
|
* Uses Response.json() internally which automatically serializes the body.
|
|
112
112
|
* @param {any} body - The data to serialize as JSON
|
|
113
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
113
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options.
|
|
114
|
+
* @param {boolean} [isStringified=false] - Do not stringify the body as it was already stringified.
|
|
114
115
|
* @returns {Response} A Response object with application/json content-type
|
|
115
116
|
* @example
|
|
116
117
|
* json({ message: "Success" }); // { "message": "Success" }
|
|
117
118
|
* json({ error: "Not found" }, { status: 404 }); // { "error": "Not found" }
|
|
118
119
|
*/
|
|
119
|
-
export declare const json: (body: any, init?: ResponseInit) => Response;
|
|
120
|
+
export declare const json: (body: any, init?: ResponseInit, isStringified?: boolean) => Response;
|
|
120
121
|
/**
|
|
121
122
|
* Creates an RJSON Response.
|
|
122
123
|
* Defaults to status 200 and 'application/rjson; charset=utf-8' content-type if not specified.
|
|
123
124
|
* Uses Response.json() internally which automatically serializes the body.
|
|
124
125
|
* @param {any} body - The data to serialize as RJSON
|
|
125
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
126
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options.
|
|
127
|
+
* @param {boolean} [isStringified=false] - Do not stringify the body as it was already stringified.
|
|
126
128
|
* @returns {Response} A Response object with application/rjson content-type
|
|
127
129
|
* @example
|
|
128
130
|
* rjson({ message: "Success" }); // (message:'Success')
|
|
129
131
|
* rjson({ error: "Not found" }, { status: 404 }); // (error:'Not found')
|
|
130
132
|
*/
|
|
131
|
-
export declare const rjson: (body: any, init?: ResponseInit) => Response;
|
|
133
|
+
export declare const rjson: (body: any, init?: ResponseInit, isStringified?: boolean) => Response;
|
|
132
134
|
/**
|
|
133
135
|
* Creates a Response from a Blob.
|
|
134
136
|
* Automatically sets content-type from blob.type or defaults to application/octet-stream.
|
|
135
137
|
* Also sets content-length header.
|
|
136
138
|
* @param {Blob} blob - The blob data to return
|
|
137
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
139
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
138
140
|
* @returns {Response} A Response object with appropriate content-type
|
|
139
141
|
* @example
|
|
140
142
|
* const blob = new Blob(["file content"], { type: "text/plain" });
|
|
@@ -146,9 +148,11 @@ export declare const blob: (blob: Blob, init?: ResponseInit) => Response;
|
|
|
146
148
|
* Forces octet-stream content-type.
|
|
147
149
|
* Also sets content-length header.
|
|
148
150
|
* @param {Blob|ArrayBuffer} octetStream - The blob data to return
|
|
149
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
151
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
150
152
|
* @returns {Response} A Response object with application/octet-stream content-type
|
|
151
153
|
* @example
|
|
154
|
+
* octetStream(Readable.toWeb(createReadStream(filepath)));
|
|
155
|
+
* @example
|
|
152
156
|
* const blob = new Blob([binaryData]);
|
|
153
157
|
* octetStream(blob);
|
|
154
158
|
*/
|
|
@@ -156,7 +160,7 @@ export declare const octetStream: (octet: Blob | ArrayBuffer | ReadableStream, i
|
|
|
156
160
|
/**
|
|
157
161
|
* Creates a Response from FormData.
|
|
158
162
|
* @param {FormData} [formData] - The form data to return
|
|
159
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
163
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
160
164
|
* @returns {Response} A Response object
|
|
161
165
|
* @example
|
|
162
166
|
* const form = new FormData();
|
|
@@ -167,7 +171,7 @@ export declare const formData: (formData?: FormData, init?: ResponseInit) => Res
|
|
|
167
171
|
/**
|
|
168
172
|
* Creates a Response from URLSearchParams with application/x-www-form-urlencoded content-type.
|
|
169
173
|
* @param {URLSearchParams} [usp] - The URL search parameters to return
|
|
170
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
174
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
171
175
|
* @returns {Response} A Response object with application/x-www-form-urlencoded content-type
|
|
172
176
|
* @example
|
|
173
177
|
* const params = new URLSearchParams({ q: "search term" });
|
|
@@ -178,7 +182,7 @@ export declare const usp: (usp?: URLSearchParams, init?: ResponseInit) => Respon
|
|
|
178
182
|
* Creates a Response from various body types with automatic content-type detection.
|
|
179
183
|
* Supports strings, objects (JSON), Blobs, ArrayBuffers, FormData, URLSearchParams, and ReadableStreams.
|
|
180
184
|
* @param {BodyInit|Record<string, unknown>} [body] - The body content to return
|
|
181
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
185
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
182
186
|
* @returns {Response} A Response object with appropriate content-type
|
|
183
187
|
* @example
|
|
184
188
|
* send("text"); // text/plain
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/helpers.ts"],"names":[],"mappings":"AAGA,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC,OAAO,KAAK,EACV,gCAAgC,EAChC,uCAAuC,EACvC,2BAA2B,EAC3B,2BAA2B,EAE3B,OAAO,EACP,UAAU,EACV,6BAA6B,EAC9B,MAAM,YAAY,CAAC;AAoBpB;;;;;GAKG;AACH,eAAO,MAAM,0BAA0B,GACrC,QACI,gCAAgC,GAChC,2BAA2B,GAC3B,CAAC,2BAA2B,GAAG,GAC5B,CAAC,IAAI,MAAM,IAAI,CAAC,SACb,MAAM,uCAAuC,GAC7C,2BAA2B,GAC3B,eAAe,GACf,KAAK,GACL,CAAC,GAAG,2BAA2B,GAAG,2BAA2B,EAAE,GACpE,CAAC,KACL,MA8BF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,4BAA4B,GACvC,yBAAyB,6BAA6B,KACrD,MAiBF,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,MAAM,GACjB,QAAQ,MAAM,EACd,UAAU,MAAM,GAAG,IAAI,EACvB,OAAO,YAAY,KAClB,QAYF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ,GAAI,UAAU,MAAM,EAAE,OAAO,YAAY,KAAG,QAWhE,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,GAC5B,UAAU,MAAM,EAChB,OAAO,IAAI,CAAC,YAAY,EAAE,QAAQ,GAAG,YAAY,CAAC,KACjD,QAWF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,GAC5B,UAAU,MAAM,EAChB,OAAO,IAAI,CAAC,YAAY,EAAE,QAAQ,GAAG,YAAY,CAAC,KACjD,QAWF,CAAC;AAEF;;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AAC,EAAE,QAAQ,GAAG,KAAK,GAAG,MAAM,CAAC;CACtC;AAED;;;GAGG;AACH,KAAK,WAAW,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAEpC;;;;;;;;;GASG;AACH,eAAO,MAAM,SAAS,GACpB,MAAM,MAAM,EACZ,OAAO,MAAM,EACb,UAAU,aAAa,KACtB,WAcF,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,WAAW,GACtB,MAAM,MAAM,EACZ,UAAU,aAAa,KACtB,WAgBF,CAAC"}
|
package/dist/cjs/helpers.js
CHANGED
|
@@ -95,7 +95,7 @@ exports.buildStrictTransportSecurity = buildStrictTransportSecurity;
|
|
|
95
95
|
* Defaults to 'text/plain; charset=utf-8' content-type if not provided in init.headers.
|
|
96
96
|
* @param {number} status - The HTTP status code
|
|
97
97
|
* @param {string|null} [content] - The response body content
|
|
98
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
98
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
99
99
|
* @returns {Response} A Response object
|
|
100
100
|
* @example
|
|
101
101
|
* status(200, "Success");
|
|
@@ -118,7 +118,7 @@ exports.status = status;
|
|
|
118
118
|
* Creates a redirect Response.
|
|
119
119
|
* Defaults to 302 Found unless another status is provided.
|
|
120
120
|
* @param {string} location - The URL to redirect to
|
|
121
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
121
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
122
122
|
* @returns {Response} A Response object with Location header
|
|
123
123
|
*/
|
|
124
124
|
const redirect = (location, init) => {
|
|
@@ -240,7 +240,7 @@ exports.forward = forward;
|
|
|
240
240
|
* Creates a text/plain Response.
|
|
241
241
|
* Defaults to status 200 and 'text/plain; charset=utf-8' content-type if not specified.
|
|
242
242
|
* @param {string} content - The text content to return
|
|
243
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
243
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
244
244
|
* @returns {Response} A Response object with text/plain content-type
|
|
245
245
|
* @example
|
|
246
246
|
* text("Hello, world!");
|
|
@@ -251,9 +251,8 @@ const text = (content, init) => {
|
|
|
251
251
|
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
252
252
|
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : (0, status_ts_1.getHttpStatusText)(status);
|
|
253
253
|
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
254
|
+
headers.set("Content-Type", "text/plain; charset=utf-8");
|
|
255
|
+
headers.set("Content-Length", String(content.length));
|
|
257
256
|
return new Response(content, Object.assign(Object.assign({}, init), { status,
|
|
258
257
|
statusText,
|
|
259
258
|
headers }));
|
|
@@ -263,7 +262,7 @@ exports.text = text;
|
|
|
263
262
|
* Creates an HTML Response.
|
|
264
263
|
* Defaults to status 200 and text/html content-type if not specified.
|
|
265
264
|
* @param {string} content - The HTML content to return
|
|
266
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
265
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
267
266
|
* @returns {Response} A Response object with text/html content-type
|
|
268
267
|
* @example
|
|
269
268
|
* html("<h1>Hello</h1>");
|
|
@@ -274,9 +273,8 @@ const html = (content, init) => {
|
|
|
274
273
|
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
275
274
|
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : (0, status_ts_1.getHttpStatusText)(status);
|
|
276
275
|
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
}
|
|
276
|
+
headers.set("Content-Type", "text/html; charset=utf-8");
|
|
277
|
+
headers.set("Content-Length", String(content.length));
|
|
280
278
|
return new Response(content, Object.assign(Object.assign({}, init), { status,
|
|
281
279
|
statusText,
|
|
282
280
|
headers }));
|
|
@@ -287,21 +285,22 @@ exports.html = html;
|
|
|
287
285
|
* Defaults to status 200 and 'application/json; charset=utf-8' content-type if not specified.
|
|
288
286
|
* Uses Response.json() internally which automatically serializes the body.
|
|
289
287
|
* @param {any} body - The data to serialize as JSON
|
|
290
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
288
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options.
|
|
289
|
+
* @param {boolean} [isStringified=false] - Do not stringify the body as it was already stringified.
|
|
291
290
|
* @returns {Response} A Response object with application/json content-type
|
|
292
291
|
* @example
|
|
293
292
|
* json({ message: "Success" }); // { "message": "Success" }
|
|
294
293
|
* json({ error: "Not found" }, { status: 404 }); // { "error": "Not found" }
|
|
295
294
|
*/
|
|
296
|
-
const json = (body, init) => {
|
|
295
|
+
const json = (body, init, isStringified) => {
|
|
297
296
|
var _a, _b;
|
|
297
|
+
const content = (isStringified ? body : JSON.stringify(body));
|
|
298
298
|
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
299
299
|
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : (0, status_ts_1.getHttpStatusText)(status);
|
|
300
300
|
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
return Response.json(body, Object.assign(Object.assign({}, init), { status,
|
|
301
|
+
headers.set("Content-Type", "application/json; charset=utf-8");
|
|
302
|
+
headers.set("Content-Length", String(content.length));
|
|
303
|
+
return new Response(content, Object.assign(Object.assign({}, init), { status,
|
|
305
304
|
statusText,
|
|
306
305
|
headers }));
|
|
307
306
|
};
|
|
@@ -311,21 +310,22 @@ exports.json = json;
|
|
|
311
310
|
* Defaults to status 200 and 'application/rjson; charset=utf-8' content-type if not specified.
|
|
312
311
|
* Uses Response.json() internally which automatically serializes the body.
|
|
313
312
|
* @param {any} body - The data to serialize as RJSON
|
|
314
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
313
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options.
|
|
314
|
+
* @param {boolean} [isStringified=false] - Do not stringify the body as it was already stringified.
|
|
315
315
|
* @returns {Response} A Response object with application/rjson content-type
|
|
316
316
|
* @example
|
|
317
317
|
* rjson({ message: "Success" }); // (message:'Success')
|
|
318
318
|
* rjson({ error: "Not found" }, { status: 404 }); // (error:'Not found')
|
|
319
319
|
*/
|
|
320
|
-
const rjson = (body, init) => {
|
|
320
|
+
const rjson = (body, init, isStringified) => {
|
|
321
321
|
var _a, _b;
|
|
322
|
+
const content = (isStringified ? body : rjson_1.default.stringify(body));
|
|
322
323
|
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
323
324
|
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : (0, status_ts_1.getHttpStatusText)(status);
|
|
324
325
|
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
}
|
|
328
|
-
return new Response(rjson_1.default.stringify(body), Object.assign(Object.assign({}, init), { status,
|
|
326
|
+
headers.set("Content-Type", "application/rjson; charset=utf-8");
|
|
327
|
+
headers.set("Content-Length", String(content.length));
|
|
328
|
+
return new Response(content, Object.assign(Object.assign({}, init), { status,
|
|
329
329
|
statusText,
|
|
330
330
|
headers }));
|
|
331
331
|
};
|
|
@@ -335,7 +335,7 @@ exports.rjson = rjson;
|
|
|
335
335
|
* Automatically sets content-type from blob.type or defaults to application/octet-stream.
|
|
336
336
|
* Also sets content-length header.
|
|
337
337
|
* @param {Blob} blob - The blob data to return
|
|
338
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
338
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
339
339
|
* @returns {Response} A Response object with appropriate content-type
|
|
340
340
|
* @example
|
|
341
341
|
* const blob = new Blob(["file content"], { type: "text/plain" });
|
|
@@ -346,10 +346,8 @@ const blob = (blob, init) => {
|
|
|
346
346
|
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
347
347
|
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : (0, status_ts_1.getHttpStatusText)(status);
|
|
348
348
|
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
}
|
|
352
|
-
headers.set("content-length", blob.size.toFixed());
|
|
349
|
+
headers.set("Content-Type", blob.type || "application/octet-stream");
|
|
350
|
+
headers.set("Content-Length", String(blob.size));
|
|
353
351
|
return new Response(blob, Object.assign(Object.assign({}, init), { status,
|
|
354
352
|
statusText,
|
|
355
353
|
headers }));
|
|
@@ -360,9 +358,11 @@ exports.blob = blob;
|
|
|
360
358
|
* Forces octet-stream content-type.
|
|
361
359
|
* Also sets content-length header.
|
|
362
360
|
* @param {Blob|ArrayBuffer} octetStream - The blob data to return
|
|
363
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
361
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
364
362
|
* @returns {Response} A Response object with application/octet-stream content-type
|
|
365
363
|
* @example
|
|
364
|
+
* octetStream(Readable.toWeb(createReadStream(filepath)));
|
|
365
|
+
* @example
|
|
366
366
|
* const blob = new Blob([binaryData]);
|
|
367
367
|
* octetStream(blob);
|
|
368
368
|
*/
|
|
@@ -371,11 +371,9 @@ const octetStream = (octet, init) => {
|
|
|
371
371
|
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
372
372
|
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : (0, status_ts_1.getHttpStatusText)(status);
|
|
373
373
|
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
374
|
-
|
|
375
|
-
headers.set("content-type", "application/octet-stream");
|
|
376
|
-
}
|
|
374
|
+
headers.set("Content-Type", "application/octet-stream");
|
|
377
375
|
if (!(octet instanceof ReadableStream)) {
|
|
378
|
-
headers.set("
|
|
376
|
+
headers.set("Content-Length", String((octet instanceof Blob ? octet.size : octet.byteLength)));
|
|
379
377
|
}
|
|
380
378
|
return new Response(octet, Object.assign(Object.assign({}, init), { status,
|
|
381
379
|
statusText,
|
|
@@ -385,7 +383,7 @@ exports.octetStream = octetStream;
|
|
|
385
383
|
/**
|
|
386
384
|
* Creates a Response from FormData.
|
|
387
385
|
* @param {FormData} [formData] - The form data to return
|
|
388
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
386
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
389
387
|
* @returns {Response} A Response object
|
|
390
388
|
* @example
|
|
391
389
|
* const form = new FormData();
|
|
@@ -403,7 +401,7 @@ exports.formData = formData;
|
|
|
403
401
|
/**
|
|
404
402
|
* Creates a Response from URLSearchParams with application/x-www-form-urlencoded content-type.
|
|
405
403
|
* @param {URLSearchParams} [usp] - The URL search parameters to return
|
|
406
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
404
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
407
405
|
* @returns {Response} A Response object with application/x-www-form-urlencoded content-type
|
|
408
406
|
* @example
|
|
409
407
|
* const params = new URLSearchParams({ q: "search term" });
|
|
@@ -414,9 +412,7 @@ const usp = (usp, init) => {
|
|
|
414
412
|
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
415
413
|
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : (0, status_ts_1.getHttpStatusText)(status);
|
|
416
414
|
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
417
|
-
|
|
418
|
-
headers.set("content-type", "application/x-www-form-urlencoded");
|
|
419
|
-
}
|
|
415
|
+
headers.set("Content-Type", "application/x-www-form-urlencoded");
|
|
420
416
|
return new Response(usp, Object.assign(Object.assign({}, init), { status,
|
|
421
417
|
statusText,
|
|
422
418
|
headers }));
|
|
@@ -426,7 +422,7 @@ exports.usp = usp;
|
|
|
426
422
|
* Creates a Response from various body types with automatic content-type detection.
|
|
427
423
|
* Supports strings, objects (JSON), Blobs, ArrayBuffers, FormData, URLSearchParams, and ReadableStreams.
|
|
428
424
|
* @param {BodyInit|Record<string, unknown>} [body] - The body content to return
|
|
429
|
-
* @param {ResponseInit} [init] - Additional response initialization options
|
|
425
|
+
* @param {ResponseInit} [init=undefined] - Additional response initialization options
|
|
430
426
|
* @returns {Response} A Response object with appropriate content-type
|
|
431
427
|
* @example
|
|
432
428
|
* send("text"); // text/plain
|
|
@@ -443,7 +439,7 @@ const send = (body, init) => {
|
|
|
443
439
|
const isContentTypeNotSet = !headers.has("content-type");
|
|
444
440
|
if (body instanceof URLSearchParams) {
|
|
445
441
|
if (isContentTypeNotSet) {
|
|
446
|
-
headers.set("
|
|
442
|
+
headers.set("Content-Type", "application/x-www-form-urlencoded");
|
|
447
443
|
}
|
|
448
444
|
}
|
|
449
445
|
else if (body instanceof FormData) {
|
|
@@ -451,26 +447,35 @@ const send = (body, init) => {
|
|
|
451
447
|
}
|
|
452
448
|
else if (typeof body === "string") {
|
|
453
449
|
if (isContentTypeNotSet) {
|
|
454
|
-
headers.set("
|
|
450
|
+
headers.set("Content-Type", "text/plain; charset=utf-8");
|
|
455
451
|
}
|
|
452
|
+
headers.set("Content-Length", String(body.length));
|
|
456
453
|
}
|
|
457
454
|
else if (body instanceof Blob) {
|
|
458
455
|
if (isContentTypeNotSet) {
|
|
459
|
-
headers.set("
|
|
456
|
+
headers.set("Content-Type", body.type || "application/octet-stream");
|
|
460
457
|
}
|
|
458
|
+
headers.set("Content-Length", String(body.size));
|
|
461
459
|
}
|
|
462
460
|
else if (body instanceof ArrayBuffer ||
|
|
463
|
-
ArrayBuffer.isView(body)
|
|
464
|
-
|
|
461
|
+
ArrayBuffer.isView(body)) {
|
|
462
|
+
if (isContentTypeNotSet) {
|
|
463
|
+
headers.set("Content-Type", "application/octet-stream");
|
|
464
|
+
}
|
|
465
|
+
headers.set("Content-Length", String(body.byteLength));
|
|
466
|
+
}
|
|
467
|
+
else if (body instanceof ReadableStream) {
|
|
465
468
|
if (isContentTypeNotSet) {
|
|
466
|
-
headers.set("
|
|
469
|
+
headers.set("Content-Type", "application/octet-stream");
|
|
467
470
|
}
|
|
468
471
|
}
|
|
469
472
|
else if (body != null) {
|
|
470
473
|
if (isContentTypeNotSet) {
|
|
471
|
-
headers.set("
|
|
474
|
+
headers.set("Content-Type", "application/json; charset=utf-8");
|
|
472
475
|
}
|
|
473
|
-
|
|
476
|
+
const content = JSON.stringify(body);
|
|
477
|
+
headers.set("Content-Length", String(content.length));
|
|
478
|
+
return new Response(content, Object.assign(Object.assign({}, init), { status,
|
|
474
479
|
statusText,
|
|
475
480
|
headers }));
|
|
476
481
|
}
|