@aeriajs/http 0.0.57 → 0.0.58

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.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './cors.js';
2
+ export * from './next.js';
2
3
  export * from './options.js';
3
4
  export * from './routing.js';
4
5
  export * from './payload.js';
package/dist/index.js CHANGED
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./cors.js"), exports);
18
+ __exportStar(require("./next.js"), exports);
18
19
  __exportStar(require("./options.js"), exports);
19
20
  __exportStar(require("./routing.js"), exports);
20
21
  __exportStar(require("./payload.js"), exports);
package/dist/index.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  export * from "./cors.mjs";
3
+ export * from "./next.mjs";
3
4
  export * from "./options.mjs";
4
5
  export * from "./routing.mjs";
5
6
  export * from "./payload.mjs";
package/dist/next.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export declare const NEXT_SYMBOL: unique symbol;
2
+ type NextObject = {
3
+ [NEXT_SYMBOL]: null;
4
+ };
5
+ export declare const next: () => {
6
+ [NEXT_SYMBOL]: null;
7
+ };
8
+ export declare const isNext: (object: any) => object is NextObject;
9
+ export {};
package/dist/next.js ADDED
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isNext = exports.next = exports.NEXT_SYMBOL = void 0;
4
+ exports.NEXT_SYMBOL = Symbol('NEXT_SYMBOL');
5
+ const next = () => {
6
+ return {
7
+ [exports.NEXT_SYMBOL]: null,
8
+ };
9
+ };
10
+ exports.next = next;
11
+ const isNext = (object) => {
12
+ const sym = Object.getOwnPropertyDescriptor(object, exports.NEXT_SYMBOL);
13
+ return !!sym;
14
+ };
15
+ exports.isNext = isNext;
package/dist/next.mjs ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ export const NEXT_SYMBOL = Symbol("NEXT_SYMBOL");
3
+ export const next = () => {
4
+ return {
5
+ [NEXT_SYMBOL]: null
6
+ };
7
+ };
8
+ export const isNext = (object) => {
9
+ const sym = Object.getOwnPropertyDescriptor(object, NEXT_SYMBOL);
10
+ return !!sym;
11
+ };
package/dist/routing.d.ts CHANGED
@@ -29,7 +29,7 @@ export declare const matches: <TRequest extends GenericRequest>(req: TRequest, m
29
29
  fragments: string[];
30
30
  } | undefined;
31
31
  export declare const registerRoute: (context: RouteContext, method: RequestMethod | RequestMethod[], exp: RouteUri, cb: (context: RouteContext) => any, contract?: ContractWithRoles, options?: RouterOptions) => Promise<any>;
32
- export declare const wrapRouteExecution: (res: GenericResponse, cb: () => any | Promise<any>) => Promise<any>;
32
+ export declare const wrapRouteExecution: (response: GenericResponse, cb: () => any | Promise<any>) => Promise<any>;
33
33
  export declare const createRouter: (options?: Partial<RouterOptions>) => ProxiedRouter<{
34
34
  route: <const TContractWithRoles extends ContractWithRoles, TCallback extends (TContractWithRoles extends {
35
35
  response: infer Response;
package/dist/routing.js CHANGED
@@ -7,6 +7,7 @@ const common_1 = require("@aeriajs/common");
7
7
  const validation_1 = require("@aeriajs/validation");
8
8
  const entrypoint_1 = require("@aeriajs/entrypoint");
9
9
  const payload_js_1 = require("./payload.js");
10
+ const next_js_1 = require("./next.js");
10
11
  const checkUnprocessable = (validationEither, context) => {
11
12
  if ((0, common_1.isLeft)(validationEither)) {
12
13
  context.response.writeHead(422, {
@@ -60,12 +61,11 @@ const registerRoute = async (context, method, exp, cb, contract, options = {}) =
60
61
  });
61
62
  }
62
63
  catch (err) {
63
- context.response.writeHead(500);
64
- context.response.end((0, common_1.left)({
64
+ return context.error({
65
65
  httpCode: 500,
66
+ code: 'INVALID_JSON',
66
67
  message: 'Invalid JSON',
67
- }));
68
- return null;
68
+ });
69
69
  }
70
70
  }
71
71
  Object.assign(context.request, match);
@@ -100,46 +100,42 @@ const registerRoute = async (context, method, exp, cb, contract, options = {}) =
100
100
  }
101
101
  };
102
102
  exports.registerRoute = registerRoute;
103
- const wrapRouteExecution = async (res, cb) => {
103
+ const wrapRouteExecution = async (response, cb) => {
104
104
  try {
105
105
  const result = await cb();
106
106
  if (result === null) {
107
- if (!res.headersSent) {
108
- res.writeHead(204);
109
- res.end();
107
+ if (!response.headersSent) {
108
+ response.writeHead(204);
109
+ response.end();
110
110
  }
111
111
  return;
112
112
  }
113
- if (!res.headersSent && result && (0, common_1.isLeft)(result)) {
114
- const error = (0, common_1.unwrapEither)(result);
115
- if (error.httpCode) {
116
- res.writeHead(error.httpCode);
117
- }
118
- }
119
113
  if (result instanceof stream_1.Stream) {
120
114
  try {
121
- result.pipe(res);
115
+ result.pipe(response);
122
116
  }
123
117
  catch (err) {
124
118
  }
125
119
  return;
126
120
  }
127
- if (!res.writableEnded) {
128
- res.end(result);
121
+ if (!response.writableEnded) {
122
+ response.end(result);
129
123
  }
130
124
  return result;
131
125
  }
132
126
  catch (e) {
133
127
  console.trace(e);
134
- if (!res.headersSent) {
135
- res.writeHead(500);
128
+ if (!response.headersSent) {
129
+ response.writeHead(500);
136
130
  }
137
- if (!res.writableEnded) {
138
- const error = (0, common_1.left)({
131
+ if (!response.writableEnded) {
132
+ return (0, common_1.endpointError)({
139
133
  httpCode: 500,
134
+ code: 'UNKNOWN_ERROR',
140
135
  message: 'Internal server error',
136
+ }, {
137
+ response,
141
138
  });
142
- res.end(error);
143
139
  }
144
140
  }
145
141
  };
@@ -185,7 +181,11 @@ const createRouter = (options = {}) => {
185
181
  });
186
182
  };
187
183
  const routerPipe = (0, common_1.pipe)(routes, {
188
- returnFirst: true,
184
+ returnFirst: (value) => {
185
+ if (value && !(0, next_js_1.isNext)(value)) {
186
+ return value;
187
+ }
188
+ },
189
189
  });
190
190
  const router = {
191
191
  route,
@@ -198,9 +198,10 @@ const createRouter = (options = {}) => {
198
198
  };
199
199
  router.install = async (context, options) => {
200
200
  const result = await routerPipe(undefined, context, options);
201
- if (exhaust && result === undefined) {
202
- return (0, common_1.left)({
201
+ if (exhaust && (result === undefined || (0, next_js_1.isNext)(result))) {
202
+ return context.error({
203
203
  httpCode: 404,
204
+ code: 'NOT_FOUND',
204
205
  message: 'Not found',
205
206
  });
206
207
  }
package/dist/routing.mjs CHANGED
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  import { Stream } from "stream";
3
3
  import { ACErrors, REQUEST_METHODS } from "@aeriajs/types";
4
- import { pipe, isGranted, left, isLeft, unwrapEither, deepMerge } from "@aeriajs/common";
4
+ import { pipe, isGranted, isLeft, deepMerge, endpointError } from "@aeriajs/common";
5
5
  import { validate } from "@aeriajs/validation";
6
6
  import { getConfig } from "@aeriajs/entrypoint";
7
7
  import { safeJson } from "./payload.mjs";
8
+ import { isNext } from "./next.mjs";
8
9
  const checkUnprocessable = (validationEither, context) => {
9
10
  if (isLeft(validationEither)) {
10
11
  context.response.writeHead(422, {
@@ -58,12 +59,11 @@ export const registerRoute = async (context, method, exp, cb, contract, options
58
59
  }
59
60
  );
60
61
  } catch (err) {
61
- context.response.writeHead(500);
62
- context.response.end(left({
62
+ return context.error({
63
63
  httpCode: 500,
64
+ code: "INVALID_JSON",
64
65
  message: "Invalid JSON"
65
- }));
66
- return null;
66
+ });
67
67
  }
68
68
  }
69
69
  Object.assign(context.request, match);
@@ -95,44 +95,40 @@ export const registerRoute = async (context, method, exp, cb, contract, options
95
95
  return result === void 0 ? null : result;
96
96
  }
97
97
  };
98
- export const wrapRouteExecution = async (res, cb) => {
98
+ export const wrapRouteExecution = async (response, cb) => {
99
99
  try {
100
100
  const result = await cb();
101
101
  if (result === null) {
102
- if (!res.headersSent) {
103
- res.writeHead(204);
104
- res.end();
102
+ if (!response.headersSent) {
103
+ response.writeHead(204);
104
+ response.end();
105
105
  }
106
106
  return;
107
107
  }
108
- if (!res.headersSent && result && isLeft(result)) {
109
- const error = unwrapEither(result);
110
- if (error.httpCode) {
111
- res.writeHead(error.httpCode);
112
- }
113
- }
114
108
  if (result instanceof Stream) {
115
109
  try {
116
- result.pipe(res);
110
+ result.pipe(response);
117
111
  } catch (err) {
118
112
  }
119
113
  return;
120
114
  }
121
- if (!res.writableEnded) {
122
- res.end(result);
115
+ if (!response.writableEnded) {
116
+ response.end(result);
123
117
  }
124
118
  return result;
125
119
  } catch (e) {
126
120
  console.trace(e);
127
- if (!res.headersSent) {
128
- res.writeHead(500);
121
+ if (!response.headersSent) {
122
+ response.writeHead(500);
129
123
  }
130
- if (!res.writableEnded) {
131
- const error = left({
124
+ if (!response.writableEnded) {
125
+ return endpointError({
132
126
  httpCode: 500,
127
+ code: "UNKNOWN_ERROR",
133
128
  message: "Internal server error"
129
+ }, {
130
+ response
134
131
  });
135
- res.end(error);
136
132
  }
137
133
  }
138
134
  };
@@ -181,7 +177,11 @@ export const createRouter = (options = {}) => {
181
177
  });
182
178
  };
183
179
  const routerPipe = pipe(routes, {
184
- returnFirst: true
180
+ returnFirst: (value) => {
181
+ if (value && !isNext(value)) {
182
+ return value;
183
+ }
184
+ }
185
185
  });
186
186
  const router = {
187
187
  route,
@@ -194,9 +194,10 @@ export const createRouter = (options = {}) => {
194
194
  };
195
195
  router.install = async (context, options2) => {
196
196
  const result = await routerPipe(void 0, context, options2);
197
- if (exhaust && result === void 0) {
198
- return left({
197
+ if (exhaust && (result === void 0 || isNext(result))) {
198
+ return context.error({
199
199
  httpCode: 404,
200
+ code: "NOT_FOUND",
200
201
  message: "Not found"
201
202
  });
202
203
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/http",
3
- "version": "0.0.57",
3
+ "version": "0.0.58",
4
4
  "description": "## Installation",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -28,10 +28,10 @@
28
28
  "@aeriajs/validation": "link:../validation"
29
29
  },
30
30
  "peerDependencies": {
31
- "@aeriajs/common": "^0.0.49",
32
- "@aeriajs/entrypoint": "^0.0.49",
33
- "@aeriajs/types": "^0.0.46",
34
- "@aeriajs/validation": "^0.0.52"
31
+ "@aeriajs/common": "^0.0.50",
32
+ "@aeriajs/entrypoint": "^0.0.50",
33
+ "@aeriajs/types": "^0.0.47",
34
+ "@aeriajs/validation": "^0.0.53"
35
35
  },
36
36
  "scripts": {
37
37
  "test": "echo skipping",