vovk 3.0.0-beta.39 → 3.0.0-beta.40

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.
@@ -15,7 +15,7 @@ const getHandlerPath = (endpoint, params, query) => {
15
15
  for (const [key, value] of Object.entries(params ?? {})) {
16
16
  result = result.replace(`{${key}}`, value);
17
17
  }
18
- return `${result}${queryStr ? '?' : ''}${queryStr}`;
18
+ return `${result}${queryStr ? `?${queryStr}` : ''}`;
19
19
  };
20
20
  const createRPC = (givenSchema, segmentName, rpcModuleName, givenFetcher, options) => {
21
21
  const schema = givenSchema; // fixes incompatibilities with JSON module
@@ -37,10 +37,11 @@ const createRPC = (givenSchema, segmentName, rpcModuleName, givenFetcher, option
37
37
  }
38
38
  const controllerPrefix = trimPath(controllerSchema.prefix ?? '');
39
39
  const forceApiRoot = controllerSchema.forceApiRoot ?? segmentSchema.forceApiRoot;
40
- const originalApiRoot = forceApiRoot ?? options?.apiRoot ?? '/api';
40
+ const configRootEntry = schema.meta?.config?.rootEntry;
41
+ const originalApiRoot = forceApiRoot ?? options?.apiRoot ?? (configRootEntry ? `/${configRootEntry}` : '/api');
41
42
  for (const [staticMethodName, handlerSchema] of Object.entries(controllerSchema.handlers ?? {})) {
42
43
  const { path, httpMethod, validation } = handlerSchema;
43
- const getEndpoint = ({ apiRoot, params, query }) => {
44
+ const getURL = ({ apiRoot, params, query } = {}) => {
44
45
  apiRoot = apiRoot ?? originalApiRoot;
45
46
  const endpoint = [
46
47
  apiRoot.startsWith('http://') || apiRoot.startsWith('https://') || apiRoot.startsWith('/') ? '' : '/',
@@ -74,7 +75,7 @@ const createRPC = (givenSchema, segmentName, rpcModuleName, givenFetcher, option
74
75
  const internalOptions = {
75
76
  name: staticMethodName,
76
77
  httpMethod: httpMethod,
77
- getEndpoint,
78
+ getURL,
78
79
  validate,
79
80
  defaultHandler: defaultHandler_1.defaultHandler,
80
81
  defaultStreamHandler: defaultStreamHandler_1.defaultStreamHandler,
@@ -99,7 +100,7 @@ const createRPC = (givenSchema, segmentName, rpcModuleName, givenFetcher, option
99
100
  handler.fullSchema = schema;
100
101
  handler.isRPC = true;
101
102
  handler.apiRoot = originalApiRoot;
102
- handler.path = [segmentNamePath, controllerPrefix, path].filter(Boolean).join('/');
103
+ handler.getURL = getURL; // TODO fix typing
103
104
  handler.queryKey = (key) => [
104
105
  handler.segmentSchema.segmentName,
105
106
  handler.controllerSchema.prefix ?? '',
@@ -8,14 +8,14 @@ exports.DEFAULT_ERROR_MESSAGE = 'Unknown error at default fetcher';
8
8
  function createFetcher({ prepareRequestInit, transformResponse, onSuccess, onError, } = {}) {
9
9
  // fetcher uses HttpException class to throw errors of fake HTTP status 0 if client-side error occurs
10
10
  // For normal HTTP errors, it uses message and status code from the response of VovkErrorResponse type
11
- const newFetcher = async ({ httpMethod, getEndpoint, validate, defaultHandler, defaultStreamHandler, schema }, inputOptions) => {
11
+ const newFetcher = async ({ httpMethod, getURL, validate, defaultHandler, defaultStreamHandler, schema }, inputOptions) => {
12
12
  let response = null;
13
13
  let respData = null;
14
14
  let requestInit = null;
15
15
  try {
16
16
  const { meta, apiRoot, disableClientValidation, init, interpretAs } = inputOptions;
17
17
  let { body, query, params } = inputOptions;
18
- const endpoint = getEndpoint({ apiRoot, params, query });
18
+ const endpoint = getURL({ apiRoot, params, query });
19
19
  const unusedParams = Array.from(new URL(endpoint.startsWith('/') ? `http://localhost${endpoint}` : endpoint).pathname.matchAll(/\{([^}]+)\}/g)).map((m) => m[1]);
20
20
  if (unusedParams.length) {
21
21
  throw new HttpException_1.HttpException(types_1.HttpStatus.NULL, `Unused params: ${unusedParams.join(', ')} in ${endpoint}`, {
@@ -58,7 +58,11 @@ export type ClientMethod<T extends ((req: VovkRequest<KnownAny, KnownAny, KnownA
58
58
  controllerSchema: VovkControllerSchema;
59
59
  segmentSchema: VovkSegmentSchema;
60
60
  fullSchema: VovkSchema;
61
- path: string;
61
+ getURL: IsEmptyObject<StaticMethodInput<T>> extends true ? (urlInput?: {
62
+ apiRoot?: string;
63
+ }) => string : (urlInput: Pick<Prettify<StaticMethodInput<T>>, Extract<'params' | 'query', keyof Prettify<StaticMethodInput<T>>>> & {
64
+ apiRoot?: string;
65
+ }) => string;
62
66
  apiRoot: string;
63
67
  queryKey: (key?: unknown[]) => unknown[];
64
68
  __types: T['__types'];
@@ -77,7 +81,7 @@ export type VovkRPCModule<T, TFetcherOptions extends {
77
81
  export type VovkFetcher<TFetcherOptions> = (options: {
78
82
  name: string;
79
83
  httpMethod: HttpMethod;
80
- getEndpoint: (data: {
84
+ getURL: (data: {
81
85
  apiRoot: string | undefined;
82
86
  params: unknown;
83
87
  query: unknown;
@@ -1,14 +1,14 @@
1
- import type { KnownAny, VovkHandlerSchema, VovkLLMTool, VovkBasicJSONSchema } from '../types';
1
+ import type { KnownAny, VovkHandlerSchema, VovkLLMTool } from '../types';
2
2
  type Handler = ((...args: KnownAny[]) => KnownAny) & {
3
3
  fn?: (input: KnownAny) => KnownAny;
4
4
  isRPC?: boolean;
5
5
  schema?: VovkHandlerSchema;
6
6
  models?: {
7
- body?: VovkBasicJSONSchema;
8
- query?: VovkBasicJSONSchema;
9
- params?: VovkBasicJSONSchema;
10
- output?: VovkBasicJSONSchema;
11
- iteration?: VovkBasicJSONSchema;
7
+ body?: KnownAny;
8
+ query?: KnownAny;
9
+ params?: KnownAny;
10
+ output?: KnownAny;
11
+ iteration?: KnownAny;
12
12
  };
13
13
  };
14
14
  type CallerInput = {
@@ -15,7 +15,7 @@ const getHandlerPath = (endpoint, params, query) => {
15
15
  for (const [key, value] of Object.entries(params ?? {})) {
16
16
  result = result.replace(`{${key}}`, value);
17
17
  }
18
- return `${result}${queryStr ? '?' : ''}${queryStr}`;
18
+ return `${result}${queryStr ? `?${queryStr}` : ''}`;
19
19
  };
20
20
  const createRPC = (givenSchema, segmentName, rpcModuleName, givenFetcher, options) => {
21
21
  const schema = givenSchema; // fixes incompatibilities with JSON module
@@ -37,10 +37,11 @@ const createRPC = (givenSchema, segmentName, rpcModuleName, givenFetcher, option
37
37
  }
38
38
  const controllerPrefix = trimPath(controllerSchema.prefix ?? '');
39
39
  const forceApiRoot = controllerSchema.forceApiRoot ?? segmentSchema.forceApiRoot;
40
- const originalApiRoot = forceApiRoot ?? options?.apiRoot ?? '/api';
40
+ const configRootEntry = schema.meta?.config?.rootEntry;
41
+ const originalApiRoot = forceApiRoot ?? options?.apiRoot ?? (configRootEntry ? `/${configRootEntry}` : '/api');
41
42
  for (const [staticMethodName, handlerSchema] of Object.entries(controllerSchema.handlers ?? {})) {
42
43
  const { path, httpMethod, validation } = handlerSchema;
43
- const getEndpoint = ({ apiRoot, params, query }) => {
44
+ const getURL = ({ apiRoot, params, query } = {}) => {
44
45
  apiRoot = apiRoot ?? originalApiRoot;
45
46
  const endpoint = [
46
47
  apiRoot.startsWith('http://') || apiRoot.startsWith('https://') || apiRoot.startsWith('/') ? '' : '/',
@@ -74,7 +75,7 @@ const createRPC = (givenSchema, segmentName, rpcModuleName, givenFetcher, option
74
75
  const internalOptions = {
75
76
  name: staticMethodName,
76
77
  httpMethod: httpMethod,
77
- getEndpoint,
78
+ getURL,
78
79
  validate,
79
80
  defaultHandler: defaultHandler_1.defaultHandler,
80
81
  defaultStreamHandler: defaultStreamHandler_1.defaultStreamHandler,
@@ -99,7 +100,7 @@ const createRPC = (givenSchema, segmentName, rpcModuleName, givenFetcher, option
99
100
  handler.fullSchema = schema;
100
101
  handler.isRPC = true;
101
102
  handler.apiRoot = originalApiRoot;
102
- handler.path = [segmentNamePath, controllerPrefix, path].filter(Boolean).join('/');
103
+ handler.getURL = getURL; // TODO fix typing
103
104
  handler.queryKey = (key) => [
104
105
  handler.segmentSchema.segmentName,
105
106
  handler.controllerSchema.prefix ?? '',
@@ -8,14 +8,14 @@ exports.DEFAULT_ERROR_MESSAGE = 'Unknown error at default fetcher';
8
8
  function createFetcher({ prepareRequestInit, transformResponse, onSuccess, onError, } = {}) {
9
9
  // fetcher uses HttpException class to throw errors of fake HTTP status 0 if client-side error occurs
10
10
  // For normal HTTP errors, it uses message and status code from the response of VovkErrorResponse type
11
- const newFetcher = async ({ httpMethod, getEndpoint, validate, defaultHandler, defaultStreamHandler, schema }, inputOptions) => {
11
+ const newFetcher = async ({ httpMethod, getURL, validate, defaultHandler, defaultStreamHandler, schema }, inputOptions) => {
12
12
  let response = null;
13
13
  let respData = null;
14
14
  let requestInit = null;
15
15
  try {
16
16
  const { meta, apiRoot, disableClientValidation, init, interpretAs } = inputOptions;
17
17
  let { body, query, params } = inputOptions;
18
- const endpoint = getEndpoint({ apiRoot, params, query });
18
+ const endpoint = getURL({ apiRoot, params, query });
19
19
  const unusedParams = Array.from(new URL(endpoint.startsWith('/') ? `http://localhost${endpoint}` : endpoint).pathname.matchAll(/\{([^}]+)\}/g)).map((m) => m[1]);
20
20
  if (unusedParams.length) {
21
21
  throw new HttpException_1.HttpException(types_1.HttpStatus.NULL, `Unused params: ${unusedParams.join(', ')} in ${endpoint}`, {
@@ -58,7 +58,11 @@ export type ClientMethod<T extends ((req: VovkRequest<KnownAny, KnownAny, KnownA
58
58
  controllerSchema: VovkControllerSchema;
59
59
  segmentSchema: VovkSegmentSchema;
60
60
  fullSchema: VovkSchema;
61
- path: string;
61
+ getURL: IsEmptyObject<StaticMethodInput<T>> extends true ? (urlInput?: {
62
+ apiRoot?: string;
63
+ }) => string : (urlInput: Pick<Prettify<StaticMethodInput<T>>, Extract<'params' | 'query', keyof Prettify<StaticMethodInput<T>>>> & {
64
+ apiRoot?: string;
65
+ }) => string;
62
66
  apiRoot: string;
63
67
  queryKey: (key?: unknown[]) => unknown[];
64
68
  __types: T['__types'];
@@ -77,7 +81,7 @@ export type VovkRPCModule<T, TFetcherOptions extends {
77
81
  export type VovkFetcher<TFetcherOptions> = (options: {
78
82
  name: string;
79
83
  httpMethod: HttpMethod;
80
- getEndpoint: (data: {
84
+ getURL: (data: {
81
85
  apiRoot: string | undefined;
82
86
  params: unknown;
83
87
  query: unknown;
@@ -1,14 +1,14 @@
1
- import type { KnownAny, VovkHandlerSchema, VovkLLMTool, VovkBasicJSONSchema } from '../types';
1
+ import type { KnownAny, VovkHandlerSchema, VovkLLMTool } from '../types';
2
2
  type Handler = ((...args: KnownAny[]) => KnownAny) & {
3
3
  fn?: (input: KnownAny) => KnownAny;
4
4
  isRPC?: boolean;
5
5
  schema?: VovkHandlerSchema;
6
6
  models?: {
7
- body?: VovkBasicJSONSchema;
8
- query?: VovkBasicJSONSchema;
9
- params?: VovkBasicJSONSchema;
10
- output?: VovkBasicJSONSchema;
11
- iteration?: VovkBasicJSONSchema;
7
+ body?: KnownAny;
8
+ query?: KnownAny;
9
+ params?: KnownAny;
10
+ output?: KnownAny;
11
+ iteration?: KnownAny;
12
12
  };
13
13
  };
14
14
  type CallerInput = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk",
3
- "version": "3.0.0-beta.39",
3
+ "version": "3.0.0-beta.40",
4
4
  "main": "./cjs/index.js",
5
5
  "module": "./mjs/index.js",
6
6
  "types": "./mjs/index.d.ts",