vovk 3.0.0-draft.111 → 3.0.0-draft.113

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.
@@ -8,6 +8,15 @@ exports.DEFAULT_ERROR_MESSAGE = 'Unknown error at the defaultFetcher';
8
8
  // For normal HTTP errors, it uses message and status code from the response of VovkErrorResponse type
9
9
  const fetcher = async ({ httpMethod, getEndpoint, validate, defaultHandler, defaultStreamHandler }, { params, query, body, apiRoot = '/api', ...options }) => {
10
10
  const endpoint = getEndpoint({ apiRoot, params, query });
11
+ const unusedParams = new URL(endpoint).pathname.split('/').filter((segment) => segment.startsWith(':'));
12
+ if (unusedParams.length) {
13
+ throw new HttpException_1.HttpException(types_1.HttpStatus.NULL, `Unused params: ${unusedParams.join(', ')} in ${endpoint}`, {
14
+ body,
15
+ query,
16
+ params,
17
+ endpoint,
18
+ });
19
+ }
11
20
  if (!options.disableClientValidation) {
12
21
  try {
13
22
  await validate({ body, query, params, endpoint });
package/dist/types.d.ts CHANGED
@@ -117,7 +117,7 @@ export type VovkEnv = {
117
117
  __VOVK_EXIT__?: 'true' | 'false';
118
118
  };
119
119
  export type GenerateFromTemplate = {
120
- templatePath: string;
120
+ templateGlob: string | null;
121
121
  outDir?: string;
122
122
  templateName?: string;
123
123
  fullSchemaJSON?: string | boolean;
@@ -1,4 +1,4 @@
1
1
  import type { StaticClass } from '../types';
2
2
  export declare function generateStaticAPI(c: Record<string, StaticClass>, slug?: string): {
3
- [x: string]: string[];
3
+ [slug]: string[];
4
4
  }[];
@@ -4,7 +4,7 @@ export declare function getControllerSchema(controller: VovkController, controll
4
4
  originalControllerName: string;
5
5
  prefix: string;
6
6
  handlers: {
7
- [x: string]: {
7
+ [k: string]: {
8
8
  path: string;
9
9
  httpMethod: string;
10
10
  openapi?: import("openapi3-ts/oas31").OperationObject;
@@ -1,6 +1,6 @@
1
1
  import { VovkHandlerSchema, VovkValidationType, type KnownAny, type VovkRequest } from '../types';
2
- export declare function withValidation<T extends (req: KnownAny, params: KnownAny) => KnownAny, BODY_MODEL, QUERY_MODEL, PARAMS_MODEL, OUTPUT_MODEL, ITERATION_MODEL>({ skipServerSideValidation, skipSchemaEmission, validateEveryIteration, body, query, params, output, iteration, handle, getHandlerSchema, validate, }: {
3
- skipServerSideValidation?: boolean | VovkValidationType[];
2
+ export declare function withValidation<T extends (req: KnownAny, params: KnownAny) => KnownAny, BODY_MODEL, QUERY_MODEL, PARAMS_MODEL, OUTPUT_MODEL, ITERATION_MODEL>({ disableServerSideValidation, skipSchemaEmission, validateEveryIteration, body, query, params, output, iteration, handle, getHandlerSchema, validate, }: {
3
+ disableServerSideValidation?: boolean | VovkValidationType[];
4
4
  skipSchemaEmission?: boolean | VovkValidationType[];
5
5
  validateEveryIteration?: boolean;
6
6
  body?: BODY_MODEL;
@@ -16,5 +16,6 @@ export declare function withValidation<T extends (req: KnownAny, params: KnownAn
16
16
  type: VovkValidationType;
17
17
  req: VovkRequest<KnownAny, KnownAny>;
18
18
  status?: number;
19
+ i?: number;
19
20
  }) => KnownAny;
20
21
  }): T;
@@ -5,25 +5,25 @@ const HttpException_1 = require("../HttpException");
5
5
  const types_1 = require("../types");
6
6
  const setHandlerSchema_1 = require("./setHandlerSchema");
7
7
  const validationTypes = ['body', 'query', 'params', 'output', 'iteration'];
8
- function withValidation({ skipServerSideValidation, skipSchemaEmission, validateEveryIteration, body, query, params, output, iteration, handle, getHandlerSchema, validate, }) {
9
- const skipServerSideValidationKeys = skipServerSideValidation === false
8
+ function withValidation({ disableServerSideValidation, skipSchemaEmission, validateEveryIteration, body, query, params, output, iteration, handle, getHandlerSchema, validate, }) {
9
+ const disableServerSideValidationKeys = disableServerSideValidation === false
10
10
  ? []
11
- : skipServerSideValidation === true
11
+ : disableServerSideValidation === true
12
12
  ? validationTypes
13
- : (skipServerSideValidation ?? []);
13
+ : (disableServerSideValidation ?? []);
14
14
  const skipSchemaEmissionKeys = skipSchemaEmission === false ? [] : skipSchemaEmission === true ? validationTypes : (skipSchemaEmission ?? []);
15
15
  const outputHandler = async (req, handlerParams) => {
16
16
  const data = await handle(req, handlerParams);
17
17
  if (output && iteration) {
18
18
  throw new HttpException_1.HttpException(types_1.HttpStatus.INTERNAL_SERVER_ERROR, "Output and iteration are mutually exclusive. You can't use them together.");
19
19
  }
20
- if (output && !skipServerSideValidationKeys.includes('output')) {
20
+ if (output && !disableServerSideValidationKeys.includes('output')) {
21
21
  if (!data) {
22
22
  throw new HttpException_1.HttpException(types_1.HttpStatus.INTERNAL_SERVER_ERROR, 'Output is required. You probably forgot to return something from your handler.');
23
23
  }
24
24
  await validate(data, output, { type: 'output', req });
25
25
  }
26
- if (iteration && !skipServerSideValidationKeys.includes('iteration')) {
26
+ if (iteration && !disableServerSideValidationKeys.includes('iteration')) {
27
27
  // We assume `data` is an async iterable here; you might want to check that:
28
28
  if (!data || typeof data[Symbol.asyncIterator] !== 'function') {
29
29
  throw new HttpException_1.HttpException(types_1.HttpStatus.INTERNAL_SERVER_ERROR, 'Data is not an async iterable but iteration validation is defined.');
@@ -33,7 +33,7 @@ function withValidation({ skipServerSideValidation, skipSchemaEmission, validate
33
33
  let i = 0;
34
34
  for await (const item of data) {
35
35
  if (validateEveryIteration || i === 0) {
36
- await validate(item, iteration, { type: 'iteration', req, status: 200 });
36
+ await validate(item, iteration, { type: 'iteration', req, status: 200, i });
37
37
  }
38
38
  i++;
39
39
  yield item;
@@ -46,19 +46,19 @@ function withValidation({ skipServerSideValidation, skipSchemaEmission, validate
46
46
  return data;
47
47
  };
48
48
  const resultHandler = async (req, handlerParams) => {
49
- if (body && !skipServerSideValidationKeys.includes('body')) {
49
+ if (body && !disableServerSideValidationKeys.includes('body')) {
50
50
  const data = await req.json();
51
51
  const instance = (await validate(data, body, { type: 'body', req })) ?? data;
52
52
  // redeclare to add ability to call req.json() again
53
53
  req.json = () => Promise.resolve(data);
54
54
  req.vovk.body = () => Promise.resolve(instance);
55
55
  }
56
- if (query && !skipServerSideValidationKeys.includes('query')) {
56
+ if (query && !disableServerSideValidationKeys.includes('query')) {
57
57
  const data = req.vovk.query();
58
58
  const instance = (await validate(data, query, { type: 'query', req })) ?? data;
59
59
  req.vovk.query = () => instance;
60
60
  }
61
- if (params && !skipServerSideValidationKeys.includes('params')) {
61
+ if (params && !disableServerSideValidationKeys.includes('params')) {
62
62
  const data = req.vovk.params();
63
63
  const instance = (await validate(data, params, { type: 'params', req })) ?? data;
64
64
  req.vovk.params = () => instance;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk",
3
- "version": "3.0.0-draft.111",
3
+ "version": "3.0.0-draft.113",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "description": "RESTful RPC for Next.js - Transforms Next.js into a powerful REST API platform with RPC capabilities.",