@openapi-typescript-infra/service 3.0.0 → 3.0.2
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 +15 -0
- package/build/bootstrap.d.ts +3 -2
- package/build/bootstrap.js.map +1 -1
- package/build/config/schema.d.ts +2 -1
- package/build/development/repl.d.ts +3 -2
- package/build/development/repl.js.map +1 -1
- package/build/error.d.ts +4 -3
- package/build/error.js.map +1 -1
- package/build/express-app/app.d.ts +7 -6
- package/build/express-app/app.js +5 -2
- package/build/express-app/app.js.map +1 -1
- package/build/express-app/internal-server.d.ts +3 -2
- package/build/express-app/internal-server.js.map +1 -1
- package/build/express-app/route-loader.d.ts +3 -2
- package/build/express-app/route-loader.js.map +1 -1
- package/build/express-app/types.d.ts +4 -3
- package/build/hook.d.ts +4 -3
- package/build/hook.js +1 -1
- package/build/hook.js.map +1 -1
- package/build/index.d.ts +0 -1
- package/build/index.js +0 -1
- package/build/index.js.map +1 -1
- package/build/openapi.d.ts +3 -2
- package/build/openapi.js.map +1 -1
- package/build/telemetry/index.d.ts +3 -2
- package/build/telemetry/index.js.map +1 -1
- package/build/telemetry/requestLogger.d.ts +4 -3
- package/build/telemetry/requestLogger.js.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/build/types.d.ts +13 -11
- package/package.json +4 -5
- package/src/bootstrap.ts +3 -2
- package/src/config/schema.ts +2 -1
- package/src/development/repl.ts +6 -2
- package/src/error.ts +6 -3
- package/src/express-app/app.ts +21 -13
- package/src/express-app/internal-server.ts +6 -3
- package/src/express-app/route-loader.ts +5 -2
- package/src/express-app/types.ts +5 -3
- package/src/hook.ts +4 -3
- package/src/index.ts +0 -1
- package/src/openapi.ts +6 -3
- package/src/telemetry/index.ts +3 -1
- package/src/telemetry/requestLogger.ts +9 -12
- package/src/types.ts +32 -13
- package/build/service-calls/index.d.ts +0 -16
- package/build/service-calls/index.js +0 -85
- package/build/service-calls/index.js.map +0 -1
- package/src/service-calls/index.ts +0 -121
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.throwOrGetResponse = exports.createServiceInterface = void 0;
|
|
7
|
-
const node_url_1 = require("node:url");
|
|
8
|
-
const eventsource_1 = __importDefault(require("eventsource"));
|
|
9
|
-
const error_1 = require("../error");
|
|
10
|
-
class CustomEventSource extends eventsource_1.default {
|
|
11
|
-
activeListeners = [];
|
|
12
|
-
addEventListener(name, handler) {
|
|
13
|
-
super.addEventListener(name, handler);
|
|
14
|
-
this.activeListeners.push({ name, handler });
|
|
15
|
-
return this;
|
|
16
|
-
}
|
|
17
|
-
removeAllListeners() {
|
|
18
|
-
this.activeListeners.forEach((l) => {
|
|
19
|
-
super.removeEventListener(l.name, l.handler);
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Return a factory that will make instances of an OpenAPI/Swagger client for each request
|
|
25
|
-
*/
|
|
26
|
-
function createServiceInterface(service, name, Implementation) {
|
|
27
|
-
const appConfig = service.locals.config;
|
|
28
|
-
const config = {
|
|
29
|
-
...(appConfig.get('connections:default') || {}),
|
|
30
|
-
...(appConfig.get(`connections:${name}`) || {}),
|
|
31
|
-
};
|
|
32
|
-
const protocol = config?.protocol || 'http';
|
|
33
|
-
const port = config?.port || 8000;
|
|
34
|
-
const host = config?.host || name;
|
|
35
|
-
const baseUrl = `${protocol}${protocol.endsWith(':') ? '//' : '://'}${host}:${port}${config?.basePath || ''}`;
|
|
36
|
-
const fetchConfig = {
|
|
37
|
-
fetch,
|
|
38
|
-
AbortController,
|
|
39
|
-
EventSource: CustomEventSource,
|
|
40
|
-
FormData,
|
|
41
|
-
baseUrl,
|
|
42
|
-
};
|
|
43
|
-
// In development, it can be useful to route requests through
|
|
44
|
-
// a centralized local proxy (we use https://github.com/gas-buddy/container-proxy).
|
|
45
|
-
// This allows you to run a subset of services locally and route the rest
|
|
46
|
-
// of the requests to another (typically remote) environment.
|
|
47
|
-
if (config?.proxy) {
|
|
48
|
-
const proxyUrl = new node_url_1.URL(config.proxy);
|
|
49
|
-
const proxyPort = proxyUrl.protocol === 'https:' ? '8443' : '8000';
|
|
50
|
-
fetchConfig.requestInterceptor = (params) => {
|
|
51
|
-
const parsedUrl = new node_url_1.URL(params.url);
|
|
52
|
-
const proto = parsedUrl.protocol.replace(/:$/, '');
|
|
53
|
-
const defaultPort = proto === 'https' ? 8443 : 8000;
|
|
54
|
-
const headers = {};
|
|
55
|
-
headers.host = `${proto}.${parsedUrl.hostname}.${port || defaultPort}`;
|
|
56
|
-
headers.source = service.locals.name;
|
|
57
|
-
parsedUrl.hostname = proxyUrl.hostname;
|
|
58
|
-
parsedUrl.protocol = proxyUrl.protocol;
|
|
59
|
-
parsedUrl.port = proxyUrl.port || proxyPort;
|
|
60
|
-
// eslint-disable-next-line no-param-reassign
|
|
61
|
-
params.headers = params.headers || {};
|
|
62
|
-
Object.assign(params.headers, headers);
|
|
63
|
-
// eslint-disable-next-line no-param-reassign
|
|
64
|
-
params.url = parsedUrl.href;
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
return new Implementation(fetchConfig);
|
|
68
|
-
}
|
|
69
|
-
exports.createServiceInterface = createServiceInterface;
|
|
70
|
-
function readResponse(app, response, errorSpec) {
|
|
71
|
-
if (response.responseType === 'response') {
|
|
72
|
-
return response;
|
|
73
|
-
}
|
|
74
|
-
const { message, ...spec } = errorSpec || {};
|
|
75
|
-
throw new error_1.ServiceError(app, message || response.body.message || 'Internal Error', {
|
|
76
|
-
status: response.status,
|
|
77
|
-
...spec,
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
async function throwOrGetResponse(app, exec, errorSpec) {
|
|
81
|
-
const response = await exec();
|
|
82
|
-
return readResponse(app, response, errorSpec);
|
|
83
|
-
}
|
|
84
|
-
exports.throwOrGetResponse = throwOrGetResponse;
|
|
85
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/service-calls/index.ts"],"names":[],"mappings":";;;;;;AAAA,uCAA+B;AAG/B,8DAAsC;AAEtC,oCAA0D;AAM1D,MAAM,iBAAkB,SAAQ,qBAAW;IACjC,eAAe,GAAgE,EAAE,CAAC;IAE1F,gBAAgB,CAAI,IAAY,EAAE,OAAwC;QACxE,KAAK,CAAC,gBAAgB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QACtC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;QAC7C,OAAO,IAAI,CAAC;IACd,CAAC;IAED,kBAAkB;QAChB,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE;YACjC,KAAK,CAAC,mBAAmB,CAAC,CAAC,CAAC,IAAiC,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC;QAC5E,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAED;;GAEG;AACH,SAAgB,sBAAsB,CACpC,OAAuB,EACvB,IAAY,EACZ,cAAqD;IAErD,MAAM,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC;IACxC,MAAM,MAAM,GAAG;QACb,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,qBAAqB,CAAC,IAAI,EAAE,CAAC;QAC/C,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,eAAe,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC;KACxB,CAAC;IAC1B,MAAM,QAAQ,GAAG,MAAM,EAAE,QAAQ,IAAI,MAAM,CAAC;IAC5C,MAAM,IAAI,GAAG,MAAM,EAAE,IAAI,IAAI,IAAI,CAAC;IAClC,M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-
|
|
6
|
-
import { ServiceError, ServiceErrorSpec } from '../error';
|
|
7
|
-
import type { ServiceExpress, ServiceLike, ServiceLocals } from '../types';
|
|
8
|
-
import type { ServiceConfiguration } from '../config/schema';
|
|
9
|
-
|
|
10
|
-
type UntypedEventSourceHandler = Parameters<EventSource['addEventListener']>[1];
|
|
11
|
-
|
|
12
|
-
class CustomEventSource extends EventSource {
|
|
13
|
-
private activeListeners: Array<{ handler: UntypedEventSourceHandler; name: string }> = [];
|
|
14
|
-
|
|
15
|
-
addEventListener<T>(name: string, handler: (data: MessageEvent<T>) => void): this {
|
|
16
|
-
super.addEventListener(name, handler);
|
|
17
|
-
this.activeListeners.push({ name, handler });
|
|
18
|
-
return this;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
removeAllListeners() {
|
|
22
|
-
this.activeListeners.forEach((l) => {
|
|
23
|
-
super.removeEventListener(l.name as keyof EventSourceEventMap, l.handler);
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Return a factory that will make instances of an OpenAPI/Swagger client for each request
|
|
30
|
-
*/
|
|
31
|
-
export function createServiceInterface<ServiceType>(
|
|
32
|
-
service: ServiceExpress,
|
|
33
|
-
name: string,
|
|
34
|
-
Implementation: { new (c: FetchConfig): ServiceType },
|
|
35
|
-
): ServiceType {
|
|
36
|
-
const appConfig = service.locals.config;
|
|
37
|
-
const config = {
|
|
38
|
-
...(appConfig.get('connections:default') || {}),
|
|
39
|
-
...(appConfig.get(`connections:${name}`) || {}),
|
|
40
|
-
} as ServiceConfiguration;
|
|
41
|
-
const protocol = config?.protocol || 'http';
|
|
42
|
-
const port = config?.port || 8000;
|
|
43
|
-
const host = config?.host || name;
|
|
44
|
-
const baseUrl = `${protocol}${protocol.endsWith(':') ? '//' : '://'}${host}:${port}${
|
|
45
|
-
config?.basePath || ''
|
|
46
|
-
}`;
|
|
47
|
-
|
|
48
|
-
const fetchConfig: FetchConfig = {
|
|
49
|
-
fetch,
|
|
50
|
-
AbortController,
|
|
51
|
-
EventSource: CustomEventSource,
|
|
52
|
-
FormData,
|
|
53
|
-
baseUrl,
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
// In development, it can be useful to route requests through
|
|
57
|
-
// a centralized local proxy (we use https://github.com/gas-buddy/container-proxy).
|
|
58
|
-
// This allows you to run a subset of services locally and route the rest
|
|
59
|
-
// of the requests to another (typically remote) environment.
|
|
60
|
-
if (config?.proxy) {
|
|
61
|
-
const proxyUrl = new URL(config.proxy);
|
|
62
|
-
const proxyPort = proxyUrl.protocol === 'https:' ? '8443' : '8000';
|
|
63
|
-
|
|
64
|
-
fetchConfig.requestInterceptor = (params: FetchRequest) => {
|
|
65
|
-
const parsedUrl = new URL(params.url);
|
|
66
|
-
const proto = parsedUrl.protocol.replace(/:$/, '');
|
|
67
|
-
const defaultPort = proto === 'https' ? 8443 : 8000;
|
|
68
|
-
const headers: FetchRequest['headers'] = {};
|
|
69
|
-
headers.host = `${proto}.${parsedUrl.hostname}.${port || defaultPort}`;
|
|
70
|
-
headers.source = service.locals.name;
|
|
71
|
-
parsedUrl.hostname = proxyUrl.hostname;
|
|
72
|
-
parsedUrl.protocol = proxyUrl.protocol;
|
|
73
|
-
parsedUrl.port = proxyUrl.port || proxyPort;
|
|
74
|
-
// eslint-disable-next-line no-param-reassign
|
|
75
|
-
params.headers = params.headers || {};
|
|
76
|
-
Object.assign(params.headers, headers);
|
|
77
|
-
// eslint-disable-next-line no-param-reassign
|
|
78
|
-
params.url = parsedUrl.href;
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return new Implementation(fetchConfig);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
interface SpecWithMessage extends ServiceErrorSpec {
|
|
86
|
-
message?: string;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function readResponse<
|
|
90
|
-
SLocals extends ServiceLocals,
|
|
91
|
-
AppType extends ServiceLike<SLocals>,
|
|
92
|
-
ResType extends RestApiResponse<number, SuccessResponseType>,
|
|
93
|
-
SuccessResponseType,
|
|
94
|
-
>(
|
|
95
|
-
app: AppType,
|
|
96
|
-
response: ResType,
|
|
97
|
-
errorSpec?: SpecWithMessage,
|
|
98
|
-
): Extract<ResType, { responseType: 'response' }> {
|
|
99
|
-
if (response.responseType === 'response') {
|
|
100
|
-
return response as Extract<ResType, { responseType: 'response' }>;
|
|
101
|
-
}
|
|
102
|
-
const { message, ...spec } = errorSpec || {};
|
|
103
|
-
throw new ServiceError(app, message || (response.body as Error).message || 'Internal Error', {
|
|
104
|
-
status: response.status,
|
|
105
|
-
...spec,
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export async function throwOrGetResponse<
|
|
110
|
-
SLocals extends ServiceLocals,
|
|
111
|
-
AppType extends ServiceLike<SLocals>,
|
|
112
|
-
ResType extends RestApiResponse<number, SuccessResponseType>,
|
|
113
|
-
SuccessResponseType,
|
|
114
|
-
>(
|
|
115
|
-
app: AppType,
|
|
116
|
-
exec: () => Promise<ResType>,
|
|
117
|
-
errorSpec?: SpecWithMessage,
|
|
118
|
-
): Promise<Extract<ResType, { responseType: 'response' }>> {
|
|
119
|
-
const response = await exec();
|
|
120
|
-
return readResponse(app, response, errorSpec);
|
|
121
|
-
}
|