@arkstack/http 0.3.15

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Toneflix Technologies Limited
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # @arkstack/http
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.
6
+
7
+ ```ts
8
+ import { Request, Response } from '@arkstack/http';
9
+
10
+ const request = Request.from({
11
+ headers: {
12
+ authorization: 'Bearer token',
13
+ },
14
+ method: 'GET',
15
+ path: '/account',
16
+ });
17
+
18
+ const token = request.bearerToken();
19
+
20
+ const response = new Response().status(200).json({ ok: true });
21
+ ```
22
+
23
+ Runtime-specific handlers should continue to use their native framework request and response objects. Driver middleware can translate those objects into `@arkstack/http` wrappers when calling shared services.
@@ -0,0 +1,80 @@
1
+ //#region src/types/Http.d.ts
2
+ type HeaderValue = string | string[] | number | boolean | null | undefined;
3
+ type HeaderMap = Record<string, string>;
4
+ type HeaderSource = Headers | Record<string, HeaderValue>;
5
+ type RequestSource<TUser = unknown> = {
6
+ headers?: HeaderSource;
7
+ method?: string;
8
+ url?: string;
9
+ originalUrl?: string;
10
+ path?: string;
11
+ ip?: string;
12
+ user?: TUser;
13
+ authToken?: string;
14
+ req?: RequestSource<TUser>;
15
+ request?: RequestSource<TUser>;
16
+ };
17
+ type ResponseSource = {
18
+ statusCode?: number;
19
+ status?: number | ((code: number) => unknown);
20
+ headers?: HeaderSource;
21
+ setHeader?: (name: string, value: string) => unknown;
22
+ json?: (body: unknown) => unknown;
23
+ send?: (body: unknown) => unknown;
24
+ };
25
+ type RequestOptions<TUser = unknown> = {
26
+ headers?: HeaderSource;
27
+ method?: string;
28
+ url?: string;
29
+ path?: string;
30
+ ip?: string | null;
31
+ user?: TUser;
32
+ authToken?: string;
33
+ source?: unknown;
34
+ };
35
+ //#endregion
36
+ //#region src/Request.d.ts
37
+ declare class Request<TUser = unknown> {
38
+ readonly headers: HeaderMap;
39
+ readonly method?: string;
40
+ readonly url?: string;
41
+ readonly path?: string;
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 | undefined;
49
+ bearerToken(): string | null;
50
+ setUser(user: TUser): this;
51
+ }
52
+ //#endregion
53
+ //#region src/Response.d.ts
54
+ declare class Response<TBody = unknown> {
55
+ statusCode: number;
56
+ readonly headers: HeaderMap;
57
+ body?: TBody;
58
+ readonly source?: unknown;
59
+ constructor(options?: {
60
+ statusCode?: number;
61
+ headers?: HeaderSource;
62
+ body?: TBody;
63
+ source?: unknown;
64
+ });
65
+ static from<TBody = unknown>(source?: Response<TBody> | ResponseSource): Response<TBody> | undefined;
66
+ status(code: number): this;
67
+ header(name: string, value: string): this;
68
+ json(body: TBody): any;
69
+ send(body: TBody): any;
70
+ }
71
+ //#endregion
72
+ //#region src/helpers.d.ts
73
+ declare const unwrapRequestSource: <TUser>(source: RequestSource<TUser>) => RequestSource<TUser>;
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, normalizeHeaderValue, normalizeHeaders, unwrapRequestSource };
80
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,139 @@
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
@@ -0,0 +1 @@
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,w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AO,OAAO;AAGvB,SAAO;;;;;;;;;;;ACnEf,IAAa,WAAb,MAAa,SAA0B;CACnC;CACA,AAAS;CACT;CACA,AAAS;CAET,YAAY,UAKR,EAAE,EAAE;AACJ,OAAK,aAAa,QAAQ,cAAc;AACxC,OAAK,UAAU,iBAAiB,QAAQ,QAAQ;AAChD,OAAK,OAAO,QAAQ;AACpB,OAAK,SAAS,QAAQ;;CAG1B,OAAO,KACH,QAC2B;AAC3B,MAAI,CAAC,OACD;AAGJ,MAAI,kBAAkB,SAClB,QAAO;AAGX,SAAO,IAAI,SAAgB;GACvB,YAAY,OAAO,OAAO,WAAW,WAAW,OAAO,SAAS,OAAO;GACvE,SAAS,OAAO;GAChB;GACH,CAAC;;CAGN,OAAQ,MAAc;AAClB,OAAK,aAAa;AAElB,MAAI,SAAS,KAAK,OAAO,CACrB,KAAI,OAAO,KAAK,OAAO,WAAW,WAC9B,MAAK,OAAO,OAAO,KAAK;MAExB,MAAK,OAAO,aAAa;AAIjC,SAAO;;CAGX,OAAQ,MAAc,OAAe;AACjC,OAAK,QAAQ,KAAK,aAAa,IAAI;AAEnC,MAAI,SAAS,KAAK,OAAO,IAAI,OAAO,KAAK,OAAO,cAAc,WAC1D,MAAK,OAAO,UAAU,MAAM,MAAM;AAGtC,SAAO;;CAGX,KAAM,MAAa;AACf,OAAK,OAAO;AAEZ,MAAI,SAAS,KAAK,OAAO,IAAI,OAAO,KAAK,OAAO,SAAS,WACrD,QAAO,KAAK,OAAO,KAAK,KAAK;AAGjC,SAAO;;CAGX,KAAM,MAAa;AACf,OAAK,OAAO;AAEZ,MAAI,SAAS,KAAK,OAAO,IAAI,OAAO,KAAK,OAAO,SAAS,WACrD,QAAO,KAAK,OAAO,KAAK,KAAK;AAGjC,SAAO"}
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@arkstack/http",
3
+ "version": "0.3.15",
4
+ "type": "module",
5
+ "description": "Framework-agnostic HTTP request and response primitives for Arkstack.",
6
+ "homepage": "https://arkstack.toneflix.net",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/arkstack-hq/arkstack.git",
10
+ "directory": "packages/http"
11
+ },
12
+ "keywords": [
13
+ "http",
14
+ "request",
15
+ "response",
16
+ "adapter",
17
+ "arkstack"
18
+ ],
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "exports": {
26
+ ".": "./dist/index.js",
27
+ "./package.json": "./package.json"
28
+ },
29
+ "scripts": {
30
+ "build": "tsdown --config-loader unconfig",
31
+ "version:patch": "pnpm version patch"
32
+ }
33
+ }