@arkstack/http 0.7.15 → 0.7.17
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/{Response-DxFGT4o4.js → Response-BJ056_6U.js} +3 -1
- package/dist/Response-BJ056_6U.js.map +1 -0
- package/dist/index-3Je4lNvZ.d.ts +84 -0
- package/dist/index.d.ts +2 -80
- package/dist/index.js +1 -1
- package/dist/setup.d.ts +8 -1
- package/dist/setup.js +1 -1
- package/dist/setup.js.map +1 -1
- package/package.json +1 -1
- package/dist/Response-DxFGT4o4.js.map +0 -1
|
@@ -54,6 +54,7 @@ var Request$1 = class Request$1 extends Request {
|
|
|
54
54
|
this.user = options.user;
|
|
55
55
|
this.authToken = options.authToken;
|
|
56
56
|
this.source = options.source;
|
|
57
|
+
globalThis.request = (key) => key ? this.input(key) : this;
|
|
57
58
|
}
|
|
58
59
|
static from(source) {
|
|
59
60
|
if (!source) return;
|
|
@@ -102,6 +103,7 @@ var Response$1 = class Response$1 extends Response {
|
|
|
102
103
|
});
|
|
103
104
|
this.body = options.body ?? {};
|
|
104
105
|
this.source = options.source;
|
|
106
|
+
globalThis.response = () => this;
|
|
105
107
|
}
|
|
106
108
|
static from(source) {
|
|
107
109
|
if (!source) return;
|
|
@@ -140,4 +142,4 @@ var Response$1 = class Response$1 extends Response {
|
|
|
140
142
|
//#endregion
|
|
141
143
|
export { makeHeaders as a, unwrapRequestSource as c, isRecord as i, Request$1 as n, normalizeHeaderValue as o, isHeaders as r, normalizeHeaders as s, Response$1 as t };
|
|
142
144
|
|
|
143
|
-
//# sourceMappingURL=Response-
|
|
145
|
+
//# sourceMappingURL=Response-BJ056_6U.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Response-BJ056_6U.js","names":["Request","BaseRequest","Response","BaseResponse"],"sources":["../src/helpers.ts","../src/Request.ts","../src/Response.ts"],"sourcesContent":["import { HeaderMap, HeaderSource, HeaderValue, RequestSource } from './types/Http'\n\nexport const unwrapRequestSource = <TUser> (\n source: RequestSource<TUser>\n): RequestSource<TUser> => {\n if (source.headers) {\n return source\n }\n\n if (source.req) {\n return source.req\n }\n\n if (source.request) {\n return source.request\n }\n\n return source\n}\n\nexport const makeHeaders = (headers?: HeaderSource) => {\n return new Headers(normalizeHeaders(headers))\n}\n\nexport const normalizeHeaders = (headers?: HeaderSource): HeaderMap => {\n const normalized: HeaderMap = {}\n\n if (!headers) {\n return normalized\n }\n\n if (isHeaders(headers)) {\n headers.forEach((value, key) => {\n normalized[key.toLowerCase()] = value\n })\n\n return normalized\n }\n\n for (const [key, value] of Object.entries(headers)) {\n const normalizedValue = normalizeHeaderValue(value)\n\n if (typeof normalizedValue === 'string') {\n normalized[key.toLowerCase()] = normalizedValue\n }\n }\n\n return normalized\n}\n\nexport const normalizeHeaderValue = (value: HeaderValue) => {\n if (Array.isArray(value)) {\n return value.join(', ')\n }\n\n if (typeof value === 'number' || typeof value === 'boolean') {\n return String(value)\n }\n\n return value ?? undefined\n}\n\nexport const isHeaders = (value: unknown): value is Headers => (\n typeof Headers !== 'undefined' && value instanceof Headers\n)\n\nexport const isRecord = (value: unknown): value is Record<string, any> => (\n typeof value === 'object' && value !== null\n)\n","import { HeaderMap, RequestOptions, RequestSource } from './types/Http'\nimport { isRecord, normalizeHeaders, unwrapRequestSource } from './helpers'\n\nimport { Request as BaseRequest } from 'clear-router'\n\n/**\n * Represents an HTTP request, providing a consistent interface for accessing request data.\n * \n * @author 3m1n3nc3\n */\nexport class Request<TUser = unknown> extends BaseRequest {\n readonly headers: HeaderMap\n readonly ip: string | null\n readonly source?: unknown\n user?: TUser\n authToken?: string\n\n constructor(options: RequestOptions<TUser> = {}) {\n super(options)\n\n this.headers = normalizeHeaders(options.headers)\n if (this.method)\n this.method = options.method!\n if (this.url)\n this.url = options.url!\n if (this.path)\n this.path = options.path!\n this.ip = options.ip ?? null\n this.user = options.user\n this.authToken = options.authToken\n this.source = options.source\n\n globalThis.request = (key?: string) => key ? this.input(key) : this\n }\n\n static from<TUser = unknown> (\n source?: Request<TUser> | RequestSource<TUser>\n ): Request<TUser> | undefined {\n if (!source) {\n return undefined\n }\n\n if (source instanceof Request) {\n return source\n }\n\n const request = unwrapRequestSource(source)\n\n return new Request<TUser>({\n headers: request.headers,\n method: request.method,\n url: request.originalUrl ?? request.url,\n path: request.path,\n ip: request.ip ?? null,\n user: request.user,\n authToken: request.authToken,\n source,\n })\n }\n\n header (name: string): string {\n return this.headers[name.toLowerCase()]\n }\n\n bearerToken (): string | null {\n const authorization = this.header('authorization')\n\n if (!authorization?.startsWith('Bearer ')) {\n return null\n }\n\n return authorization.substring(7)\n }\n\n setUser (user: TUser) {\n this.user = user\n\n if (isRecord(this.source)) {\n this.source.user = user\n }\n\n return this\n }\n}","import { HeaderSource, ResponseSource } from './types/Http'\nimport { isRecord, makeHeaders, normalizeHeaders } from './helpers'\n\nimport { Response as BaseResponse } from 'clear-router'\nimport { RequestData } from 'clear-router/types/basic'\n\n/**\n * Represents an HTTP response, providing a consistent interface for accessing response data.\n * \n * @author 3m1n3nc3\n */\nexport class Response<TBody = unknown> extends BaseResponse {\n override body: TBody\n readonly source?: unknown\n\n constructor(options: {\n statusCode?: number;\n headers?: HeaderSource;\n body?: TBody;\n source?: unknown;\n } = {}) {\n super({\n body: options.body,\n headers: makeHeaders(options.headers),\n statusCode: options.statusCode ?? 200,\n })\n\n this.body = options.body ?? {} as TBody\n this.source = options.source\n\n globalThis.response = () => this\n }\n\n static from<TBody extends RequestData = RequestData> (\n source?: Response<TBody> | ResponseSource\n ): Response<TBody> | undefined {\n if (!source) {\n return undefined\n }\n\n if (source instanceof Response) {\n return source\n }\n\n return new Response<TBody>({\n statusCode: typeof source.status === 'number' ? source.status : source.statusCode,\n headers: source.headers,\n source,\n })\n }\n\n status (code: number) {\n this.statusCode = code\n\n if (isRecord(this.source)) {\n if (typeof this.source.status === 'function') {\n this.source.status(code)\n } else {\n this.source.statusCode = code\n }\n }\n\n return this\n }\n\n header (name: string, value: string) {\n this.headers.set(name.toLowerCase(), value)\n\n if (isRecord(this.source) && typeof this.source.setHeader === 'function') {\n this.source.setHeader(name, value)\n }\n\n return this\n }\n\n getHeaders () {\n return normalizeHeaders(this.headers)\n }\n\n json (body: TBody) {\n this.body = body\n\n if (isRecord(this.source) && typeof this.source.json === 'function') {\n return this.source.json(body)\n }\n\n return body\n }\n\n send (body: TBody) {\n this.body = body\n\n if (isRecord(this.source) && typeof this.source.send === 'function') {\n return this.source.send(body)\n }\n\n return body\n }\n}"],"mappings":";;AAEA,MAAa,uBACT,WACuB;CACvB,IAAI,OAAO,SACP,OAAO;CAGX,IAAI,OAAO,KACP,OAAO,OAAO;CAGlB,IAAI,OAAO,SACP,OAAO,OAAO;CAGlB,OAAO;;AAGX,MAAa,eAAe,YAA2B;CACnD,OAAO,IAAI,QAAQ,iBAAiB,QAAQ,CAAC;;AAGjD,MAAa,oBAAoB,YAAsC;CACnE,MAAM,aAAwB,EAAE;CAEhC,IAAI,CAAC,SACD,OAAO;CAGX,IAAI,UAAU,QAAQ,EAAE;EACpB,QAAQ,SAAS,OAAO,QAAQ;GAC5B,WAAW,IAAI,aAAa,IAAI;IAClC;EAEF,OAAO;;CAGX,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,QAAQ,EAAE;EAChD,MAAM,kBAAkB,qBAAqB,MAAM;EAEnD,IAAI,OAAO,oBAAoB,UAC3B,WAAW,IAAI,aAAa,IAAI;;CAIxC,OAAO;;AAGX,MAAa,wBAAwB,UAAuB;CACxD,IAAI,MAAM,QAAQ,MAAM,EACpB,OAAO,MAAM,KAAK,KAAK;CAG3B,IAAI,OAAO,UAAU,YAAY,OAAO,UAAU,WAC9C,OAAO,OAAO,MAAM;CAGxB,OAAO,SAAS,KAAA;;AAGpB,MAAa,aAAa,UACtB,OAAO,YAAY,eAAe,iBAAiB;AAGvD,MAAa,YAAY,UACrB,OAAO,UAAU,YAAY,UAAU;;;;;;;;ACzD3C,I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AQ,cAAc;GACrC,CAAC;EAEF,KAAK,OAAO,QAAQ,QAAQ,EAAE;EAC9B,KAAK,SAAS,QAAQ;EAEtB,WAAW,iBAAiB;;CAGhC,OAAO,KACH,QAC2B;EAC3B,IAAI,CAAC,QACD;EAGJ,IAAI,kBAAkBD,YAClB,OAAO;EAGX,OAAO,IAAIA,WAAgB;GACvB,YAAY,OAAO,OAAO,WAAW,WAAW,OAAO,SAAS,OAAO;GACvE,SAAS,OAAO;GAChB;GACH,CAAC;;CAGN,OAAQ,MAAc;EAClB,KAAK,aAAa;EAElB,IAAI,SAAS,KAAK,OAAO,EACrB,IAAI,OAAO,KAAK,OAAO,WAAW,YAC9B,KAAK,OAAO,OAAO,KAAK;OAExB,KAAK,OAAO,aAAa;EAIjC,OAAO;;CAGX,OAAQ,MAAc,OAAe;EACjC,KAAK,QAAQ,IAAI,KAAK,aAAa,EAAE,MAAM;EAE3C,IAAI,SAAS,KAAK,OAAO,IAAI,OAAO,KAAK,OAAO,cAAc,YAC1D,KAAK,OAAO,UAAU,MAAM,MAAM;EAGtC,OAAO;;CAGX,aAAc;EACV,OAAO,iBAAiB,KAAK,QAAQ;;CAGzC,KAAM,MAAa;EACf,KAAK,OAAO;EAEZ,IAAI,SAAS,KAAK,OAAO,IAAI,OAAO,KAAK,OAAO,SAAS,YACrD,OAAO,KAAK,OAAO,KAAK,KAAK;EAGjC,OAAO;;CAGX,KAAM,MAAa;EACf,KAAK,OAAO;EAEZ,IAAI,SAAS,KAAK,OAAO,IAAI,OAAO,KAAK,OAAO,SAAS,YACrD,OAAO,KAAK,OAAO,KAAK,KAAK;EAGjC,OAAO"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Request, Response } from "clear-router";
|
|
2
|
+
import { RequestData } from "clear-router/types/basic";
|
|
3
|
+
|
|
4
|
+
//#region src/types/Http.d.ts
|
|
5
|
+
type HeaderValue = string | string[] | number | boolean | null | undefined;
|
|
6
|
+
type HeaderMap = Record<string, string>;
|
|
7
|
+
type HeaderSource = Headers | Record<string, HeaderValue>;
|
|
8
|
+
type RequestSource<TUser = unknown> = {
|
|
9
|
+
headers?: HeaderSource;
|
|
10
|
+
method?: string;
|
|
11
|
+
url?: string;
|
|
12
|
+
originalUrl?: string;
|
|
13
|
+
path?: string;
|
|
14
|
+
ip?: string;
|
|
15
|
+
user?: TUser;
|
|
16
|
+
authToken?: string;
|
|
17
|
+
req?: RequestSource<TUser>;
|
|
18
|
+
request?: RequestSource<TUser>;
|
|
19
|
+
};
|
|
20
|
+
type ResponseSource = {
|
|
21
|
+
statusCode?: number;
|
|
22
|
+
status?: number | ((code: number) => unknown);
|
|
23
|
+
headers?: HeaderSource;
|
|
24
|
+
setHeader?: (name: string, value: string) => unknown;
|
|
25
|
+
json?: (body: unknown) => unknown;
|
|
26
|
+
send?: (body: unknown) => unknown;
|
|
27
|
+
};
|
|
28
|
+
type RequestOptions<TUser = unknown> = {
|
|
29
|
+
headers?: HeaderSource;
|
|
30
|
+
method?: string;
|
|
31
|
+
url?: string;
|
|
32
|
+
path?: string;
|
|
33
|
+
ip?: string | null;
|
|
34
|
+
user?: TUser;
|
|
35
|
+
authToken?: string;
|
|
36
|
+
source?: unknown;
|
|
37
|
+
};
|
|
38
|
+
interface RequestHelper<TUser = unknown> {
|
|
39
|
+
(): Request$1;
|
|
40
|
+
<X extends string>(key: X): Request$1<TUser>['body'][X];
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/Request.d.ts
|
|
44
|
+
declare class Request$1<TUser = unknown> extends Request {
|
|
45
|
+
readonly headers: HeaderMap;
|
|
46
|
+
readonly ip: string | null;
|
|
47
|
+
readonly source?: unknown;
|
|
48
|
+
user?: TUser;
|
|
49
|
+
authToken?: string;
|
|
50
|
+
constructor(options?: RequestOptions<TUser>);
|
|
51
|
+
static from<TUser = unknown>(source?: Request$1<TUser> | RequestSource<TUser>): Request$1<TUser> | undefined;
|
|
52
|
+
header(name: string): string;
|
|
53
|
+
bearerToken(): string | null;
|
|
54
|
+
setUser(user: TUser): this;
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/Response.d.ts
|
|
58
|
+
declare class Response$1<TBody = unknown> extends Response {
|
|
59
|
+
body: TBody;
|
|
60
|
+
readonly source?: unknown;
|
|
61
|
+
constructor(options?: {
|
|
62
|
+
statusCode?: number;
|
|
63
|
+
headers?: HeaderSource;
|
|
64
|
+
body?: TBody;
|
|
65
|
+
source?: unknown;
|
|
66
|
+
});
|
|
67
|
+
static from<TBody extends RequestData = RequestData>(source?: Response$1<TBody> | ResponseSource): Response$1<TBody> | undefined;
|
|
68
|
+
status(code: number): this;
|
|
69
|
+
header(name: string, value: string): this;
|
|
70
|
+
getHeaders(): HeaderMap;
|
|
71
|
+
json(body: TBody): any;
|
|
72
|
+
send(body: TBody): any;
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region src/helpers.d.ts
|
|
76
|
+
declare const unwrapRequestSource: <TUser>(source: RequestSource<TUser>) => RequestSource<TUser>;
|
|
77
|
+
declare const makeHeaders: (headers?: HeaderSource) => Headers;
|
|
78
|
+
declare const normalizeHeaders: (headers?: HeaderSource) => HeaderMap;
|
|
79
|
+
declare const normalizeHeaderValue: (value: HeaderValue) => string | undefined;
|
|
80
|
+
declare const isHeaders: (value: unknown) => value is Headers;
|
|
81
|
+
declare const isRecord: (value: unknown) => value is Record<string, any>;
|
|
82
|
+
//#endregion
|
|
83
|
+
export { normalizeHeaders as a, Request$1 as c, HeaderValue as d, RequestHelper as f, ResponseSource as h, normalizeHeaderValue as i, HeaderMap as l, RequestSource as m, isRecord as n, unwrapRequestSource as o, RequestOptions as p, makeHeaders as r, Response$1 as s, isHeaders as t, HeaderSource as u };
|
|
84
|
+
//# sourceMappingURL=index-3Je4lNvZ.d.ts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,80 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
//#region src/types/Http.d.ts
|
|
5
|
-
type HeaderValue = string | string[] | number | boolean | null | undefined;
|
|
6
|
-
type HeaderMap = Record<string, string>;
|
|
7
|
-
type HeaderSource = Headers | Record<string, HeaderValue>;
|
|
8
|
-
type RequestSource<TUser = unknown> = {
|
|
9
|
-
headers?: HeaderSource;
|
|
10
|
-
method?: string;
|
|
11
|
-
url?: string;
|
|
12
|
-
originalUrl?: string;
|
|
13
|
-
path?: string;
|
|
14
|
-
ip?: string;
|
|
15
|
-
user?: TUser;
|
|
16
|
-
authToken?: string;
|
|
17
|
-
req?: RequestSource<TUser>;
|
|
18
|
-
request?: RequestSource<TUser>;
|
|
19
|
-
};
|
|
20
|
-
type ResponseSource = {
|
|
21
|
-
statusCode?: number;
|
|
22
|
-
status?: number | ((code: number) => unknown);
|
|
23
|
-
headers?: HeaderSource;
|
|
24
|
-
setHeader?: (name: string, value: string) => unknown;
|
|
25
|
-
json?: (body: unknown) => unknown;
|
|
26
|
-
send?: (body: unknown) => unknown;
|
|
27
|
-
};
|
|
28
|
-
type RequestOptions<TUser = unknown> = {
|
|
29
|
-
headers?: HeaderSource;
|
|
30
|
-
method?: string;
|
|
31
|
-
url?: string;
|
|
32
|
-
path?: string;
|
|
33
|
-
ip?: string | null;
|
|
34
|
-
user?: TUser;
|
|
35
|
-
authToken?: string;
|
|
36
|
-
source?: unknown;
|
|
37
|
-
};
|
|
38
|
-
//#endregion
|
|
39
|
-
//#region src/Request.d.ts
|
|
40
|
-
declare class Request<TUser = unknown> extends Request$1 {
|
|
41
|
-
readonly headers: HeaderMap;
|
|
42
|
-
readonly ip: string | null;
|
|
43
|
-
readonly source?: unknown;
|
|
44
|
-
user?: TUser;
|
|
45
|
-
authToken?: string;
|
|
46
|
-
constructor(options?: RequestOptions<TUser>);
|
|
47
|
-
static from<TUser = unknown>(source?: Request<TUser> | RequestSource<TUser>): Request<TUser> | undefined;
|
|
48
|
-
header(name: string): string;
|
|
49
|
-
bearerToken(): string | null;
|
|
50
|
-
setUser(user: TUser): this;
|
|
51
|
-
}
|
|
52
|
-
//#endregion
|
|
53
|
-
//#region src/Response.d.ts
|
|
54
|
-
declare class Response<TBody = unknown> extends Response$1 {
|
|
55
|
-
body: TBody;
|
|
56
|
-
readonly source?: unknown;
|
|
57
|
-
constructor(options?: {
|
|
58
|
-
statusCode?: number;
|
|
59
|
-
headers?: HeaderSource;
|
|
60
|
-
body?: TBody;
|
|
61
|
-
source?: unknown;
|
|
62
|
-
});
|
|
63
|
-
static from<TBody extends RequestData = RequestData>(source?: Response<TBody> | ResponseSource): Response<TBody> | undefined;
|
|
64
|
-
status(code: number): this;
|
|
65
|
-
header(name: string, value: string): this;
|
|
66
|
-
getHeaders(): HeaderMap;
|
|
67
|
-
json(body: TBody): any;
|
|
68
|
-
send(body: TBody): any;
|
|
69
|
-
}
|
|
70
|
-
//#endregion
|
|
71
|
-
//#region src/helpers.d.ts
|
|
72
|
-
declare const unwrapRequestSource: <TUser>(source: RequestSource<TUser>) => RequestSource<TUser>;
|
|
73
|
-
declare const makeHeaders: (headers?: HeaderSource) => Headers;
|
|
74
|
-
declare const normalizeHeaders: (headers?: HeaderSource) => HeaderMap;
|
|
75
|
-
declare const normalizeHeaderValue: (value: HeaderValue) => string | undefined;
|
|
76
|
-
declare const isHeaders: (value: unknown) => value is Headers;
|
|
77
|
-
declare const isRecord: (value: unknown) => value is Record<string, any>;
|
|
78
|
-
//#endregion
|
|
79
|
-
export { HeaderMap, HeaderSource, HeaderValue, Request, RequestOptions, RequestSource, Response, ResponseSource, isHeaders, isRecord, makeHeaders, normalizeHeaderValue, normalizeHeaders, unwrapRequestSource };
|
|
80
|
-
//# sourceMappingURL=index.d.ts.map
|
|
1
|
+
import { a as normalizeHeaders, c as Request, d as HeaderValue, f as RequestHelper, h as ResponseSource, i as normalizeHeaderValue, l as HeaderMap, m as RequestSource, n as isRecord, o as unwrapRequestSource, p as RequestOptions, r as makeHeaders, s as Response, t as isHeaders, u as HeaderSource } from "./index-3Je4lNvZ.js";
|
|
2
|
+
export { HeaderMap, HeaderSource, HeaderValue, Request, RequestHelper, RequestOptions, RequestSource, Response, ResponseSource, isHeaders, isRecord, makeHeaders, normalizeHeaderValue, normalizeHeaders, unwrapRequestSource };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as makeHeaders, c as unwrapRequestSource, i as isRecord, n as Request, o as normalizeHeaderValue, r as isHeaders, s as normalizeHeaders, t as Response } from "./Response-
|
|
1
|
+
import { a as makeHeaders, c as unwrapRequestSource, i as isRecord, n as Request, o as normalizeHeaderValue, r as isHeaders, s as normalizeHeaders, t as Response } from "./Response-BJ056_6U.js";
|
|
2
2
|
export { Request, Response, isHeaders, isRecord, makeHeaders, normalizeHeaderValue, normalizeHeaders, unwrapRequestSource };
|
package/dist/setup.d.ts
CHANGED
package/dist/setup.js
CHANGED
package/dist/setup.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setup.js","names":["Request","Response"],"sources":["../src/setup.ts"],"sourcesContent":["import { CoreRouter } from 'clear-router'\nimport { Request } from './Request'\nimport { Response } from './Response'\n\nCoreRouter.setRequestProvider(Request as never)\nCoreRouter.setResponseProvider(Response)"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"setup.js","names":["Request","Response"],"sources":["../src/setup.ts"],"sourcesContent":["import { CoreRouter } from 'clear-router'\nimport { Request } from './Request'\nimport type { RequestHelper } from './types/Http'\nimport { Response } from './Response'\n\ndeclare global {\n var request: RequestHelper\n var response: () => Response\n}\n\nCoreRouter.setRequestProvider(Request as never)\nCoreRouter.setResponseProvider(Response)\n"],"mappings":";;;AAUA,WAAW,mBAAmBA,UAAiB;AAC/C,WAAW,oBAAoBC,WAAS"}
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Response-DxFGT4o4.js","names":["Request","BaseRequest","Response","BaseResponse"],"sources":["../src/helpers.ts","../src/Request.ts","../src/Response.ts"],"sourcesContent":["import { HeaderMap, HeaderSource, HeaderValue, RequestSource } from './types/Http'\n\nexport const unwrapRequestSource = <TUser> (\n source: RequestSource<TUser>\n): RequestSource<TUser> => {\n if (source.headers) {\n return source\n }\n\n if (source.req) {\n return source.req\n }\n\n if (source.request) {\n return source.request\n }\n\n return source\n}\n\nexport const makeHeaders = (headers?: HeaderSource) => {\n return new Headers(normalizeHeaders(headers))\n}\n\nexport const normalizeHeaders = (headers?: HeaderSource): HeaderMap => {\n const normalized: HeaderMap = {}\n\n if (!headers) {\n return normalized\n }\n\n if (isHeaders(headers)) {\n headers.forEach((value, key) => {\n normalized[key.toLowerCase()] = value\n })\n\n return normalized\n }\n\n for (const [key, value] of Object.entries(headers)) {\n const normalizedValue = normalizeHeaderValue(value)\n\n if (typeof normalizedValue === 'string') {\n normalized[key.toLowerCase()] = normalizedValue\n }\n }\n\n return normalized\n}\n\nexport const normalizeHeaderValue = (value: HeaderValue) => {\n if (Array.isArray(value)) {\n return value.join(', ')\n }\n\n if (typeof value === 'number' || typeof value === 'boolean') {\n return String(value)\n }\n\n return value ?? undefined\n}\n\nexport const isHeaders = (value: unknown): value is Headers => (\n typeof Headers !== 'undefined' && value instanceof Headers\n)\n\nexport const isRecord = (value: unknown): value is Record<string, any> => (\n typeof value === 'object' && value !== null\n)\n","import { HeaderMap, RequestOptions, RequestSource } from './types/Http'\nimport { isRecord, normalizeHeaders, unwrapRequestSource } from './helpers'\n\nimport { Request as BaseRequest } from 'clear-router'\n\n/**\n * Represents an HTTP request, providing a consistent interface for accessing request data.\n * \n * @author 3m1n3nc3\n */\nexport class Request<TUser = unknown> extends BaseRequest {\n readonly headers: HeaderMap\n readonly ip: string | null\n readonly source?: unknown\n user?: TUser\n authToken?: string\n\n constructor(options: RequestOptions<TUser> = {}) {\n super(options)\n\n this.headers = normalizeHeaders(options.headers)\n if (this.method)\n this.method = options.method!\n if (this.url)\n this.url = options.url!\n if (this.path)\n this.path = options.path!\n this.ip = options.ip ?? null\n this.user = options.user\n this.authToken = options.authToken\n this.source = options.source\n }\n\n static from<TUser = unknown> (\n source?: Request<TUser> | RequestSource<TUser>\n ): Request<TUser> | undefined {\n if (!source) {\n return undefined\n }\n\n if (source instanceof Request) {\n return source\n }\n\n const request = unwrapRequestSource(source)\n\n return new Request<TUser>({\n headers: request.headers,\n method: request.method,\n url: request.originalUrl ?? request.url,\n path: request.path,\n ip: request.ip ?? null,\n user: request.user,\n authToken: request.authToken,\n source,\n })\n }\n\n header (name: string): string {\n return this.headers[name.toLowerCase()]\n }\n\n bearerToken (): string | null {\n const authorization = this.header('authorization')\n\n if (!authorization?.startsWith('Bearer ')) {\n return null\n }\n\n return authorization.substring(7)\n }\n\n setUser (user: TUser) {\n this.user = user\n\n if (isRecord(this.source)) {\n this.source.user = user\n }\n\n return this\n }\n}","import { HeaderSource, ResponseSource } from './types/Http'\nimport { isRecord, makeHeaders, normalizeHeaders } from './helpers'\n\nimport { Response as BaseResponse } from 'clear-router'\nimport { RequestData } from 'clear-router/types/basic'\n\n/**\n * Represents an HTTP response, providing a consistent interface for accessing response data.\n * \n * @author 3m1n3nc3\n */\nexport class Response<TBody = unknown> extends BaseResponse {\n override body: TBody\n readonly source?: unknown\n\n constructor(options: {\n statusCode?: number;\n headers?: HeaderSource;\n body?: TBody;\n source?: unknown;\n } = {}) {\n super({\n body: options.body,\n headers: makeHeaders(options.headers),\n statusCode: options.statusCode ?? 200,\n })\n\n this.body = options.body ?? {} as TBody\n this.source = options.source\n }\n\n static from<TBody extends RequestData = RequestData> (\n source?: Response<TBody> | ResponseSource\n ): Response<TBody> | undefined {\n if (!source) {\n return undefined\n }\n\n if (source instanceof Response) {\n return source\n }\n\n return new Response<TBody>({\n statusCode: typeof source.status === 'number' ? source.status : source.statusCode,\n headers: source.headers,\n source,\n })\n }\n\n status (code: number) {\n this.statusCode = code\n\n if (isRecord(this.source)) {\n if (typeof this.source.status === 'function') {\n this.source.status(code)\n } else {\n this.source.statusCode = code\n }\n }\n\n return this\n }\n\n header (name: string, value: string) {\n this.headers.set(name.toLowerCase(), value)\n\n if (isRecord(this.source) && typeof this.source.setHeader === 'function') {\n this.source.setHeader(name, value)\n }\n\n return this\n }\n\n getHeaders () {\n return normalizeHeaders(this.headers)\n }\n\n json (body: TBody) {\n this.body = body\n\n if (isRecord(this.source) && typeof this.source.json === 'function') {\n return this.source.json(body)\n }\n\n return body\n }\n\n send (body: TBody) {\n this.body = body\n\n if (isRecord(this.source) && typeof this.source.send === 'function') {\n return this.source.send(body)\n }\n\n return body\n }\n}"],"mappings":";;AAEA,MAAa,uBACT,WACuB;CACvB,IAAI,OAAO,SACP,OAAO;CAGX,IAAI,OAAO,KACP,OAAO,OAAO;CAGlB,IAAI,OAAO,SACP,OAAO,OAAO;CAGlB,OAAO;;AAGX,MAAa,eAAe,YAA2B;CACnD,OAAO,IAAI,QAAQ,iBAAiB,QAAQ,CAAC;;AAGjD,MAAa,oBAAoB,YAAsC;CACnE,MAAM,aAAwB,EAAE;CAEhC,IAAI,CAAC,SACD,OAAO;CAGX,IAAI,UAAU,QAAQ,EAAE;EACpB,QAAQ,SAAS,OAAO,QAAQ;GAC5B,WAAW,IAAI,aAAa,IAAI;IAClC;EAEF,OAAO;;CAGX,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,QAAQ,EAAE;EAChD,MAAM,kBAAkB,qBAAqB,MAAM;EAEnD,IAAI,OAAO,oBAAoB,UAC3B,WAAW,IAAI,aAAa,IAAI;;CAIxC,OAAO;;AAGX,MAAa,wBAAwB,UAAuB;CACxD,IAAI,MAAM,QAAQ,MAAM,EACpB,OAAO,MAAM,KAAK,KAAK;CAG3B,IAAI,OAAO,UAAU,YAAY,OAAO,UAAU,WAC9C,OAAO,OAAO,MAAM;CAGxB,OAAO,SAAS,KAAA;;AAGpB,MAAa,aAAa,UACtB,OAAO,YAAY,eAAe,iBAAiB;AAGvD,MAAa,YAAY,UACrB,OAAO,UAAU,YAAY,UAAU;;;;;;;;ACzD3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