maxserver 0.1.16 → 0.1.18

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maxserver",
3
- "version": "0.1.16",
3
+ "version": "0.1.18",
4
4
  "description": "Node server setup based fastify",
5
5
  "author": "Max Matinpalo",
6
6
  "type": "module",
@@ -0,0 +1,23 @@
1
+ // Development sounds to make api development even more fun 😃
2
+
3
+
4
+ import { exec } from 'child_process';
5
+
6
+ export async function setupDevSounds(app) {
7
+ if (!app.maxserver.sounds) return;
8
+ if (app.maxserver.env === 'production' || process.env.CI) return;
9
+
10
+ // Using here some builtin macos sounds see file paths
11
+ // For windows we should add alternative
12
+ if (process.platform !== 'darwin') return;
13
+ const ok = '/System/Library/Sounds/Glass.aiff';
14
+ const err = '/System/Library/Sounds/Submarine.aiff';
15
+
16
+ const play = file => exec(`afplay "${file}" >/dev/null 2>&1 &`);
17
+
18
+ app.addHook('onResponse', async (req, reply) => {
19
+ const code = reply.statusCode;
20
+ if (code >= 500) play(err);
21
+ else if (code < 400) play(ok);
22
+ });
23
+ }
package/src/index.js CHANGED
@@ -13,7 +13,7 @@ import {
13
13
  import { getAddress } from "./getAddress.js";
14
14
  import { setupDocs } from "./setupDocs.js";
15
15
  import { setupRoutes } from "./setupRoutes.js";
16
-
16
+ import { setupDevSounds } from "./devSounds.js";
17
17
 
18
18
  export default async function maxserver(config = {}) {
19
19
 
@@ -29,6 +29,7 @@ export default async function maxserver(config = {}) {
29
29
  routesDir = process.env.ROUTESDIR || "src",
30
30
  scalar = {},
31
31
  openapiInfo,
32
+ sounds,
32
33
  static: isStatic = process.env.STATIC,
33
34
  public: isPublic = process.env.PUBLIC === "true",
34
35
 
@@ -38,7 +39,7 @@ export default async function maxserver(config = {}) {
38
39
  } = config;
39
40
 
40
41
  const maxserverConfig = {
41
- port, secret, mongodb, docs, cors, env, openapiInfo, routesDir, scalar,
42
+ port, secret, mongodb, docs, cors, env, openapiInfo, routesDir, scalar, sounds,
42
43
  static: isStatic,
43
44
  public: isPublic
44
45
  };
@@ -78,6 +79,7 @@ export default async function maxserver(config = {}) {
78
79
  await setupStatic(app);
79
80
  await setupDocs(app);
80
81
  await setupRoutes(app);
82
+ await setupDevSounds(app);
81
83
 
82
84
  global.createError = function (code, message) {
83
85
  const err = new Error(message);
package/src/setupDocs.js CHANGED
@@ -68,6 +68,7 @@ export async function setupDocs(app) {
68
68
  persistAuth: true,
69
69
  showDeveloperTools: "never",
70
70
  operationsSorter: "alpha",
71
+ orderSchemaPropertiesBy: "preserve",
71
72
  metaData: {
72
73
  title: "API Docs 👨‍💻",
73
74
  },
@@ -65,6 +65,9 @@ export async function setupRoutes(app) {
65
65
  const root = path.resolve(app.maxserver.routesDir || "src");
66
66
  const files = walk(root);
67
67
 
68
+ /*
69
+ // Old one which only graps default exports
70
+
68
71
  // 1. Pass One: Register Global Schemas (Lonely .schema.js files)
69
72
  for (const file of files) {
70
73
  if (file.endsWith(".schema.js")) {
@@ -76,6 +79,20 @@ export async function setupRoutes(app) {
76
79
  }
77
80
  }
78
81
  }
82
+ */
83
+
84
+
85
+ // 1. Pass One: Register Global Schemas (Lonely .schema.js files)
86
+ for (const file of files) {
87
+ // Only process if no matching handler file exists
88
+ if (file.endsWith(".schema.js") && !fs.existsSync(file.replace(".schema.js", ".js"))) {
89
+ const mod = await import(pathToFileURL(file).href);
90
+
91
+ // Register default export + all named exports if they have an $id
92
+ for (const schema of Object.values(mod))
93
+ if (schema?.$id) app.addSchema(schema);
94
+ }
95
+ }
79
96
 
80
97
  // 2. Pass Two: Register Routes
81
98
  const seen = new Map();