@arkstack/http 0.8.0 → 0.9.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/dist/app.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { ErrorBag } from '.'
2
+
3
+ declare module 'clear-router' {
4
+ interface ClearHttpContext {
5
+ errors: ErrorBag
6
+ }
7
+ }
@@ -0,0 +1,363 @@
1
+ import * as _$clear_router0 from "clear-router";
2
+ import { Request, Response } from "clear-router";
3
+ import * as _$kanun from "kanun";
4
+ import { RequestData } from "clear-router/types/basic";
5
+
6
+ //#region src/Response.d.ts
7
+ declare class Response$1<TBody = unknown> extends Response {
8
+ body: TBody;
9
+ readonly source?: unknown;
10
+ constructor(options?: {
11
+ statusCode?: number;
12
+ headers?: HeaderSource;
13
+ body?: TBody;
14
+ source?: unknown;
15
+ });
16
+ static from<TBody extends RequestData = RequestData>(source?: Response$1<TBody> | ResponseSource): Response$1<TBody> | undefined;
17
+ status(code: number): this;
18
+ header(name: string, value: string): this;
19
+ getHeaders(): HeaderMap;
20
+ json(body: TBody): any;
21
+ send(body: TBody): any;
22
+ }
23
+ //#endregion
24
+ //#region src/session/FlashBag.d.ts
25
+ declare class FlashBag<T = unknown> {
26
+ protected bag: Record<string, T>;
27
+ private sweepKeys;
28
+ constructor(items?: Record<string, T>);
29
+ put(key: string, value: T): this;
30
+ set(key: string, value: T): this;
31
+ get(key: string, defaultValue?: T): T;
32
+ has(key?: string | string[] | null): boolean;
33
+ any(): boolean;
34
+ isEmpty(): boolean;
35
+ isNotEmpty(): boolean;
36
+ keys(): string[];
37
+ all(): {
38
+ [x: string]: T;
39
+ };
40
+ clear(key?: string | string[]): this;
41
+ forget(key: string): this;
42
+ markForSweep(keys?: string[]): this;
43
+ sweep(): this;
44
+ toJSON(): {
45
+ [x: string]: T;
46
+ };
47
+ }
48
+ //#endregion
49
+ //#region src/session/types.d.ts
50
+ type SessionDriverType = 'file' | 'cookie' | 'database' | SessionDriver;
51
+ type SessionErrorValue = string | string[] | Error | unknown;
52
+ type SessionErrorRecord = Record<string, SessionErrorValue>;
53
+ interface SessionMessageProvider {
54
+ getMessageBag?: () => SessionMessageProvider;
55
+ getMessages?: () => SessionErrorRecord;
56
+ messagesRaw?: () => SessionErrorRecord;
57
+ toArray?: () => SessionErrorRecord;
58
+ all?: (...args: any[]) => SessionErrorRecord | string[];
59
+ errors?: (() => SessionErrorRecord | SessionMessageProvider) | SessionErrorRecord | SessionMessageProvider;
60
+ }
61
+ type SessionErrorSource = SessionErrorRecord | ErrorBag | SessionMessageProvider;
62
+ interface SessionInitialState {
63
+ data?: Record<string, any>;
64
+ errors?: SessionErrorSource;
65
+ flash?: Record<string, any> | FlashBag;
66
+ }
67
+ type SessionPayload = {
68
+ data?: Record<string, any>;
69
+ errors?: SessionErrorRecord;
70
+ flash?: Record<string, any>;
71
+ };
72
+ type cookie_options = {
73
+ path?: string;
74
+ domain?: string;
75
+ httpOnly?: boolean;
76
+ secure?: boolean;
77
+ sameSite?: 'Strict' | 'Lax' | 'None';
78
+ maxAge?: number;
79
+ expires?: Date;
80
+ };
81
+ type HttpContextLike = Record<string, any>;
82
+ type SessionDriverResult = {
83
+ id: string;
84
+ state?: SessionPayload;
85
+ save: (payload: SessionPayload) => void | Promise<void>;
86
+ destroy?: () => void | Promise<void>;
87
+ };
88
+ interface SessionDriver {
89
+ start(context: HttpContextLike): Promise<SessionDriverResult>;
90
+ }
91
+ type BaseSessionDriverOptions = {
92
+ cookie?: string;
93
+ secret?: string;
94
+ ttl?: number;
95
+ cookie_options?: cookie_options;
96
+ };
97
+ type DatabaseSessionDriverOptions = BaseSessionDriverOptions & {
98
+ table?: string;
99
+ };
100
+ type PersistentSessionConfig = {
101
+ driver?: SessionDriverType;
102
+ cookie?: string;
103
+ secret?: string;
104
+ ttl?: number;
105
+ cookie_options?: cookie_options;
106
+ file?: {
107
+ directory?: string;
108
+ };
109
+ database?: {
110
+ table?: string;
111
+ };
112
+ };
113
+ type SessionConfig = {
114
+ secret?: string;
115
+ driver?: SessionDriverType;
116
+ cookie?: string;
117
+ ttl?: number;
118
+ http_only?: boolean;
119
+ secure?: boolean;
120
+ same_site?: cookie_options['sameSite'];
121
+ path?: string;
122
+ table?: string;
123
+ directory?: string;
124
+ };
125
+ //#endregion
126
+ //#region src/session/ErrorBag.d.ts
127
+ declare class ErrorBag extends FlashBag<string[]> {
128
+ constructor(errors?: SessionErrorSource);
129
+ add(field: string, message: SessionErrorValue): this;
130
+ addIf(condition: boolean, field: string, message: SessionErrorValue): this;
131
+ merge(errors: SessionErrorSource): ErrorBag;
132
+ validation(error: unknown): ErrorBag;
133
+ keys(): string[];
134
+ get(field?: string): string[];
135
+ first(field?: string | null): string;
136
+ has(field?: string | string[] | null): boolean;
137
+ hasAny(fields: string | string[]): boolean;
138
+ missing(fields: string | string[]): boolean;
139
+ any(): boolean;
140
+ isEmpty(): boolean;
141
+ isNotEmpty(): boolean;
142
+ count(): number;
143
+ all(): never;
144
+ unique(): unknown[];
145
+ clear(field?: string | string[]): this;
146
+ forget(field: string): this;
147
+ messagesRaw(): Record<string, string[]>;
148
+ getMessages(): Record<string, string[]>;
149
+ getMessageBag(): this;
150
+ toArray(): Record<string, string[]>;
151
+ toJSON(): Record<string, string[]>;
152
+ }
153
+ //#endregion
154
+ //#region src/session/Session.d.ts
155
+ declare class Session {
156
+ readonly errors: ErrorBag;
157
+ readonly flashBag: FlashBag;
158
+ readonly id?: string;
159
+ private data;
160
+ private persistent?;
161
+ private saveQueue;
162
+ constructor(initial?: SessionInitialState | Record<string, any> | Session, persistent?: SessionDriverResult);
163
+ private snapshot;
164
+ private queuePersist;
165
+ save(): Promise<this>;
166
+ destroy(): Promise<this>;
167
+ get<T = any>(key: string, defaultValue?: T): T;
168
+ put<T = any>(key: string, value: T): this;
169
+ set<T = any>(key: string, value: T): this;
170
+ has(key: string): boolean;
171
+ forget(key: string): this;
172
+ clear(): this;
173
+ all(): {
174
+ [x: string]: any;
175
+ };
176
+ flash<T = any>(key: string, value: T): this;
177
+ getFlash<T = any>(key: string, defaultValue?: T): T;
178
+ sweepFlash(): Promise<this>;
179
+ addError(field: string, message: SessionErrorValue): this;
180
+ addErrors(errors: SessionErrorRecord | ErrorBag): this;
181
+ addValidationErrors(error: unknown): this;
182
+ hasErrors(field?: string): boolean;
183
+ clearErrors(field?: string): this;
184
+ forView(): {
185
+ errors: ErrorBag;
186
+ flash: FlashBag<unknown>;
187
+ };
188
+ toJSON(): {
189
+ errors: Record<string, string[]>;
190
+ flash: {
191
+ [x: string]: unknown;
192
+ };
193
+ };
194
+ }
195
+ //#endregion
196
+ //#region src/session/plugins.d.ts
197
+ declare const arkstackHttpPlugin: _$clear_router0.ClearRouterPlugin<any, HttpContext>;
198
+ declare const kanunSessionPlugin: _$kanun.ValidatorPlugin;
199
+ //#endregion
200
+ //#region src/session/helpers.d.ts
201
+ declare const registerResponseFlashSweep: (target: unknown, session?: Session) => void;
202
+ declare const attachViewState: (target: Record<PropertyKey, any>, session: Session) => void;
203
+ declare const ensureSession: (ctx: unknown, initial?: SessionInitialState | Record<string, any>, persistent?: SessionDriverResult) => Session;
204
+ declare const getSession: (ctx: unknown) => Session | undefined;
205
+ //#endregion
206
+ //#region src/session/config.d.ts
207
+ declare const createSessionDriver: (config?: PersistentSessionConfig) => SessionDriver;
208
+ declare const configureSession: (config: PersistentSessionConfig | SessionDriver) => SessionDriver;
209
+ declare const getSessionDriver: () => SessionDriver;
210
+ //#endregion
211
+ //#region src/session/cookie.d.ts
212
+ declare const generateSessionId: () => string;
213
+ declare const signValue: (value: string, secret: string) => string;
214
+ declare const encodeSignedValue: (value: string, secret: string) => string;
215
+ declare const decodeSignedValue: (value: string | undefined, secret: string) => string | undefined;
216
+ declare const encodeJson: (value: unknown) => string;
217
+ declare const decodeJson: <T = unknown>(value: string | undefined) => T | undefined;
218
+ declare const parseCookies: (header?: string | string[] | null) => Record<string, string>;
219
+ declare const getCookie: (context: HttpContextLike, name: string) => string;
220
+ declare const serializeCookie: (name: string, value: string, options?: cookie_options) => string;
221
+ declare const setCookie: (context: HttpContextLike, name: string, value: string, options?: cookie_options) => string;
222
+ //#endregion
223
+ //#region src/session/encryption.d.ts
224
+ declare const encryptSessionValue: (value: string, secret: string) => string;
225
+ declare const decryptSessionValue: (payload: string | undefined, secret: string) => string | undefined;
226
+ //#endregion
227
+ //#region src/session/serialization.d.ts
228
+ declare const encodeSessionPayload: (payload: SessionPayload & {
229
+ id?: string;
230
+ }) => string;
231
+ declare const decodeSessionPayload: <T extends SessionPayload & {
232
+ id?: string;
233
+ } = SessionPayload & {
234
+ id?: string;
235
+ }>(value: string | undefined) => T | undefined;
236
+ //#endregion
237
+ //#region src/session/drivers/BaseSessionDriver.d.ts
238
+ declare abstract class BaseSessionDriver implements SessionDriver {
239
+ readonly cookie: string;
240
+ readonly secret: string;
241
+ readonly ttl?: number;
242
+ readonly cookie_options: cookie_options;
243
+ constructor(options?: BaseSessionDriverOptions);
244
+ protected readSessionId(context: HttpContextLike): string | undefined;
245
+ protected encryptPayload(value: string): string;
246
+ protected decryptPayload(value: string | undefined): string | undefined;
247
+ protected writeSessionId(context: HttpContextLike, id: string): void;
248
+ abstract start(context: HttpContextLike): Promise<SessionDriverResult>;
249
+ }
250
+ //#endregion
251
+ //#region src/session/drivers/CookieSessionDriver.d.ts
252
+ declare class CookieSessionDriver extends BaseSessionDriver {
253
+ start(context: HttpContextLike): Promise<SessionDriverResult>;
254
+ }
255
+ //#endregion
256
+ //#region src/session/drivers/DatabaseSessionDriver.d.ts
257
+ declare class DatabaseSessionDriver extends BaseSessionDriver {
258
+ readonly tableName: string;
259
+ constructor(options?: DatabaseSessionDriverOptions);
260
+ private table;
261
+ start(context: HttpContextLike): Promise<SessionDriverResult>;
262
+ }
263
+ //#endregion
264
+ //#region src/session/drivers/FileSessionDriver.d.ts
265
+ declare class FileSessionDriver extends BaseSessionDriver {
266
+ readonly directory: string;
267
+ constructor(options?: BaseSessionDriverOptions & {
268
+ directory?: string;
269
+ });
270
+ private path;
271
+ start(context: HttpContextLike): Promise<SessionDriverResult>;
272
+ }
273
+ //#endregion
274
+ //#region src/types/Http.d.ts
275
+ type HeaderValue = string | string[] | number | boolean | null | undefined;
276
+ type HeaderMap = Record<string, string>;
277
+ type HeaderSource = Headers | Record<string, HeaderValue>;
278
+ type RequestSource<TUser = unknown> = {
279
+ headers?: HeaderSource;
280
+ method?: string;
281
+ url?: string;
282
+ originalUrl?: string;
283
+ path?: string;
284
+ ip?: string;
285
+ user?: TUser;
286
+ authToken?: string;
287
+ req?: RequestSource<TUser>;
288
+ request?: RequestSource<TUser>;
289
+ };
290
+ type ResponseSource = {
291
+ statusCode?: number;
292
+ status?: number | ((code: number) => unknown);
293
+ headers?: HeaderSource;
294
+ setHeader?: (name: string, value: string | string[]) => unknown;
295
+ getHeader?: (name: string) => string | string[] | number | undefined;
296
+ json?: (body: unknown) => unknown;
297
+ send?: (body: unknown) => unknown;
298
+ redirect?: (status: number, path: string) => unknown;
299
+ };
300
+ type RequestOptions<TUser = unknown> = {
301
+ headers?: HeaderSource;
302
+ method?: string;
303
+ url?: string;
304
+ path?: string;
305
+ ip?: string | null;
306
+ user?: TUser;
307
+ authToken?: string;
308
+ source?: unknown;
309
+ };
310
+ interface RequestHelper<TUser = unknown> {
311
+ (): Request$1;
312
+ <X extends string>(key: X): Request$1<TUser>['body'][X];
313
+ }
314
+ interface SessionHelper {
315
+ (): Session;
316
+ <X extends string>(key: X): any;
317
+ }
318
+ interface RedirectHelper {
319
+ (): Response$1;
320
+ (to?: string, status?: number): Response$1;
321
+ }
322
+ interface OldHelper {
323
+ (): Record<string, any>;
324
+ <T = any>(key: string, defaultValue?: T): T;
325
+ }
326
+ //#endregion
327
+ //#region src/Request.d.ts
328
+ declare class Request$1<TUser = unknown> extends Request {
329
+ readonly headers: HeaderMap;
330
+ readonly ip: string | null;
331
+ readonly source?: unknown;
332
+ user?: TUser;
333
+ authToken?: string;
334
+ constructor(options?: RequestOptions<TUser>);
335
+ static from<TUser = unknown>(source?: Request$1<TUser> | RequestSource<TUser>): Request$1<TUser> | undefined;
336
+ header(name: string): string;
337
+ bearerToken(): string | null;
338
+ setUser(user: TUser): this;
339
+ }
340
+ //#endregion
341
+ //#region src/helpers.d.ts
342
+ declare const unwrapRequestSource: <TUser>(source: RequestSource<TUser>) => RequestSource<TUser>;
343
+ declare const makeHeaders: (headers?: HeaderSource) => Headers;
344
+ declare const normalizeHeaders: (headers?: HeaderSource) => HeaderMap;
345
+ declare const normalizeHeaderValue: (value: HeaderValue) => string | undefined;
346
+ declare const isHeaders: (value: unknown) => value is Headers;
347
+ declare const isRecord: (value: unknown) => value is Record<PropertyKey, any>;
348
+ //#endregion
349
+ //#region src/redirect.d.ts
350
+ declare const redirectBackTarget: (fallback?: string) => string;
351
+ declare const resolveRedirectTarget: (to?: string, fallback?: string) => string;
352
+ declare const redirect: (to?: string, status?: number) => Response$1<unknown>;
353
+ //#endregion
354
+ //#region src/old.d.ts
355
+ declare const old: <T = any>(key?: string, defaultValue?: T) => T;
356
+ //#endregion
357
+ //#region src/middlewares/web.d.ts
358
+ declare const webMiddlewareKey: unique symbol;
359
+ declare const web: (...args: any[]) => Promise<any>;
360
+ declare const isWebRequest: (target: unknown) => boolean;
361
+ //#endregion
362
+ export { ErrorBag as $, encodeSessionPayload as A, serializeCookie as B, ResponseSource as C, CookieSessionDriver as D, DatabaseSessionDriver as E, encodeJson as F, getSessionDriver as G, signValue as H, encodeSignedValue as I, getSession as J, attachViewState as K, generateSessionId as L, encryptSessionValue as M, decodeJson as N, BaseSessionDriver as O, decodeSignedValue as P, Session as Q, getCookie as R, RequestSource as S, FileSessionDriver as T, configureSession as U, setCookie as V, createSessionDriver as W, arkstackHttpPlugin as X, registerResponseFlashSweep as Y, kanunSessionPlugin as Z, HeaderValue as _, redirect as a, SessionDriver as at, RequestHelper as b, isHeaders as c, SessionErrorRecord as ct, normalizeHeaderValue as d, SessionInitialState as dt, BaseSessionDriverOptions as et, normalizeHeaders as f, SessionMessageProvider as ft, HeaderSource as g, Response$1 as gt, HeaderMap as h, FlashBag as ht, old as i, SessionConfig as it, decryptSessionValue as j, decodeSessionPayload as k, isRecord as l, SessionErrorSource as lt, Request$1 as m, cookie_options as mt, web as n, HttpContextLike as nt, redirectBackTarget as o, SessionDriverResult as ot, unwrapRequestSource as p, SessionPayload as pt, ensureSession as q, webMiddlewareKey as r, PersistentSessionConfig as rt, resolveRedirectTarget as s, SessionDriverType as st, isWebRequest as t, DatabaseSessionDriverOptions as tt, makeHeaders as u, SessionErrorValue as ut, OldHelper as v, SessionHelper as w, RequestOptions as x, RedirectHelper as y, parseCookies as z };
363
+ //# sourceMappingURL=index-BfxuYaPt.d.ts.map
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
- import { C as Session, D as SessionErrorValue, E as SessionErrorSource, O as SessionInitialState, S as kanunSessionPlugin, T as SessionErrorRecord, _ as SessionHelper, a as normalizeHeaders, b as getSession, c as Request, d as HeaderValue, f as HttpContext, g as ResponseSource, h as RequestSource, i as normalizeHeaderValue, k as SessionMessageProvider, l as HeaderMap, m as RequestOptions, n as isRecord, o as unwrapRequestSource, p as RequestHelper, r as makeHeaders, s as Response, t as isHeaders, u as HeaderSource, v as attachViewState, w as ErrorBag, x as clearRouterSessionPlugin, y as ensureSession } from "./index-_YjYYnYr.js";
2
- export { ErrorBag, HeaderMap, HeaderSource, HeaderValue, HttpContext, Request, RequestHelper, RequestOptions, RequestSource, Response, ResponseSource, Session, SessionErrorRecord, SessionErrorSource, SessionErrorValue, SessionHelper, SessionInitialState, SessionMessageProvider, attachViewState, clearRouterSessionPlugin, ensureSession, getSession, isHeaders, isRecord, kanunSessionPlugin, makeHeaders, normalizeHeaderValue, normalizeHeaders, unwrapRequestSource };
1
+ /// <reference path="./app.d.ts" />
2
+ import { $ as ErrorBag, A as encodeSessionPayload, B as serializeCookie, C as ResponseSource, D as CookieSessionDriver, E as DatabaseSessionDriver, F as encodeJson, G as getSessionDriver, H as signValue, I as encodeSignedValue, J as getSession, K as attachViewState, L as generateSessionId, M as encryptSessionValue, N as decodeJson, O as BaseSessionDriver, P as decodeSignedValue, Q as Session, R as getCookie, S as RequestSource, T as FileSessionDriver, U as configureSession, V as setCookie, W as createSessionDriver, X as arkstackHttpPlugin, Y as registerResponseFlashSweep, Z as kanunSessionPlugin, _ as HeaderValue, a as redirect, at as SessionDriver, b as RequestHelper, c as isHeaders, ct as SessionErrorRecord, d as normalizeHeaderValue, dt as SessionInitialState, et as BaseSessionDriverOptions, f as normalizeHeaders, ft as SessionMessageProvider, g as HeaderSource, gt as Response, h as HeaderMap, ht as FlashBag, i as old, it as SessionConfig, j as decryptSessionValue, k as decodeSessionPayload, l as isRecord, lt as SessionErrorSource, m as Request, mt as cookie_options, n as web, nt as HttpContextLike, o as redirectBackTarget, ot as SessionDriverResult, p as unwrapRequestSource, pt as SessionPayload, q as ensureSession, r as webMiddlewareKey, rt as PersistentSessionConfig, s as resolveRedirectTarget, st as SessionDriverType, t as isWebRequest, tt as DatabaseSessionDriverOptions, u as makeHeaders, ut as SessionErrorValue, v as OldHelper, w as SessionHelper, x as RequestOptions, y as RedirectHelper, z as parseCookies } from "./index-BfxuYaPt.js";
3
+ export { BaseSessionDriver, BaseSessionDriverOptions, CookieSessionDriver, DatabaseSessionDriver, DatabaseSessionDriverOptions, ErrorBag, FileSessionDriver, FlashBag, HeaderMap, HeaderSource, HeaderValue, HttpContextLike, OldHelper, PersistentSessionConfig, RedirectHelper, Request, RequestHelper, RequestOptions, RequestSource, Response, ResponseSource, Session, SessionConfig, SessionDriver, SessionDriverResult, SessionDriverType, SessionErrorRecord, SessionErrorSource, SessionErrorValue, SessionHelper, SessionInitialState, SessionMessageProvider, SessionPayload, arkstackHttpPlugin, attachViewState, configureSession, cookie_options, createSessionDriver, decodeJson, decodeSessionPayload, decodeSignedValue, decryptSessionValue, encodeJson, encodeSessionPayload, encodeSignedValue, encryptSessionValue, ensureSession, generateSessionId, getCookie, getSession, getSessionDriver, isHeaders, isRecord, isWebRequest, kanunSessionPlugin, makeHeaders, normalizeHeaderValue, normalizeHeaders, old, parseCookies, redirect, redirectBackTarget, registerResponseFlashSweep, resolveRedirectTarget, serializeCookie, setCookie, signValue, unwrapRequestSource, web, webMiddlewareKey };
package/dist/index.js CHANGED
@@ -1,2 +1,50 @@
1
- import { a as getSession, c as Response, d as isRecord, f as makeHeaders, h as unwrapRequestSource, i as ensureSession, l as Request, m as normalizeHeaders, n as kanunSessionPlugin, o as Session, p as normalizeHeaderValue, r as attachViewState, s as ErrorBag, t as clearRouterSessionPlugin, u as isHeaders } from "./session-gK9S8Go9.js";
2
- export { ErrorBag, Request, Response, Session, attachViewState, clearRouterSessionPlugin, ensureSession, getSession, isHeaders, isRecord, kanunSessionPlugin, makeHeaders, normalizeHeaderValue, normalizeHeaders, unwrapRequestSource };
1
+ import { A as registerResponseFlashSweep, B as normalizeHeaderValue, C as parseCookies, D as attachViewState, E as signValue, F as Response, H as unwrapRequestSource, I as Request, L as isHeaders, M as Session, N as ErrorBag, O as ensureSession, P as FlashBag, R as isRecord, S as getCookie, T as setCookie, V as normalizeHeaders, _ as decodeJson, a as kanunSessionPlugin, b as encodeSignedValue, c as getSessionDriver, d as CookieSessionDriver, f as BaseSessionDriver, g as encodeSessionPayload, h as decodeSessionPayload, i as arkstackHttpPlugin, j as old, k as getSession, l as FileSessionDriver, m as encryptSessionValue, n as redirectBackTarget, o as configureSession, p as decryptSessionValue, r as resolveRedirectTarget, s as createSessionDriver, t as redirect, u as DatabaseSessionDriver, v as decodeSignedValue, w as serializeCookie, x as generateSessionId, y as encodeJson, z as makeHeaders } from "./redirect-CZvhBHqO.js";
2
+ //#region src/middlewares/web.ts
3
+ const webMiddlewareKey = Symbol.for("arkstack:http:web");
4
+ const markWebRequest = (target) => {
5
+ if (isRecord(target)) {
6
+ target[webMiddlewareKey] = true;
7
+ target.arkstackWeb = true;
8
+ }
9
+ };
10
+ const startWebSession = async (args) => {
11
+ const [first, second, third] = args;
12
+ const context = typeof third === "function" ? {
13
+ ctx: {
14
+ req: first,
15
+ res: second
16
+ },
17
+ req: first,
18
+ res: second
19
+ } : first;
20
+ if (!isRecord(context)) return;
21
+ const existingSession = context.httpSession || (context.session instanceof Session ? context.session : void 0) || (isRecord(context.req) ? context.req.httpSession : void 0) || (isRecord(context.context) ? context.context.httpSession : void 0);
22
+ if (existingSession instanceof Session) return existingSession;
23
+ const persistent = await getSessionDriver().start(context);
24
+ return ensureSession(context, persistent.state, persistent);
25
+ };
26
+ const web = async (...args) => {
27
+ const [first, second, third] = args;
28
+ markWebRequest(first);
29
+ markWebRequest(first?.context);
30
+ markWebRequest(first?.req);
31
+ if (isRecord(second)) markWebRequest(second);
32
+ const session = await startWebSession(args);
33
+ if (typeof third === "function") {
34
+ registerResponseFlashSweep({ res: second }, session);
35
+ return third();
36
+ }
37
+ if (typeof second === "function") {
38
+ const result = await second();
39
+ await session?.sweepFlash();
40
+ return result;
41
+ }
42
+ };
43
+ const isWebRequest = (target) => {
44
+ if (!isRecord(target)) return false;
45
+ return target[webMiddlewareKey] === true || target.arkstackWeb === true || isWebRequest(target.context) || isWebRequest(target.req);
46
+ };
47
+ //#endregion
48
+ export { BaseSessionDriver, CookieSessionDriver, DatabaseSessionDriver, ErrorBag, FileSessionDriver, FlashBag, Request, Response, Session, arkstackHttpPlugin, attachViewState, configureSession, createSessionDriver, decodeJson, decodeSessionPayload, decodeSignedValue, decryptSessionValue, encodeJson, encodeSessionPayload, encodeSignedValue, encryptSessionValue, ensureSession, generateSessionId, getCookie, getSession, getSessionDriver, isHeaders, isRecord, isWebRequest, kanunSessionPlugin, makeHeaders, normalizeHeaderValue, normalizeHeaders, old, parseCookies, redirect, redirectBackTarget, registerResponseFlashSweep, resolveRedirectTarget, serializeCookie, setCookie, signValue, unwrapRequestSource, web, webMiddlewareKey };
49
+
50
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/middlewares/web.ts"],"sourcesContent":["import { getSessionDriver } from '../session/config'\nimport { ensureSession, registerResponseFlashSweep } from '../session/helpers'\nimport { Session } from '../session/Session'\nimport { isRecord } from '../helpers'\n\nexport const webMiddlewareKey = Symbol.for('arkstack:http:web')\n\nconst markWebRequest = (target: unknown) => {\n if (isRecord(target)) {\n target[webMiddlewareKey] = true\n target.arkstackWeb = true\n }\n}\n\nconst startWebSession = async (args: any[]): Promise<Session | undefined> => {\n const [first, second, third] = args\n const context = typeof third === 'function'\n ? {\n ctx: { req: first, res: second },\n req: first,\n res: second,\n }\n : first\n\n if (!isRecord(context)) {\n return\n }\n\n const existingSession = context.httpSession\n || (context.session instanceof Session ? context.session : undefined)\n || (isRecord(context.req) ? context.req.httpSession : undefined)\n || (isRecord(context.context) ? context.context.httpSession : undefined)\n\n if (existingSession instanceof Session) {\n return existingSession\n }\n\n const persistent = await getSessionDriver().start(context)\n\n return ensureSession(context, persistent.state, persistent)\n}\n\nexport const web = async (...args: any[]) => {\n const [first, second, third] = args\n\n markWebRequest(first)\n markWebRequest(first?.context)\n markWebRequest(first?.req)\n\n if (isRecord(second)) {\n markWebRequest(second)\n }\n\n const session = await startWebSession(args)\n\n if (typeof third === 'function') {\n registerResponseFlashSweep({ res: second }, session)\n\n return third()\n }\n\n if (typeof second === 'function') {\n const result = await second()\n\n await session?.sweepFlash()\n\n return result\n }\n}\n\nexport const isWebRequest = (target: unknown): boolean => {\n if (!isRecord(target)) {\n return false\n }\n\n return target[webMiddlewareKey] === true\n || target.arkstackWeb === true\n || isWebRequest(target.context)\n || isWebRequest(target.req)\n}\n"],"mappings":";;AAKA,MAAa,mBAAmB,OAAO,IAAI,oBAAoB;AAE/D,MAAM,kBAAkB,WAAoB;CACxC,IAAI,SAAS,OAAO,EAAE;EAClB,OAAO,oBAAoB;EAC3B,OAAO,cAAc;;;AAI7B,MAAM,kBAAkB,OAAO,SAA8C;CACzE,MAAM,CAAC,OAAO,QAAQ,SAAS;CAC/B,MAAM,UAAU,OAAO,UAAU,aAC3B;EACE,KAAK;GAAE,KAAK;GAAO,KAAK;GAAQ;EAChC,KAAK;EACL,KAAK;EACR,GACC;CAEN,IAAI,CAAC,SAAS,QAAQ,EAClB;CAGJ,MAAM,kBAAkB,QAAQ,gBACxB,QAAQ,mBAAmB,UAAU,QAAQ,UAAU,KAAA,OACvD,SAAS,QAAQ,IAAI,GAAG,QAAQ,IAAI,cAAc,KAAA,OAClD,SAAS,QAAQ,QAAQ,GAAG,QAAQ,QAAQ,cAAc,KAAA;CAElE,IAAI,2BAA2B,SAC3B,OAAO;CAGX,MAAM,aAAa,MAAM,kBAAkB,CAAC,MAAM,QAAQ;CAE1D,OAAO,cAAc,SAAS,WAAW,OAAO,WAAW;;AAG/D,MAAa,MAAM,OAAO,GAAG,SAAgB;CACzC,MAAM,CAAC,OAAO,QAAQ,SAAS;CAE/B,eAAe,MAAM;CACrB,eAAe,OAAO,QAAQ;CAC9B,eAAe,OAAO,IAAI;CAE1B,IAAI,SAAS,OAAO,EAChB,eAAe,OAAO;CAG1B,MAAM,UAAU,MAAM,gBAAgB,KAAK;CAE3C,IAAI,OAAO,UAAU,YAAY;EAC7B,2BAA2B,EAAE,KAAK,QAAQ,EAAE,QAAQ;EAEpD,OAAO,OAAO;;CAGlB,IAAI,OAAO,WAAW,YAAY;EAC9B,MAAM,SAAS,MAAM,QAAQ;EAE7B,MAAM,SAAS,YAAY;EAE3B,OAAO;;;AAIf,MAAa,gBAAgB,WAA6B;CACtD,IAAI,CAAC,SAAS,OAAO,EACjB,OAAO;CAGX,OAAO,OAAO,sBAAsB,QAC7B,OAAO,gBAAgB,QACvB,aAAa,OAAO,QAAQ,IAC5B,aAAa,OAAO,IAAI"}