@arkstack/http 0.4.3 → 0.5.1

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 CHANGED
@@ -1,8 +1,6 @@
1
1
  # @arkstack/http
2
2
 
3
- Framework-neutral HTTP request and response wrappers for Arkstack shared packages.
4
-
5
- Use this package when code needs request data but should not import Express, H3, or another runtime directly.
3
+ HTTP module for Arkstack, providing framework-agnostic request and response primitives.
6
4
 
7
5
  ```ts
8
6
  import { Request, Response } from '@arkstack/http';
@@ -0,0 +1,143 @@
1
+ import { Request, Response } from "clear-router";
2
+ //#region src/helpers.ts
3
+ const unwrapRequestSource = (source) => {
4
+ if (source.headers) return source;
5
+ if (source.req) return source.req;
6
+ if (source.request) return source.request;
7
+ return source;
8
+ };
9
+ const makeHeaders = (headers) => {
10
+ return new Headers(normalizeHeaders(headers));
11
+ };
12
+ const normalizeHeaders = (headers) => {
13
+ const normalized = {};
14
+ if (!headers) return normalized;
15
+ if (isHeaders(headers)) {
16
+ headers.forEach((value, key) => {
17
+ normalized[key.toLowerCase()] = value;
18
+ });
19
+ return normalized;
20
+ }
21
+ for (const [key, value] of Object.entries(headers)) {
22
+ const normalizedValue = normalizeHeaderValue(value);
23
+ if (typeof normalizedValue === "string") normalized[key.toLowerCase()] = normalizedValue;
24
+ }
25
+ return normalized;
26
+ };
27
+ const normalizeHeaderValue = (value) => {
28
+ if (Array.isArray(value)) return value.join(", ");
29
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
30
+ return value ?? void 0;
31
+ };
32
+ const isHeaders = (value) => typeof Headers !== "undefined" && value instanceof Headers;
33
+ const isRecord = (value) => typeof value === "object" && value !== null;
34
+ //#endregion
35
+ //#region src/Request.ts
36
+ /**
37
+ * Represents an HTTP request, providing a consistent interface for accessing request data.
38
+ *
39
+ * @author 3m1n3nc3
40
+ */
41
+ var Request$1 = class Request$1 extends Request {
42
+ headers;
43
+ ip;
44
+ source;
45
+ user;
46
+ authToken;
47
+ constructor(options = {}) {
48
+ super(options);
49
+ this.headers = normalizeHeaders(options.headers);
50
+ if (this.method) this.method = options.method;
51
+ if (this.url) this.url = options.url;
52
+ if (this.path) this.path = options.path;
53
+ this.ip = options.ip ?? null;
54
+ this.user = options.user;
55
+ this.authToken = options.authToken;
56
+ this.source = options.source;
57
+ }
58
+ static from(source) {
59
+ if (!source) return;
60
+ if (source instanceof Request$1) return source;
61
+ const request = unwrapRequestSource(source);
62
+ return new Request$1({
63
+ headers: request.headers,
64
+ method: request.method,
65
+ url: request.originalUrl ?? request.url,
66
+ path: request.path,
67
+ ip: request.ip ?? null,
68
+ user: request.user,
69
+ authToken: request.authToken,
70
+ source
71
+ });
72
+ }
73
+ header(name) {
74
+ return this.headers[name.toLowerCase()];
75
+ }
76
+ bearerToken() {
77
+ const authorization = this.header("authorization");
78
+ if (!authorization?.startsWith("Bearer ")) return null;
79
+ return authorization.substring(7);
80
+ }
81
+ setUser(user) {
82
+ this.user = user;
83
+ if (isRecord(this.source)) this.source.user = user;
84
+ return this;
85
+ }
86
+ };
87
+ //#endregion
88
+ //#region src/Response.ts
89
+ /**
90
+ * Represents an HTTP response, providing a consistent interface for accessing response data.
91
+ *
92
+ * @author 3m1n3nc3
93
+ */
94
+ var Response$1 = class Response$1 extends Response {
95
+ body;
96
+ source;
97
+ constructor(options = {}) {
98
+ super({
99
+ body: options.body,
100
+ headers: makeHeaders(options.headers),
101
+ statusCode: options.statusCode ?? 200
102
+ });
103
+ this.body = options.body ?? {};
104
+ this.source = options.source;
105
+ }
106
+ static from(source) {
107
+ if (!source) return;
108
+ if (source instanceof Response$1) return source;
109
+ return new Response$1({
110
+ statusCode: typeof source.status === "number" ? source.status : source.statusCode,
111
+ headers: source.headers,
112
+ source
113
+ });
114
+ }
115
+ status(code) {
116
+ this.statusCode = code;
117
+ if (isRecord(this.source)) if (typeof this.source.status === "function") this.source.status(code);
118
+ else this.source.statusCode = code;
119
+ return this;
120
+ }
121
+ header(name, value) {
122
+ this.headers.set(name.toLowerCase(), value);
123
+ if (isRecord(this.source) && typeof this.source.setHeader === "function") this.source.setHeader(name, value);
124
+ return this;
125
+ }
126
+ getHeaders() {
127
+ return normalizeHeaders(this.headers);
128
+ }
129
+ json(body) {
130
+ this.body = body;
131
+ if (isRecord(this.source) && typeof this.source.json === "function") return this.source.json(body);
132
+ return body;
133
+ }
134
+ send(body) {
135
+ this.body = body;
136
+ if (isRecord(this.source) && typeof this.source.send === "function") return this.source.send(body);
137
+ return body;
138
+ }
139
+ };
140
+ //#endregion
141
+ 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
+
143
+ //# sourceMappingURL=Response-DxFGT4o4.js.map
@@ -0,0 +1 @@
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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Q;;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"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import { Request as Request$1, Response as Response$1 } from "clear-router";
2
+ import { RequestData } from "clear-router/types/basic";
3
+
1
4
  //#region src/types/Http.d.ts
2
5
  type HeaderValue = string | string[] | number | boolean | null | undefined;
3
6
  type HeaderMap = Record<string, string>;
@@ -34,27 +37,22 @@ type RequestOptions<TUser = unknown> = {
34
37
  };
35
38
  //#endregion
36
39
  //#region src/Request.d.ts
37
- declare class Request<TUser = unknown> {
40
+ declare class Request<TUser = unknown> extends Request$1 {
38
41
  readonly headers: HeaderMap;
39
- readonly method?: string;
40
- readonly url?: string;
41
- readonly path?: string;
42
42
  readonly ip: string | null;
43
43
  readonly source?: unknown;
44
44
  user?: TUser;
45
45
  authToken?: string;
46
46
  constructor(options?: RequestOptions<TUser>);
47
47
  static from<TUser = unknown>(source?: Request<TUser> | RequestSource<TUser>): Request<TUser> | undefined;
48
- header(name: string): string | undefined;
48
+ header(name: string): string;
49
49
  bearerToken(): string | null;
50
50
  setUser(user: TUser): this;
51
51
  }
52
52
  //#endregion
53
53
  //#region src/Response.d.ts
54
- declare class Response<TBody = unknown> {
55
- statusCode: number;
56
- readonly headers: HeaderMap;
57
- body?: TBody;
54
+ declare class Response<TBody = unknown> extends Response$1 {
55
+ body: TBody;
58
56
  readonly source?: unknown;
59
57
  constructor(options?: {
60
58
  statusCode?: number;
@@ -62,19 +60,21 @@ declare class Response<TBody = unknown> {
62
60
  body?: TBody;
63
61
  source?: unknown;
64
62
  });
65
- static from<TBody = unknown>(source?: Response<TBody> | ResponseSource): Response<TBody> | undefined;
63
+ static from<TBody extends RequestData = RequestData>(source?: Response<TBody> | ResponseSource): Response<TBody> | undefined;
66
64
  status(code: number): this;
67
65
  header(name: string, value: string): this;
66
+ getHeaders(): HeaderMap;
68
67
  json(body: TBody): any;
69
68
  send(body: TBody): any;
70
69
  }
71
70
  //#endregion
72
71
  //#region src/helpers.d.ts
73
72
  declare const unwrapRequestSource: <TUser>(source: RequestSource<TUser>) => RequestSource<TUser>;
73
+ declare const makeHeaders: (headers?: HeaderSource) => Headers;
74
74
  declare const normalizeHeaders: (headers?: HeaderSource) => HeaderMap;
75
75
  declare const normalizeHeaderValue: (value: HeaderValue) => string | undefined;
76
76
  declare const isHeaders: (value: unknown) => value is Headers;
77
77
  declare const isRecord: (value: unknown) => value is Record<string, any>;
78
78
  //#endregion
79
- export { HeaderMap, HeaderSource, HeaderValue, Request, RequestOptions, RequestSource, Response, ResponseSource, isHeaders, isRecord, normalizeHeaderValue, normalizeHeaders, unwrapRequestSource };
79
+ export { HeaderMap, HeaderSource, HeaderValue, Request, RequestOptions, RequestSource, Response, ResponseSource, isHeaders, isRecord, makeHeaders, normalizeHeaderValue, normalizeHeaders, unwrapRequestSource };
80
80
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,139 +1,2 @@
1
- //#region src/helpers.ts
2
- const unwrapRequestSource = (source) => {
3
- if (source.headers) return source;
4
- if (source.req) return source.req;
5
- if (source.request) return source.request;
6
- return source;
7
- };
8
- const normalizeHeaders = (headers) => {
9
- const normalized = {};
10
- if (!headers) return normalized;
11
- if (isHeaders(headers)) {
12
- headers.forEach((value, key) => {
13
- normalized[key.toLowerCase()] = value;
14
- });
15
- return normalized;
16
- }
17
- for (const [key, value] of Object.entries(headers)) {
18
- const normalizedValue = normalizeHeaderValue(value);
19
- if (typeof normalizedValue === "string") normalized[key.toLowerCase()] = normalizedValue;
20
- }
21
- return normalized;
22
- };
23
- const normalizeHeaderValue = (value) => {
24
- if (Array.isArray(value)) return value.join(", ");
25
- if (typeof value === "number" || typeof value === "boolean") return String(value);
26
- return value ?? void 0;
27
- };
28
- const isHeaders = (value) => typeof Headers !== "undefined" && value instanceof Headers;
29
- const isRecord = (value) => typeof value === "object" && value !== null;
30
-
31
- //#endregion
32
- //#region src/Request.ts
33
- /**
34
- * Represents an HTTP request, providing a consistent interface for accessing request data.
35
- *
36
- * @author 3m1n3nc3
37
- */
38
- var Request = class Request {
39
- headers;
40
- method;
41
- url;
42
- path;
43
- ip;
44
- source;
45
- user;
46
- authToken;
47
- constructor(options = {}) {
48
- this.headers = normalizeHeaders(options.headers);
49
- this.method = options.method;
50
- this.url = options.url;
51
- this.path = options.path;
52
- this.ip = options.ip ?? null;
53
- this.user = options.user;
54
- this.authToken = options.authToken;
55
- this.source = options.source;
56
- }
57
- static from(source) {
58
- if (!source) return;
59
- if (source instanceof Request) return source;
60
- const request = unwrapRequestSource(source);
61
- return new Request({
62
- headers: request.headers,
63
- method: request.method,
64
- url: request.originalUrl ?? request.url,
65
- path: request.path,
66
- ip: request.ip ?? null,
67
- user: request.user,
68
- authToken: request.authToken,
69
- source
70
- });
71
- }
72
- header(name) {
73
- return this.headers[name.toLowerCase()];
74
- }
75
- bearerToken() {
76
- const authorization = this.header("authorization");
77
- if (!authorization?.startsWith("Bearer ")) return null;
78
- return authorization.substring(7);
79
- }
80
- setUser(user) {
81
- this.user = user;
82
- if (isRecord(this.source)) this.source.user = user;
83
- return this;
84
- }
85
- };
86
-
87
- //#endregion
88
- //#region src/Response.ts
89
- /**
90
- * Represents an HTTP response, providing a consistent interface for accessing response data.
91
- *
92
- * @author 3m1n3nc3
93
- */
94
- var Response = class Response {
95
- statusCode;
96
- headers;
97
- body;
98
- source;
99
- constructor(options = {}) {
100
- this.statusCode = options.statusCode ?? 200;
101
- this.headers = normalizeHeaders(options.headers);
102
- this.body = options.body;
103
- this.source = options.source;
104
- }
105
- static from(source) {
106
- if (!source) return;
107
- if (source instanceof Response) return source;
108
- return new Response({
109
- statusCode: typeof source.status === "number" ? source.status : source.statusCode,
110
- headers: source.headers,
111
- source
112
- });
113
- }
114
- status(code) {
115
- this.statusCode = code;
116
- if (isRecord(this.source)) if (typeof this.source.status === "function") this.source.status(code);
117
- else this.source.statusCode = code;
118
- return this;
119
- }
120
- header(name, value) {
121
- this.headers[name.toLowerCase()] = value;
122
- if (isRecord(this.source) && typeof this.source.setHeader === "function") this.source.setHeader(name, value);
123
- return this;
124
- }
125
- json(body) {
126
- this.body = body;
127
- if (isRecord(this.source) && typeof this.source.json === "function") return this.source.json(body);
128
- return body;
129
- }
130
- send(body) {
131
- this.body = body;
132
- if (isRecord(this.source) && typeof this.source.send === "function") return this.source.send(body);
133
- return body;
134
- }
135
- };
136
-
137
- //#endregion
138
- export { Request, Response, isHeaders, isRecord, normalizeHeaderValue, normalizeHeaders, unwrapRequestSource };
139
- //# sourceMappingURL=index.js.map
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-DxFGT4o4.js";
2
+ export { Request, Response, isHeaders, isRecord, makeHeaders, normalizeHeaderValue, normalizeHeaders, unwrapRequestSource };
@@ -0,0 +1 @@
1
+ export { };
package/dist/setup.js ADDED
@@ -0,0 +1,9 @@
1
+ import { n as Request$1, t as Response$1 } from "./Response-DxFGT4o4.js";
2
+ import { CoreRouter } from "clear-router";
3
+ //#region src/setup.ts
4
+ CoreRouter.setRequestProvider(Request$1);
5
+ CoreRouter.setResponseProvider(Response$1);
6
+ //#endregion
7
+ export {};
8
+
9
+ //# sourceMappingURL=setup.js.map
@@ -0,0 +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":";;;AAIA,WAAW,mBAAmBA,UAAiB;AAC/C,WAAW,oBAAoBC,WAAAA"}
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@arkstack/http",
3
- "version": "0.4.3",
3
+ "version": "0.5.1",
4
4
  "type": "module",
5
- "description": "Framework-agnostic HTTP request and response primitives for Arkstack.",
5
+ "description": "HTTP module for Arkstack, providing framework-agnostic request and response primitives.",
6
6
  "homepage": "https://arkstack.toneflix.net/guide/http",
7
7
  "repository": {
8
8
  "type": "git",
@@ -24,10 +24,14 @@
24
24
  },
25
25
  "exports": {
26
26
  ".": "./dist/index.js",
27
+ "./setup": "./dist/setup.js",
27
28
  "./package.json": "./package.json"
28
29
  },
30
+ "dependencies": {
31
+ "clear-router": "^2.6.4"
32
+ },
29
33
  "scripts": {
30
- "build": "tsdown --config-loader unconfig",
34
+ "build": "tsdown --config-loader unrun",
31
35
  "version:patch": "pnpm version patch"
32
36
  }
33
37
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/types/Http.ts","../src/Request.ts","../src/Response.ts","../src/helpers.ts"],"mappings":";KAAY,WAAA;AAAA,KACA,SAAA,GAAY,MAAA;AAAA,KACZ,YAAA,GAAe,OAAA,GAAU,MAAA,SAAe,WAAA;AAAA,KAExC,aAAA;EACR,OAAA,GAAU,YAAA;EACV,MAAA;EACA,GAAA;EACA,WAAA;EACA,IAAA;EACA,EAAA;EACA,IAAA,GAAO,KAAA;EACP,SAAA;EACA,GAAA,GAAM,aAAA,CAAc,KAAA;EACpB,OAAA,GAAU,aAAA,CAAc,KAAA;AAAA;AAAA,KAGhB,cAAA;EACR,UAAA;EACA,MAAA,cAAoB,IAAA;EACpB,OAAA,GAAU,YAAA;EACV,SAAA,IAAa,IAAA,UAAc,KAAA;EAC3B,IAAA,IAAQ,IAAA;EACR,IAAA,IAAQ,IAAA;AAAA;AAAA,KAGA,cAAA;EACR,OAAA,GAAU,YAAA;EACV,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1 +0,0 @@
1
- {"version":3,"file":"index.js","names":[],"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 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\n/**\n * Represents an HTTP request, providing a consistent interface for accessing request data.\n * \n * @author 3m1n3nc3\n */\nexport class Request<TUser = unknown> {\n readonly headers: HeaderMap\n readonly method?: string\n readonly url?: string\n readonly path?: string\n readonly ip: string | null\n readonly source?: unknown\n user?: TUser\n authToken?: string\n\n constructor(options: RequestOptions<TUser> = {}) {\n this.headers = normalizeHeaders(options.headers)\n this.method = options.method\n this.url = options.url\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 | undefined {\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 { HeaderMap, HeaderSource, ResponseSource } from './types/Http'\nimport { isRecord, normalizeHeaders } from './helpers'\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> {\n statusCode: number\n readonly headers: HeaderMap\n body?: TBody\n readonly source?: unknown\n\n constructor(options: {\n statusCode?: number;\n headers?: HeaderSource;\n body?: TBody;\n source?: unknown;\n } = {}) {\n this.statusCode = options.statusCode ?? 200\n this.headers = normalizeHeaders(options.headers)\n this.body = options.body\n this.source = options.source\n }\n\n static from<TBody = unknown> (\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[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 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;AACvB,KAAI,OAAO,QACP,QAAO;AAGX,KAAI,OAAO,IACP,QAAO,OAAO;AAGlB,KAAI,OAAO,QACP,QAAO,OAAO;AAGlB,QAAO;;AAGX,MAAa,oBAAoB,YAAsC;CACnE,MAAM,aAAwB,EAAE;AAEhC,KAAI,CAAC,QACD,QAAO;AAGX,KAAI,UAAU,QAAQ,EAAE;AACpB,UAAQ,SAAS,OAAO,QAAQ;AAC5B,cAAW,IAAI,aAAa,IAAI;IAClC;AAEF,SAAO;;AAGX,MAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,QAAQ,EAAE;EAChD,MAAM,kBAAkB,qBAAqB,MAAM;AAEnD,MAAI,OAAO,oBAAoB,SAC3B,YAAW,IAAI,aAAa,IAAI;;AAIxC,QAAO;;AAGX,MAAa,wBAAwB,UAAuB;AACxD,KAAI,MAAM,QAAQ,MAAM,CACpB,QAAO,MAAM,KAAK,KAAK;AAG3B,KAAI,OAAO,UAAU,YAAY,OAAO,UAAU,UAC9C,QAAO,OAAO,MAAM;AAGxB,QAAO,SAAS;;AAGpB,MAAa,aAAa,UACtB,OAAO,YAAY,eAAe,iBAAiB;AAGvD,MAAa,YAAY,UACrB,OAAO,UAAU,YAAY,UAAU;;;;;;;;;ACvD3C,IAAa,UAAb,MAAa,QAAyB;CAClC,AAAS;CACT,AAAS;CACT,AAAS;CACT,AAAS;CACT,AAAS;CACT,AAAS;CACT;CACA;CAEA,YAAY,UAAiC,EAAE,EAAE;AAC7C,OAAK,UAAU,iBAAiB,QAAQ,QAAQ;AAChD,OAAK,SAAS,QAAQ;AACtB,OAAK,MAAM,QAAQ;A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