@miqro/core 1.5.4 → 1.5.6

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/proxy.d.ts CHANGED
@@ -1,7 +1,16 @@
1
- import { Context, Handler } from "./common";
2
- import { RequestOptions, RequestResponse } from "@miqro/request";
3
- export interface ProxyServiceInterface {
4
- resolve(ctx: Context): Promise<RequestOptions>;
1
+ import { Context } from "./common";
2
+ import { Router, RouterOptions } from "./router";
3
+ import { PathPartToken } from "./common/tokenize-match";
4
+ export interface ProxyRouterOptions extends RouterOptions {
5
+ url: string;
5
6
  }
6
- export declare const ProxyHandler: (resolver: ProxyServiceInterface) => Handler<RequestResponse>;
7
- export declare const ProxyResponseHandler: () => Handler<void>;
7
+ declare class ProxyRouter extends Router {
8
+ private proxyURL;
9
+ constructor(options: ProxyRouterOptions);
10
+ run(ctx: Context, pathTokens?: {
11
+ value: string;
12
+ lower: string;
13
+ }[], prePathTokens?: PathPartToken[]): Promise<boolean>;
14
+ }
15
+ export declare const Proxy: (options: ProxyRouterOptions) => ProxyRouter;
16
+ export {};
package/dist/proxy.js CHANGED
@@ -1,39 +1,45 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ProxyResponseHandler = exports.ProxyHandler = void 0;
3
+ exports.Proxy = void 0;
4
4
  const request_1 = require("@miqro/request");
5
- const ProxyHandler = (resolver) => async function ProxyHandler(ctx) {
6
- const requestConfig = await resolver.resolve(ctx);
7
- try {
8
- ctx.logger.debug("proxy resolveRequest to [%o]", requestConfig);
9
- const response = await (0, request_1.request)(requestConfig);
10
- ctx.logger.trace("response %o", response);
11
- return response;
5
+ const router_1 = require("./router");
6
+ const tokenize_match_1 = require("./common/tokenize-match");
7
+ const url_1 = require("url");
8
+ class ProxyRouter extends router_1.Router {
9
+ constructor(options) {
10
+ super(options);
11
+ const proxyURL = new url_1.URL(options.url);
12
+ proxyURL.pathname = (0, tokenize_match_1.normalizePath)(proxyURL.pathname);
13
+ this.proxyURL = proxyURL;
12
14
  }
13
- catch (e) {
14
- ctx.logger.error("Error connecting to endpoint in [%s] [%s]", requestConfig.url, e.message);
15
- if (e.status && e.headers && e.data) {
16
- return e;
15
+ async run(ctx, pathTokens, prePathTokens = []) {
16
+ const level = prePathTokens.length;
17
+ const shouldContinue = await super.run(ctx, pathTokens, prePathTokens);
18
+ if (shouldContinue) {
19
+ const relative = ctx.path.split("/").map(s => s.trim()).filter(s => s).slice(level).join("/");
20
+ const nUrl = new url_1.URL(this.proxyURL.toString());
21
+ nUrl.pathname = nUrl.pathname + relative;
22
+ const url = nUrl.toString();
23
+ ctx.logger.info("proxy to %s", url.toString());
24
+ const response = await (0, request_1.request)({
25
+ url: url.toString(),
26
+ disableThrow: true,
27
+ disableUserAgent: true,
28
+ method: ctx.method,
29
+ headers: ctx.headers,
30
+ data: ctx.method !== "GET" ? ctx.buffer : undefined
31
+ });
32
+ await ctx.end({
33
+ headers: response.headers,
34
+ body: response.data,
35
+ status: response.status
36
+ });
17
37
  }
18
- else {
19
- throw e;
20
- }
21
- }
22
- };
23
- exports.ProxyHandler = ProxyHandler;
24
- const ProxyResponseHandler = () => async function ProxyResponseHandler(ctx) {
25
- if (!ctx.results || ctx.results.length === 0) {
26
- throw new Error(`no response to send. Is your handler returning a value differente than boolean 'true|false' ?`);
27
- }
28
- else {
29
- const lastResult = ctx.results[ctx.results.length - 1];
30
- ctx.logger.debug("response %o", lastResult);
31
- await ctx.end({
32
- headers: lastResult.headers,
33
- body: lastResult.data,
34
- status: lastResult.status
35
- });
38
+ return !ctx.res.headersSent;
36
39
  }
40
+ }
41
+ const Proxy = (options) => {
42
+ return new ProxyRouter(options);
37
43
  };
38
- exports.ProxyResponseHandler = ProxyResponseHandler;
44
+ exports.Proxy = Proxy;
39
45
  //# sourceMappingURL=proxy.js.map
package/dist/proxy.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"proxy.js","sourceRoot":"","sources":["../src/pro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