@xata.io/client 0.5.1 → 0.7.1
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/.eslintrc.cjs +13 -0
- package/CHANGELOG.md +35 -0
- package/dist/api/client.d.ts +1 -1
- package/dist/api/client.js +11 -9
- package/dist/api/components.d.ts +7 -6
- package/dist/api/components.js +7 -6
- package/dist/api/fetcher.d.ts +15 -0
- package/dist/api/fetcher.js +23 -22
- package/dist/api/providers.js +3 -2
- package/dist/api/responses.d.ts +6 -0
- package/dist/schema/config.d.ts +4 -0
- package/dist/schema/config.js +83 -0
- package/dist/schema/filters.d.ts +93 -17
- package/dist/schema/filters.js +0 -22
- package/dist/schema/filters.spec.d.ts +1 -0
- package/dist/schema/filters.spec.js +175 -0
- package/dist/schema/index.d.ts +1 -0
- package/dist/schema/index.js +4 -1
- package/dist/schema/operators.d.ts +26 -24
- package/dist/schema/operators.js +13 -11
- package/dist/schema/pagination.d.ts +13 -13
- package/dist/schema/pagination.js +0 -1
- package/dist/schema/query.d.ts +39 -50
- package/dist/schema/query.js +25 -37
- package/dist/schema/record.d.ts +25 -3
- package/dist/schema/record.js +11 -0
- package/dist/schema/repository.d.ts +79 -35
- package/dist/schema/repository.js +212 -114
- package/dist/schema/selection.d.ts +24 -11
- package/dist/schema/selection.spec.d.ts +1 -0
- package/dist/schema/selection.spec.js +203 -0
- package/dist/schema/sorting.d.ts +17 -0
- package/dist/schema/sorting.js +28 -0
- package/dist/schema/sorting.spec.d.ts +1 -0
- package/dist/schema/sorting.spec.js +9 -0
- package/dist/util/environment.d.ts +5 -0
- package/dist/util/environment.js +68 -0
- package/dist/util/fetch.d.ts +2 -0
- package/dist/util/fetch.js +13 -0
- package/dist/util/lang.d.ts +3 -0
- package/dist/util/lang.js +13 -1
- package/dist/util/types.d.ts +22 -1
- package/package.json +5 -2
package/.eslintrc.cjs
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module.exports = {
|
2
|
+
ignorePatterns: ["dist"],
|
3
|
+
parserOptions: {
|
4
|
+
ecmaVersion: 2020,
|
5
|
+
sourceType: 'module',
|
6
|
+
project: 'client/tsconfig.json'
|
7
|
+
},
|
8
|
+
rules: {
|
9
|
+
'@typescript-eslint/no-explicit-any': 'off',
|
10
|
+
'@typescript-eslint/ban-types': 'off',
|
11
|
+
'@typescript-eslint/no-floating-promises': 'error',
|
12
|
+
}
|
13
|
+
};
|
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,40 @@
|
|
1
1
|
# @xata.io/client
|
2
2
|
|
3
|
+
## 0.7.1
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- 01aef78: Fix bundle for browsers
|
8
|
+
- 56be1fd: Allow sending updates with link object
|
9
|
+
- fc51771: Add includes operator helper methods
|
10
|
+
|
11
|
+
## 0.7.0
|
12
|
+
|
13
|
+
### Minor Changes
|
14
|
+
|
15
|
+
- 6ce5512: Add bulk operations for all methods
|
16
|
+
- 2a1fa4f: Introduced automatic branch resolution mechanism
|
17
|
+
|
18
|
+
### Patch Changes
|
19
|
+
|
20
|
+
- d033f3a: Improve column selection output type with non-nullable columns
|
21
|
+
- b1e92db: Include stack trace with errors
|
22
|
+
- deed570: Improve sorting with multiple properties
|
23
|
+
- 80b5417: Improve filtering types
|
24
|
+
|
25
|
+
## 0.6.0
|
26
|
+
|
27
|
+
### Minor Changes
|
28
|
+
|
29
|
+
- 084f5df: Add type inference for columns
|
30
|
+
- bb73c89: Unify create and insert in a single method overload
|
31
|
+
|
32
|
+
### Patch Changes
|
33
|
+
|
34
|
+
- 716c487: Forward nullable types on links
|
35
|
+
- bb66bb2: Fix error handling with createMany
|
36
|
+
- 084f5df: Fix circular dependencies on selectable column
|
37
|
+
|
3
38
|
## 0.5.1
|
4
39
|
|
5
40
|
### Patch Changes
|
package/dist/api/client.d.ts
CHANGED
package/dist/api/client.js
CHANGED
@@ -13,24 +13,24 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
13
13
|
var _XataApiClient_extraProps;
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
15
15
|
exports.XataApiClient = void 0;
|
16
|
+
const config_1 = require("../schema/config");
|
17
|
+
const fetch_1 = require("../util/fetch");
|
16
18
|
const components_1 = require("./components");
|
17
19
|
const providers_1 = require("./providers");
|
18
20
|
class XataApiClient {
|
19
21
|
constructor(options) {
|
20
22
|
var _a, _b;
|
21
23
|
_XataApiClient_extraProps.set(this, void 0);
|
22
|
-
const
|
23
|
-
const
|
24
|
-
if (!
|
25
|
-
|
26
|
-
throw new Error(`The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`);
|
24
|
+
const provider = (_a = options.host) !== null && _a !== void 0 ? _a : 'production';
|
25
|
+
const apiKey = (_b = options === null || options === void 0 ? void 0 : options.apiKey) !== null && _b !== void 0 ? _b : (0, config_1.getAPIKey)();
|
26
|
+
if (!apiKey) {
|
27
|
+
throw new Error('Could not resolve a valid apiKey');
|
27
28
|
}
|
28
|
-
const provider = (_b = options.host) !== null && _b !== void 0 ? _b : 'production';
|
29
29
|
__classPrivateFieldSet(this, _XataApiClient_extraProps, {
|
30
30
|
apiUrl: (0, providers_1.getHostUrl)(provider, 'main'),
|
31
31
|
workspacesApiUrl: (0, providers_1.getHostUrl)(provider, 'workspaces'),
|
32
|
-
fetchImpl,
|
33
|
-
apiKey
|
32
|
+
fetchImpl: (0, fetch_1.getFetchImplementation)(options.fetch),
|
33
|
+
apiKey
|
34
34
|
}, "f");
|
35
35
|
}
|
36
36
|
get user() {
|
@@ -221,7 +221,9 @@ class RecordsApi {
|
|
221
221
|
deleteRecord(workspace, database, branch, tableName, recordId) {
|
222
222
|
return components_1.operationsByTag.records.deleteRecord(Object.assign({ pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId } }, this.extraProps));
|
223
223
|
}
|
224
|
-
getRecord(workspace, database, branch, tableName, recordId,
|
224
|
+
getRecord(workspace, database, branch, tableName, recordId,
|
225
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
226
|
+
options = {}) {
|
225
227
|
return components_1.operationsByTag.records.getRecord(Object.assign({ pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId } }, this.extraProps));
|
226
228
|
}
|
227
229
|
bulkInsertTableRecords(workspace, database, branch, tableName, records) {
|
package/dist/api/components.d.ts
CHANGED
@@ -990,8 +990,9 @@ export declare type QueryTableVariables = {
|
|
990
990
|
* },
|
991
991
|
* {
|
992
992
|
* "name": "r2",
|
993
|
-
* }
|
994
|
-
* ]
|
993
|
+
* }
|
994
|
+
* ]
|
995
|
+
* }
|
995
996
|
* }
|
996
997
|
* ```
|
997
998
|
*
|
@@ -1001,7 +1002,7 @@ export declare type QueryTableVariables = {
|
|
1001
1002
|
* {
|
1002
1003
|
* "filter": {
|
1003
1004
|
* "$exists": "settings",
|
1004
|
-
* }
|
1005
|
+
* }
|
1005
1006
|
* }
|
1006
1007
|
* ```
|
1007
1008
|
*
|
@@ -1016,8 +1017,8 @@ export declare type QueryTableVariables = {
|
|
1016
1017
|
* },
|
1017
1018
|
* {
|
1018
1019
|
* "$exists": "name",
|
1019
|
-
* }
|
1020
|
-
* ]
|
1020
|
+
* }
|
1021
|
+
* ]
|
1021
1022
|
* }
|
1022
1023
|
* }
|
1023
1024
|
* ```
|
@@ -1028,7 +1029,7 @@ export declare type QueryTableVariables = {
|
|
1028
1029
|
* {
|
1029
1030
|
* "filter": {
|
1030
1031
|
* "$notExists": "settings",
|
1031
|
-
* }
|
1032
|
+
* }
|
1032
1033
|
* }
|
1033
1034
|
* ```
|
1034
1035
|
*
|
package/dist/api/components.js
CHANGED
@@ -573,8 +573,9 @@ exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
|
573
573
|
* },
|
574
574
|
* {
|
575
575
|
* "name": "r2",
|
576
|
-
* }
|
577
|
-
* ]
|
576
|
+
* }
|
577
|
+
* ]
|
578
|
+
* }
|
578
579
|
* }
|
579
580
|
* ```
|
580
581
|
*
|
@@ -584,7 +585,7 @@ exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
|
584
585
|
* {
|
585
586
|
* "filter": {
|
586
587
|
* "$exists": "settings",
|
587
|
-
* }
|
588
|
+
* }
|
588
589
|
* }
|
589
590
|
* ```
|
590
591
|
*
|
@@ -599,8 +600,8 @@ exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
|
599
600
|
* },
|
600
601
|
* {
|
601
602
|
* "$exists": "name",
|
602
|
-
* }
|
603
|
-
* ]
|
603
|
+
* }
|
604
|
+
* ]
|
604
605
|
* }
|
605
606
|
* }
|
606
607
|
* ```
|
@@ -611,7 +612,7 @@ exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
|
611
612
|
* {
|
612
613
|
* "filter": {
|
613
614
|
* "$notExists": "settings",
|
614
|
-
* }
|
615
|
+
* }
|
615
616
|
* }
|
616
617
|
* ```
|
617
618
|
*
|
package/dist/api/fetcher.d.ts
CHANGED
@@ -23,3 +23,18 @@ export declare type FetcherOptions<TBody, THeaders, TQueryParams, TPathParams> =
|
|
23
23
|
pathParams?: TPathParams;
|
24
24
|
};
|
25
25
|
export declare function fetch<TData, TBody extends Record<string, unknown> | undefined, THeaders extends Record<string, unknown>, TQueryParams extends Record<string, unknown>, TPathParams extends Record<string, string>>({ url: path, method, body, headers, pathParams, queryParams, fetchImpl, apiKey, apiUrl, workspacesApiUrl }: FetcherOptions<TBody, THeaders, TQueryParams, TPathParams> & FetcherExtraProps): Promise<TData>;
|
26
|
+
export declare class FetcherError extends Error {
|
27
|
+
status: number;
|
28
|
+
errors: Array<{
|
29
|
+
status: number;
|
30
|
+
message?: string;
|
31
|
+
}> | undefined;
|
32
|
+
constructor(data: {
|
33
|
+
message: string;
|
34
|
+
status: number;
|
35
|
+
errors?: Array<{
|
36
|
+
status: number;
|
37
|
+
message?: string;
|
38
|
+
}>;
|
39
|
+
}, parent?: Error);
|
40
|
+
}
|
package/dist/api/fetcher.js
CHANGED
@@ -9,13 +9,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
9
9
|
});
|
10
10
|
};
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
-
exports.fetch = void 0;
|
12
|
+
exports.FetcherError = exports.fetch = void 0;
|
13
|
+
const lang_1 = require("../util/lang");
|
13
14
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
14
15
|
const query = new URLSearchParams(queryParams).toString();
|
15
16
|
const queryString = query.length > 0 ? `?${query}` : '';
|
16
17
|
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
17
18
|
};
|
18
|
-
const fallbackError = { message: 'Network response was not ok' };
|
19
19
|
function buildBaseUrl({ path, workspacesApiUrl, apiUrl, pathParams }) {
|
20
20
|
if (!(pathParams === null || pathParams === void 0 ? void 0 : pathParams.workspace))
|
21
21
|
return `${apiUrl}${path}`;
|
@@ -51,28 +51,29 @@ function fetch({ url: path, method, body, headers, pathParams, queryParams, fetc
|
|
51
51
|
if (response.ok) {
|
52
52
|
return jsonResponse;
|
53
53
|
}
|
54
|
-
|
55
|
-
|
56
|
-
}
|
57
|
-
else {
|
58
|
-
throw withStatus(fallbackError, response.status);
|
59
|
-
}
|
54
|
+
const { message = 'Unknown error', errors } = jsonResponse;
|
55
|
+
throw new FetcherError({ message, status: response.status, errors });
|
60
56
|
}
|
61
|
-
catch (
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
};
|
66
|
-
throw withStatus(error, response.status);
|
67
|
-
}
|
68
|
-
else if (typeof e === 'object' && typeof e.message === 'string') {
|
69
|
-
throw withStatus(e, response.status);
|
70
|
-
}
|
71
|
-
else {
|
72
|
-
throw withStatus(fallbackError, response.status);
|
73
|
-
}
|
57
|
+
catch (error) {
|
58
|
+
const message = hasMessage(error) ? error.message : 'Unknown network error';
|
59
|
+
const parent = error instanceof Error ? error : undefined;
|
60
|
+
throw new FetcherError({ message, status: response.status }, parent);
|
74
61
|
}
|
75
62
|
});
|
76
63
|
}
|
77
64
|
exports.fetch = fetch;
|
78
|
-
const
|
65
|
+
const hasMessage = (error) => {
|
66
|
+
return (0, lang_1.isObject)(error) && (0, lang_1.isString)(error.message);
|
67
|
+
};
|
68
|
+
class FetcherError extends Error {
|
69
|
+
constructor(data, parent) {
|
70
|
+
super(data.message);
|
71
|
+
this.status = data.status;
|
72
|
+
this.errors = data.errors;
|
73
|
+
if (parent) {
|
74
|
+
this.stack = parent.stack;
|
75
|
+
this.cause = parent.cause;
|
76
|
+
}
|
77
|
+
}
|
78
|
+
}
|
79
|
+
exports.FetcherError = FetcherError;
|
package/dist/api/providers.js
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.getHostUrl = void 0;
|
4
|
+
const lang_1 = require("../util/lang");
|
4
5
|
function getHostUrl(provider, type) {
|
5
6
|
if (isValidAlias(provider)) {
|
6
7
|
return providers[provider][type];
|
@@ -22,8 +23,8 @@ const providers = {
|
|
22
23
|
}
|
23
24
|
};
|
24
25
|
function isValidAlias(alias) {
|
25
|
-
return
|
26
|
+
return (0, lang_1.isString)(alias) && Object.keys(providers).includes(alias);
|
26
27
|
}
|
27
28
|
function isValidBuilder(builder) {
|
28
|
-
return
|
29
|
+
return (0, lang_1.isObject)(builder) && (0, lang_1.isString)(builder.main) && (0, lang_1.isString)(builder.workspaces);
|
29
30
|
}
|
package/dist/api/responses.d.ts
CHANGED
@@ -19,6 +19,12 @@ export declare type AuthError = {
|
|
19
19
|
id?: string;
|
20
20
|
message: string;
|
21
21
|
};
|
22
|
+
export declare type BulkError = {
|
23
|
+
errors: {
|
24
|
+
message?: string;
|
25
|
+
status?: number;
|
26
|
+
}[];
|
27
|
+
};
|
22
28
|
export declare type BranchMigrationPlan = {
|
23
29
|
version: number;
|
24
30
|
migration: Schemas.BranchMigration;
|
@@ -0,0 +1,4 @@
|
|
1
|
+
import { FetcherExtraProps } from '../api/fetcher';
|
2
|
+
export declare function getBranch(fetchProps: Omit<FetcherExtraProps, 'workspacesApiUrl'>): Promise<string | undefined>;
|
3
|
+
export declare function getDatabaseUrl(): string | undefined;
|
4
|
+
export declare function getAPIKey(): string | undefined;
|
@@ -0,0 +1,83 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
+
exports.getAPIKey = exports.getDatabaseUrl = exports.getBranch = void 0;
|
13
|
+
const api_1 = require("../api");
|
14
|
+
const environment_1 = require("../util/environment");
|
15
|
+
const lang_1 = require("../util/lang");
|
16
|
+
const envBranchNames = [
|
17
|
+
'XATA_BRANCH',
|
18
|
+
'VERCEL_GIT_COMMIT_REF',
|
19
|
+
'CF_PAGES_BRANCH',
|
20
|
+
'BRANCH' // Netlify. Putting it the last one because it is more ambiguous
|
21
|
+
];
|
22
|
+
function getBranch(fetchProps) {
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
24
|
+
const env = getBranchByEnvVariable();
|
25
|
+
if (env)
|
26
|
+
return env;
|
27
|
+
const branch = yield (0, environment_1.getGitBranch)();
|
28
|
+
if (!branch)
|
29
|
+
return undefined;
|
30
|
+
// TODO: in the future, call /resolve endpoint
|
31
|
+
// For now, call API to see if the branch exists. If not, use a default value.
|
32
|
+
const [protocol, , host, , database] = fetchProps.apiUrl.split('/');
|
33
|
+
const [workspace] = host.split('.');
|
34
|
+
const dbBranchName = `${database}:${branch}`;
|
35
|
+
try {
|
36
|
+
yield (0, api_1.getBranchDetails)(Object.assign(Object.assign({}, fetchProps), { workspacesApiUrl: `${protocol}//${host}`, pathParams: {
|
37
|
+
dbBranchName,
|
38
|
+
workspace
|
39
|
+
} }));
|
40
|
+
}
|
41
|
+
catch (err) {
|
42
|
+
if ((0, lang_1.isObject)(err) && err.status === 404)
|
43
|
+
return 'main';
|
44
|
+
throw err;
|
45
|
+
}
|
46
|
+
return branch;
|
47
|
+
});
|
48
|
+
}
|
49
|
+
exports.getBranch = getBranch;
|
50
|
+
function getBranchByEnvVariable() {
|
51
|
+
for (const name of envBranchNames) {
|
52
|
+
const value = (0, environment_1.getEnvVariable)(name);
|
53
|
+
if (value) {
|
54
|
+
return value;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
try {
|
58
|
+
return XATA_BRANCH;
|
59
|
+
}
|
60
|
+
catch (err) {
|
61
|
+
// Ignore ReferenceError. Only CloudFlare workers set env variables as global variables
|
62
|
+
}
|
63
|
+
}
|
64
|
+
function getDatabaseUrl() {
|
65
|
+
var _a;
|
66
|
+
try {
|
67
|
+
return (_a = (0, environment_1.getEnvVariable)('XATA_DATABASE_URL')) !== null && _a !== void 0 ? _a : XATA_DATABASE_URL;
|
68
|
+
}
|
69
|
+
catch (err) {
|
70
|
+
return undefined;
|
71
|
+
}
|
72
|
+
}
|
73
|
+
exports.getDatabaseUrl = getDatabaseUrl;
|
74
|
+
function getAPIKey() {
|
75
|
+
var _a;
|
76
|
+
try {
|
77
|
+
return (_a = (0, environment_1.getEnvVariable)('XATA_API_KEY')) !== null && _a !== void 0 ? _a : XATA_API_KEY;
|
78
|
+
}
|
79
|
+
catch (err) {
|
80
|
+
return undefined;
|
81
|
+
}
|
82
|
+
}
|
83
|
+
exports.getAPIKey = getAPIKey;
|
package/dist/schema/filters.d.ts
CHANGED
@@ -1,20 +1,96 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
import { SingleOrArray } from '../util/types';
|
2
|
+
import { SelectableColumn, ValueAtColumn } from './selection';
|
3
|
+
/**
|
4
|
+
* PropertyMatchFilter
|
5
|
+
* Example:
|
6
|
+
{
|
7
|
+
"filter": {
|
8
|
+
"name": "value",
|
9
|
+
"name": {
|
10
|
+
"$is": "value",
|
11
|
+
"$any": [ "value1", "value2" ],
|
12
|
+
},
|
13
|
+
"settings.plan": {"$any": ["free", "paid"]},
|
14
|
+
"settings.plan": "free",
|
15
|
+
"settings": {
|
16
|
+
"plan": "free"
|
17
|
+
},
|
18
|
+
}
|
19
|
+
}
|
20
|
+
*/
|
21
|
+
declare type PropertyAccessFilter<Record> = {
|
22
|
+
[key in SelectableColumn<Record>]?: NestedApiFilter<ValueAtColumn<Record, key>> | PropertyFilter<ValueAtColumn<Record, key>>;
|
5
23
|
};
|
6
|
-
export declare type
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
[
|
24
|
+
export declare type PropertyFilter<T> = T | {
|
25
|
+
$is: T;
|
26
|
+
} | {
|
27
|
+
$isNot: T;
|
28
|
+
} | {
|
29
|
+
$any: T[];
|
30
|
+
} | {
|
31
|
+
$none: T[];
|
32
|
+
} | ValueTypeFilters<T>;
|
33
|
+
declare type IncludesFilter<T> = PropertyFilter<T> | {
|
34
|
+
[key in '$all' | '$none' | '$any']?: IncludesFilter<T> | Array<IncludesFilter<T> | {
|
35
|
+
$not: IncludesFilter<T>;
|
36
|
+
}>;
|
14
37
|
};
|
15
|
-
export declare type
|
16
|
-
[key in
|
17
|
-
} : Constraint<T>;
|
18
|
-
export declare type FilterConstraints<T> = {
|
19
|
-
[key in keyof T]?: T[key] extends Record<string, any> ? FilterConstraints<T[key]> : T[key] | DeepConstraint<T[key]>;
|
38
|
+
export declare type StringTypeFilter = {
|
39
|
+
[key in '$contains' | '$pattern' | '$startsWith' | '$endsWith']?: string;
|
20
40
|
};
|
41
|
+
export declare type ComparableType = number | Date;
|
42
|
+
export declare type ComparableTypeFilter<T extends ComparableType> = {
|
43
|
+
[key in '$gt' | '$lt' | '$ge' | '$le']?: T;
|
44
|
+
};
|
45
|
+
export declare type ArrayFilter<T> = {
|
46
|
+
[key in '$includes']?: SingleOrArray<PropertyFilter<T> | ValueTypeFilters<T>> | IncludesFilter<T>;
|
47
|
+
} | {
|
48
|
+
[key in '$includesAll' | '$includesNone' | '$includesAny']?: T | Array<PropertyFilter<T> | {
|
49
|
+
$not: PropertyFilter<T>;
|
50
|
+
}>;
|
51
|
+
};
|
52
|
+
declare type ValueTypeFilters<T> = T | T extends string ? StringTypeFilter : T extends number ? ComparableTypeFilter<number> : T extends Date ? ComparableTypeFilter<Date> : T extends Array<infer T> ? ArrayFilter<T> : never;
|
53
|
+
/**
|
54
|
+
* AggregatorFilter
|
55
|
+
* Example:
|
56
|
+
{
|
57
|
+
"filter": {
|
58
|
+
"$any": {
|
59
|
+
"settings.dark": true,
|
60
|
+
"settings.plan": "free"
|
61
|
+
}
|
62
|
+
},
|
63
|
+
}
|
64
|
+
{
|
65
|
+
"filter": {
|
66
|
+
"$any": [
|
67
|
+
{
|
68
|
+
"name": "r1",
|
69
|
+
},
|
70
|
+
{
|
71
|
+
"name": "r2",
|
72
|
+
},
|
73
|
+
],
|
74
|
+
}
|
75
|
+
*/
|
76
|
+
declare type AggregatorFilter<Record> = {
|
77
|
+
[key in '$all' | '$any' | '$not' | '$none']?: SingleOrArray<Filter<Record>>;
|
78
|
+
};
|
79
|
+
/**
|
80
|
+
* Existance filter
|
81
|
+
* Example: { filter: { $exists: "settings" } }
|
82
|
+
*/
|
83
|
+
export declare type ExistanceFilter<Record> = {
|
84
|
+
[key in '$exists' | '$notExists']?: SelectableColumn<Record>;
|
85
|
+
};
|
86
|
+
declare type BaseApiFilter<Record> = PropertyAccessFilter<Record> | AggregatorFilter<Record> | ExistanceFilter<Record>;
|
87
|
+
/**
|
88
|
+
* Nested filter
|
89
|
+
* Injects the Api filters on nested properties
|
90
|
+
* Example: { filter: { settings: { plan: { $any: ['free', 'trial'] } } } }
|
91
|
+
*/
|
92
|
+
declare type NestedApiFilter<T> = T extends Record<string, any> ? {
|
93
|
+
[key in keyof T]?: T[key] extends Record<string, any> ? SingleOrArray<Filter<T[key]>> : PropertyFilter<T[key]>;
|
94
|
+
} : PropertyFilter<T>;
|
95
|
+
export declare type Filter<Record> = BaseApiFilter<Record> | NestedApiFilter<Record>;
|
96
|
+
export {};
|
package/dist/schema/filters.js
CHANGED
@@ -1,24 +1,2 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.buildSortFilter = exports.isSortFilterObject = void 0;
|
4
|
-
function isSortFilterObject(filter) {
|
5
|
-
return typeof filter === 'object' && filter.column !== undefined;
|
6
|
-
}
|
7
|
-
exports.isSortFilterObject = isSortFilterObject;
|
8
|
-
function buildSortFilter(filter) {
|
9
|
-
if (!filter)
|
10
|
-
return undefined;
|
11
|
-
const filters = Array.isArray(filter) ? filter : [filter];
|
12
|
-
return filters.reduce((acc, item) => {
|
13
|
-
if (typeof item === 'string') {
|
14
|
-
return Object.assign(Object.assign({}, acc), { [item]: 'asc' });
|
15
|
-
}
|
16
|
-
else if (isSortFilterObject(item)) {
|
17
|
-
return Object.assign(Object.assign({}, acc), { [item.column]: item.direction });
|
18
|
-
}
|
19
|
-
else {
|
20
|
-
return acc;
|
21
|
-
}
|
22
|
-
}, {});
|
23
|
-
}
|
24
|
-
exports.buildSortFilter = buildSortFilter;
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|