@ez4/gateway 0.20.0 → 0.22.0

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.
@@ -59,24 +59,28 @@ export interface HttpPathParameters {
59
59
  export interface HttpQueryStrings {
60
60
  }
61
61
  /**
62
- * Request body payload.
62
+ * Json body payload.
63
63
  */
64
64
  export interface HttpJsonBody {
65
65
  }
66
+ /**
67
+ * Raw body payload.
68
+ */
69
+ export type HttpRawBody = string;
66
70
  /**
67
71
  * Authorizer request.
68
72
  */
69
73
  export interface HttpAuthRequest {
70
74
  /**
71
- * Expected headers.
75
+ * Expected HTTP headers.
72
76
  */
73
77
  headers?: HttpHeaders;
74
78
  /**
75
- * Expected path parameters.
79
+ * Expected HTTP path parameters.
76
80
  */
77
81
  parameters?: HttpPathParameters;
78
82
  /**
79
- * Expected query strings.
83
+ * Expected HTTP query strings.
80
84
  */
81
85
  query?: HttpQueryStrings;
82
86
  }
@@ -93,43 +97,43 @@ export interface HttpAuthResponse {
93
97
  * HTTP request.
94
98
  */
95
99
  export interface HttpRequest {
96
- /**
97
- * Expected headers.
98
- */
99
- headers?: HttpHeaders;
100
100
  /**
101
101
  * Expected identity.
102
102
  */
103
103
  identity?: HttpIdentity;
104
104
  /**
105
- * Expected path parameters.
105
+ * Expected HTTP headers.
106
+ */
107
+ headers?: HttpHeaders;
108
+ /**
109
+ * Expected HTTP path parameters.
106
110
  */
107
111
  parameters?: HttpPathParameters;
108
112
  /**
109
- * Expected query strings.
113
+ * Expected HTTP query strings.
110
114
  */
111
115
  query?: HttpQueryStrings;
112
116
  /**
113
- * Expected JSON body payload.
117
+ * Expected HTTP body payload.
114
118
  */
115
- body?: HttpJsonBody;
119
+ body?: HttpJsonBody | HttpRawBody;
116
120
  }
117
121
  /**
118
122
  * HTTP response.
119
123
  */
120
124
  export interface HttpResponse {
121
125
  /**
122
- * Response status code.
126
+ * HTTP status code.
123
127
  */
124
128
  status: number;
125
129
  /**
126
- * Response headers.
130
+ * HTTP headers.
127
131
  */
128
132
  headers?: HttpHeaders;
129
133
  /**
130
- * Response body.
134
+ * HTTP body payload.
131
135
  */
132
- body?: HttpJsonBody;
136
+ body?: HttpJsonBody | HttpRawBody;
133
137
  }
134
138
  export interface HttpProvider {
135
139
  /**
@@ -145,6 +149,10 @@ export type HttpIncoming<T extends HttpRequest | HttpAuthRequest> = T & {
145
149
  * Request tracking Id.
146
150
  */
147
151
  requestId: string;
152
+ /**
153
+ * Determines whether request is base64 encoded or not.
154
+ */
155
+ encoded?: boolean;
148
156
  /**
149
157
  * Request timestamp.
150
158
  */
@@ -1,7 +1,8 @@
1
1
  import type { Service } from '@ez4/common';
2
2
  import type { LinkedVariables } from '@ez4/project/library';
3
3
  import type { HttpPath } from '../types/common.js';
4
- import type { HttpHeaders, HttpIdentity, HttpPathParameters, HttpQueryStrings, HttpJsonBody, HttpAuthRequest, HttpAuthResponse, HttpRequest, HttpResponse, HttpErrors, HttpProvider, HttpIncoming, HttpListener, HttpAuthorizer, HttpHandler, HttpCache, HttpCors } from './common.js';
4
+ import type { HttpSuccessStatuses, HttpEmptySuccessResponse, HttpSuccessResponse } from './utils.js';
5
+ import type { HttpHeaders, HttpIdentity, HttpPathParameters, HttpQueryStrings, HttpJsonBody, HttpRawBody, HttpAuthRequest, HttpAuthResponse, HttpRequest, HttpResponse, HttpErrors, HttpProvider, HttpIncoming, HttpListener, HttpAuthorizer, HttpHandler, HttpCache, HttpCors } from './common.js';
5
6
  /**
6
7
  * Provide all contracts for a self-managed HTTP service.
7
8
  */
@@ -11,6 +12,7 @@ export declare namespace Http {
11
12
  type PathParameters = HttpPathParameters;
12
13
  type QueryStrings = HttpQueryStrings;
13
14
  type JsonBody = HttpJsonBody;
15
+ type RawBody = HttpRawBody;
14
16
  type AuthRequest = HttpAuthRequest;
15
17
  type Request = HttpRequest;
16
18
  type AuthResponse = HttpAuthResponse;
@@ -24,6 +26,8 @@ export declare namespace Http {
24
26
  type Authorizer<T extends AuthRequest> = HttpAuthorizer<T>;
25
27
  type Handler<T extends Request> = HttpHandler<T>;
26
28
  type ServiceEvent<T extends Request | AuthRequest = Request> = Service.Event<Incoming<T>>;
29
+ type EmptySuccessResponse<S extends HttpSuccessStatuses = 204> = HttpEmptySuccessResponse<S>;
30
+ type SuccessResponse<S extends HttpSuccessStatuses, T extends HttpRawBody | HttpJsonBody> = HttpSuccessResponse<S, T>;
27
31
  /**
28
32
  * HTTP route.
29
33
  */
@@ -44,6 +48,10 @@ export declare namespace Http {
44
48
  * Route handler.
45
49
  */
46
50
  handler: Handler<T>;
51
+ /**
52
+ * Default log retention (in days) for the handlers.
53
+ */
54
+ logRetention?: number;
47
55
  /**
48
56
  * Map status codes and errors for all known exceptions.
49
57
  */
@@ -0,0 +1,24 @@
1
+ import type { HttpResponse, HttpJsonBody, HttpRawBody } from './common.js';
2
+ export type HttpSuccessStatuses = 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208;
3
+ /**
4
+ * Common HTTP success response without a response body.
5
+ */
6
+ export declare class HttpEmptySuccessResponse<S extends HttpSuccessStatuses> implements HttpResponse {
7
+ /**
8
+ * HTTP status code.
9
+ */
10
+ status: S;
11
+ }
12
+ /**
13
+ * Common HTTP success response.
14
+ */
15
+ export declare class HttpSuccessResponse<S extends HttpSuccessStatuses, T extends HttpJsonBody | HttpRawBody> implements HttpResponse {
16
+ /**
17
+ * HTTP status code.
18
+ */
19
+ status: S;
20
+ /**
21
+ * HTTP response payload.
22
+ */
23
+ body: T;
24
+ }
@@ -16,7 +16,7 @@ export type HttpRequest = {
16
16
  identity?: ObjectSchema | UnionSchema | null;
17
17
  parameters?: ObjectSchema | null;
18
18
  query?: ObjectSchema | null;
19
- body?: ObjectSchema | UnionSchema | ArraySchema | null;
19
+ body?: ObjectSchema | UnionSchema | ArraySchema | ScalarSchema | null;
20
20
  };
21
21
  export type HttpResponse = {
22
22
  status: number;
@@ -0,0 +1,4 @@
1
+ import type { AnySchema } from '@ez4/schema';
2
+ import type { Http } from '../services/contract.js';
3
+ export declare const getRequestBody: <T extends Http.JsonBody | Http.RawBody>(input: T, schema: AnySchema) => Promise<T>;
4
+ export declare const getResponseBody: (body: unknown, schema: AnySchema) => unknown;
@@ -0,0 +1,13 @@
1
+ import type { HttpError } from '@ez4/gateway';
2
+ /**
3
+ * Get a JSON error response for the given HTTP error.
4
+ *
5
+ * @returns Returns an error response containing `status`, `message` and `details`.
6
+ */
7
+ export declare const getJsonError: ({ status, message, details }: HttpError) => {
8
+ status: number;
9
+ body: {
10
+ message: string;
11
+ details: string[] | undefined;
12
+ };
13
+ };
@@ -0,0 +1,3 @@
1
+ import type { ObjectSchema } from '@ez4/schema';
2
+ import type { Http } from '@ez4/gateway';
3
+ export declare const getHeaders: <T extends Http.Headers>(input: T, schema: ObjectSchema) => Promise<T>;
@@ -0,0 +1,3 @@
1
+ import type { ObjectSchema, UnionSchema } from '@ez4/schema';
2
+ import type { Http } from '@ez4/gateway';
3
+ export declare const getIdentity: <T extends Http.Identity>(input: T, schema: ObjectSchema | UnionSchema) => Promise<T>;
@@ -0,0 +1,3 @@
1
+ import type { ObjectSchema } from '@ez4/schema';
2
+ import type { Http } from '@ez4/gateway';
3
+ export declare const getPathParameters: <T extends Http.PathParameters>(input: T, schema: ObjectSchema) => Promise<T>;
@@ -0,0 +1,3 @@
1
+ import type { ObjectSchema } from '@ez4/schema';
2
+ import type { Http } from '@ez4/gateway';
3
+ export declare const getQueryStrings: <T extends Http.QueryStrings>(input: T, schema: ObjectSchema) => Promise<T>;
package/dist/utils.cjs ADDED
@@ -0,0 +1,23 @@
1
+ "use strict";var y=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var s=(e,t)=>y(e,"name",{value:t,configurable:!0});var b=(e,t)=>{for(var r in t)y(e,r,{get:t[r],enumerable:!0})},E=(e,t,r,o)=>{if(t&&
2
+ typeof t=="object"||typeof t=="function")for(let a of q(t))!S.call(e,a)&&a!==r&&
3
+ y(e,a,{get:()=>t[a],enumerable:!(o=H(t,a))||o.enumerable});return e};var v=e=>E(y({},"__esModule",{value:!0}),e);var R={};b(R,{getHeaders:()=>j,getIdentity:()=>B,getJsonError:()=>O,getPathParameters:()=>z,
4
+ getQueryStrings:()=>M,getRequestBody:()=>C,getResponseBody:()=>P});module.exports=
5
+ v(R);var m=require("@ez4/validator"),d=require("@ez4/transform"),x=require("@ez4/gateway");var z=s(async(e,t)=>{let r=(0,d.transform)(e,t,(0,d.createTransformContext)({convert:!1})),
6
+ o=await(0,m.validate)(r,t,(0,m.createValidatorContext)({property:"$path"}));if(o.
7
+ length){let a=(0,m.getUniqueErrorMessages)(o);throw new x.HttpBadRequestError("M\
8
+ alformed path parameters.",a)}return r},"getPathParameters");var n=require("@ez4/validator"),g=require("@ez4/transform"),l=require("@ez4/gateway");var M=s(async(e,t)=>{let r=(0,g.transform)(e,t,(0,g.createTransformContext)({convert:!0})),
9
+ o=await(0,n.validate)(r,t,(0,n.createValidatorContext)({property:"$query"}));if(o.
10
+ length){let a=(0,n.getUniqueErrorMessages)(o);throw new l.HttpBadRequestError("M\
11
+ alformed query strings.",a)}return r},"getQueryStrings");var p=require("@ez4/validator"),h=require("@ez4/transform"),u=require("@ez4/gateway");var j=s(async(e,t)=>{let r=(0,h.transform)(e,t,(0,h.createTransformContext)({convert:!1})),
12
+ o=await(0,p.validate)(r,t,(0,p.createValidatorContext)({property:"$header"}));if(o.
13
+ length){let a=(0,p.getUniqueErrorMessages)(o);throw new u.HttpBadRequestError("M\
14
+ alformed request headers.",a)}return r},"getHeaders");var i=require("@ez4/validator"),T=require("@ez4/gateway");var B=s(async(e,t)=>{let r=await(0,i.validate)(e,t,(0,i.createValidatorContext)(
15
+ {property:"$identity"}));if(r.length){let o=(0,i.getUniqueErrorMessages)(r);throw new T.HttpBadRequestError(
16
+ "Malformed authorizer identity.",o)}return e},"getIdentity");var c=require("@ez4/validator"),f=require("@ez4/transform"),w=require("@ez4/gateway");var C=s(async(e,t)=>{let r=await(0,c.validate)(e,t,(0,c.createValidatorContext)(
17
+ {property:"$body"}));if(r.length){let a=(0,c.getUniqueErrorMessages)(r);throw new w.HttpBadRequestError(
18
+ "Malformed body payload.",a)}return(0,f.transform)(e,t,(0,f.createTransformContext)(
19
+ {convert:!1}))},"getRequestBody"),P=s((e,t)=>(0,f.transform)(e,t,(0,f.createTransformContext)(
20
+ {convert:!1})),"getResponseBody");var O=s(({status:e,message:t,details:r})=>({status:e,body:{message:t,details:r}}),
21
+ "getJsonError");0&&(module.exports={getHeaders,getIdentity,getJsonError,getPathParameters,getQueryStrings,
22
+ getRequestBody,getResponseBody});
23
+ //# sourceMappingURL=utils.cjs.map
@@ -0,0 +1,6 @@
1
+ export * from './utils/parameters.js';
2
+ export * from './utils/query.js';
3
+ export * from './utils/headers.js';
4
+ export * from './utils/identity.js';
5
+ export * from './utils/body.js';
6
+ export * from './utils/errors.js';
package/dist/utils.mjs ADDED
@@ -0,0 +1,19 @@
1
+ var p=Object.defineProperty;var o=(e,t)=>p(e,"name",{value:t,configurable:!0});import{validate as i,getUniqueErrorMessages as c,createValidatorContext as f}from"@ez4/validator";
2
+ import{createTransformContext as y,transform as d}from"@ez4/transform";import{HttpBadRequestError as g}from"@ez4/gateway";var I=o(async(e,t)=>{let r=d(e,t,y({convert:!1})),a=await i(r,t,f({property:"$pa\
3
+ th"}));if(a.length){let s=c(a);throw new g("Malformed path parameters.",s)}return r},
4
+ "getPathParameters");import{validate as h,createValidatorContext as x,getUniqueErrorMessages as l}from"@ez4/validator";
5
+ import{createTransformContext as u,transform as T}from"@ez4/transform";import{HttpBadRequestError as w}from"@ez4/gateway";var G=o(async(e,t)=>{let r=T(e,t,u({convert:!0})),a=await h(r,t,x({property:"$qu\
6
+ ery"}));if(a.length){let s=l(a);throw new w("Malformed query strings.",s)}return r},
7
+ "getQueryStrings");import{validate as H,createValidatorContext as q,getUniqueErrorMessages as S}from"@ez4/validator";
8
+ import{createTransformContext as b,transform as E}from"@ez4/transform";import{HttpBadRequestError as v}from"@ez4/gateway";var Y=o(async(e,t)=>{let r=E(e,t,b({convert:!1})),a=await H(r,t,q({property:"$he\
9
+ ader"}));if(a.length){let s=S(a);throw new v("Malformed request headers.",s)}return r},
10
+ "getHeaders");import{validate as z,createValidatorContext as M,getUniqueErrorMessages as j}from"@ez4/validator";
11
+ import{HttpBadRequestError as B}from"@ez4/gateway";var rt=o(async(e,t)=>{let r=await z(e,t,M({property:"$identity"}));if(r.length){
12
+ let a=j(r);throw new B("Malformed authorizer identity.",a)}return e},"getIdentit\
13
+ y");import{validate as C,createValidatorContext as P,getUniqueErrorMessages as O}from"@ez4/validator";
14
+ import{createTransformContext as m,transform as n}from"@ez4/transform";import{HttpBadRequestError as R}from"@ez4/gateway";var pt=o(async(e,t)=>{let r=await C(e,t,P({property:"$body"}));if(r.length){let s=O(
15
+ r);throw new R("Malformed body payload.",s)}return n(e,t,m({convert:!1}))},"getR\
16
+ equestBody"),it=o((e,t)=>n(e,t,m({convert:!1})),"getResponseBody");var yt=o(({status:e,message:t,details:r})=>({status:e,body:{message:t,details:r}}),
17
+ "getJsonError");export{Y as getHeaders,rt as getIdentity,yt as getJsonError,I as getPathParameters,
18
+ G as getQueryStrings,pt as getRequestBody,it as getResponseBody};
19
+ //# sourceMappingURL=utils.mjs.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ez4/gateway",
3
3
  "description": "EZ4: Components to build gateway services",
4
- "version": "0.20.0",
4
+ "version": "0.22.0",
5
5
  "author": "Silas B.",
6
6
  "license": "MIT",
7
7
  "type": "module",
@@ -14,7 +14,7 @@
14
14
  "directory": "packages/gateway"
15
15
  },
16
16
  "engines": {
17
- "node": ">=22.3.0"
17
+ "node": ">=22.7"
18
18
  },
19
19
  "exports": {
20
20
  ".": {
@@ -26,6 +26,11 @@
26
26
  "types": "./dist/library.d.ts",
27
27
  "require": "./dist/library.cjs",
28
28
  "import": "./dist/library.mjs"
29
+ },
30
+ "./utils": {
31
+ "types": "./dist/utils.d.ts",
32
+ "require": "./dist/utils.cjs",
33
+ "import": "./dist/utils.mjs"
29
34
  }
30
35
  },
31
36
  "workspaces": [
@@ -42,10 +47,10 @@
42
47
  "live:publish": "npm run test && npm publish --access public"
43
48
  },
44
49
  "dependencies": {
45
- "@ez4/common": "^0.20.0",
46
- "@ez4/project": "^0.20.0",
47
- "@ez4/reflection": "^0.20.0",
48
- "@ez4/schema": "^0.20.0",
49
- "@ez4/utils": "^0.20.0"
50
+ "@ez4/common": "^0.22.0",
51
+ "@ez4/project": "^0.22.0",
52
+ "@ez4/reflection": "^0.22.0",
53
+ "@ez4/schema": "^0.22.0",
54
+ "@ez4/utils": "^0.22.0"
50
55
  }
51
56
  }