@redmix/api-server 0.0.1

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.
Files changed (66) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +64 -0
  3. package/dist/apiCLIConfig.d.ts +6 -0
  4. package/dist/apiCLIConfig.d.ts.map +1 -0
  5. package/dist/apiCLIConfig.js +71 -0
  6. package/dist/apiCLIConfigHandler.d.ts +3 -0
  7. package/dist/apiCLIConfigHandler.d.ts.map +1 -0
  8. package/dist/apiCLIConfigHandler.js +68 -0
  9. package/dist/bin.d.ts +2 -0
  10. package/dist/bin.d.ts.map +1 -0
  11. package/dist/bin.js +850 -0
  12. package/dist/bothCLIConfig.d.ts +6 -0
  13. package/dist/bothCLIConfig.d.ts.map +1 -0
  14. package/dist/bothCLIConfig.js +76 -0
  15. package/dist/bothCLIConfigHandler.d.ts +3 -0
  16. package/dist/bothCLIConfigHandler.d.ts.map +1 -0
  17. package/dist/bothCLIConfigHandler.js +107 -0
  18. package/dist/buildManager.d.ts +15 -0
  19. package/dist/buildManager.d.ts.map +1 -0
  20. package/dist/buildManager.js +74 -0
  21. package/dist/cliHelpers.d.ts +5 -0
  22. package/dist/cliHelpers.d.ts.map +1 -0
  23. package/dist/cliHelpers.js +52 -0
  24. package/dist/createServer.d.ts +30 -0
  25. package/dist/createServer.d.ts.map +1 -0
  26. package/dist/createServer.js +142 -0
  27. package/dist/createServerHelpers.d.ts +39 -0
  28. package/dist/createServerHelpers.d.ts.map +1 -0
  29. package/dist/createServerHelpers.js +101 -0
  30. package/dist/fastify.d.ts +14 -0
  31. package/dist/fastify.d.ts.map +1 -0
  32. package/dist/fastify.js +88 -0
  33. package/dist/logFormatter/ambient.d.js +1 -0
  34. package/dist/logFormatter/bin.d.ts +2 -0
  35. package/dist/logFormatter/bin.d.ts.map +1 -0
  36. package/dist/logFormatter/bin.js +369 -0
  37. package/dist/logFormatter/formatters.d.ts +30 -0
  38. package/dist/logFormatter/formatters.d.ts.map +1 -0
  39. package/dist/logFormatter/formatters.js +275 -0
  40. package/dist/logFormatter/index.d.ts +2 -0
  41. package/dist/logFormatter/index.d.ts.map +1 -0
  42. package/dist/logFormatter/index.js +184 -0
  43. package/dist/plugins/api.d.ts +13 -0
  44. package/dist/plugins/api.d.ts.map +1 -0
  45. package/dist/plugins/api.js +77 -0
  46. package/dist/plugins/graphql.d.ts +10 -0
  47. package/dist/plugins/graphql.d.ts.map +1 -0
  48. package/dist/plugins/graphql.js +115 -0
  49. package/dist/plugins/lambdaLoader.d.ts +23 -0
  50. package/dist/plugins/lambdaLoader.d.ts.map +1 -0
  51. package/dist/plugins/lambdaLoader.js +116 -0
  52. package/dist/requestHandlers/awsLambdaFastify.d.ts +5 -0
  53. package/dist/requestHandlers/awsLambdaFastify.d.ts.map +1 -0
  54. package/dist/requestHandlers/awsLambdaFastify.js +103 -0
  55. package/dist/requestHandlers/utils.d.ts +26 -0
  56. package/dist/requestHandlers/utils.d.ts.map +1 -0
  57. package/dist/requestHandlers/utils.js +55 -0
  58. package/dist/serverManager.d.ts +8 -0
  59. package/dist/serverManager.d.ts.map +1 -0
  60. package/dist/serverManager.js +138 -0
  61. package/dist/types.d.ts +19 -0
  62. package/dist/types.d.ts.map +1 -0
  63. package/dist/watch.d.ts +2 -0
  64. package/dist/watch.d.ts.map +1 -0
  65. package/dist/watch.js +274 -0
  66. package/package.json +77 -0
@@ -0,0 +1,6 @@
1
+ import type { Argv } from 'yargs';
2
+ import type { BothParsedOptions } from './types';
3
+ export declare const description = "Start a server for serving the api and web sides";
4
+ export declare function builder(yargs: Argv<BothParsedOptions>): void;
5
+ export declare function handler(options: BothParsedOptions): Promise<void>;
6
+ //# sourceMappingURL=bothCLIConfig.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bothCLIConfig.d.ts","sourceRoot":"","sources":["../src/bothCLIConfig.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,OAAO,CAAA;AAEjC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAEhD,eAAO,MAAM,WAAW,qDAAqD,CAAA;AAE7E,wBAAgB,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,iBAAiB,CAAC,QA+BrD;AAED,wBAAsB,OAAO,CAAC,OAAO,EAAE,iBAAiB,iBAGvD"}
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var bothCLIConfig_exports = {};
30
+ __export(bothCLIConfig_exports, {
31
+ builder: () => builder,
32
+ description: () => description,
33
+ handler: () => handler
34
+ });
35
+ module.exports = __toCommonJS(bothCLIConfig_exports);
36
+ const description = "Start a server for serving the api and web sides";
37
+ function builder(yargs) {
38
+ yargs.options({
39
+ webPort: {
40
+ description: "The port for the web server to listen on",
41
+ type: "number",
42
+ alias: ["web-port"]
43
+ },
44
+ webHost: {
45
+ description: "The host for the web server to listen on. Note that you most likely want this to be '0.0.0.0' in production",
46
+ type: "string",
47
+ alias: ["web-host"]
48
+ },
49
+ apiPort: {
50
+ description: "The port for the api server to listen on",
51
+ type: "number",
52
+ alias: ["api-port"]
53
+ },
54
+ apiHost: {
55
+ description: "The host for the api server to listen on. Note that you most likely want this to be '0.0.0.0' in production",
56
+ type: "string",
57
+ alias: ["api-host"]
58
+ },
59
+ apiRootPath: {
60
+ description: "Root path where your api functions are served",
61
+ type: "string",
62
+ alias: ["api-root-path", "rootPath", "root-path"],
63
+ default: "/"
64
+ }
65
+ });
66
+ }
67
+ async function handler(options) {
68
+ const { handler: handler2 } = await import("./bothCLIConfigHandler.js");
69
+ await handler2(options);
70
+ }
71
+ // Annotate the CommonJS export names for ESM import in node:
72
+ 0 && (module.exports = {
73
+ builder,
74
+ description,
75
+ handler
76
+ });
@@ -0,0 +1,3 @@
1
+ import type { BothParsedOptions } from './types';
2
+ export declare function handler(options: BothParsedOptions): Promise<void>;
3
+ //# sourceMappingURL=bothCLIConfigHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bothCLIConfigHandler.d.ts","sourceRoot":"","sources":["../src/bothCLIConfigHandler.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAEhD,wBAAsB,OAAO,CAAC,OAAO,EAAE,iBAAiB,iBAmEvD"}
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var bothCLIConfigHandler_exports = {};
30
+ __export(bothCLIConfigHandler_exports, {
31
+ handler: () => handler
32
+ });
33
+ module.exports = __toCommonJS(bothCLIConfigHandler_exports);
34
+ var import_chalk = __toESM(require("chalk"));
35
+ var import_fastify_web = require("@redmix/fastify-web");
36
+ var import_cliHelpers = require("./cliHelpers");
37
+ var import_createServer = require("./createServer");
38
+ var import_fastify = __toESM(require("./fastify"));
39
+ async function handler(options) {
40
+ const timeStart = Date.now();
41
+ console.log(import_chalk.default.dim.italic("Starting API and Web Servers..."));
42
+ options.webHost ??= (0, import_cliHelpers.getWebHost)();
43
+ options.webPort ??= (0, import_cliHelpers.getWebPort)();
44
+ options.apiHost ??= (0, import_cliHelpers.getAPIHost)();
45
+ options.apiPort ??= (0, import_cliHelpers.getAPIPort)();
46
+ options.apiRootPath = (0, import_fastify_web.coerceRootPath)(options.apiRootPath ?? "/");
47
+ const apiProxyTarget = [
48
+ "http://",
49
+ options.apiHost.includes(":") ? `[${options.apiHost}]` : options.apiHost,
50
+ ":",
51
+ options.apiPort,
52
+ options.apiRootPath
53
+ ].join("");
54
+ const webFastify = await (0, import_fastify.default)();
55
+ webFastify.register(import_fastify_web.redwoodFastifyWeb, {
56
+ redwood: {
57
+ apiProxyTarget
58
+ }
59
+ });
60
+ const apiFastify = await (0, import_createServer.createServer)({
61
+ apiRootPath: options.apiRootPath
62
+ });
63
+ await webFastify.listen({
64
+ port: options.webPort,
65
+ host: options.webHost,
66
+ listenTextResolver: getListenTextResolver("Web")
67
+ });
68
+ webFastify.log.trace(
69
+ { custom: { ...webFastify.initialConfig } },
70
+ "Fastify server configuration"
71
+ );
72
+ webFastify.log.trace(`Registered plugins
73
+ ${webFastify.printPlugins()}`);
74
+ await apiFastify.listen({
75
+ port: options.apiPort,
76
+ host: options.apiHost,
77
+ listenTextResolver: getListenTextResolver("API")
78
+ });
79
+ apiFastify.log.trace(
80
+ { custom: { ...apiFastify.initialConfig } },
81
+ "Fastify server configuration"
82
+ );
83
+ apiFastify.log.trace(`Registered plugins
84
+ ${apiFastify.printPlugins()}`);
85
+ console.log(import_chalk.default.dim.italic("Took " + (Date.now() - timeStart) + " ms"));
86
+ const webServer = import_chalk.default.green(webFastify.listeningOrigin);
87
+ const apiServer = import_chalk.default.magenta(
88
+ `${apiFastify.listeningOrigin}${options.apiRootPath}`
89
+ );
90
+ const graphqlEndpoint = import_chalk.default.magenta(`${apiServer}graphql`);
91
+ console.log(`Web server listening at ${webServer}`);
92
+ console.log(`API server listening at ${apiServer}`);
93
+ console.log(`GraphQL endpoint at ${graphqlEndpoint}`);
94
+ process?.send?.("ready");
95
+ }
96
+ function getListenTextResolver(side) {
97
+ return (address) => {
98
+ if (process.env.NODE_ENV !== "production") {
99
+ address = address.replace(/http:\/\/\[::\]/, "http://localhost");
100
+ }
101
+ return `${side} server listening at ${address}`;
102
+ };
103
+ }
104
+ // Annotate the CommonJS export names for ESM import in node:
105
+ 0 && (module.exports = {
106
+ handler
107
+ });
@@ -0,0 +1,15 @@
1
+ export type BuildAndRestartOptions = {
2
+ rebuild?: boolean;
3
+ clean?: boolean;
4
+ };
5
+ declare class BuildManager {
6
+ private shouldRebuild;
7
+ private shouldClean;
8
+ private debouncedBuild;
9
+ private buildFn;
10
+ constructor(buildFn: (options: BuildAndRestartOptions) => Promise<void>);
11
+ run(options: BuildAndRestartOptions): Promise<void>;
12
+ cancelScheduledBuild(): void;
13
+ }
14
+ export { BuildManager };
15
+ //# sourceMappingURL=buildManager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"buildManager.d.ts","sourceRoot":"","sources":["../src/buildManager.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,sBAAsB,GAAG;IACnC,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB,CAAA;AAED,cAAM,YAAY;IAChB,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,cAAc,CAA6B;IACnD,OAAO,CAAC,OAAO,CAAoD;gBAEvD,OAAO,EAAE,CAAC,OAAO,EAAE,sBAAsB,KAAK,OAAO,CAAC,IAAI,CAAC;IAgCjE,GAAG,CAAC,OAAO,EAAE,sBAAsB;IAWzC,oBAAoB;CAGrB;AAED,OAAO,EAAE,YAAY,EAAE,CAAA"}
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var buildManager_exports = {};
20
+ __export(buildManager_exports, {
21
+ BuildManager: () => BuildManager
22
+ });
23
+ module.exports = __toCommonJS(buildManager_exports);
24
+ var import_lodash = require("lodash");
25
+ class BuildManager {
26
+ shouldRebuild;
27
+ shouldClean;
28
+ debouncedBuild;
29
+ buildFn;
30
+ constructor(buildFn) {
31
+ this.shouldRebuild = true;
32
+ this.shouldClean = false;
33
+ this.buildFn = buildFn;
34
+ this.debouncedBuild = (0, import_lodash.debounce)(
35
+ async (options) => {
36
+ try {
37
+ await this.buildFn({
38
+ ...options,
39
+ rebuild: this.shouldRebuild,
40
+ clean: this.shouldClean
41
+ });
42
+ } finally {
43
+ this.shouldRebuild = true;
44
+ this.shouldClean = false;
45
+ }
46
+ },
47
+ // We want to delay execution when multiple files are modified on the filesystem,
48
+ // this usually happens when running RedwoodJS generator commands.
49
+ // Local writes are very fast, but writes in e2e environments are not,
50
+ // so allow the default to be adjusted with an env-var.
51
+ //
52
+ process.env.RWJS_DELAY_RESTART ? parseInt(process.env.RWJS_DELAY_RESTART, 10) : 500
53
+ );
54
+ }
55
+ // Wrapper method to handle options and set precedence flags.
56
+ // If we ever see a `rebuild: false` option while debouncing, we never want to rebuild.
57
+ // If we ever see a `clean: true` option, we always want to clean.
58
+ async run(options) {
59
+ if (options.rebuild === false) {
60
+ this.shouldRebuild = false;
61
+ }
62
+ if (options.clean) {
63
+ this.shouldClean = true;
64
+ }
65
+ await this.debouncedBuild(options);
66
+ }
67
+ cancelScheduledBuild() {
68
+ this.debouncedBuild.cancel();
69
+ }
70
+ }
71
+ // Annotate the CommonJS export names for ESM import in node:
72
+ 0 && (module.exports = {
73
+ BuildManager
74
+ });
@@ -0,0 +1,5 @@
1
+ export declare function getAPIHost(): string;
2
+ export declare function getAPIPort(): number;
3
+ export declare function getWebHost(): string;
4
+ export declare function getWebPort(): number;
5
+ //# sourceMappingURL=cliHelpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cliHelpers.d.ts","sourceRoot":"","sources":["../src/cliHelpers.ts"],"names":[],"mappings":"AAEA,wBAAgB,UAAU,WAKzB;AAED,wBAAgB,UAAU,WAIzB;AAED,wBAAgB,UAAU,WAKzB;AAED,wBAAgB,UAAU,WAIzB"}
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var cliHelpers_exports = {};
20
+ __export(cliHelpers_exports, {
21
+ getAPIHost: () => getAPIHost,
22
+ getAPIPort: () => getAPIPort,
23
+ getWebHost: () => getWebHost,
24
+ getWebPort: () => getWebPort
25
+ });
26
+ module.exports = __toCommonJS(cliHelpers_exports);
27
+ var import_project_config = require("@redmix/project-config");
28
+ function getAPIHost() {
29
+ let host = process.env.REDWOOD_API_HOST;
30
+ host ??= (0, import_project_config.getConfig)().api.host;
31
+ host ??= process.env.NODE_ENV === "production" ? "0.0.0.0" : "::";
32
+ return host;
33
+ }
34
+ function getAPIPort() {
35
+ return process.env.REDWOOD_API_PORT ? parseInt(process.env.REDWOOD_API_PORT) : (0, import_project_config.getConfig)().api.port;
36
+ }
37
+ function getWebHost() {
38
+ let host = process.env.REDWOOD_WEB_HOST;
39
+ host ??= (0, import_project_config.getConfig)().web.host;
40
+ host ??= process.env.NODE_ENV === "production" ? "0.0.0.0" : "::";
41
+ return host;
42
+ }
43
+ function getWebPort() {
44
+ return process.env.REDWOOD_WEB_PORT ? parseInt(process.env.REDWOOD_WEB_PORT) : (0, import_project_config.getConfig)().web.port;
45
+ }
46
+ // Annotate the CommonJS export names for ESM import in node:
47
+ 0 && (module.exports = {
48
+ getAPIHost,
49
+ getAPIPort,
50
+ getWebHost,
51
+ getWebPort
52
+ });
@@ -0,0 +1,30 @@
1
+ import type { CreateServerOptions, Server } from './createServerHelpers';
2
+ /**
3
+ * Creates a server for api functions:
4
+ *
5
+ * ```js
6
+ * import { createServer } from '@redmix/api-server'
7
+ *
8
+ * import { logger } from 'src/lib/logger'
9
+ *
10
+ async function main() {
11
+ * const server = await createServer({
12
+ * logger,
13
+ * apiRootPath: 'api'
14
+ * configureApiServer: (server) => {
15
+ * // Configure the API server fastify instance, e.g. add content type parsers
16
+ * },
17
+ * })
18
+ *
19
+ * // Configure the returned fastify instance:
20
+ * server.register(myPlugin)
21
+ *
22
+ * // When ready, start the server:
23
+ * await server.start()
24
+ * }
25
+ *
26
+ * main()
27
+ * ```
28
+ */
29
+ export declare function createServer(options?: CreateServerOptions): Promise<Server>;
30
+ //# sourceMappingURL=createServer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createServer.d.ts","sourceRoot":"","sources":["../src/createServer.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EACV,mBAAmB,EACnB,MAAM,EAEP,MAAM,uBAAuB,CAAA;AAqB9B;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAsB,YAAY,CAAC,OAAO,GAAE,mBAAwB,mBAiHnE"}
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var createServer_exports = {};
30
+ __export(createServer_exports, {
31
+ createServer: () => createServer
32
+ });
33
+ module.exports = __toCommonJS(createServer_exports);
34
+ var import_fs = __toESM(require("fs"));
35
+ var import_path = __toESM(require("path"));
36
+ var import_chalk = __toESM(require("chalk"));
37
+ var import_dotenv_defaults = require("dotenv-defaults");
38
+ var import_fast_glob = __toESM(require("fast-glob"));
39
+ var import_fastify = __toESM(require("fastify"));
40
+ var import_store = require("@redmix/context/dist/store");
41
+ var import_project_config = require("@redmix/project-config");
42
+ var import_createServerHelpers = require("./createServerHelpers");
43
+ var import_api = require("./plugins/api");
44
+ if (!process.env.REDWOOD_ENV_FILES_LOADED) {
45
+ (0, import_dotenv_defaults.config)({
46
+ path: import_path.default.join((0, import_project_config.getPaths)().base, ".env"),
47
+ defaults: import_path.default.join((0, import_project_config.getPaths)().base, ".env.defaults"),
48
+ multiline: true
49
+ });
50
+ process.env.REDWOOD_ENV_FILES_LOADED = "true";
51
+ }
52
+ async function createServer(options = {}) {
53
+ const {
54
+ apiRootPath,
55
+ fastifyServerOptions,
56
+ configureApiServer,
57
+ apiPort,
58
+ apiHost
59
+ } = (0, import_createServerHelpers.resolveOptions)(options);
60
+ const serverConfigPath = import_path.default.join(
61
+ (0, import_project_config.getPaths)().base,
62
+ (0, import_project_config.getConfig)().api.serverConfig
63
+ );
64
+ if (import_fs.default.existsSync(serverConfigPath)) {
65
+ console.warn(
66
+ import_chalk.default.yellow(
67
+ [
68
+ "",
69
+ `Ignoring \`config\` and \`configureServer\` in api/server.config.js.`,
70
+ `Migrate them to api/src/server.{ts,js}:`,
71
+ "",
72
+ `\`\`\`js title="api/src/server.{ts,js}"`,
73
+ "// Pass your config to `createServer`",
74
+ "const server = createServer({",
75
+ " fastifyServerOptions: myFastifyConfig",
76
+ "})",
77
+ "",
78
+ "// Then inline your `configureFastify` logic:",
79
+ "server.register(myFastifyPlugin)",
80
+ "```",
81
+ ""
82
+ ].join("\n")
83
+ )
84
+ );
85
+ }
86
+ const server = Object.assign((0, import_fastify.default)(fastifyServerOptions), {
87
+ // `start` will get replaced further down in this file
88
+ start: async () => {
89
+ throw new Error("Not implemented yet");
90
+ }
91
+ });
92
+ server.addHook("onRequest", (_req, _reply, done) => {
93
+ (0, import_store.getAsyncStoreInstance)().run(/* @__PURE__ */ new Map(), done);
94
+ });
95
+ await server.register(import_api.redwoodFastifyAPI, {
96
+ redwood: {
97
+ apiRootPath,
98
+ fastGlobOptions: {
99
+ ignore: ["**/dist/functions/graphql.js"]
100
+ },
101
+ configureServer: configureApiServer
102
+ }
103
+ });
104
+ const [graphqlFunctionPath] = await (0, import_fast_glob.default)("dist/functions/graphql.{ts,js}", {
105
+ cwd: (0, import_project_config.getPaths)().api.base,
106
+ absolute: true
107
+ });
108
+ if (graphqlFunctionPath) {
109
+ const { redwoodFastifyGraphQLServer } = await import("./plugins/graphql.js");
110
+ const { __rw_graphqlOptions } = await import(`file://${graphqlFunctionPath}`);
111
+ await server.register(redwoodFastifyGraphQLServer, {
112
+ redwood: {
113
+ apiRootPath,
114
+ graphql: __rw_graphqlOptions
115
+ }
116
+ });
117
+ }
118
+ server.addHook("onReady", (done) => {
119
+ process.send?.("ready");
120
+ done();
121
+ });
122
+ server.addHook("onListen", (done) => {
123
+ console.log(
124
+ `Server listening at ${import_chalk.default.magenta(
125
+ `${server.listeningOrigin}${apiRootPath}`
126
+ )}`
127
+ );
128
+ done();
129
+ });
130
+ server.start = (options2 = {}) => {
131
+ return server.listen({
132
+ ...options2,
133
+ port: apiPort,
134
+ host: apiHost
135
+ });
136
+ };
137
+ return server;
138
+ }
139
+ // Annotate the CommonJS export names for ESM import in node:
140
+ 0 && (module.exports = {
141
+ createServer
142
+ });
@@ -0,0 +1,39 @@
1
+ import type { FastifyListenOptions, FastifyServerOptions, FastifyInstance } from 'fastify';
2
+ export type StartOptions = Omit<FastifyListenOptions, 'port' | 'host'>;
3
+ export interface Server extends FastifyInstance {
4
+ start: (options?: StartOptions) => Promise<string>;
5
+ }
6
+ export interface CreateServerOptions {
7
+ /**
8
+ * The prefix for all routes. Defaults to `/`.
9
+ */
10
+ apiRootPath?: string;
11
+ /**
12
+ * Logger instance or options.
13
+ */
14
+ logger?: FastifyServerOptions['logger'];
15
+ /**
16
+ * Options for the fastify server instance.
17
+ * Omitting logger here because we move it up.
18
+ */
19
+ fastifyServerOptions?: Omit<FastifyServerOptions, 'logger'>;
20
+ /**
21
+ * Customise the API server fastify plugin before it is registered
22
+ */
23
+ configureApiServer?: (server: Server) => void | Promise<void>;
24
+ /**
25
+ * Whether to parse args or not. Defaults to `true`.
26
+ */
27
+ parseArgs?: boolean;
28
+ }
29
+ type DefaultCreateServerOptions = Required<Omit<CreateServerOptions, 'fastifyServerOptions'> & {
30
+ fastifyServerOptions: FastifyServerOptions;
31
+ }>;
32
+ export declare const DEFAULT_CREATE_SERVER_OPTIONS: DefaultCreateServerOptions;
33
+ export declare function resolveOptions(options?: CreateServerOptions, args?: string[]): Required<Omit<CreateServerOptions, "logger" | "fastifyServerOptions" | "parseArgs"> & {
34
+ fastifyServerOptions: FastifyServerOptions;
35
+ apiPort: number;
36
+ apiHost: string;
37
+ }>;
38
+ export {};
39
+ //# sourceMappingURL=createServerHelpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createServerHelpers.d.ts","sourceRoot":"","sources":["../src/createServerHelpers.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,oBAAoB,EACpB,oBAAoB,EACpB,eAAe,EAChB,MAAM,SAAS,CAAA;AAMhB,MAAM,MAAM,YAAY,GAAG,IAAI,CAAC,oBAAoB,EAAE,MAAM,GAAG,MAAM,CAAC,CAAA;AAEtE,MAAM,WAAW,MAAO,SAAQ,eAAe;IAC7C,KAAK,EAAE,CAAC,OAAO,CAAC,EAAE,YAAY,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;CACnD;AAED,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IAEpB;;OAEG;IACH,MAAM,CAAC,EAAE,oBAAoB,CAAC,QAAQ,CAAC,CAAA;IAEvC;;;OAGG;IACH,oBAAoB,CAAC,EAAE,IAAI,CAAC,oBAAoB,EAAE,QAAQ,CAAC,CAAA;IAE3D;;OAEG;IACH,kBAAkB,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAE7D;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;CACpB;AAED,KAAK,0BAA0B,GAAG,QAAQ,CACxC,IAAI,CAAC,mBAAmB,EAAE,sBAAsB,CAAC,GAAG;IAClD,oBAAoB,EAAE,oBAAoB,CAAA;CAC3C,CACF,CAAA;AAED,eAAO,MAAM,6BAA6B,EAAE,0BAa3C,CAAA;AAUD,wBAAgB,cAAc,CAC5B,OAAO,GAAE,mBAAwB,EACjC,IAAI,CAAC,EAAE,MAAM,EAAE;0BARS,oBAAoB;aACjC,MAAM;aACN,MAAM;GA+ElB"}