@tsed/apollo 6.88.1 → 6.92.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.
File without changes
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApolloModule = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const common_1 = require("@tsed/common");
6
+ const di_1 = require("@tsed/di");
7
+ const ApolloService_1 = require("./services/ApolloService");
8
+ let ApolloModule = class ApolloModule {
9
+ get settings() {
10
+ return this.configuration.get("apollo");
11
+ }
12
+ $onRoutesInit() {
13
+ const { settings } = this;
14
+ if (settings) {
15
+ const promises = Object.entries(settings).map(async ([key, options]) => {
16
+ return this.service.createServer(key, options);
17
+ });
18
+ return Promise.all(promises);
19
+ }
20
+ }
21
+ $afterListen() {
22
+ const host = this.configuration.getBestHost();
23
+ const displayLog = (key, path) => {
24
+ const url = typeof host.port === "number" ? `${host.protocol}://${host.address}:${host.port}` : "";
25
+ this.logger.info(`[${key}] Apollo server is available on ${url}${path.replace(/^\//, "")}`);
26
+ };
27
+ const { settings } = this;
28
+ if (settings) {
29
+ Object.entries(settings).map(async ([key, options]) => {
30
+ const { path } = options;
31
+ displayLog(key, path);
32
+ });
33
+ }
34
+ }
35
+ };
36
+ (0, tslib_1.__decorate)([
37
+ (0, di_1.Inject)(),
38
+ (0, tslib_1.__metadata)("design:type", common_1.Logger)
39
+ ], ApolloModule.prototype, "logger", void 0);
40
+ (0, tslib_1.__decorate)([
41
+ (0, di_1.Inject)(),
42
+ (0, tslib_1.__metadata)("design:type", ApolloService_1.ApolloService)
43
+ ], ApolloModule.prototype, "service", void 0);
44
+ (0, tslib_1.__decorate)([
45
+ (0, di_1.Configuration)(),
46
+ (0, tslib_1.__metadata)("design:type", Object)
47
+ ], ApolloModule.prototype, "configuration", void 0);
48
+ ApolloModule = (0, tslib_1.__decorate)([
49
+ (0, di_1.Module)()
50
+ ], ApolloModule);
51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EAAC,QAAQ,EAAC,GAAG,IAAI,CAAC;QAExB,IAAI,QAAQ,EAAE;YACZ,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,EAAE,OAAO,CAAC,EAAE,EAAE;gBACpD,MAAM,EAAC,IAAI,EAAC,GAAG,OAAO,CAAC;gBAEvB,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;YACxB,CAAC,CAAC,CAAC;SACJ;IACH,CAAC;CACF,CAAA;AA3CC;IADC,IAAA,WAAM,GAAE;2CACS,eAAM;4CAAC;AAGzB;IADC,IAAA,WAAM,GAAE;2CACU,6BAAa;6CAAC;AAGjC;IADC,IAAA,kBAAa,GAAE;;mDACuB;AAR5B,YAAY;IADxB,IAAA,WAAM,GAAE;GACI,YAAY,CA6CxB;AA7CY,oCAAY","sourcesContent":["import {AfterListen, Logger, OnRoutesInit} from \"@tsed/common\";\nimport {Configuration, Inject, Module} from \"@tsed/di\";\nimport {ApolloSettings} from \"./interfaces/ApolloSettings\";\nimport {ApolloService} from \"./services/ApolloService\";\n\n@Module()\nexport class ApolloModule implements OnRoutesInit, AfterListen {\n @Inject()\n protected logger: Logger;\n\n @Inject()\n protected service: ApolloService;\n\n @Configuration()\n protected configuration: Configuration;\n\n get settings(): {[key: string]: ApolloSettings} | undefined {\n return this.configuration.get(\"apollo\");\n }\n\n $onRoutesInit(): Promise<any> | void {\n const {settings} = this;\n\n if (settings) {\n const promises = Object.entries(settings).map(async ([key, options]) => {\n return this.service.createServer(key, options);\n });\n\n return Promise.all(promises);\n }\n }\n\n $afterListen(): Promise<any> | void {\n const host = this.configuration.getBestHost();\n\n const displayLog = (key: string, path: string) => {\n const url = typeof host.port === \"number\" ? `${host.protocol}://${host.address}:${host.port}` : \"\";\n\n this.logger.info(`[${key}] Apollo server is available on ${url}${path.replace(/^\\//, \"\")}`);\n };\n\n const {settings} = this;\n\n if (settings) {\n Object.entries(settings).map(async ([key, options]) => {\n const {path} = options;\n\n displayLog(key, path);\n });\n }\n }\n}\n"]}
File without changes
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ (0, tslib_1.__exportStar)(require("./interfaces/ApolloSettings"), exports);
5
+ (0, tslib_1.__exportStar)(require("./services/ApolloService"), exports);
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,2EAA4C;AAC5C,wEAAyC","sourcesContent":["export * from \"./interfaces/ApolloSettings\";\nexport * from \"./services/ApolloService\";\n"]}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=ApolloSettings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApolloSettings.js","sourceRoot":"","sources":["../../../src/interfaces/ApolloSettings.ts"],"names":[],"mappings":"","sourcesContent":["import type {ApolloServerBase, Config, ApolloServerPluginLandingPageGraphQLPlaygroundOptions} from \"apollo-server-core\";\n\nexport type ApolloMiddlewareOptions = Record<string, any>;\nexport type ApolloServer = ApolloServerBase & {applyMiddleware(settings: ApolloMiddlewareOptions): any};\nexport type ApolloCustomServerCB = (config: ApolloConfig) => ApolloServer;\nexport type ApolloConfig = Config;\n\nexport interface ApolloSettings extends ApolloConfig {\n // Basic options\n path: string;\n server?: ApolloCustomServerCB;\n playground?: boolean | ApolloServerPluginLandingPageGraphQLPlaygroundOptions;\n // ApplyMiddleware Options\n // See options descriptions on https://www.apollographql.com/docs/apollo-server/api/apollo-server.html\n serverRegistration?: ApolloMiddlewareOptions;\n middlewareOptions?: ApolloMiddlewareOptions;\n}\n"]}
@@ -16,8 +16,6 @@ export declare class ApolloService {
16
16
  instance: ApolloServerBase;
17
17
  schema: GraphQLSchema | undefined;
18
18
  }>;
19
- private httpPort;
20
- private httpsPort;
21
19
  private app;
22
20
  private httpServer;
23
21
  private httpsServer;
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApolloService = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const di_1 = require("@tsed/di");
6
+ const logger_1 = require("@tsed/logger");
7
+ const common_1 = require("@tsed/common");
8
+ const apollo_server_core_1 = require("apollo-server-core");
9
+ const http_1 = (0, tslib_1.__importDefault)(require("http"));
10
+ const https_1 = (0, tslib_1.__importDefault)(require("https"));
11
+ let ApolloService = class ApolloService {
12
+ constructor() {
13
+ /**
14
+ *
15
+ * @type {Map<any, any>}
16
+ * @private
17
+ */
18
+ this.servers = new Map();
19
+ }
20
+ async createServer(id, settings) {
21
+ if (this.has(id)) {
22
+ return this.get(id);
23
+ }
24
+ try {
25
+ const { path, middlewareOptions = {}, server: customServer, ...config } = settings;
26
+ this.logger.info(`Create server with Apollo for: ${id}`);
27
+ this.logger.debug(`options: ${JSON.stringify({ path })}`);
28
+ const server = this.createInstance({
29
+ ...config,
30
+ plugins: this.getPlugins(settings)
31
+ }, customServer);
32
+ if (server) {
33
+ this.servers.set(id || "default", {
34
+ instance: server,
35
+ schema: settings.schema
36
+ });
37
+ await server.start();
38
+ await server.applyMiddleware({
39
+ path: settings.path,
40
+ ...middlewareOptions,
41
+ app: this.app.raw
42
+ });
43
+ return server;
44
+ }
45
+ }
46
+ catch (err) {
47
+ /* istanbul ignore next */
48
+ this.logger.error(err);
49
+ /* istanbul ignore next */
50
+ process.exit(-1);
51
+ }
52
+ }
53
+ /**
54
+ * Get an instance of ApolloServer from his id
55
+ * @returns ApolloServer
56
+ */
57
+ get(id = "default") {
58
+ var _a;
59
+ return (_a = this.servers.get(id)) === null || _a === void 0 ? void 0 : _a.instance;
60
+ }
61
+ /**
62
+ * Get an instance of GraphQL schema from his id
63
+ * @returns GraphQLSchema
64
+ */
65
+ getSchema(id = "default") {
66
+ var _a;
67
+ return (_a = this.servers.get(id)) === null || _a === void 0 ? void 0 : _a.schema;
68
+ }
69
+ /**
70
+ *
71
+ * @param {string} id
72
+ * @returns {boolean}
73
+ */
74
+ has(id = "default") {
75
+ return this.servers.has(id);
76
+ }
77
+ createInstance(options, server) {
78
+ // istanbul ignore next
79
+ if (server) {
80
+ return server(options);
81
+ }
82
+ // istanbul ignore next
83
+ try {
84
+ const Server = require(`apollo-server-${this.platformName || "express"}`).ApolloServer;
85
+ return new Server(options);
86
+ }
87
+ catch (er) {
88
+ this.logger.error(`Platform "${this.platformName}" not supported by @tsed/apollo`);
89
+ }
90
+ }
91
+ getPlugins(serverSettings) {
92
+ const playground = serverSettings.playground || serverSettings.playground === undefined;
93
+ return [
94
+ playground && process.env.NODE_ENV === "production"
95
+ ? (0, apollo_server_core_1.ApolloServerPluginLandingPageDisabled)()
96
+ : (0, apollo_server_core_1.ApolloServerPluginLandingPageGraphQLPlayground)(),
97
+ this.httpServer &&
98
+ (0, apollo_server_core_1.ApolloServerPluginDrainHttpServer)({
99
+ httpServer: this.httpServer
100
+ }),
101
+ this.httpsServer &&
102
+ (0, apollo_server_core_1.ApolloServerPluginDrainHttpServer)({
103
+ httpServer: this.httpsServer
104
+ }),
105
+ ...(serverSettings.plugins || [])
106
+ ].filter(Boolean);
107
+ }
108
+ };
109
+ (0, tslib_1.__decorate)([
110
+ (0, di_1.Constant)("PLATFORM_NAME"),
111
+ (0, tslib_1.__metadata)("design:type", String)
112
+ ], ApolloService.prototype, "platformName", void 0);
113
+ (0, tslib_1.__decorate)([
114
+ (0, di_1.Inject)(),
115
+ (0, tslib_1.__metadata)("design:type", logger_1.Logger)
116
+ ], ApolloService.prototype, "logger", void 0);
117
+ (0, tslib_1.__decorate)([
118
+ (0, di_1.Inject)(),
119
+ (0, tslib_1.__metadata)("design:type", common_1.PlatformApplication)
120
+ ], ApolloService.prototype, "app", void 0);
121
+ (0, tslib_1.__decorate)([
122
+ (0, di_1.Inject)(http_1.default.Server),
123
+ (0, tslib_1.__metadata)("design:type", Object)
124
+ ], ApolloService.prototype, "httpServer", void 0);
125
+ (0, tslib_1.__decorate)([
126
+ (0, di_1.Inject)(https_1.default.Server),
127
+ (0, tslib_1.__metadata)("design:type", Object)
128
+ ], ApolloService.prototype, "httpsServer", void 0);
129
+ ApolloService = (0, tslib_1.__decorate)([
130
+ (0, di_1.Service)()
131
+ ], ApolloService);
132
+ exports.ApolloService = ApolloService;
133
+ //# sourceMappingURL=ApolloService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApolloService.js","sourceRoot":"","sources":["../../../src/services/ApolloService.ts"],"names":[],"mappings":";;;;AAAA,iCAAmD;AACnD,yCAAoC;AACpC,yCAAiD;AAEjD,2DAK4B;AAI5B,6DAAwB;AACxB,+DAA0B;AAG1B,IAAa,aAAa,GAA1B,MAAa,aAAa;IAA1B;QAOE;;;;WAIG;QACO,YAAO,GAMb,IAAI,GAAG,EAAE,CAAC;IAiHhB,CAAC;IAtGC,KAAK,CAAC,YAAY,CAAC,EAAU,EAAE,QAAwB;QACrD,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;YAChB,OAAO,IAAI,CAAC,GAAG,CAAC,EAAE,CAAE,CAAC;SACtB;QAED,IAAI;YACF,MAAM,EAAC,IAAI,EAAE,iBAAiB,GAAG,EAAE,EAAE,MAAM,EAAE,YAAY,EAAE,GAAG,MAAM,EAAC,GAAG,QAAQ,CAAC;YAEjF,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,kCAAkC,EAAE,EAAE,CAAC,CAAC;YACzD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,YAAY,IAAI,CAAC,SAAS,CAAC,EAAC,IAAI,EAAC,CAAC,EAAE,CAAC,CAAC;YAExD,MAAM,MAAM,GAAG,IAAI,CAAC,cAAc,CAChC;gBACE,GAAG,MAAM;gBACT,OAAO,EAAE,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC;aACnC,EACD,YAAY,CACb,CAAC;YAEF,IAAI,MA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AAC,YAAY,CAAC;YAEvF,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC,CAAC;SAC5B;QAAC,OAAO,EAAE,EAAE;YACX,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,aAAa,IAAI,CAAC,YAAY,iCAAiC,CAAC,CAAC;SACpF;IACH,CAAC;IAEO,UAAU,CAAC,cAA8B;QAC/C,MAAM,UAAU,GAAG,cAAc,CAAC,UAAU,IAAI,cAAc,CAAC,UAAU,KAAK,SAAS,CAAC;QAExF,OAAO;YACL,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY;gBACjD,CAAC,CAAC,IAAA,0DAAqC,GAAE;gBACzC,CAAC,CAAC,IAAA,mEAA8C,GAAE;YACpD,IAAI,CAAC,UAAU;gBACb,IAAA,sDAAiC,EAAC;oBAChC,UAAU,EAAE,IAAI,CAAC,UAAU;iBAC5B,CAAC;YACJ,IAAI,CAAC,WAAW;gBACd,IAAA,sDAAiC,EAAC;oBAChC,UAAU,EAAE,IAAI,CAAC,WAAW;iBAC7B,CAAC;YACJ,GAAG,CAAC,cAAc,CAAC,OAAO,IAAI,EAAE,CAAC;SAClC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACpB,CAAC;CACF,CAAA;AAjIC;IADC,IAAA,aAAQ,EAAC,eAAe,CAAC;;mDACL;AAGrB;IADC,IAAA,WAAM,GAAE;2CACS,eAAM;6CAAC;AAgBzB;IADC,IAAA,WAAM,GAAE;2CACI,4BAAmB;0CAAC;AAGjC;IADC,IAAA,WAAM,EAAC,cAAI,CAAC,MAAM,CAAC;;iDACmB;AAGvC;IADC,IAAA,WAAM,EAAC,eAAK,CAAC,MAAM,CAAC;;kDACoB;AA3B9B,aAAa;IADzB,IAAA,YAAO,GAAE;GACG,aAAa,CAmIzB;AAnIY,sCAAa","sourcesContent":["import {Constant, Inject, Service} from \"@tsed/di\";\nimport {Logger} from \"@tsed/logger\";\nimport {PlatformApplication} from \"@tsed/common\";\nimport type {Config} from \"apollo-server-core\";\nimport {\n ApolloServerBase,\n ApolloServerPluginDrainHttpServer,\n ApolloServerPluginLandingPageDisabled,\n ApolloServerPluginLandingPageGraphQLPlayground\n} from \"apollo-server-core\";\nimport type {GraphQLSchema} from \"graphql\";\nimport type {ApolloServer, ApolloSettings} from \"../interfaces/ApolloSettings\";\nimport {ApolloCustomServerCB} from \"../interfaces/ApolloSettings\";\nimport Http from \"http\";\nimport Https from \"https\";\n\n@Service()\nexport class ApolloService {\n @Constant(\"PLATFORM_NAME\")\n platformName: string;\n\n @Inject()\n protected logger: Logger;\n\n /**\n *\n * @type {Map<any, any>}\n * @private\n */\n protected servers: Map<\n string,\n {\n instance: ApolloServerBase;\n schema: GraphQLSchema | undefined;\n }\n > = new Map();\n\n @Inject()\n private app: PlatformApplication;\n\n @Inject(Http.Server)\n private httpServer: Http.Server | null;\n\n @Inject(Https.Server)\n private httpsServer: Https.Server | null;\n\n async createServer(id: string, settings: ApolloSettings): Promise<any> {\n if (this.has(id)) {\n return this.get(id)!;\n }\n\n try {\n const {path, middlewareOptions = {}, server: customServer, ...config} = settings;\n\n this.logger.info(`Create server with Apollo for: ${id}`);\n this.logger.debug(`options: ${JSON.stringify({path})}`);\n\n const server = this.createInstance(\n {\n ...config,\n plugins: this.getPlugins(settings)\n },\n customServer\n );\n\n if (server) {\n this.servers.set(id || \"default\", {\n instance: server,\n schema: settings.schema\n });\n\n await server.start();\n\n await server.applyMiddleware({\n path: settings.path,\n ...middlewareOptions,\n app: this.app.raw\n });\n\n return server;\n }\n } catch (err) {\n /* istanbul ignore next */\n this.logger.error(err);\n /* istanbul ignore next */\n process.exit(-1);\n }\n }\n\n /**\n * Get an instance of ApolloServer from his id\n * @returns ApolloServer\n */\n get(id: string = \"default\"): ApolloServerBase | undefined {\n return this.servers.get(id)?.instance;\n }\n\n /**\n * Get an instance of GraphQL schema from his id\n * @returns GraphQLSchema\n */\n getSchema(id: string = \"default\"): GraphQLSchema | undefined {\n return this.servers.get(id)?.schema;\n }\n\n /**\n *\n * @param {string} id\n * @returns {boolean}\n */\n has(id: string = \"default\"): boolean {\n return this.servers.has(id);\n }\n\n protected createInstance(options: Config, server?: ApolloCustomServerCB): ApolloServer | undefined {\n // istanbul ignore next\n if (server) {\n return server(options);\n }\n\n // istanbul ignore next\n try {\n const Server = require(`apollo-server-${this.platformName || \"express\"}`).ApolloServer;\n\n return new Server(options);\n } catch (er) {\n this.logger.error(`Platform \"${this.platformName}\" not supported by @tsed/apollo`);\n }\n }\n\n private getPlugins(serverSettings: ApolloSettings): any[] {\n const playground = serverSettings.playground || serverSettings.playground === undefined;\n\n return [\n playground && process.env.NODE_ENV === \"production\"\n ? ApolloServerPluginLandingPageDisabled()\n : ApolloServerPluginLandingPageGraphQLPlayground(),\n this.httpServer &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpServer\n }),\n this.httpsServer &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpsServer\n }),\n ...(serverSettings.plugins || [])\n ].filter(Boolean);\n }\n}\n"]}
@@ -0,0 +1,49 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { Logger } from "@tsed/common";
3
+ import { Configuration, Inject, Module } from "@tsed/di";
4
+ import { ApolloService } from "./services/ApolloService";
5
+ let ApolloModule = class ApolloModule {
6
+ get settings() {
7
+ return this.configuration.get("apollo");
8
+ }
9
+ $onRoutesInit() {
10
+ const { settings } = this;
11
+ if (settings) {
12
+ const promises = Object.entries(settings).map(async ([key, options]) => {
13
+ return this.service.createServer(key, options);
14
+ });
15
+ return Promise.all(promises);
16
+ }
17
+ }
18
+ $afterListen() {
19
+ const host = this.configuration.getBestHost();
20
+ const displayLog = (key, path) => {
21
+ const url = typeof host.port === "number" ? `${host.protocol}://${host.address}:${host.port}` : "";
22
+ this.logger.info(`[${key}] Apollo server is available on ${url}${path.replace(/^\//, "")}`);
23
+ };
24
+ const { settings } = this;
25
+ if (settings) {
26
+ Object.entries(settings).map(async ([key, options]) => {
27
+ const { path } = options;
28
+ displayLog(key, path);
29
+ });
30
+ }
31
+ }
32
+ };
33
+ __decorate([
34
+ Inject(),
35
+ __metadata("design:type", Logger)
36
+ ], ApolloModule.prototype, "logger", void 0);
37
+ __decorate([
38
+ Inject(),
39
+ __metadata("design:type", ApolloService)
40
+ ], ApolloModule.prototype, "service", void 0);
41
+ __decorate([
42
+ Configuration(),
43
+ __metadata("design:type", Object)
44
+ ], ApolloModule.prototype, "configuration", void 0);
45
+ ApolloModule = __decorate([
46
+ Module()
47
+ ], ApolloModule);
48
+ export { ApolloModule };
49
+ //# sourceMappingURL=ApolloModule.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApolloModule.js","sourceRoot":"","sources":["../../src/ApolloModule.ts"],"names":[],"mappings":";AAAA,OAAO,EAAc,MAAM,EAAe,MAAM,cAAc,CAAC;AAC/D,OAAO,EAAC,aAAa,EAAE,MAAM,EAAE,MAAM,EAAC,MAAM,UAAU,CAAC;AAEvD,OAAO,EAAC,aAAa,EAAC,MAAM,0BAA0B,CAAC;AAGvD,IAAa,YAAY,GAAzB,MAAa,YAAY;IAUvB,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC1C,CAAC;IAED,aAAa;QACX,MAAM,EAAC,QAAQ,EAAC,GAAG,IAAI,CAAC;QAExB,IAAI,QAAQ,EAAE;YACZ,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,EAAE,OAAO,CAAC,EAAE,EAAE;gBACrE,OAAO,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;YACjD,CAAC,CAAC,CAAC;YAEH,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;SAC9B;IACH,CAAC;IAED,YAAY;QACV,MAAM,IAAI,GAAG,IAAI,CAAC,aAAa,CAAC,WAAW,EAAE,CAAC;QAE9C,MAAM,UAAU,GAAG,CAAC,GAAW,EAAE,IAAY,EAAE,EAAE;YAC/C,MAAM,GAAG,GAAG,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,QAAQ,MAAM,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YAEnG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,GAAG,mCAAmC,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC;QAC9F,CAAC,CAAC;QAEF,MAAM,EAAC,QAAQ,EAAC,GAAG,IAAI,CAAC;QAExB,IAAI,QAAQ,EAAE;YACZ,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,EAAE,OAAO,CAAC,EAAE,EAAE;gBACpD,MAAM,EAAC,IAAI,EAAC,GAAG,OAAO,CAAC;gBAEvB,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;YACxB,CAAC,CAAC,CAAC;SACJ;IACH,CAAC;CACF,CAAA;AA3CC;IADC,MAAM,EAAE;8BACS,MAAM;4CAAC;AAGzB;IADC,MAAM,EAAE;8BACU,aAAa;6CAAC;AAGjC;IADC,aAAa,EAAE;;mDACuB;AAR5B,YAAY;IADxB,MAAM,EAAE;GACI,YAAY,CA6CxB;SA7CY,YAAY","sourcesContent":["import {AfterListen, Logger, OnRoutesInit} from \"@tsed/common\";\nimport {Configuration, Inject, Module} from \"@tsed/di\";\nimport {ApolloSettings} from \"./interfaces/ApolloSettings\";\nimport {ApolloService} from \"./services/ApolloService\";\n\n@Module()\nexport class ApolloModule implements OnRoutesInit, AfterListen {\n @Inject()\n protected logger: Logger;\n\n @Inject()\n protected service: ApolloService;\n\n @Configuration()\n protected configuration: Configuration;\n\n get settings(): {[key: string]: ApolloSettings} | undefined {\n return this.configuration.get(\"apollo\");\n }\n\n $onRoutesInit(): Promise<any> | void {\n const {settings} = this;\n\n if (settings) {\n const promises = Object.entries(settings).map(async ([key, options]) => {\n return this.service.createServer(key, options);\n });\n\n return Promise.all(promises);\n }\n }\n\n $afterListen(): Promise<any> | void {\n const host = this.configuration.getBestHost();\n\n const displayLog = (key: string, path: string) => {\n const url = typeof host.port === \"number\" ? `${host.protocol}://${host.address}:${host.port}` : \"\";\n\n this.logger.info(`[${key}] Apollo server is available on ${url}${path.replace(/^\\//, \"\")}`);\n };\n\n const {settings} = this;\n\n if (settings) {\n Object.entries(settings).map(async ([key, options]) => {\n const {path} = options;\n\n displayLog(key, path);\n });\n }\n }\n}\n"]}
@@ -0,0 +1,3 @@
1
+ export * from "./interfaces/ApolloSettings";
2
+ export * from "./services/ApolloService";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0BAA0B,CAAC","sourcesContent":["export * from \"./interfaces/ApolloSettings\";\nexport * from \"./services/ApolloService\";\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ApolloSettings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApolloSettings.js","sourceRoot":"","sources":["../../../src/interfaces/ApolloSettings.ts"],"names":[],"mappings":"","sourcesContent":["import type {ApolloServerBase, Config, ApolloServerPluginLandingPageGraphQLPlaygroundOptions} from \"apollo-server-core\";\n\nexport type ApolloMiddlewareOptions = Record<string, any>;\nexport type ApolloServer = ApolloServerBase & {applyMiddleware(settings: ApolloMiddlewareOptions): any};\nexport type ApolloCustomServerCB = (config: ApolloConfig) => ApolloServer;\nexport type ApolloConfig = Config;\n\nexport interface ApolloSettings extends ApolloConfig {\n // Basic options\n path: string;\n server?: ApolloCustomServerCB;\n playground?: boolean | ApolloServerPluginLandingPageGraphQLPlaygroundOptions;\n // ApplyMiddleware Options\n // See options descriptions on https://www.apollographql.com/docs/apollo-server/api/apollo-server.html\n serverRegistration?: ApolloMiddlewareOptions;\n middlewareOptions?: ApolloMiddlewareOptions;\n}\n"]}
@@ -0,0 +1,130 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { Constant, Inject, Service } from "@tsed/di";
3
+ import { Logger } from "@tsed/logger";
4
+ import { PlatformApplication } from "@tsed/common";
5
+ import { ApolloServerPluginDrainHttpServer, ApolloServerPluginLandingPageDisabled, ApolloServerPluginLandingPageGraphQLPlayground } from "apollo-server-core";
6
+ import Http from "http";
7
+ import Https from "https";
8
+ let ApolloService = class ApolloService {
9
+ constructor() {
10
+ /**
11
+ *
12
+ * @type {Map<any, any>}
13
+ * @private
14
+ */
15
+ this.servers = new Map();
16
+ }
17
+ async createServer(id, settings) {
18
+ if (this.has(id)) {
19
+ return this.get(id);
20
+ }
21
+ try {
22
+ const { path, middlewareOptions = {}, server: customServer, ...config } = settings;
23
+ this.logger.info(`Create server with Apollo for: ${id}`);
24
+ this.logger.debug(`options: ${JSON.stringify({ path })}`);
25
+ const server = this.createInstance({
26
+ ...config,
27
+ plugins: this.getPlugins(settings)
28
+ }, customServer);
29
+ if (server) {
30
+ this.servers.set(id || "default", {
31
+ instance: server,
32
+ schema: settings.schema
33
+ });
34
+ await server.start();
35
+ await server.applyMiddleware({
36
+ path: settings.path,
37
+ ...middlewareOptions,
38
+ app: this.app.raw
39
+ });
40
+ return server;
41
+ }
42
+ }
43
+ catch (err) {
44
+ /* istanbul ignore next */
45
+ this.logger.error(err);
46
+ /* istanbul ignore next */
47
+ process.exit(-1);
48
+ }
49
+ }
50
+ /**
51
+ * Get an instance of ApolloServer from his id
52
+ * @returns ApolloServer
53
+ */
54
+ get(id = "default") {
55
+ var _a;
56
+ return (_a = this.servers.get(id)) === null || _a === void 0 ? void 0 : _a.instance;
57
+ }
58
+ /**
59
+ * Get an instance of GraphQL schema from his id
60
+ * @returns GraphQLSchema
61
+ */
62
+ getSchema(id = "default") {
63
+ var _a;
64
+ return (_a = this.servers.get(id)) === null || _a === void 0 ? void 0 : _a.schema;
65
+ }
66
+ /**
67
+ *
68
+ * @param {string} id
69
+ * @returns {boolean}
70
+ */
71
+ has(id = "default") {
72
+ return this.servers.has(id);
73
+ }
74
+ createInstance(options, server) {
75
+ // istanbul ignore next
76
+ if (server) {
77
+ return server(options);
78
+ }
79
+ // istanbul ignore next
80
+ try {
81
+ const Server = require(`apollo-server-${this.platformName || "express"}`).ApolloServer;
82
+ return new Server(options);
83
+ }
84
+ catch (er) {
85
+ this.logger.error(`Platform "${this.platformName}" not supported by @tsed/apollo`);
86
+ }
87
+ }
88
+ getPlugins(serverSettings) {
89
+ const playground = serverSettings.playground || serverSettings.playground === undefined;
90
+ return [
91
+ playground && process.env.NODE_ENV === "production"
92
+ ? ApolloServerPluginLandingPageDisabled()
93
+ : ApolloServerPluginLandingPageGraphQLPlayground(),
94
+ this.httpServer &&
95
+ ApolloServerPluginDrainHttpServer({
96
+ httpServer: this.httpServer
97
+ }),
98
+ this.httpsServer &&
99
+ ApolloServerPluginDrainHttpServer({
100
+ httpServer: this.httpsServer
101
+ }),
102
+ ...(serverSettings.plugins || [])
103
+ ].filter(Boolean);
104
+ }
105
+ };
106
+ __decorate([
107
+ Constant("PLATFORM_NAME"),
108
+ __metadata("design:type", String)
109
+ ], ApolloService.prototype, "platformName", void 0);
110
+ __decorate([
111
+ Inject(),
112
+ __metadata("design:type", Logger)
113
+ ], ApolloService.prototype, "logger", void 0);
114
+ __decorate([
115
+ Inject(),
116
+ __metadata("design:type", PlatformApplication)
117
+ ], ApolloService.prototype, "app", void 0);
118
+ __decorate([
119
+ Inject(Http.Server),
120
+ __metadata("design:type", Object)
121
+ ], ApolloService.prototype, "httpServer", void 0);
122
+ __decorate([
123
+ Inject(Https.Server),
124
+ __metadata("design:type", Object)
125
+ ], ApolloService.prototype, "httpsServer", void 0);
126
+ ApolloService = __decorate([
127
+ Service()
128
+ ], ApolloService);
129
+ export { ApolloService };
130
+ //# sourceMappingURL=ApolloService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApolloService.js","sourceRoot":"","sources":["../../../src/services/ApolloService.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAC,MAAM,UAAU,CAAC;AACnD,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AACpC,OAAO,EAAC,mBAAmB,EAAC,MAAM,cAAc,CAAC;AAEjD,OAAO,EAEL,iCAAiC,EACjC,qCAAqC,EACrC,8CAA8C,EAC/C,MAAM,oBAAoB,CAAC;AAI5B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B,IAAa,aAAa,GAA1B,MAAa,aAAa;IAA1B;QAOE;;;;WAIG;QACO,YAAO,GAMb,IAAI,GAAG,EAAE,CAAC;IAiHhB,CAAC;IAtGC,KAAK,CAAC,YAAY,CAAC,EAAU,EAAE,QAAwB;QACrD,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;YAChB,OAAO,IAAI,CAAC,GAAG,CAAC,EAAE,CAAE,CAAC;SACtB;QAED,IAAI;YACF,MAAM,EAAC,IAAI,EAAE,iBAAiB,GAAG,EAAE,EAAE,MAAM,EAAE,YAAY,EAAE,GAAG,MAAM,EAAC,GAAG,QAAQ,CAAC;YAEjF,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,kCAAkC,EAAE,EAAE,CAAC,CAAC;YACzD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,YAAY,IAAI,CAAC,SAAS,CAAC,EAAC,IAAI,EAAC,CAAC,EAAE,CAAC,CAAC;YAExD,MAAM,MAAM,GAAG,IAAI,CAAC,cAAc,CAChC;gBACE,GAAG,MAAM;gBACT,OAAO,EAAE,IAAI,CAAC,UAAU,CAAC,QAAQ,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AiB,IAAI,CAAC,YAAY,IAAI,SAAS,EAAE,CAAC,CAAC,YAAY,CAAC;YAEvF,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC,CAAC;SAC5B;QAAC,OAAO,EAAE,EAAE;YACX,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,aAAa,IAAI,CAAC,YAAY,iCAAiC,CAAC,CAAC;SACpF;IACH,CAAC;IAEO,UAAU,CAAC,cAA8B;QAC/C,MAAM,UAAU,GAAG,cAAc,CAAC,UAAU,IAAI,cAAc,CAAC,UAAU,KAAK,SAAS,CAAC;QAExF,OAAO;YACL,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY;gBACjD,CAAC,CAAC,qCAAqC,EAAE;gBACzC,CAAC,CAAC,8CAA8C,EAAE;YACpD,IAAI,CAAC,UAAU;gBACb,iCAAiC,CAAC;oBAChC,UAAU,EAAE,IAAI,CAAC,UAAU;iBAC5B,CAAC;YACJ,IAAI,CAAC,WAAW;gBACd,iCAAiC,CAAC;oBAChC,UAAU,EAAE,IAAI,CAAC,WAAW;iBAC7B,CAAC;YACJ,GAAG,CAAC,cAAc,CAAC,OAAO,IAAI,EAAE,CAAC;SAClC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACpB,CAAC;CACF,CAAA;AAjIC;IADC,QAAQ,CAAC,eAAe,CAAC;;mDACL;AAGrB;IADC,MAAM,EAAE;8BACS,MAAM;6CAAC;AAgBzB;IADC,MAAM,EAAE;8BACI,mBAAmB;0CAAC;AAGjC;IADC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC;;iDACmB;AAGvC;IADC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC;;kDACoB;AA3B9B,aAAa;IADzB,OAAO,EAAE;GACG,aAAa,CAmIzB;SAnIY,aAAa","sourcesContent":["import {Constant, Inject, Service} from \"@tsed/di\";\nimport {Logger} from \"@tsed/logger\";\nimport {PlatformApplication} from \"@tsed/common\";\nimport type {Config} from \"apollo-server-core\";\nimport {\n ApolloServerBase,\n ApolloServerPluginDrainHttpServer,\n ApolloServerPluginLandingPageDisabled,\n ApolloServerPluginLandingPageGraphQLPlayground\n} from \"apollo-server-core\";\nimport type {GraphQLSchema} from \"graphql\";\nimport type {ApolloServer, ApolloSettings} from \"../interfaces/ApolloSettings\";\nimport {ApolloCustomServerCB} from \"../interfaces/ApolloSettings\";\nimport Http from \"http\";\nimport Https from \"https\";\n\n@Service()\nexport class ApolloService {\n @Constant(\"PLATFORM_NAME\")\n platformName: string;\n\n @Inject()\n protected logger: Logger;\n\n /**\n *\n * @type {Map<any, any>}\n * @private\n */\n protected servers: Map<\n string,\n {\n instance: ApolloServerBase;\n schema: GraphQLSchema | undefined;\n }\n > = new Map();\n\n @Inject()\n private app: PlatformApplication;\n\n @Inject(Http.Server)\n private httpServer: Http.Server | null;\n\n @Inject(Https.Server)\n private httpsServer: Https.Server | null;\n\n async createServer(id: string, settings: ApolloSettings): Promise<any> {\n if (this.has(id)) {\n return this.get(id)!;\n }\n\n try {\n const {path, middlewareOptions = {}, server: customServer, ...config} = settings;\n\n this.logger.info(`Create server with Apollo for: ${id}`);\n this.logger.debug(`options: ${JSON.stringify({path})}`);\n\n const server = this.createInstance(\n {\n ...config,\n plugins: this.getPlugins(settings)\n },\n customServer\n );\n\n if (server) {\n this.servers.set(id || \"default\", {\n instance: server,\n schema: settings.schema\n });\n\n await server.start();\n\n await server.applyMiddleware({\n path: settings.path,\n ...middlewareOptions,\n app: this.app.raw\n });\n\n return server;\n }\n } catch (err) {\n /* istanbul ignore next */\n this.logger.error(err);\n /* istanbul ignore next */\n process.exit(-1);\n }\n }\n\n /**\n * Get an instance of ApolloServer from his id\n * @returns ApolloServer\n */\n get(id: string = \"default\"): ApolloServerBase | undefined {\n return this.servers.get(id)?.instance;\n }\n\n /**\n * Get an instance of GraphQL schema from his id\n * @returns GraphQLSchema\n */\n getSchema(id: string = \"default\"): GraphQLSchema | undefined {\n return this.servers.get(id)?.schema;\n }\n\n /**\n *\n * @param {string} id\n * @returns {boolean}\n */\n has(id: string = \"default\"): boolean {\n return this.servers.has(id);\n }\n\n protected createInstance(options: Config, server?: ApolloCustomServerCB): ApolloServer | undefined {\n // istanbul ignore next\n if (server) {\n return server(options);\n }\n\n // istanbul ignore next\n try {\n const Server = require(`apollo-server-${this.platformName || \"express\"}`).ApolloServer;\n\n return new Server(options);\n } catch (er) {\n this.logger.error(`Platform \"${this.platformName}\" not supported by @tsed/apollo`);\n }\n }\n\n private getPlugins(serverSettings: ApolloSettings): any[] {\n const playground = serverSettings.playground || serverSettings.playground === undefined;\n\n return [\n playground && process.env.NODE_ENV === \"production\"\n ? ApolloServerPluginLandingPageDisabled()\n : ApolloServerPluginLandingPageGraphQLPlayground(),\n this.httpServer &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpServer\n }),\n this.httpsServer &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpsServer\n }),\n ...(serverSettings.plugins || [])\n ].filter(Boolean);\n }\n}\n"]}
@@ -0,0 +1,14 @@
1
+ import { AfterListen, Logger, OnRoutesInit } from "@tsed/common";
2
+ import { Configuration } from "@tsed/di";
3
+ import { ApolloSettings } from "./interfaces/ApolloSettings";
4
+ import { ApolloService } from "./services/ApolloService";
5
+ export declare class ApolloModule implements OnRoutesInit, AfterListen {
6
+ protected logger: Logger;
7
+ protected service: ApolloService;
8
+ protected configuration: Configuration;
9
+ get settings(): {
10
+ [key: string]: ApolloSettings;
11
+ } | undefined;
12
+ $onRoutesInit(): Promise<any> | void;
13
+ $afterListen(): Promise<any> | void;
14
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./interfaces/ApolloSettings";
2
+ export * from "./services/ApolloService";
@@ -0,0 +1,14 @@
1
+ import type { ApolloServerBase, Config, ApolloServerPluginLandingPageGraphQLPlaygroundOptions } from "apollo-server-core";
2
+ export declare type ApolloMiddlewareOptions = Record<string, any>;
3
+ export declare type ApolloServer = ApolloServerBase & {
4
+ applyMiddleware(settings: ApolloMiddlewareOptions): any;
5
+ };
6
+ export declare type ApolloCustomServerCB = (config: ApolloConfig) => ApolloServer;
7
+ export declare type ApolloConfig = Config;
8
+ export interface ApolloSettings extends ApolloConfig {
9
+ path: string;
10
+ server?: ApolloCustomServerCB;
11
+ playground?: boolean | ApolloServerPluginLandingPageGraphQLPlaygroundOptions;
12
+ serverRegistration?: ApolloMiddlewareOptions;
13
+ middlewareOptions?: ApolloMiddlewareOptions;
14
+ }
@@ -0,0 +1,41 @@
1
+ import { Logger } from "@tsed/logger";
2
+ import type { Config } from "apollo-server-core";
3
+ import { ApolloServerBase } from "apollo-server-core";
4
+ import type { GraphQLSchema } from "graphql";
5
+ import type { ApolloServer, ApolloSettings } from "../interfaces/ApolloSettings";
6
+ import { ApolloCustomServerCB } from "../interfaces/ApolloSettings";
7
+ export declare class ApolloService {
8
+ platformName: string;
9
+ protected logger: Logger;
10
+ /**
11
+ *
12
+ * @type {Map<any, any>}
13
+ * @private
14
+ */
15
+ protected servers: Map<string, {
16
+ instance: ApolloServerBase;
17
+ schema: GraphQLSchema | undefined;
18
+ }>;
19
+ private app;
20
+ private httpServer;
21
+ private httpsServer;
22
+ createServer(id: string, settings: ApolloSettings): Promise<any>;
23
+ /**
24
+ * Get an instance of ApolloServer from his id
25
+ * @returns ApolloServer
26
+ */
27
+ get(id?: string): ApolloServerBase | undefined;
28
+ /**
29
+ * Get an instance of GraphQL schema from his id
30
+ * @returns GraphQLSchema
31
+ */
32
+ getSchema(id?: string): GraphQLSchema | undefined;
33
+ /**
34
+ *
35
+ * @param {string} id
36
+ * @returns {boolean}
37
+ */
38
+ has(id?: string): boolean;
39
+ protected createInstance(options: Config, server?: ApolloCustomServerCB): ApolloServer | undefined;
40
+ private getPlugins;
41
+ }
package/package.json CHANGED
@@ -1,16 +1,20 @@
1
1
  {
2
2
  "name": "@tsed/apollo",
3
- "version": "6.88.1",
3
+ "version": "6.92.0",
4
4
  "description": "Apollo package for Ts.ED framework",
5
5
  "source": "./src/index.ts",
6
- "main": "./lib/index.js",
7
- "typings": "./lib/index.d.ts",
6
+ "main": "./lib/cjs/index.js",
7
+ "typings": "./lib/types/index.d.ts",
8
8
  "exports": {
9
- "require": "./lib/index.js",
10
- "default": "./lib/index.modern.js"
9
+ "types": "./lib/types/index.d.ts",
10
+ "import": "./lib/esm/index.js",
11
+ "require": "./lib/cjs/index.js",
12
+ "default": "./lib/esm/index.js"
11
13
  },
12
14
  "scripts": {
13
- "build": "microbundle --target node --no-compress --format modern,cjs --tsconfig ./tsconfig.compile.json",
15
+ "build": "yarn run build:esm && yarn run build:cjs",
16
+ "build:cjs": "tsc --build tsconfig.compile.json",
17
+ "build:esm": "tsc --build tsconfig.compile.esm.json",
14
18
  "test": "cross-env NODE_ENV=test jest"
15
19
  },
16
20
  "dependencies": {
@@ -18,9 +22,9 @@
18
22
  },
19
23
  "private": false,
20
24
  "devDependencies": {
21
- "@tsed/common": "6.88.1",
22
- "@tsed/core": "6.88.1",
23
- "@tsed/di": "6.88.1",
25
+ "@tsed/common": "6.92.0",
26
+ "@tsed/core": "6.92.0",
27
+ "@tsed/di": "6.92.0",
24
28
  "@tsed/logger": "^6.0.0",
25
29
  "@types/graphql": "14.5.0",
26
30
  "apollo-datasource": "^3.2.0",
@@ -32,9 +36,9 @@
32
36
  "graphql": "15.7.2"
33
37
  },
34
38
  "peerDependencies": {
35
- "@tsed/common": "^6.88.1",
36
- "@tsed/core": "^6.88.1",
37
- "@tsed/di": "^6.88.1",
39
+ "@tsed/common": "^6.92.0",
40
+ "@tsed/core": "^6.92.0",
41
+ "@tsed/di": "^6.92.0",
38
42
  "@tsed/logger": ">6.0.0",
39
43
  "apollo-datasource": ">=3.0.0",
40
44
  "apollo-datasource-rest": ">=3.0.0",
@@ -0,0 +1,23 @@
1
+ {
2
+ "extends": "./tsconfig.compile.json",
3
+ "compilerOptions": {
4
+ "baseUrl": ".",
5
+ "module": "ES2020",
6
+ "rootDir": "src",
7
+ "outDir": "./lib/esm",
8
+ "declaration": true,
9
+ "declarationDir": "./lib/types"
10
+ },
11
+ "exclude": [
12
+ "node_modules",
13
+ "test",
14
+ "lib",
15
+ "benchmark",
16
+ "coverage",
17
+ "spec",
18
+ "**/*.benchmark.ts",
19
+ "**/*.spec.ts",
20
+ "keys",
21
+ "jest.config.js"
22
+ ]
23
+ }
package/lib/index.js DELETED
@@ -1,168 +0,0 @@
1
- var tslib = require('tslib');
2
- var di = require('@tsed/di');
3
- var logger = require('@tsed/logger');
4
- var common = require('@tsed/common');
5
- var apolloServerCore = require('apollo-server-core');
6
-
7
- function _extends() {
8
- _extends = Object.assign || function (target) {
9
- for (var i = 1; i < arguments.length; i++) {
10
- var source = arguments[i];
11
-
12
- for (var key in source) {
13
- if (Object.prototype.hasOwnProperty.call(source, key)) {
14
- target[key] = source[key];
15
- }
16
- }
17
- }
18
-
19
- return target;
20
- };
21
-
22
- return _extends.apply(this, arguments);
23
- }
24
-
25
- function _objectWithoutPropertiesLoose(source, excluded) {
26
- if (source == null) return {};
27
- var target = {};
28
- var sourceKeys = Object.keys(source);
29
- var key, i;
30
-
31
- for (i = 0; i < sourceKeys.length; i++) {
32
- key = sourceKeys[i];
33
- if (excluded.indexOf(key) >= 0) continue;
34
- target[key] = source[key];
35
- }
36
-
37
- return target;
38
- }
39
-
40
- const _excluded = ["path", "middlewareOptions", "server"];
41
- exports.ApolloService = class ApolloService {
42
- constructor() {
43
- /**
44
- *
45
- * @type {Map<any, any>}
46
- * @private
47
- */
48
- this.servers = new Map();
49
- }
50
-
51
- async createServer(id, settings) {
52
- if (this.has(id)) {
53
- return this.get(id);
54
- }
55
-
56
- try {
57
- const {
58
- path,
59
- middlewareOptions = {},
60
- server: customServer
61
- } = settings,
62
- config = _objectWithoutPropertiesLoose(settings, _excluded);
63
-
64
- this.logger.info(`Create server with Apollo for: ${id}`);
65
- this.logger.debug(`options: ${JSON.stringify({
66
- path
67
- })}`);
68
- const server = this.createInstance(_extends({}, config, {
69
- plugins: this.getPlugins(settings)
70
- }), customServer);
71
-
72
- if (server) {
73
- this.servers.set(id || "default", {
74
- instance: server,
75
- schema: settings.schema
76
- });
77
- await server.start();
78
- await server.applyMiddleware(_extends({
79
- path: settings.path
80
- }, middlewareOptions, {
81
- app: this.app.raw
82
- }));
83
- return server;
84
- }
85
- } catch (err) {
86
- /* istanbul ignore next */
87
- this.logger.error(err);
88
- /* istanbul ignore next */
89
-
90
- process.exit(-1);
91
- }
92
- }
93
- /**
94
- * Get an instance of ApolloServer from his id
95
- * @returns ApolloServer
96
- */
97
-
98
-
99
- get(id = "default") {
100
- var _this$servers$get;
101
-
102
- return (_this$servers$get = this.servers.get(id)) == null ? void 0 : _this$servers$get.instance;
103
- }
104
- /**
105
- * Get an instance of GraphQL schema from his id
106
- * @returns GraphQLSchema
107
- */
108
-
109
-
110
- getSchema(id = "default") {
111
- var _this$servers$get2;
112
-
113
- return (_this$servers$get2 = this.servers.get(id)) == null ? void 0 : _this$servers$get2.schema;
114
- }
115
- /**
116
- *
117
- * @param {string} id
118
- * @returns {boolean}
119
- */
120
-
121
-
122
- has(id = "default") {
123
- return this.servers.has(id);
124
- }
125
-
126
- createInstance(options, server) {
127
- // istanbul ignore next
128
- if (server) {
129
- return server(options);
130
- } // istanbul ignore next
131
-
132
-
133
- try {
134
- const Server = require(`apollo-server-${this.platformName || "express"}`).ApolloServer;
135
-
136
- return new Server(options);
137
- } catch (er) {
138
- this.logger.error(`Platform "${this.platformName}" not supported by @tsed/apollo`);
139
- }
140
- }
141
-
142
- getPlugins(serverSettings) {
143
- const playground = serverSettings.playground || serverSettings.playground === undefined;
144
- return [playground && process.env.NODE_ENV === "production" ? apolloServerCore.ApolloServerPluginLandingPageDisabled() : apolloServerCore.ApolloServerPluginLandingPageGraphQLPlayground(), this.httpPort && apolloServerCore.ApolloServerPluginDrainHttpServer({
145
- httpServer: this.httpServer
146
- }), this.httpsPort && apolloServerCore.ApolloServerPluginDrainHttpServer({
147
- httpServer: this.httpsServer
148
- }), ...(serverSettings.plugins || [])].filter(Boolean);
149
- }
150
-
151
- };
152
-
153
- tslib.__decorate([di.Constant("PLATFORM_NAME"), tslib.__metadata("design:type", String)], exports.ApolloService.prototype, "platformName", void 0);
154
-
155
- tslib.__decorate([di.Inject(), tslib.__metadata("design:type", logger.Logger)], exports.ApolloService.prototype, "logger", void 0);
156
-
157
- tslib.__decorate([di.Constant("httpPort"), tslib.__metadata("design:type", Object)], exports.ApolloService.prototype, "httpPort", void 0);
158
-
159
- tslib.__decorate([di.Constant("httpsPort"), tslib.__metadata("design:type", Object)], exports.ApolloService.prototype, "httpsPort", void 0);
160
-
161
- tslib.__decorate([di.Inject(), tslib.__metadata("design:type", common.PlatformApplication)], exports.ApolloService.prototype, "app", void 0);
162
-
163
- tslib.__decorate([di.Inject(common.HttpServer), tslib.__metadata("design:type", Object)], exports.ApolloService.prototype, "httpServer", void 0);
164
-
165
- tslib.__decorate([di.Inject(common.HttpsServer), tslib.__metadata("design:type", Object)], exports.ApolloService.prototype, "httpsServer", void 0);
166
-
167
- exports.ApolloService = tslib.__decorate([di.Service()], exports.ApolloService);
168
- //# sourceMappingURL=index.js.map
package/lib/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":["../src/services/ApolloService.ts"],"sourcesContent":["import {Constant, Inject, Service} from \"@tsed/di\";\nimport {Logger} from \"@tsed/logger\";\nimport {HttpServer, HttpsServer, PlatformApplication} from \"@tsed/common\";\nimport type {Config} from \"apollo-server-core\";\nimport {\n ApolloServerBase,\n ApolloServerPluginDrainHttpServer,\n ApolloServerPluginLandingPageDisabled,\n ApolloServerPluginLandingPageGraphQLPlayground\n} from \"apollo-server-core\";\nimport type {GraphQLSchema} from \"graphql\";\nimport type {ApolloServer, ApolloSettings} from \"../interfaces/ApolloSettings\";\nimport {ApolloCustomServerCB} from \"../interfaces/ApolloSettings\";\n\n@Service()\nexport class ApolloService {\n @Constant(\"PLATFORM_NAME\")\n platformName: string;\n\n @Inject()\n protected logger: Logger;\n\n /**\n *\n * @type {Map<any, any>}\n * @private\n */\n protected servers: Map<\n string,\n {\n instance: ApolloServerBase;\n schema: GraphQLSchema | undefined;\n }\n > = new Map();\n\n @Constant(\"httpPort\")\n private httpPort: string | number;\n\n @Constant(\"httpsPort\")\n private httpsPort: string | number;\n\n @Inject()\n private app: PlatformApplication;\n\n @Inject(HttpServer)\n private httpServer: HttpServer;\n\n @Inject(HttpsServer)\n private httpsServer: HttpsServer;\n\n async createServer(id: string, settings: ApolloSettings): Promise<any> {\n if (this.has(id)) {\n return this.get(id)!;\n }\n\n try {\n const {path, middlewareOptions = {}, server: customServer, ...config} = settings;\n\n this.logger.info(`Create server with Apollo for: ${id}`);\n this.logger.debug(`options: ${JSON.stringify({path})}`);\n\n const server = this.createInstance(\n {\n ...config,\n plugins: this.getPlugins(settings)\n },\n customServer\n );\n\n if (server) {\n this.servers.set(id || \"default\", {\n instance: server,\n schema: settings.schema\n });\n\n await server.start();\n\n await server.applyMiddleware({\n path: settings.path,\n ...middlewareOptions,\n app: this.app.raw\n });\n\n return server;\n }\n } catch (err) {\n /* istanbul ignore next */\n this.logger.error(err);\n /* istanbul ignore next */\n process.exit(-1);\n }\n }\n\n /**\n * Get an instance of ApolloServer from his id\n * @returns ApolloServer\n */\n get(id: string = \"default\"): ApolloServerBase | undefined {\n return this.servers.get(id)?.instance;\n }\n\n /**\n * Get an instance of GraphQL schema from his id\n * @returns GraphQLSchema\n */\n getSchema(id: string = \"default\"): GraphQLSchema | undefined {\n return this.servers.get(id)?.schema;\n }\n\n /**\n *\n * @param {string} id\n * @returns {boolean}\n */\n has(id: string = \"default\"): boolean {\n return this.servers.has(id);\n }\n\n protected createInstance(options: Config, server?: ApolloCustomServerCB): ApolloServer | undefined {\n // istanbul ignore next\n if (server) {\n return server(options);\n }\n\n // istanbul ignore next\n try {\n const Server = require(`apollo-server-${this.platformName || \"express\"}`).ApolloServer;\n\n return new Server(options);\n } catch (er) {\n this.logger.error(`Platform \"${this.platformName}\" not supported by @tsed/apollo`);\n }\n }\n\n private getPlugins(serverSettings: ApolloSettings): any[] {\n const playground = serverSettings.playground || serverSettings.playground === undefined;\n\n return [\n playground && process.env.NODE_ENV === \"production\"\n ? ApolloServerPluginLandingPageDisabled()\n : ApolloServerPluginLandingPageGraphQLPlayground(),\n this.httpPort &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpServer\n }),\n this.httpsPort &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpsServer\n }),\n ...(serverSettings.plugins || [])\n ].filter(Boolean);\n }\n}\n"],"names":["ApolloService","constructor","Map","createServer","id","settings","has","get","path","middlewareOptions","server","customServer","config","logger","info","debug","JSON","stringify","createInstance","plugins","getPlugins","servers","set","instance","schema","start","applyMiddleware","app","raw","err","error","process","exit","getSchema","options","Server","require","platformName","ApolloServer","er","serverSettings","playground","undefined","env","NODE_ENV","ApolloServerPluginLandingPageDisabled","ApolloServerPluginLandingPageGraphQLPlayground","httpPort","ApolloServerPluginDrainHttpServer","httpServer","httpsPort","httpsServer","filter","Boolean","__decorate","Constant","Inject","Logger","PlatformApplication","HttpServer","HttpsServer","Service"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAeaA,qBAAa,GAA1B,MAAaA,aAAb;AAAAC,EAAAA;AAOE;;;;;AAKU,gBAAA,GAMN,IAAIC,GAAJ,EANM;AA6HX;;AAtGmB,QAAZC,YAAY,CAACC,EAAD,EAAaC,QAAb;AAChB,QAAI,KAAKC,GAAL,CAASF,EAAT,CAAJ,EAAkB;AAChB,aAAO,KAAKG,GAAL,CAASH,EAAT,CAAP;AACD;;AAED,QAAI;AACF,YAAM;AAACI,QAAAA,IAAD;AAAOC,QAAAA,iBAAiB,GAAG,EAA3B;AAA+BC,QAAAA,MAAM,EAAEC;AAAvC,UAAkEN,QAAxE;AAAA,YAA8DO,MAA9D,iCAAwEP,QAAxE;;AAEA,WAAKQ,MAAL,CAAYC,IAAZ,mCAAmDV,IAAnD;AAC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 'tslib';
2
- import { Constant, Inject, Service } from '@tsed/di';
3
- import { Logger } from '@tsed/logger';
4
- import { PlatformApplication, HttpServer, HttpsServer } from '@tsed/common';
5
- import { ApolloServerPluginLandingPageDisabled, ApolloServerPluginLandingPageGraphQLPlayground, ApolloServerPluginDrainHttpServer } from 'apollo-server-core';
6
-
7
- function _extends() {
8
- _extends = Object.assign || function (target) {
9
- for (var i = 1; i < arguments.length; i++) {
10
- var source = arguments[i];
11
-
12
- for (var key in source) {
13
- if (Object.prototype.hasOwnProperty.call(source, key)) {
14
- target[key] = source[key];
15
- }
16
- }
17
- }
18
-
19
- return target;
20
- };
21
-
22
- return _extends.apply(this, arguments);
23
- }
24
-
25
- function _objectWithoutPropertiesLoose(source, excluded) {
26
- if (source == null) return {};
27
- var target = {};
28
- var sourceKeys = Object.keys(source);
29
- var key, i;
30
-
31
- for (i = 0; i < sourceKeys.length; i++) {
32
- key = sourceKeys[i];
33
- if (excluded.indexOf(key) >= 0) continue;
34
- target[key] = source[key];
35
- }
36
-
37
- return target;
38
- }
39
-
40
- const _excluded = ["path", "middlewareOptions", "server"];
41
- let ApolloService = class ApolloService {
42
- constructor() {
43
- /**
44
- *
45
- * @type {Map<any, any>}
46
- * @private
47
- */
48
- this.servers = new Map();
49
- }
50
-
51
- async createServer(id, settings) {
52
- if (this.has(id)) {
53
- return this.get(id);
54
- }
55
-
56
- try {
57
- const {
58
- path,
59
- middlewareOptions = {},
60
- server: customServer
61
- } = settings,
62
- config = _objectWithoutPropertiesLoose(settings, _excluded);
63
-
64
- this.logger.info(`Create server with Apollo for: ${id}`);
65
- this.logger.debug(`options: ${JSON.stringify({
66
- path
67
- })}`);
68
- const server = this.createInstance(_extends({}, config, {
69
- plugins: this.getPlugins(settings)
70
- }), customServer);
71
-
72
- if (server) {
73
- this.servers.set(id || "default", {
74
- instance: server,
75
- schema: settings.schema
76
- });
77
- await server.start();
78
- await server.applyMiddleware(_extends({
79
- path: settings.path
80
- }, middlewareOptions, {
81
- app: this.app.raw
82
- }));
83
- return server;
84
- }
85
- } catch (err) {
86
- /* istanbul ignore next */
87
- this.logger.error(err);
88
- /* istanbul ignore next */
89
-
90
- process.exit(-1);
91
- }
92
- }
93
- /**
94
- * Get an instance of ApolloServer from his id
95
- * @returns ApolloServer
96
- */
97
-
98
-
99
- get(id = "default") {
100
- var _this$servers$get;
101
-
102
- return (_this$servers$get = this.servers.get(id)) == null ? void 0 : _this$servers$get.instance;
103
- }
104
- /**
105
- * Get an instance of GraphQL schema from his id
106
- * @returns GraphQLSchema
107
- */
108
-
109
-
110
- getSchema(id = "default") {
111
- var _this$servers$get2;
112
-
113
- return (_this$servers$get2 = this.servers.get(id)) == null ? void 0 : _this$servers$get2.schema;
114
- }
115
- /**
116
- *
117
- * @param {string} id
118
- * @returns {boolean}
119
- */
120
-
121
-
122
- has(id = "default") {
123
- return this.servers.has(id);
124
- }
125
-
126
- createInstance(options, server) {
127
- // istanbul ignore next
128
- if (server) {
129
- return server(options);
130
- } // istanbul ignore next
131
-
132
-
133
- try {
134
- const Server = require(`apollo-server-${this.platformName || "express"}`).ApolloServer;
135
-
136
- return new Server(options);
137
- } catch (er) {
138
- this.logger.error(`Platform "${this.platformName}" not supported by @tsed/apollo`);
139
- }
140
- }
141
-
142
- getPlugins(serverSettings) {
143
- const playground = serverSettings.playground || serverSettings.playground === undefined;
144
- return [playground && process.env.NODE_ENV === "production" ? ApolloServerPluginLandingPageDisabled() : ApolloServerPluginLandingPageGraphQLPlayground(), this.httpPort && ApolloServerPluginDrainHttpServer({
145
- httpServer: this.httpServer
146
- }), this.httpsPort && ApolloServerPluginDrainHttpServer({
147
- httpServer: this.httpsServer
148
- }), ...(serverSettings.plugins || [])].filter(Boolean);
149
- }
150
-
151
- };
152
-
153
- __decorate([Constant("PLATFORM_NAME"), __metadata("design:type", String)], ApolloService.prototype, "platformName", void 0);
154
-
155
- __decorate([Inject(), __metadata("design:type", Logger)], ApolloService.prototype, "logger", void 0);
156
-
157
- __decorate([Constant("httpPort"), __metadata("design:type", Object)], ApolloService.prototype, "httpPort", void 0);
158
-
159
- __decorate([Constant("httpsPort"), __metadata("design:type", Object)], ApolloService.prototype, "httpsPort", void 0);
160
-
161
- __decorate([Inject(), __metadata("design:type", PlatformApplication)], ApolloService.prototype, "app", void 0);
162
-
163
- __decorate([Inject(HttpServer), __metadata("design:type", Object)], ApolloService.prototype, "httpServer", void 0);
164
-
165
- __decorate([Inject(HttpsServer), __metadata("design:type", Object)], ApolloService.prototype, "httpsServer", void 0);
166
-
167
- ApolloService = __decorate([Service()], ApolloService);
168
-
169
- export { ApolloService };
170
- //# sourceMappingURL=index.modern.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.modern.js","sources":["../src/services/ApolloService.ts"],"sourcesContent":["import {Constant, Inject, Service} from \"@tsed/di\";\nimport {Logger} from \"@tsed/logger\";\nimport {HttpServer, HttpsServer, PlatformApplication} from \"@tsed/common\";\nimport type {Config} from \"apollo-server-core\";\nimport {\n ApolloServerBase,\n ApolloServerPluginDrainHttpServer,\n ApolloServerPluginLandingPageDisabled,\n ApolloServerPluginLandingPageGraphQLPlayground\n} from \"apollo-server-core\";\nimport type {GraphQLSchema} from \"graphql\";\nimport type {ApolloServer, ApolloSettings} from \"../interfaces/ApolloSettings\";\nimport {ApolloCustomServerCB} from \"../interfaces/ApolloSettings\";\n\n@Service()\nexport class ApolloService {\n @Constant(\"PLATFORM_NAME\")\n platformName: string;\n\n @Inject()\n protected logger: Logger;\n\n /**\n *\n * @type {Map<any, any>}\n * @private\n */\n protected servers: Map<\n string,\n {\n instance: ApolloServerBase;\n schema: GraphQLSchema | undefined;\n }\n > = new Map();\n\n @Constant(\"httpPort\")\n private httpPort: string | number;\n\n @Constant(\"httpsPort\")\n private httpsPort: string | number;\n\n @Inject()\n private app: PlatformApplication;\n\n @Inject(HttpServer)\n private httpServer: HttpServer;\n\n @Inject(HttpsServer)\n private httpsServer: HttpsServer;\n\n async createServer(id: string, settings: ApolloSettings): Promise<any> {\n if (this.has(id)) {\n return this.get(id)!;\n }\n\n try {\n const {path, middlewareOptions = {}, server: customServer, ...config} = settings;\n\n this.logger.info(`Create server with Apollo for: ${id}`);\n this.logger.debug(`options: ${JSON.stringify({path})}`);\n\n const server = this.createInstance(\n {\n ...config,\n plugins: this.getPlugins(settings)\n },\n customServer\n );\n\n if (server) {\n this.servers.set(id || \"default\", {\n instance: server,\n schema: settings.schema\n });\n\n await server.start();\n\n await server.applyMiddleware({\n path: settings.path,\n ...middlewareOptions,\n app: this.app.raw\n });\n\n return server;\n }\n } catch (err) {\n /* istanbul ignore next */\n this.logger.error(err);\n /* istanbul ignore next */\n process.exit(-1);\n }\n }\n\n /**\n * Get an instance of ApolloServer from his id\n * @returns ApolloServer\n */\n get(id: string = \"default\"): ApolloServerBase | undefined {\n return this.servers.get(id)?.instance;\n }\n\n /**\n * Get an instance of GraphQL schema from his id\n * @returns GraphQLSchema\n */\n getSchema(id: string = \"default\"): GraphQLSchema | undefined {\n return this.servers.get(id)?.schema;\n }\n\n /**\n *\n * @param {string} id\n * @returns {boolean}\n */\n has(id: string = \"default\"): boolean {\n return this.servers.has(id);\n }\n\n protected createInstance(options: Config, server?: ApolloCustomServerCB): ApolloServer | undefined {\n // istanbul ignore next\n if (server) {\n return server(options);\n }\n\n // istanbul ignore next\n try {\n const Server = require(`apollo-server-${this.platformName || \"express\"}`).ApolloServer;\n\n return new Server(options);\n } catch (er) {\n this.logger.error(`Platform \"${this.platformName}\" not supported by @tsed/apollo`);\n }\n }\n\n private getPlugins(serverSettings: ApolloSettings): any[] {\n const playground = serverSettings.playground || serverSettings.playground === undefined;\n\n return [\n playground && process.env.NODE_ENV === \"production\"\n ? ApolloServerPluginLandingPageDisabled()\n : ApolloServerPluginLandingPageGraphQLPlayground(),\n this.httpPort &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpServer\n }),\n this.httpsPort &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpsServer\n }),\n ...(serverSettings.plugins || [])\n ].filter(Boolean);\n }\n}\n"],"names":["ApolloService","constructor","Map","createServer","id","settings","has","get","path","middlewareOptions","server","customServer","config","logger","info","debug","JSON","stringify","createInstance","plugins","getPlugins","servers","set","instance","schema","start","applyMiddleware","app","raw","err","error","process","exit","getSchema","options","Server","require","platformName","ApolloServer","er","serverSettings","playground","undefined","env","NODE_ENV","ApolloServerPluginLandingPageDisabled","ApolloServerPluginLandingPageGraphQLPlayground","httpPort","ApolloServerPluginDrainHttpServer","httpServer","httpsPort","httpsServer","filter","Boolean","__decorate","Constant","Inject","Logger","PlatformApplication","HttpServer","HttpsServer","Service"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAeaA,aAAa,GAA1B,MAAaA,aAAb;AAAAC,EAAAA;AAOE;;;;;AAKU,gBAAA,GAMN,IAAIC,GAAJ,EANM;AA6HX;;AAtGmB,QAAZC,YAAY,CAACC,EAAD,EAAaC,QAAb;AAChB,QAAI,KAAKC,GAAL,CAASF,EAAT,CAAJ,EAAkB;AAChB,aAAO,KAAKG,GAAL,CAASH,EAAT,CAAP;AACD;;AAED,QAAI;AACF,YAAM;AAACI,QAAAA,IAAD;AAAOC,QAAAA,iBAAiB,GAAG,EAA3B;AAA+BC,QAAAA,MAAM,EAAEC;AAAvC,UAAkEN,QAAxE;AAAA,YAA8DO,MAA9D,iCAAwEP,QAAxE;;AAEA,WAAKQ,MAAL,CAAYC,IAAZ,mCAAmDV,IAAnD;AACA,WAAKS,MAAL,CAAYE,KAAZ,aAA8BC,IAAI,CAACC,SAAL,CAAe;AAACT,QAAAA;AAAD,OAAf,GAA9B;AAEA,YAAME,MAAM,GAAG,KAAKQ,cAAL,cAERN,MAFQ;AAGXO,QAAAA,OAAO,EAAE,KAAKC,UAAL,CAAgBf,QAAhB;AAHE,UAKbM,YALa,CAAf;;AAQA,UAAID,MAAJ,EAAY;AACV,aAAKW,OAAL,CAAaC,GAAb,CAAiBlB,EAAE,IAAI,SAAvB,EA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