Skip to content

Commit d560523

Browse files
committed
fix (doc&type): improve doc and add some types to help user to understand schema and options
1 parent 6c28220 commit d560523

File tree

4 files changed

+87
-36
lines changed

4 files changed

+87
-36
lines changed

Diff for: README.md

+34-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Now there is already some other lib even in pure Vanilla to sync the query param
1212

1313
By separating the filters and the datable options vue-datatable-url-sync automatise all the desired behavior for working with datatable.
1414

15-
Example of usage with a vuetify datatable:
15+
Example of usage with a vuetify datatable (It can work with any datatable):
1616

1717
// coming soon
1818

@@ -81,7 +81,7 @@ return {
8181

8282
Prototype:
8383
```js
84-
function useDatatableUrlSync(route: any, router: any, form: GenericDictionnary, fetchDatas: Function, options: GenericDictionnary, formSchema?: GenericDictionnary, initializeForm?: Function, configurations?: VDUSConfiguration)
84+
function useDatatableUrlSync(route: any, router: any, form: GenericDictionnary, fetchDatas: Function, options: GenericDictionnary, formSchema?: VDUSFormSchema, initializeForm?: Function, configurations?: VDUSConfiguration)
8585
```
8686

8787
| Params | Description |
@@ -90,15 +90,42 @@ function useDatatableUrlSync(route: any, router: any, form: GenericDictionnary,
9090
| router | The route instance from vue router. As it differe from Vue2 and Vue3 we can't import it automatically and need to have the instance in parameter |
9191
| form | A simple object used to filter the form. This object is synchronized with the url. When it change it reset the page attribute of the options variable |
9292
| fetchDatas | Callback function called when options or form changed or after a reload with the correct parameter to send to the backend for backend pagination or custom actions. This function take 2 parameter: queryParams, queryAsObject that are the data transformed by VDUS to match your backend criteria in string or object format. |
93-
| options | The options used for the datatable. It follow a strict pattern. See [DatatableOptions](#TODO) type for more informations. If your server use other query identifier use formSchema to change their name before fetchDatas been called |
94-
| formSchema | The object that allow you to customize the defaut value, the type and the names send to the backend. See [FormSchema](#TODO) type for the structure and [the documentation section](#TODO) to understand how to use it |
95-
| initializeForm | A callback function called at the component creation to allow developer to adapt behavior depending of the query params in the url if needed. Usefull if to value are non-compatible because user change it manually. |
96-
| configurations | Object that allow to personnalise the behavior of VDUS in specific case. See [VDUSConfiguration](#TODO) type and [the documentation section](#TODO) to understand how to use it |
93+
| options | The options used for the datatable. It follow a strict pattern. See [VDUSDatatableOptions](#TODO) type for more informations. If your server use other query identifier use formSchema to change their name before fetchDatas been called |
94+
| formSchema | Optional. The object that allow you to customize the defaut value, the type and the names send to the backend. See [VDUSFormSchema](#TODO) type for the structure and [the documentation section](#TODO) to understand how to use it |
95+
| initializeForm | Optional. A callback function called at the component creation to allow developer to adapt behavior depending of the query params in the url if needed. Usefull if to value are non-compatible because user change it manually. |
96+
| configurations | Optional. Object that allow to personnalise the behavior of VDUS in specific case. See [VDUSConfiguration](#TODO) type and [the documentation section](#TODO) to understand how to use it |
9797

9898

99-
# useDatatableUrlSync returned value
99+
# useDatatableUrlSync returned values
100100

101101
| Key name | Description |
102102
| --------- | ----------------------------------------------------------------- |
103103
| loading | Boolean value to be able to wait in the template that the data is correctly setted from the url and the data retrieve or filtered in fetchDatas especially if your fetchDatas is asynchronous |
104104
| vuetifyOptions | The datatable options on the vuetify format to be able to use it directly in the template without the need to transform it |
105+
106+
107+
# FormSchema
108+
109+
The parameter formSchema allow you to adapt the default behavior of vue-datatable-url-sync for each parameter (for the form AND for the options).
110+
With it you can specify the type of the params, the default value and the query param name to send to the server.
111+
112+
[See the type to understand better](#TODO)
113+
114+
Here is the description of the type for the configuration of each params ((ParamSchema)[#TODO])
115+
116+
| Key name | Description |
117+
| --------- | ----------------------------------------------------------------- |
118+
| name | The name to send to the backend server |
119+
| default | The default value. It prevent default value to show in the url |
120+
| type | The type of the params to cast it correctly into your form or your options |
121+
122+
123+
# VDUS Types
124+
125+
| Type name | Description |
126+
| ----------- | ------------------------------------------------------------------- |
127+
| [GenericDictionnary](#TODO) | Generic object that accept any key and any value |
128+
| [VDUSParamSchema](#TODO) | Object describing how to handle query param when reading them from url or before sending them to the backend server |
129+
| [VDUSFormSchema](#TODO) | Object describing how to handle the form and options object passed as parameter of useDatatableUrlSync |
130+
| [VDUSDatatableOptions](#TODO) | Object describing the params accepted in the datatable options |
131+
| [VDUSConfiguration](#TODO) | Configuration object for vue datatable url sync |

Diff for: src/useDatatableUrlSync.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import {
1313
import cloneDeep from "lodash.clonedeep";
1414
import isEqual from "lodash.isequal";
1515

16-
import { ref, watch, nextTick, computed } from 'vue-demi'
17-
import {GenericDictionnary, VDUSConfiguration, VuetifyOptions} from "./utils/VDUSTypes"
16+
import { ref, watch, nextTick, computed, Ref } from 'vue-demi'
17+
import {GenericDictionnary, VDUSConfiguration, VuetifyOptions, VDUSFormSchema, VDUSDatatableOptions} from "./utils/VDUSTypes"
1818

1919
/*
2020
DOC here on params and return value
2121
*/
22-
export default function useDatatableUrlSync(route: any, router: any, form: GenericDictionnary, fetchDatas: Function, options: GenericDictionnary, formSchema?: GenericDictionnary, initializeForm?: Function, configurations?: VDUSConfiguration) {
22+
export default function useDatatableUrlSync(route: any, router: any, form: Ref<GenericDictionnary>, fetchDatas: Function, options: Ref<VDUSDatatableOptions>, formSchema?: VDUSFormSchema, initializeForm?: Function, configurations?: VDUSConfiguration) {
2323

2424
// ----------------------------- DEFAULTING PARAMS ------------------------------
2525
configurations = {
@@ -33,6 +33,12 @@ export default function useDatatableUrlSync(route: any, router: any, form: Gener
3333
extraQueryParams: {},
3434
...configurations || {}
3535
}
36+
options.value = {
37+
page: 1,
38+
page_size: configurations.serveurDefaultPageSize,
39+
ordering: [],
40+
...options.value
41+
}
3642
formSchema = {
3743
page: { type: "integer", default: 1 },
3844
page_size: { type: "integer", default: 10 },
@@ -202,7 +208,7 @@ export default function useDatatableUrlSync(route: any, router: any, form: Gener
202208
if (
203209
key.startsWith(configurations?.prefix || "") &&
204210
(typeof form.value[keyWithoutPrefix] !== "undefined" ||
205-
typeof options.value[keyWithoutPrefix] !== "undefined")
211+
typeof (options.value as GenericDictionnary)[keyWithoutPrefix] !== "undefined")
206212
) {
207213
newLocalQuery[keyWithoutPrefix] = convertParamIfTypeInSchema(
208214
route.query,
@@ -234,7 +240,7 @@ export default function useDatatableUrlSync(route: any, router: any, form: Gener
234240
/*
235241
Doc
236242
*/
237-
const updateOptionsIfNeeded = (newOptions: GenericDictionnary) => {
243+
const updateOptionsIfNeeded = (newOptions: VDUSDatatableOptions) => {
238244
newOptions = { ...options.value, ...newOptions };
239245
if (isEqual(options.value, newOptions)) {
240246
return false;
@@ -292,7 +298,8 @@ export default function useDatatableUrlSync(route: any, router: any, form: Gener
292298

293299
// If the form is updated because an other component pushed a new value that we was watching
294300
// And we was not on the first page we need to go back to the first page
295-
if (formUpdated && options.value.page > 1) {
301+
const currentPageNumber: number = options.value.page || 1;
302+
if (formUpdated && currentPageNumber > 1) {
296303
options.value.page = 1;
297304
}
298305

Diff for: src/utils/VDUSTypes.ts

+22-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
type GenericDictionnary = {
22
[key: string]: any;
33
}
4+
5+
type VDUSParamSchema = {
6+
name?: string;
7+
default?: any;
8+
type?: "string" | "boolean" | "integer" | "arrayInt" | "arrayString";
9+
}
10+
11+
type VDUSFormSchema = {
12+
[key: string]: VDUSParamSchema;
13+
}
14+
15+
type VDUSDatatableOptions = {
16+
page?: number;
17+
page_size?: number;
18+
ordering?: Array<string>;
19+
}
20+
421
type VDUSConfiguration = {
5-
prefix: string;
6-
debounceTime: number;
7-
serveurDefaultPageSize: number;
8-
extraQueryParams: GenericDictionnary;
22+
prefix?: string;
23+
debounceTime?: number;
24+
serveurDefaultPageSize?: number;
25+
extraQueryParams?: GenericDictionnary;
926
}
1027

1128
type VuetifySortArraysObject = {
@@ -24,4 +41,4 @@ type VuetifyOptions = {
2441
mustSort: boolean;
2542
}
2643

27-
export {GenericDictionnary, VDUSConfiguration, VuetifySortArraysObject, VuetifyOptions}
44+
export {GenericDictionnary, VDUSConfiguration, VuetifySortArraysObject, VuetifyOptions, VDUSParamSchema, VDUSFormSchema, VDUSDatatableOptions}

Diff for: src/utils/listPaginatedTools.ts

+18-18
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import {
55
extractIntegerValue
66
} from "./helpers";
77
import isEqual from "lodash.isequal";
8-
import {GenericDictionnary} from "./VDUSTypes"
8+
import {GenericDictionnary, VDUSDatatableOptions, VDUSFormSchema} from "./VDUSTypes"
99

10-
function getDefaultValueForParam(param: string, schema?: GenericDictionnary): any {
10+
const getDefaultValueForParam = (param: string, schema?: VDUSFormSchema): any => {
1111
if (schema && schema[param]) {
1212
// if there is a defautl value we change the condition to is non equality
1313
if (schema[param].default) {
@@ -18,15 +18,15 @@ function getDefaultValueForParam(param: string, schema?: GenericDictionnary): an
1818
else if (schema[param].type === "boolean") {
1919
// Default for boolean is false
2020
return false;
21-
} else if (["arrayInt", "arrayString"].includes(schema[param].type)) {
21+
} else if (["arrayInt", "arrayString"].includes(schema[param].type as string)) {
2222
// Default for array is empty array or first element null or empty
2323
return [];
2424
}
2525
}
2626
return null;
2727
}
2828

29-
function isValueDefault(value: any, param: string, schema?: GenericDictionnary): boolean {
29+
const isValueDefault = (value: any, param: string, schema?: VDUSFormSchema): boolean => {
3030
// Default is string
3131
let isValueDefault: boolean = value === "";
3232

@@ -44,7 +44,7 @@ function isValueDefault(value: any, param: string, schema?: GenericDictionnary):
4444
else if (schema[param].type === "boolean") {
4545
// Default for boolean is false
4646
isValueDefault = value === false;
47-
} else if (["arrayInt", "arrayString"].includes(schema[param].type)) {
47+
} else if (["arrayInt", "arrayString"].includes(schema[param].type as string)) {
4848
// Default for array is empty array or first element null or empty
4949
isValueDefault =
5050
value.length === 0 || value[0] === null || value[0] === "";
@@ -61,7 +61,7 @@ function isValueDefault(value: any, param: string, schema?: GenericDictionnary):
6161
if localName is true it will no replace the param key with the real used for backend query
6262
if localName is false the name will be replaced by the correct one sended to backend
6363
*/
64-
function generateQueryFromObject(object: GenericDictionnary, schema?: GenericDictionnary, localName = true): GenericDictionnary {
64+
const generateQueryFromObject = (object: GenericDictionnary, schema?: VDUSFormSchema, localName = true): GenericDictionnary => {
6565
const queryUrl: GenericDictionnary = {};
6666
for (const [key, value] of Object.entries(object)) {
6767
// We do not want to send a default value
@@ -73,15 +73,15 @@ function generateQueryFromObject(object: GenericDictionnary, schema?: GenericDic
7373
let queryKey = key;
7474
// But this can be overrided if name attribute is defined in the param schema
7575
if (!localName && schema && schema[key] && schema[key].name) {
76-
queryKey = schema[key].name;
76+
queryKey = (schema[key].name as string); // typescript error because .name can be undefined but if check it before
7777
}
7878

7979
queryUrl[queryKey] = value;
8080
}
8181
return queryUrl;
8282
}
8383

84-
function convertParamIfTypeInSchema(query: GenericDictionnary, param: string, schema?: GenericDictionnary, prefix = ""): any {
84+
const convertParamIfTypeInSchema = (query: GenericDictionnary, param: string, schema?: VDUSFormSchema, prefix = ""): any => {
8585
if (!schema || !schema[param] || !schema[param].type) {
8686
return query[prefix + param];
8787
}
@@ -104,22 +104,22 @@ function convertParamIfTypeInSchema(query: GenericDictionnary, param: string, sc
104104
/*
105105
Transform query parameter from vue router to two javascript objects representing the filtering form and the options
106106
*/
107-
function readFormAndOptionsFromLocalQuery(
107+
const readFormAndOptionsFromLocalQuery = (
108108
query: GenericDictionnary,
109109
form: GenericDictionnary,
110-
options: GenericDictionnary,
111-
schema?: GenericDictionnary,
110+
options: VDUSDatatableOptions,
111+
schema?: VDUSFormSchema,
112112
removedParam: Array<string> = []
113-
): {newOptions: GenericDictionnary; newForm: GenericDictionnary} {
113+
): {newOptions: VDUSDatatableOptions; newForm: GenericDictionnary} => {
114114

115-
const newOptions: GenericDictionnary = {};
115+
const newOptions: VDUSDatatableOptions = {};
116116
const newForm: GenericDictionnary = {};
117117

118118
for (const param in query) {
119119
if (typeof form[param] !== "undefined") {
120120
newForm[param] = convertParamIfTypeInSchema(query, param, schema);
121-
} else if (typeof options[param] !== "undefined") {
122-
newOptions[param] = convertParamIfTypeInSchema(query, param, schema);
121+
} else if (typeof (options as GenericDictionnary)[param] !== "undefined") {
122+
(newOptions as GenericDictionnary)[param] = convertParamIfTypeInSchema(query, param, schema);
123123
}
124124
}
125125
// This allow to reset to default deleted param by other component
@@ -129,14 +129,14 @@ function readFormAndOptionsFromLocalQuery(
129129
}
130130
});
131131
removedParam.forEach(param => {
132-
if (typeof options[param] !== "undefined") {
133-
newOptions[param] = getDefaultValueForParam(param, schema);
132+
if (typeof (options as GenericDictionnary)[param] !== "undefined") {
133+
(newOptions as GenericDictionnary)[param] = getDefaultValueForParam(param, schema);
134134
}
135135
});
136136
return { newOptions, newForm };
137137
}
138138

139-
function getRemovedKeyBetweenTwoObject(originalObject: GenericDictionnary, newObject: GenericDictionnary): Array<string> {
139+
const getRemovedKeyBetweenTwoObject = (originalObject: GenericDictionnary, newObject: GenericDictionnary): Array<string> => {
140140
const originalObjectKeys: Array<string> = Object.keys(originalObject);
141141
const newObjectKeys: Array<string> = Object.keys(newObject);
142142
return originalObjectKeys.filter(

0 commit comments

Comments
 (0)