@moostjs/swagger 0.6.53 → 0.7.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.
package/dist/index.cjs CHANGED
@@ -27,8 +27,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
27
27
 
28
28
  //#endregion
29
29
  let moost = require("moost");
30
- let _moostjs_event_http = require("@moostjs/event-http");
31
30
  let _wooksjs_event_http = require("@wooksjs/event-http");
31
+ let _moostjs_event_http = require("@moostjs/event-http");
32
32
  let _wooksjs_http_static = require("@wooksjs/http-static");
33
33
  let path = require("path");
34
34
  path = __toESM(path);
@@ -204,22 +204,23 @@ function mapToSwaggerSpec(metadata, options) {
204
204
  if (!swaggerSpec.paths[handlerPath]) swaggerSpec.paths[handlerPath] = {};
205
205
  let responses;
206
206
  if (hmeta?.swaggerResponses) for (const [code, responseEntry] of Object.entries(hmeta.swaggerResponses)) {
207
- const newCode = code === "0" ? getDefaultStatusCode(handlerMethod) : code;
208
- for (const [contentType, conf] of Object.entries(responseEntry.content)) {
209
- const { response, example } = conf;
210
- const schema = resolveSwaggerSchemaFromConfig(response);
211
- if (schema) {
212
- responses = responses || {};
213
- const schemaWithExample = example !== void 0 ? {
214
- ...schema,
215
- example
216
- } : schema;
217
- if (!responses[newCode]) responses[newCode] = {
218
- description: responseEntry.description || defaultStatusDescription(Number(newCode)),
219
- content: {}
220
- };
221
- responses[newCode].content[contentType] = { schema: schemaWithExample };
222
- }
207
+ const contents = Object.entries(responseEntry.content).map(([contentType, conf]) => ({
208
+ contentType,
209
+ example: conf.example,
210
+ schema: resolveSwaggerSchemaFromConfig(conf.response)
211
+ }));
212
+ const newCode = code === "0" ? getDefaultStatusCode(handlerMethod, contents.some((c) => c.schema)) : code;
213
+ for (const { contentType, example, schema } of contents) if (schema) {
214
+ responses = responses || {};
215
+ const schemaWithExample = example !== void 0 ? {
216
+ ...schema,
217
+ example
218
+ } : schema;
219
+ if (!responses[newCode]) responses[newCode] = {
220
+ description: responseEntry.description || defaultStatusDescription(Number(newCode)),
221
+ content: {}
222
+ };
223
+ responses[newCode].content[contentType] = { schema: schemaWithExample };
223
224
  }
224
225
  if (responseEntry.headers) {
225
226
  const resolvedHeaders = resolveResponseHeaders(responseEntry.headers);
@@ -396,7 +397,7 @@ function mapToSwaggerSpec(metadata, options) {
396
397
  }
397
398
  }
398
399
  for (const { endpointSpec, httpMethod, config } of deferredLinks) {
399
- const statusCode = config.statusCode === 0 ? String(getDefaultStatusCode(httpMethod)) : String(config.statusCode);
400
+ const statusCode = config.statusCode === 0 ? String(defaultLinkStatus(endpointSpec, httpMethod)) : String(config.statusCode);
400
401
  const link = {};
401
402
  if (config.handler) {
402
403
  const [ctrlClass, methodName] = config.handler;
@@ -764,13 +765,17 @@ function resolveSchemaFromRef(schema) {
764
765
  function cloneSchema(schema) {
765
766
  return JSON.parse(JSON.stringify(schema));
766
767
  }
767
- function getDefaultStatusCode(httpMethod) {
768
- return {
769
- GET: 200,
770
- PUT: 200,
771
- POST: 201,
772
- DELETE: 204
773
- }[httpMethod.toUpperCase()] || 200;
768
+ /**
769
+ * The status the runtime answers with when the handler sets none — wooks' own rule
770
+ * (`getDefaultHttpStatus`): POST with a body → 201, any other method with a body → 200,
771
+ * no body → 204.
772
+ */ function getDefaultStatusCode(httpMethod, hasBody = true) {
773
+ return (0, _wooksjs_event_http.getDefaultHttpStatus)(httpMethod.toUpperCase(), hasBody);
774
+ }
775
+ /** The default success response a link attaches to: the one the endpoint documents (with or without a body). */ function defaultLinkStatus(endpointSpec, httpMethod) {
776
+ const withBody = getDefaultStatusCode(httpMethod, true);
777
+ const noBody = getDefaultStatusCode(httpMethod, false);
778
+ return !endpointSpec.responses?.[withBody] && endpointSpec.responses?.[noBody] ? noBody : withBody;
774
779
  }
775
780
  const STATUS_DESCRIPTIONS = {
776
781
  200: "OK",
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Const, Controller, Moost, current, getMoostMate, useControllerContext } from "moost";
2
+ import { getDefaultHttpStatus, useResponse } from "@wooksjs/event-http";
2
3
  import { Get, HeaderRef, SetHeader, StatusRef, Url } from "@moostjs/event-http";
3
- import { useResponse } from "@wooksjs/event-http";
4
4
  import { serveFile } from "@wooksjs/http-static";
5
5
  import Path from "path";
6
6
  import { getAbsoluteFSPath } from "swagger-ui-dist";
@@ -175,22 +175,23 @@ function mapToSwaggerSpec(metadata, options) {
175
175
  if (!swaggerSpec.paths[handlerPath]) swaggerSpec.paths[handlerPath] = {};
176
176
  let responses;
177
177
  if (hmeta?.swaggerResponses) for (const [code, responseEntry] of Object.entries(hmeta.swaggerResponses)) {
178
- const newCode = code === "0" ? getDefaultStatusCode(handlerMethod) : code;
179
- for (const [contentType, conf] of Object.entries(responseEntry.content)) {
180
- const { response, example } = conf;
181
- const schema = resolveSwaggerSchemaFromConfig(response);
182
- if (schema) {
183
- responses = responses || {};
184
- const schemaWithExample = example !== void 0 ? {
185
- ...schema,
186
- example
187
- } : schema;
188
- if (!responses[newCode]) responses[newCode] = {
189
- description: responseEntry.description || defaultStatusDescription(Number(newCode)),
190
- content: {}
191
- };
192
- responses[newCode].content[contentType] = { schema: schemaWithExample };
193
- }
178
+ const contents = Object.entries(responseEntry.content).map(([contentType, conf]) => ({
179
+ contentType,
180
+ example: conf.example,
181
+ schema: resolveSwaggerSchemaFromConfig(conf.response)
182
+ }));
183
+ const newCode = code === "0" ? getDefaultStatusCode(handlerMethod, contents.some((c) => c.schema)) : code;
184
+ for (const { contentType, example, schema } of contents) if (schema) {
185
+ responses = responses || {};
186
+ const schemaWithExample = example !== void 0 ? {
187
+ ...schema,
188
+ example
189
+ } : schema;
190
+ if (!responses[newCode]) responses[newCode] = {
191
+ description: responseEntry.description || defaultStatusDescription(Number(newCode)),
192
+ content: {}
193
+ };
194
+ responses[newCode].content[contentType] = { schema: schemaWithExample };
194
195
  }
195
196
  if (responseEntry.headers) {
196
197
  const resolvedHeaders = resolveResponseHeaders(responseEntry.headers);
@@ -367,7 +368,7 @@ function mapToSwaggerSpec(metadata, options) {
367
368
  }
368
369
  }
369
370
  for (const { endpointSpec, httpMethod, config } of deferredLinks) {
370
- const statusCode = config.statusCode === 0 ? String(getDefaultStatusCode(httpMethod)) : String(config.statusCode);
371
+ const statusCode = config.statusCode === 0 ? String(defaultLinkStatus(endpointSpec, httpMethod)) : String(config.statusCode);
371
372
  const link = {};
372
373
  if (config.handler) {
373
374
  const [ctrlClass, methodName] = config.handler;
@@ -735,13 +736,17 @@ function resolveSchemaFromRef(schema) {
735
736
  function cloneSchema(schema) {
736
737
  return JSON.parse(JSON.stringify(schema));
737
738
  }
738
- function getDefaultStatusCode(httpMethod) {
739
- return {
740
- GET: 200,
741
- PUT: 200,
742
- POST: 201,
743
- DELETE: 204
744
- }[httpMethod.toUpperCase()] || 200;
739
+ /**
740
+ * The status the runtime answers with when the handler sets none — wooks' own rule
741
+ * (`getDefaultHttpStatus`): POST with a body → 201, any other method with a body → 200,
742
+ * no body → 204.
743
+ */ function getDefaultStatusCode(httpMethod, hasBody = true) {
744
+ return getDefaultHttpStatus(httpMethod.toUpperCase(), hasBody);
745
+ }
746
+ /** The default success response a link attaches to: the one the endpoint documents (with or without a body). */ function defaultLinkStatus(endpointSpec, httpMethod) {
747
+ const withBody = getDefaultStatusCode(httpMethod, true);
748
+ const noBody = getDefaultStatusCode(httpMethod, false);
749
+ return !endpointSpec.responses?.[withBody] && endpointSpec.responses?.[noBody] ? noBody : withBody;
745
750
  }
746
751
  const STATUS_DESCRIPTIONS = {
747
752
  200: "OK",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moostjs/swagger",
3
- "version": "0.6.53",
3
+ "version": "0.7.0",
4
4
  "description": "@moostjs/swagger",
5
5
  "keywords": [
6
6
  "composables",
@@ -45,10 +45,10 @@
45
45
  "vitest": "3.2.7"
46
46
  },
47
47
  "peerDependencies": {
48
- "@wooksjs/event-http": "^0.7.31",
49
- "@wooksjs/http-static": "^0.7.31",
50
- "@moostjs/event-http": "^0.6.53",
51
- "moost": "^0.6.53"
48
+ "@wooksjs/event-http": "^0.8.0",
49
+ "@wooksjs/http-static": "^0.8.0",
50
+ "moost": "^0.7.0",
51
+ "@moostjs/event-http": "^0.7.0"
52
52
  },
53
53
  "scripts": {
54
54
  "pub": "pnpm publish --access public",