appstage 0.2.20 → 0.2.22

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/index.cjs CHANGED
@@ -162,6 +162,15 @@ const files = (params) => {
162
162
  };
163
163
  };
164
164
 
165
+ const redirect = (params) => {
166
+ let { url, status = 302 } = typeof params === "string" ? { url: params } : params;
167
+ return (req, res) => {
168
+ let search = req.originalUrl.split("?")[1] ?? "";
169
+ if (search) search = `${url.includes("?") ? "&" : "?"}${search}`;
170
+ res.redirect(status, `${url}${search}`);
171
+ };
172
+ };
173
+
165
174
  const unhandledError = () => async (err, req, res) => {
166
175
  emitLog(req.app, "Unhandled error", {
167
176
  level: "error",
@@ -690,6 +699,7 @@ exports.init = init;
690
699
  exports.injectNonce = injectNonce;
691
700
  exports.lang = lang;
692
701
  exports.log = log;
702
+ exports.redirect = redirect;
693
703
  exports.renderStatus = renderStatus;
694
704
  exports.requestEvents = requestEvents;
695
705
  exports.serializeState = serializeState;
package/dist/index.d.ts CHANGED
@@ -32,6 +32,12 @@ type FilesParams = {
32
32
  */
33
33
  declare const files: Controller<string | FilesParams>;
34
34
 
35
+ type RedirectParams = {
36
+ url: string;
37
+ status?: number;
38
+ };
39
+ declare const redirect: Controller<string | RedirectParams>;
40
+
35
41
  type ErrorController<T = void> = (params: T) => ErrorRequestHandler;
36
42
 
37
43
  declare const unhandledError: ErrorController;
@@ -1123,4 +1129,4 @@ declare function servePipeableStream(req: Request, res: Response): ({
1123
1129
  pipe
1124
1130
  }: PipeableStream, error?: unknown) => Promise<void>;
1125
1131
 
1126
- export { Controller, ErrorController, FilesParams, LangParams, LogEventPayload, LogLevel, LogOptions, Middleware, MiddlewareSet, RenderStatus, ReqCtx, TransformContent, build, cli, createApp, cspNonce, emitLog, files, getEffectiveLocale, getLocales, getStatusMessage, init, injectNonce, lang, log, renderStatus, requestEvents, serializeState, servePipeableStream, toLanguage, unhandledError, unhandledRoute };
1132
+ export { Controller, ErrorController, FilesParams, LangParams, LogEventPayload, LogLevel, LogOptions, Middleware, MiddlewareSet, RedirectParams, RenderStatus, ReqCtx, TransformContent, build, cli, createApp, cspNonce, emitLog, files, getEffectiveLocale, getLocales, getStatusMessage, init, injectNonce, lang, log, redirect, renderStatus, requestEvents, serializeState, servePipeableStream, toLanguage, unhandledError, unhandledRoute };
package/dist/index.mjs CHANGED
@@ -136,6 +136,15 @@ const files = (params) => {
136
136
  };
137
137
  };
138
138
 
139
+ const redirect = (params) => {
140
+ let { url, status = 302 } = typeof params === "string" ? { url: params } : params;
141
+ return (req, res) => {
142
+ let search = req.originalUrl.split("?")[1] ?? "";
143
+ if (search) search = `${url.includes("?") ? "&" : "?"}${search}`;
144
+ res.redirect(status, `${url}${search}`);
145
+ };
146
+ };
147
+
139
148
  const unhandledError = () => async (err, req, res) => {
140
149
  emitLog(req.app, "Unhandled error", {
141
150
  level: "error",
@@ -651,4 +660,4 @@ function servePipeableStream(req, res) {
651
660
  };
652
661
  }
653
662
 
654
- export { build, cli, createApp, cspNonce, emitLog, files, getEffectiveLocale, getLocales, getStatusMessage, init, injectNonce, lang, log, renderStatus, requestEvents, serializeState, servePipeableStream, toLanguage, unhandledError, unhandledRoute };
663
+ export { build, cli, createApp, cspNonce, emitLog, files, getEffectiveLocale, getLocales, getStatusMessage, init, injectNonce, lang, log, redirect, renderStatus, requestEvents, serializeState, servePipeableStream, toLanguage, unhandledError, unhandledRoute };
package/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./src/controllers/files.ts";
2
+ export * from "./src/controllers/redirect.ts";
2
3
  export * from "./src/controllers/unhandledError.ts";
3
4
  export * from "./src/controllers/unhandledRoute.ts";
4
5
  export * from "./src/lib/lang/getEffectiveLocale.ts";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "appstage",
3
- "version": "0.2.20",
3
+ "version": "0.2.22",
4
4
  "description": "",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
@@ -0,0 +1,19 @@
1
+ import type { Controller } from "../types/Controller.ts";
2
+
3
+ export type RedirectParams = {
4
+ url: string;
5
+ status?: number;
6
+ };
7
+
8
+ export const redirect: Controller<string | RedirectParams> = (params) => {
9
+ let { url, status = 302 } =
10
+ typeof params === "string" ? { url: params } : params;
11
+
12
+ return (req, res) => {
13
+ let search = req.originalUrl.split("?")[1] ?? "";
14
+
15
+ if (search) search = `${url.includes("?") ? "&" : "?"}${search}`;
16
+
17
+ res.redirect(status, `${url}${search}`);
18
+ };
19
+ };