@arkstack/http 0.5.2 → 0.5.3

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/setup.d.ts CHANGED
@@ -1 +1,11 @@
1
- export { };
1
+ import { C as RequestHelper, D as SessionHelper, S as RedirectHelper, x as OldHelper, yt as Response } from "./index-C-YhZgaG.js";
2
+ import { User } from "@app/models/User";
3
+
4
+ //#region src/setup.d.ts
5
+ declare global {
6
+ var session: SessionHelper;
7
+ var request: RequestHelper<User>;
8
+ var response: () => Response;
9
+ var redirect: RedirectHelper;
10
+ var old: OldHelper;
11
+ }
package/dist/setup.js CHANGED
@@ -1,9 +1,13 @@
1
- import { n as Request$1, t as Response$1 } from "./Response-DxFGT4o4.js";
1
+ import { F as Response$1, I as Request$1, a as kanunSessionPlugin, i as arkstackHttpPlugin, j as old, t as redirect } from "./redirect-CsrtLK0U.js";
2
2
  import { CoreRouter } from "clear-router";
3
+ import { Validator } from "kanun";
4
+ import "dotenv/config";
3
5
  //#region src/setup.ts
4
6
  CoreRouter.setRequestProvider(Request$1);
5
7
  CoreRouter.setResponseProvider(Response$1);
8
+ Validator.use(kanunSessionPlugin);
9
+ CoreRouter.use(arkstackHttpPlugin);
10
+ globalThis.redirect = redirect;
11
+ globalThis.old = old;
6
12
  //#endregion
7
13
  export {};
8
-
9
- //# sourceMappingURL=setup.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkstack/http",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "type": "module",
5
5
  "description": "HTTP module for Arkstack, providing framework-agnostic request and response primitives.",
6
6
  "homepage": "https://arkstack.toneflix.net/guide/http",
@@ -28,7 +28,14 @@
28
28
  "./package.json": "./package.json"
29
29
  },
30
30
  "dependencies": {
31
- "clear-router": "^2.6.4"
31
+ "clear-router": "^2.9.0",
32
+ "dotenv": "^17.4.2",
33
+ "@arkstack/contract": "^0.5.3",
34
+ "@arkstack/common": "^0.5.3"
35
+ },
36
+ "peerDependencies": {
37
+ "arkormx": "^2.10.1",
38
+ "kanun": "^1.2.0"
32
39
  },
33
40
  "scripts": {
34
41
  "build": "tsdown --config-loader unrun",
@@ -1,143 +0,0 @@
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
@@ -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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["../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"}