@qualisero/openapi-endpoint 0.12.3 → 0.14.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.
@@ -1,27 +1,4 @@
1
- import { toValue } from 'vue';
2
- // Known option-like properties to distinguish from path parameters
3
- const OPTION_PROPERTIES = [
4
- 'enabled',
5
- 'onSuccess',
6
- 'onError',
7
- 'onSettled',
8
- 'select',
9
- 'retry',
10
- 'staleTime',
11
- 'cacheTime',
12
- 'refetchOnWindowFocus',
13
- 'refetchOnReconnect',
14
- 'refetchOnMount',
15
- 'suspense',
16
- 'useErrorBoundary',
17
- 'meta',
18
- 'mutationKey',
19
- 'mutationFn',
20
- 'queryKey',
21
- 'queryFn',
22
- 'initialData',
23
- 'context',
24
- ];
1
+ import { computed, toValue } from 'vue';
25
2
  // Helper to resolve path parameters in a URL path
26
3
  export function resolvePath(path, pathParams) {
27
4
  if (pathParams === null || pathParams === undefined)
@@ -31,22 +8,12 @@ export function resolvePath(path, pathParams) {
31
8
  return path;
32
9
  let resolvedPath = path;
33
10
  Object.entries(pathParamsValue).forEach(([key, value]) => {
34
- if (value !== undefined && value !== null) {
11
+ if (value !== null && value !== undefined) {
35
12
  resolvedPath = resolvedPath.replace(`{${key}}`, String(value));
36
13
  }
37
14
  });
38
15
  return resolvedPath;
39
16
  }
40
- function isPathParams(path, pathParams) {
41
- if (!pathParams)
42
- return false;
43
- const paramNames = Object.keys(pathParams);
44
- // If any option-like property is present, it's not path params
45
- if (paramNames.some((name) => OPTION_PROPERTIES.includes(name))) {
46
- return false;
47
- }
48
- return paramNames.every((paramName) => path.includes(`{${paramName}}`));
49
- }
50
17
  // Helper to check if all required path parameters are provided
51
18
  export function isPathResolved(path) {
52
19
  return !/{[^}]+}/.test(path);
@@ -55,29 +22,51 @@ export function isPathResolved(path) {
55
22
  export function generateQueryKey(resolvedPath) {
56
23
  return resolvedPath.split('/').filter((segment) => segment.length > 0);
57
24
  }
58
- export function getParamsOptionsFrom(path, pathParamsOrOptions, optionsOrNull) {
59
- let pathParams = undefined;
60
- let options = undefined;
61
- if (optionsOrNull === undefined) {
62
- const pathParamsOrOptionsValue = toValue(pathParamsOrOptions);
63
- if ((typeof pathParamsOrOptions === 'object' || typeof pathParamsOrOptions === 'function') &&
64
- pathParamsOrOptions !== null &&
65
- pathParamsOrOptionsValue &&
66
- typeof pathParamsOrOptionsValue === 'object' &&
67
- isPathParams(path, pathParamsOrOptionsValue)) {
68
- // Called as: useEndpointQuery(operationId, pathParams)
69
- pathParams = pathParamsOrOptions;
25
+ /**
26
+ * Composable for resolving operation paths and parameters.
27
+ * Consolidates the common pattern of computing resolved paths, query keys, and parameters.
28
+ *
29
+ * @param path - The OpenAPI path template (e.g., '/pets/{petId}')
30
+ * @param pathParams - Reactive path parameters
31
+ * @param queryParams - Optional reactive query parameters
32
+ * @param extraPathParams - Optional ref for additional path params (used by mutations)
33
+ */
34
+ export function useResolvedOperation(path, pathParams, queryParams, extraPathParams) {
35
+ // Compute all path params (base + extra for mutations)
36
+ const allPathParams = computed(() => {
37
+ const base = toValue(pathParams) || {};
38
+ if (extraPathParams?.value) {
39
+ return { ...base, ...extraPathParams.value };
70
40
  }
71
- else {
72
- // Called as: useEndpointQuery(operationId, [options])
73
- options = pathParamsOrOptions;
41
+ return base;
42
+ });
43
+ // Compute resolved path
44
+ const resolvedPath = computed(() => resolvePath(path, allPathParams.value));
45
+ // Compute query params
46
+ const allQueryParams = computed(() => {
47
+ const result = toValue(queryParams);
48
+ return (result || {});
49
+ });
50
+ // Generate query key including query params if present
51
+ const queryKey = computed(() => {
52
+ const baseKey = generateQueryKey(resolvedPath.value);
53
+ const qParams = allQueryParams.value;
54
+ if (qParams && Object.keys(qParams).length > 0) {
55
+ return [...baseKey, qParams];
74
56
  }
75
- }
76
- else {
77
- // Called as: useEndpointQuery(operationId, pathParams, options)
78
- pathParams = pathParamsOrOptions;
79
- options = optionsOrNull;
80
- }
57
+ return baseKey;
58
+ });
59
+ // Check if path is resolved
60
+ const isResolved = computed(() => isPathResolved(resolvedPath.value));
61
+ return {
62
+ pathParams: allPathParams,
63
+ resolvedPath,
64
+ queryParams: allQueryParams,
65
+ queryKey: queryKey,
66
+ isResolved,
67
+ };
68
+ }
69
+ export function normalizeParamsOptions(pathParams, options) {
81
70
  return {
82
71
  pathParams: pathParams ?? {},
83
72
  options: options ?? {},