@telorun/http-server 0.1.5 → 0.1.7

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @telorun/http-server
2
2
 
3
+ ## 0.1.7
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies
8
+ - @telorun/sdk@0.2.7
9
+
3
10
  ## 0.1.5
4
11
 
5
12
  ### Patch Changes
@@ -13,6 +13,7 @@ declare const HttpApiRouteManifest: import("@sinclair/typebox").TObject<{
13
13
  }>>;
14
14
  }>;
15
15
  handler: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TUnsafe<KindRef<Invocable<Record<string, any>, any>>>>;
16
+ inputs: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TRecord<import("@sinclair/typebox").TString, import("@sinclair/typebox").TAny>>;
16
17
  response: import("@sinclair/typebox").TArray<import("@sinclair/typebox").TObject<{
17
18
  status: import("@sinclair/typebox").TInteger;
18
19
  when: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
@@ -40,6 +41,7 @@ declare const HttpApiManifest: import("@sinclair/typebox").TObject<{
40
41
  }>>;
41
42
  }>;
42
43
  handler: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TUnsafe<KindRef<Invocable<Record<string, any>, any>>>>;
44
+ inputs: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TRecord<import("@sinclair/typebox").TString, import("@sinclair/typebox").TAny>>;
43
45
  response: import("@sinclair/typebox").TArray<import("@sinclair/typebox").TObject<{
44
46
  status: import("@sinclair/typebox").TInteger;
45
47
  when: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
@@ -1,5 +1,5 @@
1
1
  import { Type } from "@sinclair/typebox";
2
- import { Ref } from "@telorun/sdk";
2
+ import { Ref, } from "@telorun/sdk";
3
3
  import { pipeline } from "stream/promises";
4
4
  const HttpApiRouteManifest = Type.Object({
5
5
  request: Type.Object({
@@ -13,6 +13,7 @@ const HttpApiRouteManifest = Type.Object({
13
13
  })),
14
14
  }),
15
15
  handler: Type.Optional(Type.Unsafe(Ref("kernel#Invocable"))),
16
+ inputs: Type.Optional(Type.Record(Type.String(), Type.Any())),
16
17
  response: Type.Array(Type.Object({
17
18
  status: Type.Integer({ minimum: 100, maximum: 599 }),
18
19
  when: Type.Optional(Type.String()),
@@ -146,7 +147,14 @@ export class HttpServerApi {
146
147
  body: request.body,
147
148
  },
148
149
  };
149
- const result = handler ? await handler.invoke(requestContext) : undefined;
150
+ const resolvedInputs = route.inputs
151
+ ? (this.ctx.moduleContext.expandWith(route.inputs, requestContext) ?? {})
152
+ : requestContext;
153
+ const invokeInput = {
154
+ ...resolvedInputs,
155
+ inputs: resolvedInputs,
156
+ };
157
+ const result = handler ? await handler.invoke(invokeInput) : undefined;
150
158
  return dispatchResponse(route.response, result, requestContext, this.ctx.moduleContext, this.ctx.validateSchema.bind(this.ctx), reply);
151
159
  }
152
160
  catch (error) {
@@ -1,10 +1,25 @@
1
1
  import type { Invocable, KindRef, ResourceContext, ResourceInstance, RuntimeResource } from "@telorun/sdk";
2
2
  import { ResponseEntry } from "./http-api-controller.js";
3
+ type CorsOptions = {
4
+ origin?: string | boolean | string[];
5
+ methods?: string | string[];
6
+ allowedHeaders?: string | string[];
7
+ exposedHeaders?: string | string[];
8
+ credentials?: boolean;
9
+ maxAge?: number;
10
+ cacheControl?: number | string;
11
+ preflightContinue?: boolean;
12
+ optionsSuccessStatus?: number;
13
+ preflight?: boolean;
14
+ strictPreflight?: boolean;
15
+ hideOptionsRoute?: boolean;
16
+ };
3
17
  type HttpServerResource = RuntimeResource & {
4
18
  host?: string;
5
19
  port?: number;
6
20
  baseUrl?: string;
7
21
  logger?: boolean;
22
+ cors?: CorsOptions;
8
23
  contentTypeParsers?: Array<{
9
24
  contentType: string;
10
25
  parser?: Invocable;
@@ -1,3 +1,4 @@
1
+ import cors from "@fastify/cors";
1
2
  import swagger from "@fastify/swagger";
2
3
  import apiReference from "@scalar/fastify-api-reference";
3
4
  import addFormats from "ajv-formats";
@@ -23,7 +24,10 @@ class HttpServer {
23
24
  if (!this.port) {
24
25
  throw new Error("Http.Server port is required");
25
26
  }
26
- this.app = Fastify({ logger: resource.logger, ajv: { plugins: [addFormats.default] } });
27
+ this.app = Fastify({
28
+ logger: resource.logger,
29
+ ajv: { customOptions: { useDefaults: true }, plugins: [addFormats.default] },
30
+ });
27
31
  }
28
32
  async init() {
29
33
  if (!this.pluginsInitialized) {
@@ -50,6 +54,22 @@ class HttpServer {
50
54
  });
51
55
  }
52
56
  }
57
+ if (this.resource.cors) {
58
+ await this.app.register(cors, {
59
+ origin: this.resource.cors.origin,
60
+ methods: this.resource.cors.methods,
61
+ allowedHeaders: this.resource.cors.allowedHeaders,
62
+ exposedHeaders: this.resource.cors.exposedHeaders,
63
+ credentials: this.resource.cors.credentials,
64
+ maxAge: this.resource.cors.maxAge,
65
+ cacheControl: this.resource.cors.cacheControl,
66
+ preflightContinue: this.resource.cors.preflightContinue,
67
+ optionsSuccessStatus: this.resource.cors.optionsSuccessStatus,
68
+ preflight: this.resource.cors.preflight,
69
+ strictPreflight: this.resource.cors.strictPreflight,
70
+ hideOptionsRoute: this.resource.cors.hideOptionsRoute,
71
+ });
72
+ }
53
73
  // Register custom error handler for validation errors
54
74
  this.app.setErrorHandler((error, request, reply) => {
55
75
  const mappedError = convertFastifyValidationError(error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@telorun/http-server",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -17,13 +17,14 @@
17
17
  }
18
18
  },
19
19
  "dependencies": {
20
+ "@fastify/cors": "^11.2.0",
20
21
  "@fastify/swagger": "^9.6.1",
21
22
  "@scalar/fastify-api-reference": "^1.44.6",
22
23
  "@sinclair/typebox": "^0.34.48",
23
24
  "ajv": "^8.17.1",
24
25
  "ajv-formats": "^3.0.1",
25
26
  "fastify": "^5.7.2",
26
- "@telorun/sdk": "0.2.6"
27
+ "@telorun/sdk": "0.2.7"
27
28
  },
28
29
  "devDependencies": {
29
30
  "@types/node": "^20.0.0",
@@ -1,5 +1,12 @@
1
1
  import { Static, Type } from "@sinclair/typebox";
2
- import { ControllerContext, Invocable, KindRef, Ref, ResourceContext, ResourceInstance } from "@telorun/sdk";
2
+ import {
3
+ ControllerContext,
4
+ Invocable,
5
+ KindRef,
6
+ Ref,
7
+ ResourceContext,
8
+ ResourceInstance,
9
+ } from "@telorun/sdk";
3
10
  import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
4
11
  import { type Readable } from "stream";
5
12
  import { pipeline } from "stream/promises";
@@ -18,6 +25,7 @@ const HttpApiRouteManifest = Type.Object({
18
25
  ),
19
26
  }),
20
27
  handler: Type.Optional(Type.Unsafe<KindRef<Invocable>>(Ref("kernel#Invocable"))),
28
+ inputs: Type.Optional(Type.Record(Type.String(), Type.Any())),
21
29
  response: Type.Array(
22
30
  Type.Object({
23
31
  status: Type.Integer({ minimum: 100, maximum: 599 }),
@@ -189,7 +197,14 @@ export class HttpServerApi implements ResourceInstance {
189
197
  body: request.body,
190
198
  },
191
199
  };
192
- const result = handler ? await handler.invoke(requestContext) : undefined;
200
+ const resolvedInputs: Record<string, any> = route.inputs
201
+ ? ((this.ctx.moduleContext.expandWith(route.inputs, requestContext) as any) ?? {})
202
+ : requestContext;
203
+ const invokeInput: Record<string, any> = {
204
+ ...resolvedInputs,
205
+ inputs: resolvedInputs,
206
+ };
207
+ const result = handler ? await handler.invoke(invokeInput) : undefined;
193
208
 
194
209
  return dispatchResponse(
195
210
  route.response,
@@ -1,3 +1,4 @@
1
+ import cors from "@fastify/cors";
1
2
  import swagger from "@fastify/swagger";
2
3
  import apiReference from "@scalar/fastify-api-reference";
3
4
  import type {
@@ -11,11 +12,27 @@ import addFormats from "ajv-formats";
11
12
  import Fastify, { FastifyInstance } from "fastify";
12
13
  import { dispatchResponse, HttpServerApi, ResponseEntry } from "./http-api-controller.js";
13
14
 
15
+ type CorsOptions = {
16
+ origin?: string | boolean | string[];
17
+ methods?: string | string[];
18
+ allowedHeaders?: string | string[];
19
+ exposedHeaders?: string | string[];
20
+ credentials?: boolean;
21
+ maxAge?: number;
22
+ cacheControl?: number | string;
23
+ preflightContinue?: boolean;
24
+ optionsSuccessStatus?: number;
25
+ preflight?: boolean;
26
+ strictPreflight?: boolean;
27
+ hideOptionsRoute?: boolean;
28
+ };
29
+
14
30
  type HttpServerResource = RuntimeResource & {
15
31
  host?: string;
16
32
  port?: number;
17
33
  baseUrl?: string;
18
34
  logger?: boolean;
35
+ cors?: CorsOptions;
19
36
  contentTypeParsers?: Array<{ contentType: string; parser?: Invocable }>;
20
37
  openapi?: {
21
38
  info: {
@@ -66,7 +83,10 @@ class HttpServer implements ResourceInstance {
66
83
  if (!this.port) {
67
84
  throw new Error("Http.Server port is required");
68
85
  }
69
- this.app = Fastify({ logger: resource.logger, ajv: { plugins: [addFormats.default as any] } });
86
+ this.app = Fastify({
87
+ logger: resource.logger,
88
+ ajv: { customOptions: { useDefaults: true }, plugins: [addFormats.default as any] },
89
+ });
70
90
  }
71
91
 
72
92
  async init() {
@@ -80,13 +100,17 @@ class HttpServer implements ResourceInstance {
80
100
  private async setupPlugins() {
81
101
  for (const { contentType, parser } of this.resource.contentTypeParsers ?? []) {
82
102
  if (parser) {
83
- this.app.addContentTypeParser(contentType, { parseAs: "string" }, async (_req, body, done) => {
84
- try {
85
- done(null, await parser.invoke({ body }));
86
- } catch (err) {
87
- done(err as Error, undefined);
88
- }
89
- });
103
+ this.app.addContentTypeParser(
104
+ contentType,
105
+ { parseAs: "string" },
106
+ async (_req, body, done) => {
107
+ try {
108
+ done(null, await parser.invoke({ body }));
109
+ } catch (err) {
110
+ done(err as Error, undefined);
111
+ }
112
+ },
113
+ );
90
114
  } else {
91
115
  this.app.addContentTypeParser(contentType, { parseAs: "string" }, (_req, body, done) => {
92
116
  done(null, body);
@@ -94,6 +118,23 @@ class HttpServer implements ResourceInstance {
94
118
  }
95
119
  }
96
120
 
121
+ if (this.resource.cors) {
122
+ await this.app.register(cors, {
123
+ origin: this.resource.cors.origin,
124
+ methods: this.resource.cors.methods,
125
+ allowedHeaders: this.resource.cors.allowedHeaders,
126
+ exposedHeaders: this.resource.cors.exposedHeaders,
127
+ credentials: this.resource.cors.credentials,
128
+ maxAge: this.resource.cors.maxAge,
129
+ cacheControl: this.resource.cors.cacheControl,
130
+ preflightContinue: this.resource.cors.preflightContinue,
131
+ optionsSuccessStatus: this.resource.cors.optionsSuccessStatus,
132
+ preflight: this.resource.cors.preflight,
133
+ strictPreflight: this.resource.cors.strictPreflight,
134
+ hideOptionsRoute: this.resource.cors.hideOptionsRoute,
135
+ });
136
+ }
137
+
97
138
  // Register custom error handler for validation errors
98
139
  this.app.setErrorHandler((error, request, reply) => {
99
140
  const mappedError = convertFastifyValidationError(error);