fets 0.6.4 → 0.6.5

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.
@@ -190,7 +190,7 @@ function createRouterBase({ fetchAPI: givenFetchAPI, base: basePath = '/', plugi
190
190
  }
191
191
  exports.createRouterBase = createRouterBase;
192
192
  function createRouter(options = {}) {
193
- const { openAPI: { endpoint: oasEndpoint = '/openapi.json', ...openAPIDocument } = {}, swaggerUI: { endpoint: swaggerUIEndpoint = '/docs', ...swaggerUIOpts } = {}, plugins: userPlugins = [], base = '/', } = options;
193
+ const { openAPI: { endpoint: oasEndpoint = '/openapi.json', includeValidationErrors, ...openAPIDocument } = {}, swaggerUI: { endpoint: swaggerUIEndpoint = '/docs', ...swaggerUIOpts } = {}, plugins: userPlugins = [], base = '/', } = options;
194
194
  openAPIDocument.openapi = openAPIDocument.openapi || '3.0.1';
195
195
  const oasInfo = (openAPIDocument.info ||= {});
196
196
  oasInfo.title ||= 'feTS API';
@@ -210,6 +210,7 @@ function createRouter(options = {}) {
210
210
  oasEndpoint,
211
211
  swaggerUIEndpoint,
212
212
  swaggerUIOpts,
213
+ includeValidationErrors,
213
214
  }),
214
215
  ]
215
216
  : []),
@@ -39,10 +39,16 @@ const requestValidationErrorSchema = {
39
39
  },
40
40
  additionalProperties: false,
41
41
  };
42
- function useOpenAPI({ oasEndpoint, swaggerUIEndpoint, swaggerUIOpts, }) {
42
+ function useOpenAPI({ oasEndpoint, swaggerUIEndpoint, swaggerUIOpts, includeValidationErrors, }) {
43
43
  let paths;
44
44
  return {
45
45
  onRouterInit(router) {
46
+ if (includeValidationErrors) {
47
+ const components = (router.openAPIDocument.components =
48
+ router.openAPIDocument.components || {});
49
+ const schemas = (components.schemas = components.schemas || {});
50
+ schemas.RequestValidationError = requestValidationErrorSchema;
51
+ }
46
52
  paths = router.openAPIDocument.paths = router.openAPIDocument.paths || {};
47
53
  if (oasEndpoint) {
48
54
  router.route({
@@ -173,13 +179,15 @@ function useOpenAPI({ oasEndpoint, swaggerUIEndpoint, swaggerUIOpts, }) {
173
179
  },
174
180
  };
175
181
  }
176
- if (isRequestValidated && !operation.responses?.[400]) {
182
+ if (includeValidationErrors && isRequestValidated && !operation.responses?.[400]) {
177
183
  operation.responses ||= {};
178
184
  operation.responses[400] = {
179
185
  description: 'Request validation failed',
180
186
  content: {
181
187
  'application/json': {
182
- schema: requestValidationErrorSchema,
188
+ schema: {
189
+ $ref: '#/components/schemas/RequestValidationError',
190
+ },
183
191
  },
184
192
  },
185
193
  };
@@ -185,7 +185,7 @@ export function createRouterBase({ fetchAPI: givenFetchAPI, base: basePath = '/'
185
185
  };
186
186
  }
187
187
  export function createRouter(options = {}) {
188
- const { openAPI: { endpoint: oasEndpoint = '/openapi.json', ...openAPIDocument } = {}, swaggerUI: { endpoint: swaggerUIEndpoint = '/docs', ...swaggerUIOpts } = {}, plugins: userPlugins = [], base = '/', } = options;
188
+ const { openAPI: { endpoint: oasEndpoint = '/openapi.json', includeValidationErrors, ...openAPIDocument } = {}, swaggerUI: { endpoint: swaggerUIEndpoint = '/docs', ...swaggerUIOpts } = {}, plugins: userPlugins = [], base = '/', } = options;
189
189
  openAPIDocument.openapi = openAPIDocument.openapi || '3.0.1';
190
190
  const oasInfo = (openAPIDocument.info ||= {});
191
191
  oasInfo.title ||= 'feTS API';
@@ -205,6 +205,7 @@ export function createRouter(options = {}) {
205
205
  oasEndpoint,
206
206
  swaggerUIEndpoint,
207
207
  swaggerUIOpts,
208
+ includeValidationErrors,
208
209
  }),
209
210
  ]
210
211
  : []),
@@ -35,10 +35,16 @@ const requestValidationErrorSchema = {
35
35
  },
36
36
  additionalProperties: false,
37
37
  };
38
- export function useOpenAPI({ oasEndpoint, swaggerUIEndpoint, swaggerUIOpts, }) {
38
+ export function useOpenAPI({ oasEndpoint, swaggerUIEndpoint, swaggerUIOpts, includeValidationErrors, }) {
39
39
  let paths;
40
40
  return {
41
41
  onRouterInit(router) {
42
+ if (includeValidationErrors) {
43
+ const components = (router.openAPIDocument.components =
44
+ router.openAPIDocument.components || {});
45
+ const schemas = (components.schemas = components.schemas || {});
46
+ schemas.RequestValidationError = requestValidationErrorSchema;
47
+ }
42
48
  paths = router.openAPIDocument.paths = router.openAPIDocument.paths || {};
43
49
  if (oasEndpoint) {
44
50
  router.route({
@@ -169,13 +175,15 @@ export function useOpenAPI({ oasEndpoint, swaggerUIEndpoint, swaggerUIOpts, }) {
169
175
  },
170
176
  };
171
177
  }
172
- if (isRequestValidated && !operation.responses?.[400]) {
178
+ if (includeValidationErrors && isRequestValidated && !operation.responses?.[400]) {
173
179
  operation.responses ||= {};
174
180
  operation.responses[400] = {
175
181
  description: 'Request validation failed',
176
182
  content: {
177
183
  'application/json': {
178
- schema: requestValidationErrorSchema,
184
+ schema: {
185
+ $ref: '#/components/schemas/RequestValidationError',
186
+ },
179
187
  },
180
188
  },
181
189
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fets",
3
- "version": "0.6.4",
3
+ "version": "0.6.5",
4
4
  "description": "TypeScript HTTP Framework focusing on e2e type-safety, easy setup, performance & great developer experience",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -29,5 +29,6 @@ export type OpenAPIPluginOptions = {
29
29
  oasEndpoint: string | false;
30
30
  swaggerUIEndpoint: string | false;
31
31
  swaggerUIOpts: SwaggerUIOpts;
32
+ includeValidationErrors?: boolean;
32
33
  };
33
- export declare function useOpenAPI<TServerContext, TComponents extends RouterComponentsBase>({ oasEndpoint, swaggerUIEndpoint, swaggerUIOpts, }: OpenAPIPluginOptions): RouterPlugin<TServerContext, TComponents>;
34
+ export declare function useOpenAPI<TServerContext, TComponents extends RouterComponentsBase>({ oasEndpoint, swaggerUIEndpoint, swaggerUIOpts, includeValidationErrors, }: OpenAPIPluginOptions): RouterPlugin<TServerContext, TComponents>;
@@ -29,5 +29,6 @@ export type OpenAPIPluginOptions = {
29
29
  oasEndpoint: string | false;
30
30
  swaggerUIEndpoint: string | false;
31
31
  swaggerUIOpts: SwaggerUIOpts;
32
+ includeValidationErrors?: boolean;
32
33
  };
33
- export declare function useOpenAPI<TServerContext, TComponents extends RouterComponentsBase>({ oasEndpoint, swaggerUIEndpoint, swaggerUIOpts, }: OpenAPIPluginOptions): RouterPlugin<TServerContext, TComponents>;
34
+ export declare function useOpenAPI<TServerContext, TComponents extends RouterComponentsBase>({ oasEndpoint, swaggerUIEndpoint, swaggerUIOpts, includeValidationErrors, }: OpenAPIPluginOptions): RouterPlugin<TServerContext, TComponents>;
@@ -65,6 +65,7 @@ export type OpenAPIDocument = {
65
65
  export interface RouterOpenAPIOptions<TComponents extends RouterComponentsBase> extends OpenAPIDocument {
66
66
  endpoint?: string | false;
67
67
  components?: TComponents;
68
+ includeValidationErrors?: boolean;
68
69
  }
69
70
  export interface RouterSwaggerUIOptions extends SwaggerUIOpts {
70
71
  endpoint?: string | false;
@@ -65,6 +65,7 @@ export type OpenAPIDocument = {
65
65
  export interface RouterOpenAPIOptions<TComponents extends RouterComponentsBase> extends OpenAPIDocument {
66
66
  endpoint?: string | false;
67
67
  components?: TComponents;
68
+ includeValidationErrors?: boolean;
68
69
  }
69
70
  export interface RouterSwaggerUIOptions extends SwaggerUIOpts {
70
71
  endpoint?: string | false;