@piveau/sdk-vue 0.0.0-alpha-2.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/README.md +66 -0
- package/dist/__tests__/defineResourceGetters.test.d.ts +1 -0
- package/dist/__tests__/defineResourceGetters.test.js +119 -0
- package/dist/__tests__/testUtils.d.ts +10 -0
- package/dist/__tests__/testUtils.js +10 -0
- package/dist/__tests__/useResourceFactory.test.d.ts +1 -0
- package/dist/__tests__/useResourceFactory.test.js +125 -0
- package/dist/__tests__/useSearchFactory.test.d.ts +1 -0
- package/dist/__tests__/useSearchFactory.test.js +102 -0
- package/dist/__tests__/useSearchQueryParams.test.d.ts +1 -0
- package/dist/__tests__/useSearchQueryParams.test.js +89 -0
- package/dist/composables/locale.d.ts +27 -0
- package/dist/composables/locale.js +8 -0
- package/dist/composables/usePropertyTable.d.ts +1 -0
- package/dist/composables/usePropertyTable.js +2 -0
- package/dist/defineHubSearch.d.ts +37 -0
- package/dist/defineHubSearch.js +91 -0
- package/dist/getters.d.ts +26 -0
- package/dist/getters.js +28 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +29 -0
- package/dist/integrations/__tests__/dcatAp.test.d.ts +1 -0
- package/dist/integrations/__tests__/dcatAp.test.js +36 -0
- package/dist/integrations/__tests__/https-data-paysdelaloire-fr-explore-dataset-agenda-culture-de-la-region-des-pays-de-la-loire-.json +647 -0
- package/dist/integrations/dcatAp/dcatAp.d.ts +1107 -0
- package/dist/integrations/dcatAp/dcatAp.js +185 -0
- package/dist/integrations/dcatAp/index.d.ts +2 -0
- package/dist/integrations/dcatAp/index.js +2 -0
- package/dist/integrations/dcatAp/properties.d.ts +9 -0
- package/dist/integrations/dcatAp/properties.js +142 -0
- package/dist/integrations/index.d.ts +1 -0
- package/dist/integrations/index.js +1 -0
- package/dist/locale/__tests__/index.test.d.ts +1 -0
- package/dist/locale/__tests__/index.test.js +12 -0
- package/dist/locale/adapters/default.d.ts +6 -0
- package/dist/locale/adapters/default.js +60 -0
- package/dist/locale/de.d.ts +2096 -0
- package/dist/locale/de.js +2096 -0
- package/dist/locale/en.d.ts +2101 -0
- package/dist/locale/en.js +2101 -0
- package/dist/locale/index.d.ts +2 -0
- package/dist/locale/index.js +2 -0
- package/dist/mock/db.json +10862 -0
- package/dist/mock/mockedHubSearch.d.ts +4 -0
- package/dist/mock/mockedHubSearch.js +40 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.js +0 -0
- package/dist/useGettersFactory.d.ts +7 -0
- package/dist/useGettersFactory.js +10 -0
- package/dist/useMetrics.d.ts +14 -0
- package/dist/useMetrics.js +22 -0
- package/dist/useResourceFactory.d.ts +41 -0
- package/dist/useResourceFactory.js +89 -0
- package/dist/useSearchFactory.d.ts +91 -0
- package/dist/useSearchFactory.js +161 -0
- package/dist/useSearchQueryParams.d.ts +223 -0
- package/dist/useSearchQueryParams.js +91 -0
- package/dist/utils/__tests__/propertyTable.test.d.ts +1 -0
- package/dist/utils/__tests__/propertyTable.test.js +372 -0
- package/dist/utils/helpers.d.ts +92 -0
- package/dist/utils/helpers.js +46 -0
- package/dist/utils/propertyTable.d.ts +82 -0
- package/dist/utils/propertyTable.js +113 -0
- package/dist/utils/vHelpers.d.ts +58 -0
- package/dist/utils/vHelpers.js +50 -0
- package/dist/utils.d.ts +61 -0
- package/dist/utils.js +37 -0
- package/package.json +54 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { getResourceById, searchResource } from "@piveau/sdk-core";
|
|
2
|
+
import { useRoute, useRouter } from "vue-router";
|
|
3
|
+
import { useRouteQuery } from "@vueuse/router";
|
|
4
|
+
import { assertQueryClient } from "./utils.js";
|
|
5
|
+
import { useSearchFactory } from "./useSearchFactory.js";
|
|
6
|
+
import { useResourceFactory } from "./useResourceFactory.js";
|
|
7
|
+
import { useQueryClient } from "./index.js";
|
|
8
|
+
export function defineHubSearch(options, setup) {
|
|
9
|
+
const {
|
|
10
|
+
baseUrl,
|
|
11
|
+
index,
|
|
12
|
+
indexDetails,
|
|
13
|
+
schema,
|
|
14
|
+
facets = [],
|
|
15
|
+
defaultOptions = {},
|
|
16
|
+
enabled = true
|
|
17
|
+
} = options;
|
|
18
|
+
async function defaultFetcherFn(_opts) {
|
|
19
|
+
const res = await searchResource({
|
|
20
|
+
baseUrl,
|
|
21
|
+
params: {
|
|
22
|
+
..._opts.params,
|
|
23
|
+
filter: index
|
|
24
|
+
},
|
|
25
|
+
additionalParams: _opts.additionalParams,
|
|
26
|
+
headers: _opts.headers
|
|
27
|
+
});
|
|
28
|
+
return res.data;
|
|
29
|
+
}
|
|
30
|
+
const {
|
|
31
|
+
qc = useQueryClient(),
|
|
32
|
+
searchFetcherFn = defaultFetcherFn
|
|
33
|
+
} = defaultOptions;
|
|
34
|
+
assertQueryClient(qc);
|
|
35
|
+
const useSearch = useSearchFactory({
|
|
36
|
+
schema,
|
|
37
|
+
ctx: options,
|
|
38
|
+
facets,
|
|
39
|
+
index,
|
|
40
|
+
fetcherFn: searchFetcherFn,
|
|
41
|
+
enabled,
|
|
42
|
+
qc
|
|
43
|
+
}, setup);
|
|
44
|
+
const useResource = useResourceFactory({
|
|
45
|
+
baseUrl,
|
|
46
|
+
schema,
|
|
47
|
+
ctx: options,
|
|
48
|
+
index: indexDetails ?? index,
|
|
49
|
+
enabled,
|
|
50
|
+
fetcherFn: async (_options) => {
|
|
51
|
+
return getResourceById(_options);
|
|
52
|
+
},
|
|
53
|
+
qc
|
|
54
|
+
}, setup);
|
|
55
|
+
function refSyncedWithRouteQuery(searchParam, defaultValue) {
|
|
56
|
+
const route = useRoute();
|
|
57
|
+
const router = useRouter();
|
|
58
|
+
return useRouteQuery(searchParam, defaultValue, {
|
|
59
|
+
router,
|
|
60
|
+
route,
|
|
61
|
+
mode: "push"
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function refSyncedWithRouteQueryFacet(searchParam, defaultValue) {
|
|
65
|
+
const route = useRoute();
|
|
66
|
+
const router = useRouter();
|
|
67
|
+
return useRouteQuery(searchParam, defaultValue, {
|
|
68
|
+
// always transform the value to an array
|
|
69
|
+
transform: (value) => {
|
|
70
|
+
if (Array.isArray(value))
|
|
71
|
+
return value;
|
|
72
|
+
if (typeof value === "string")
|
|
73
|
+
return [value];
|
|
74
|
+
return [];
|
|
75
|
+
},
|
|
76
|
+
router,
|
|
77
|
+
route,
|
|
78
|
+
mode: "push"
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
useSearch,
|
|
83
|
+
useResource,
|
|
84
|
+
refSyncedWithRouteQuery,
|
|
85
|
+
refSyncedWithRouteQueryFacet,
|
|
86
|
+
setup
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
export function defineHubSearchIntegration(schema, setup) {
|
|
90
|
+
return { setup, schema };
|
|
91
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ComputedRef, MaybeRefOrGetter } from 'vue-demi';
|
|
2
|
+
import type { z } from 'zod';
|
|
3
|
+
/**
|
|
4
|
+
* Type of an object of Getters that infers the argument.
|
|
5
|
+
*/
|
|
6
|
+
export type GettersTree<TModelSchema = any, TContext = any> = Record<string, ((data: TModelSchema, ctx?: TContext, getters?: GettersTree<TModelSchema, TContext>) => any) | (() => any)>;
|
|
7
|
+
/**
|
|
8
|
+
* Type helper to make it easier to use define resource getters
|
|
9
|
+
* accepts a direct {@link GettersTree} object, a {@link z.Schema} schema, and a context object.
|
|
10
|
+
*/
|
|
11
|
+
export declare function defineGetters<TModelSchema = any, TContext = any, TGettersTree extends GettersTree<TModelSchema, TContext> = GettersTree<TModelSchema, TContext>>({ schema, getters, ctx, }: {
|
|
12
|
+
schema: z.Schema<TModelSchema>;
|
|
13
|
+
getters: TGettersTree;
|
|
14
|
+
ctx?: TContext;
|
|
15
|
+
}): { [K in keyof TGettersTree]: TGettersTree[K]; };
|
|
16
|
+
export type ComputedGettersTree<TModelSchema = any, TContext = any, TGettersTree extends GettersTree<TModelSchema, TContext> = GettersTree<TModelSchema, TContext>> = {
|
|
17
|
+
[K in keyof TGettersTree]: ComputedRef<ReturnType<TGettersTree[K]>>;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Composable function that takes reactive state and getters and turns it into computed getters.
|
|
21
|
+
*/
|
|
22
|
+
export declare function toComputedGetters<TModelSchema = any, TContext = any, TGettersTree extends GettersTree<TModelSchema, TContext> = GettersTree<TModelSchema, TContext>>({ data, getters, ctx, }: {
|
|
23
|
+
data?: MaybeRefOrGetter<TModelSchema | undefined>;
|
|
24
|
+
getters: TGettersTree;
|
|
25
|
+
ctx?: TContext;
|
|
26
|
+
}): ComputedGettersTree<TModelSchema, TContext, TGettersTree>;
|
package/dist/getters.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { computed, toValue } from "vue-demi";
|
|
2
|
+
export function defineGetters({
|
|
3
|
+
schema,
|
|
4
|
+
getters,
|
|
5
|
+
ctx
|
|
6
|
+
}) {
|
|
7
|
+
return { schema, getters, ctx }.getters;
|
|
8
|
+
}
|
|
9
|
+
export function toComputedGetters({
|
|
10
|
+
data,
|
|
11
|
+
getters,
|
|
12
|
+
ctx
|
|
13
|
+
}) {
|
|
14
|
+
return Object.keys(getters || {}).reduce(
|
|
15
|
+
(computedGetters, name) => {
|
|
16
|
+
const getter = getters[name];
|
|
17
|
+
computedGetters[name] = computed(() => {
|
|
18
|
+
const dataValue = toValue(data);
|
|
19
|
+
if (!dataValue)
|
|
20
|
+
return null;
|
|
21
|
+
if (typeof getter === "function")
|
|
22
|
+
return getter(dataValue, ctx);
|
|
23
|
+
});
|
|
24
|
+
return computedGetters;
|
|
25
|
+
},
|
|
26
|
+
{}
|
|
27
|
+
);
|
|
28
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { App, InjectionKey } from 'vue-demi';
|
|
2
|
+
import { QueryClient } from '@tanstack/vue-query';
|
|
3
|
+
import { type LocaleOptions } from './composables/locale.js';
|
|
4
|
+
export interface PiveauKitPluginOptions {
|
|
5
|
+
queryClient?: QueryClient;
|
|
6
|
+
locale?: LocaleOptions;
|
|
7
|
+
}
|
|
8
|
+
export declare const keyQueryClient: InjectionKey<QueryClient>;
|
|
9
|
+
export declare const plugin: {
|
|
10
|
+
install(app: App, options?: PiveauKitPluginOptions): void;
|
|
11
|
+
};
|
|
12
|
+
export declare function useQueryClient(): QueryClient | null;
|
|
13
|
+
export declare const piveauKitPlugin: {
|
|
14
|
+
install(app: App, options?: PiveauKitPluginOptions): void;
|
|
15
|
+
};
|
|
16
|
+
export * from './useResourceFactory.js';
|
|
17
|
+
export * from './useSearchFactory.js';
|
|
18
|
+
export * from './defineHubSearch.js';
|
|
19
|
+
export * from './integrations/index.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { inject, isVue2 } from "vue-demi";
|
|
2
|
+
import { QueryClient } from "@tanstack/vue-query";
|
|
3
|
+
import { localeKey } from "./composables/locale.js";
|
|
4
|
+
import { defaultLocaleAdapter } from "./locale/adapters/default.js";
|
|
5
|
+
export const keyQueryClient = Symbol("");
|
|
6
|
+
export const plugin = {
|
|
7
|
+
install(app, options = {}) {
|
|
8
|
+
const {
|
|
9
|
+
queryClient = new QueryClient(),
|
|
10
|
+
locale
|
|
11
|
+
} = options || {};
|
|
12
|
+
if (isVue2) {
|
|
13
|
+
throw new Error("Vue 2 is not supported");
|
|
14
|
+
} else {
|
|
15
|
+
const resolvedLocale = locale?.adapter ?? defaultLocaleAdapter(locale);
|
|
16
|
+
app.provide(keyQueryClient, queryClient);
|
|
17
|
+
app.provide(localeKey, resolvedLocale);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
export function useQueryClient() {
|
|
22
|
+
const maybeQueryClient = inject(keyQueryClient, null);
|
|
23
|
+
return maybeQueryClient;
|
|
24
|
+
}
|
|
25
|
+
export const piveauKitPlugin = plugin;
|
|
26
|
+
export * from "./useResourceFactory.js";
|
|
27
|
+
export * from "./useSearchFactory.js";
|
|
28
|
+
export * from "./defineHubSearch.js";
|
|
29
|
+
export * from "./integrations/index.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { nextTick, ref } from "vue-demi";
|
|
3
|
+
import { dcatApDataset } from "../dcatAp/dcatAp.js";
|
|
4
|
+
import { withSetup } from "../../../test/utils.js";
|
|
5
|
+
import dataset from "./https-data-paysdelaloire-fr-explore-dataset-agenda-culture-de-la-region-des-pays-de-la-loire-.json";
|
|
6
|
+
describe("integration dcat-ap", async () => {
|
|
7
|
+
it("should run the setup function", async () => {
|
|
8
|
+
const { setup } = dcatApDataset();
|
|
9
|
+
const { result } = await withSetup(() => setup(dataset));
|
|
10
|
+
expect(result.getId.value).toBe(dataset.id);
|
|
11
|
+
});
|
|
12
|
+
it.each([
|
|
13
|
+
{ getter: "getId", expected: dataset.id },
|
|
14
|
+
{ getter: "getTitle", expected: dataset.title.en },
|
|
15
|
+
{ getter: "getDescription", expected: dataset.description.en },
|
|
16
|
+
{ getter: "getPublisher", expected: dataset.publisher }
|
|
17
|
+
])("($getter) should return $expected", async ({ getter, expected }) => {
|
|
18
|
+
const { setup } = dcatApDataset();
|
|
19
|
+
const { result } = await withSetup(() => setup(dataset));
|
|
20
|
+
if (typeof expected === "string")
|
|
21
|
+
expect(result[getter].value).toBe(expected);
|
|
22
|
+
if (typeof expected === "object")
|
|
23
|
+
expect(result[getter].value).toEqual(expected);
|
|
24
|
+
});
|
|
25
|
+
it("should update reactively when dataset changes", async () => {
|
|
26
|
+
const dataset2 = JSON.parse(JSON.stringify(dataset));
|
|
27
|
+
dataset2.id = "new-id";
|
|
28
|
+
const datasetRef = ref(dataset);
|
|
29
|
+
const { setup } = dcatApDataset();
|
|
30
|
+
const { result } = await withSetup(() => setup(datasetRef));
|
|
31
|
+
expect(result.getId.value).toBe(dataset.id);
|
|
32
|
+
datasetRef.value = dataset2;
|
|
33
|
+
await nextTick();
|
|
34
|
+
expect(result.getId.value).toBe(dataset2.id);
|
|
35
|
+
});
|
|
36
|
+
});
|