@aeriajs/http 0.0.59 → 0.0.61

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/routing.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { RouteContext, GenericRequest, GenericResponse, RequestMethod, RouteUri, InferProperty, InferResponse, PackReferences, ContractWithRoles, ApiConfig, UserRole } from '@aeriajs/types';
1
+ import type { RouteContext, GenericRequest, GenericResponse, RequestMethod, RouteUri, InferProperty, InferResponse, PackReferences, ContractWithRoles, ApiConfig, RoleFromAccessCondition } from '@aeriajs/types';
2
2
  export type RouterOptions = {
3
3
  exhaust?: boolean;
4
4
  base?: RouteUri;
@@ -8,7 +8,7 @@ export type Middleware = (context: RouteContext) => any;
8
8
  export type RouteGroupOptions = {
9
9
  base?: RouteUri;
10
10
  };
11
- type TypedContext<TContractWithRoles extends ContractWithRoles> = RouteContext<TContractWithRoles['roles'] extends infer AccessCondition ? number extends keyof AccessCondition ? AccessCondition[number] : AccessCondition extends true ? UserRole : AccessCondition extends false ? never : AccessCondition extends 'unauthenticated-only' ? 'guest' : AccessCondition extends 'unauthenticated' ? UserRole : null : never> & {
11
+ type TypedContext<TContractWithRoles extends ContractWithRoles> = RouteContext<RoleFromAccessCondition<TContractWithRoles['roles']>> & {
12
12
  request: Omit<RouteContext['request'], 'payload' | 'query'> & {
13
13
  payload: TContractWithRoles extends {
14
14
  payload: infer Payload;
@@ -20,11 +20,7 @@ type TypedContext<TContractWithRoles extends ContractWithRoles> = RouteContext<T
20
20
  };
21
21
  export type ProxiedRouter<TRouter> = TRouter & Record<RequestMethod, <const TContractWithRoles extends ContractWithRoles, TCallback extends (TContractWithRoles extends {
22
22
  response: infer Response;
23
- } ? InferResponse<Response> : any) extends infer Response ? TContractWithRoles['roles'] extends infer AccessCondition ? AccessCondition extends true ? (context: TypedContext<TContractWithRoles> & {
24
- token: {
25
- authenticated: true;
26
- };
27
- }) => Response : (context: TypedContext<TContractWithRoles>) => Response : never : never>(exp: RouteUri, cb: TCallback, contract?: TContractWithRoles) => ReturnType<typeof registerRoute>>;
23
+ } ? InferResponse<Response> : any) extends infer Response ? (context: TypedContext<TContractWithRoles>) => Response : never>(exp: RouteUri, cb: TCallback, contract?: TContractWithRoles) => ReturnType<typeof registerRoute>>;
28
24
  export declare const matches: <TRequest extends GenericRequest>(req: TRequest, method: RequestMethod | RequestMethod[] | null, exp: string | RegExp, options: RouterOptions, config?: ApiConfig) => {
29
25
  fragments: string[];
30
26
  } | undefined;
@@ -33,20 +29,7 @@ export declare const wrapRouteExecution: (response: GenericResponse, cb: () => a
33
29
  export declare const createRouter: (options?: Partial<RouterOptions>) => ProxiedRouter<{
34
30
  route: <const TContractWithRoles extends ContractWithRoles, TCallback extends (TContractWithRoles extends {
35
31
  response: infer Response;
36
- } ? InferResponse<Response> : any) extends infer Response_1 ? TContractWithRoles["roles"] extends infer AccessCondition ? AccessCondition extends true ? (context: RouteContext<TContractWithRoles["roles"] extends infer AccessCondition_1 ? number extends keyof AccessCondition_1 ? AccessCondition_1[number] : AccessCondition_1 extends true ? any : AccessCondition_1 extends false ? never : AccessCondition_1 extends "unauthenticated-only" ? "guest" : AccessCondition_1 extends "unauthenticated" ? any : null : never> & {
37
- request: Omit<GenericRequest, "payload" | "query"> & {
38
- payload: TContractWithRoles extends {
39
- payload: infer Payload;
40
- } ? PackReferences<InferProperty<Payload>> : any;
41
- query: TContractWithRoles extends {
42
- query: infer Query;
43
- } ? InferProperty<Query> : any;
44
- };
45
- } & {
46
- token: {
47
- authenticated: true;
48
- };
49
- }) => Response_1 : (context: TypedContext<TContractWithRoles>) => Response_1 : never : never>(method: RequestMethod | RequestMethod[], exp: RouteUri, cb: TCallback, contract?: TContractWithRoles) => void;
32
+ } ? InferResponse<Response> : any) extends infer Response_1 ? (context: TypedContext<TContractWithRoles>) => Response_1 : never>(method: RequestMethod | RequestMethod[], exp: RouteUri, cb: TCallback, contract?: TContractWithRoles) => void;
50
33
  routes: ((_: unknown, context: RouteContext, groupOptions?: RouteGroupOptions) => ReturnType<typeof registerRoute>)[];
51
34
  routesMeta: RoutesMeta;
52
35
  group: <TRouter extends {
package/dist/routing.js CHANGED
@@ -10,20 +10,14 @@ const payload_js_1 = require("./payload.js");
10
10
  const next_js_1 = require("./next.js");
11
11
  const checkUnprocessable = (validationEither, context) => {
12
12
  if ((0, common_1.isLeft)(validationEither)) {
13
- context.response.writeHead(422, {
14
- 'content-type': 'application/json',
13
+ const validationError = (0, common_1.unwrapEither)(validationEither);
14
+ return context.error(types_1.HTTPStatus.UnprocessableContent, {
15
+ code: 'UNPROCESSABLE_ENTITY',
16
+ message: 'the provided payload is unprocessable',
17
+ details: validationError,
15
18
  });
16
- return validationEither;
17
19
  }
18
20
  };
19
- const unsufficientRoles = (context) => {
20
- context.response.writeHead(403, {
21
- 'content-type': 'application/json',
22
- });
23
- return {
24
- error: types_1.ACErrors.AuthorizationError,
25
- };
26
- };
27
21
  const matches = (req, method, exp, options, config) => {
28
22
  const base = config?.baseUrl && config.baseUrl !== '/'
29
23
  ? options.base
@@ -61,8 +55,7 @@ const registerRoute = async (context, method, exp, cb, contract, options = {}) =
61
55
  });
62
56
  }
63
57
  catch (err) {
64
- return context.error({
65
- httpCode: 500,
58
+ return context.error(types_1.HTTPStatus.UnprocessableContent, {
66
59
  code: 'INVALID_JSON',
67
60
  message: 'Invalid JSON',
68
61
  });
@@ -73,7 +66,10 @@ const registerRoute = async (context, method, exp, cb, contract, options = {}) =
73
66
  if (contract.roles) {
74
67
  const granted = (0, common_1.isGranted)(contract.roles, context.token);
75
68
  if (!granted) {
76
- return unsufficientRoles(context);
69
+ return context.error(types_1.HTTPStatus.Unauthorized, {
70
+ code: types_1.ACError.AuthorizationError,
71
+ message: 'your roles dont grant access to this route',
72
+ });
77
73
  }
78
74
  }
79
75
  if ('payload' in contract && contract.payload) {
@@ -199,8 +195,7 @@ const createRouter = (options = {}) => {
199
195
  router.install = async (context, options) => {
200
196
  const result = await routerPipe(undefined, context, options);
201
197
  if (exhaust && (result === undefined || (0, next_js_1.isNext)(result))) {
202
- return context.error({
203
- httpCode: 404,
198
+ return context.error(types_1.HTTPStatus.NotFound, {
204
199
  code: 'NOT_FOUND',
205
200
  message: 'Not found',
206
201
  });
package/dist/routing.mjs CHANGED
@@ -1,27 +1,21 @@
1
1
  "use strict";
2
2
  import { Stream } from "stream";
3
- import { ACErrors, REQUEST_METHODS } from "@aeriajs/types";
4
- import { pipe, isGranted, isLeft, deepMerge, error } from "@aeriajs/common";
3
+ import { ACError, HTTPStatus, REQUEST_METHODS } from "@aeriajs/types";
4
+ import { pipe, isGranted, isLeft, unwrapEither, deepMerge, error } 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
8
  import { isNext } from "./next.mjs";
9
9
  const checkUnprocessable = (validationEither, context) => {
10
10
  if (isLeft(validationEither)) {
11
- context.response.writeHead(422, {
12
- "content-type": "application/json"
11
+ const validationError = unwrapEither(validationEither);
12
+ return context.error(HTTPStatus.UnprocessableContent, {
13
+ code: "UNPROCESSABLE_ENTITY",
14
+ message: "the provided payload is unprocessable",
15
+ details: validationError
13
16
  });
14
- return validationEither;
15
17
  }
16
18
  };
17
- const unsufficientRoles = (context) => {
18
- context.response.writeHead(403, {
19
- "content-type": "application/json"
20
- });
21
- return {
22
- error: ACErrors.AuthorizationError
23
- };
24
- };
25
19
  export const matches = (req, method, exp, options, config) => {
26
20
  const base = config?.baseUrl && config.baseUrl !== "/" ? options.base ? `${config.baseUrl}${options.base}` : config.baseUrl : options.base ? options.base : "";
27
21
  if (method && method !== req.method) {
@@ -59,8 +53,7 @@ export const registerRoute = async (context, method, exp, cb, contract, options
59
53
  }
60
54
  );
61
55
  } catch (err) {
62
- return context.error({
63
- httpCode: 500,
56
+ return context.error(HTTPStatus.UnprocessableContent, {
64
57
  code: "INVALID_JSON",
65
58
  message: "Invalid JSON"
66
59
  });
@@ -71,7 +64,10 @@ export const registerRoute = async (context, method, exp, cb, contract, options
71
64
  if (contract.roles) {
72
65
  const granted = isGranted(contract.roles, context.token);
73
66
  if (!granted) {
74
- return unsufficientRoles(context);
67
+ return context.error(HTTPStatus.Unauthorized, {
68
+ code: ACError.AuthorizationError,
69
+ message: "your roles dont grant access to this route"
70
+ });
75
71
  }
76
72
  }
77
73
  if ("payload" in contract && contract.payload) {
@@ -195,8 +191,7 @@ export const createRouter = (options = {}) => {
195
191
  router.install = async (context, options2) => {
196
192
  const result = await routerPipe(void 0, context, options2);
197
193
  if (exhaust && (result === void 0 || isNext(result))) {
198
- return context.error({
199
- httpCode: 404,
194
+ return context.error(HTTPStatus.NotFound, {
200
195
  code: "NOT_FOUND",
201
196
  message: "Not found"
202
197
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/http",
3
- "version": "0.0.59",
3
+ "version": "0.0.61",
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.51",
32
- "@aeriajs/entrypoint": "^0.0.51",
33
- "@aeriajs/types": "^0.0.48",
34
- "@aeriajs/validation": "^0.0.54"
31
+ "@aeriajs/common": "^0.0.53",
32
+ "@aeriajs/entrypoint": "^0.0.53",
33
+ "@aeriajs/types": "^0.0.50",
34
+ "@aeriajs/validation": "^0.0.56"
35
35
  },
36
36
  "scripts": {
37
37
  "test": "echo skipping",