@eienjs/adonisjs-api-query 1.1.1 → 3.0.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/{LICENSE.md → LICENSE} +1 -1
- package/README.md +166 -2
- package/build/configure.d.ts +2 -2
- package/build/configure.js +10 -0
- package/build/index.d.ts +38 -31
- package/build/index.js +38 -154
- package/build/providers/api_query_provider.d.ts +48 -37
- package/build/providers/api_query_provider.js +26 -27
- package/build/src/allowed_filter.d.ts +44 -36
- package/build/src/allowed_filter.js +135 -0
- package/build/src/allowed_include.d.ts +15 -16
- package/build/src/allowed_include.js +47 -0
- package/build/src/allowed_sort.d.ts +17 -17
- package/build/src/allowed_sort.js +49 -0
- package/build/src/api_query_builder_request.d.ts +55 -30
- package/build/src/api_query_builder_request.js +200 -0
- package/build/src/base_filterable.d.ts +45 -0
- package/build/src/base_filterable.js +172 -0
- package/build/src/bindings/api_query_request.js +13 -0
- package/build/src/bindings/filterable_query.js +51 -0
- package/build/src/bindings/filters_query.js +38 -0
- package/build/src/bindings/includes_query.js +50 -0
- package/build/src/bindings/sorts_query.js +42 -0
- package/build/src/bindings/validation_query.js +27 -0
- package/build/src/decorators/filterable.d.ts +20 -0
- package/build/src/decorators/filterable.js +24 -0
- package/build/src/define_config.d.ts +3 -3
- package/build/src/define_config.js +25 -0
- package/build/src/enums/filter_operator.d.ts +8 -9
- package/build/src/enums/filter_operator.js +11 -0
- package/build/src/enums/sort_direction.d.ts +3 -4
- package/build/src/enums/sort_direction.js +6 -0
- package/build/src/exceptions/allowed_fields_must_be_called_before_allowed_includes.d.ts +5 -5
- package/build/src/exceptions/allowed_fields_must_be_called_before_allowed_includes.js +10 -0
- package/build/src/exceptions/filterable_not_configured.d.ts +5 -0
- package/build/src/exceptions/filterable_not_configured.js +12 -0
- package/build/src/exceptions/invalid_append_query.d.ts +6 -4
- package/build/src/exceptions/invalid_append_query.js +17 -0
- package/build/src/exceptions/invalid_direction.d.ts +3 -3
- package/build/src/exceptions/invalid_direction.js +9 -0
- package/build/src/exceptions/invalid_field_query.d.ts +6 -4
- package/build/src/exceptions/invalid_field_query.js +17 -0
- package/build/src/exceptions/invalid_filter_query.d.ts +6 -6
- package/build/src/exceptions/invalid_filter_query.js +17 -0
- package/build/src/exceptions/invalid_filter_value.d.ts +3 -3
- package/build/src/exceptions/invalid_filter_value.js +8 -0
- package/build/src/exceptions/invalid_include_query.d.ts +6 -6
- package/build/src/exceptions/invalid_include_query.js +21 -0
- package/build/src/exceptions/invalid_query.d.ts +3 -3
- package/build/{chunk-XIRHVLVQ.js → src/exceptions/invalid_query.js} +3 -7
- package/build/src/exceptions/invalid_sort_query.d.ts +6 -6
- package/build/src/exceptions/invalid_sort_query.js +17 -0
- package/build/src/exceptions/unknown_included_fields_query.d.ts +4 -4
- package/build/src/exceptions/unknown_included_fields_query.js +12 -0
- package/build/src/exceptions/vine_not_installed.d.ts +5 -0
- package/build/src/exceptions/vine_not_installed.js +12 -0
- package/build/src/filters/filters_begin_with_strict.d.ts +6 -6
- package/build/src/filters/filters_begin_with_strict.js +8 -0
- package/build/src/filters/filters_callback.d.ts +6 -6
- package/build/src/filters/filters_callback.js +11 -0
- package/build/src/filters/filters_ends_with_strict.d.ts +6 -6
- package/build/src/filters/filters_ends_with_strict.js +8 -0
- package/build/src/filters/filters_exact.d.ts +11 -11
- package/build/src/filters/filters_exact.js +50 -0
- package/build/src/filters/filters_operator.d.ts +13 -13
- package/build/src/filters/filters_operator.js +52 -0
- package/build/src/filters/filters_partial.d.ts +9 -11
- package/build/src/filters/filters_partial.js +37 -0
- package/build/src/filters/filters_scope.d.ts +12 -0
- package/build/src/filters/filters_scope.js +98 -0
- package/build/src/filters/filters_trashed.d.ts +5 -5
- package/build/src/filters/filters_trashed.js +13 -0
- package/build/src/includes/included_callback.d.ts +7 -7
- package/build/src/includes/included_callback.js +11 -0
- package/build/src/includes/included_count.d.ts +4 -4
- package/build/src/includes/included_count.js +11 -0
- package/build/src/includes/included_relationship.d.ts +8 -8
- package/build/src/includes/included_relationship.js +37 -0
- package/build/src/sorts/sorts_callback.d.ts +6 -6
- package/build/src/sorts/sorts_callback.js +11 -0
- package/build/src/sorts/sorts_field.d.ts +4 -4
- package/build/src/sorts/sorts_field.js +7 -0
- package/build/src/utils/helpers.js +14 -0
- package/build/src/validation/schema_builder.d.ts +3 -0
- package/build/src/validation/schema_builder.js +38 -0
- package/build/src/validation/vine_resolver.js +13 -0
- package/build/stubs/main.d.ts +1 -1
- package/build/stubs/main.js +3 -0
- package/build/types.d.ts +116 -0
- package/build/types.js +1 -0
- package/package.json +101 -88
- package/build/api_query_request-QXOB4BN6.js +0 -18
- package/build/api_query_request-QXOB4BN6.js.map +0 -1
- package/build/chunk-3ITWUZL5.js +0 -108
- package/build/chunk-3ITWUZL5.js.map +0 -1
- package/build/chunk-54HNQTDY.js +0 -348
- package/build/chunk-54HNQTDY.js.map +0 -1
- package/build/chunk-IV3B5WO7.js +0 -160
- package/build/chunk-IV3B5WO7.js.map +0 -1
- package/build/chunk-MKQ3HMMA.js +0 -198
- package/build/chunk-MKQ3HMMA.js.map +0 -1
- package/build/chunk-XIRHVLVQ.js.map +0 -1
- package/build/filters_query-LCFH2F2X.js +0 -55
- package/build/filters_query-LCFH2F2X.js.map +0 -1
- package/build/includes_query-PVONWQMC.js +0 -70
- package/build/includes_query-PVONWQMC.js.map +0 -1
- package/build/index.js.map +0 -1
- package/build/metafile-esm.json +0 -1
- package/build/providers/api_query_provider.js.map +0 -1
- package/build/sorts_query-NJTC5UXX.js +0 -63
- package/build/sorts_query-NJTC5UXX.js.map +0 -1
- package/build/src/bindings/api_query_request.d.ts +0 -2
- package/build/src/bindings/filters_query.d.ts +0 -3
- package/build/src/bindings/includes_query.d.ts +0 -3
- package/build/src/bindings/sorts_query.d.ts +0 -3
- package/build/src/types.d.ts +0 -90
- package/build/src/types.js +0 -1
- package/build/src/types.js.map +0 -1
- package/build/src/utils/helpers.d.ts +0 -3
- /package/build/{config.stub → stubs/config.stub} +0 -0
|
@@ -1,37 +1,45 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { FilterOperator } from "./enums/filter_operator.js";
|
|
2
|
+
import { EnumType, ExtractKeys, Filter, ScopeUnionKeyParams } from "../types.js";
|
|
3
|
+
import { LucidModel, ModelAttributes, ModelQueryBuilderContract } from "@adonisjs/lucid/types/model";
|
|
4
|
+
import { StrictValuesWithoutRaw } from "@adonisjs/lucid/types/querybuilder";
|
|
5
|
+
import { SchemaTypes } from "@vinejs/vine/types";
|
|
6
6
|
export declare class AllowedFilter<Model extends LucidModel> {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
7
|
+
protected name: string;
|
|
8
|
+
protected filterClass: Filter<Model>;
|
|
9
|
+
protected internalName: string;
|
|
10
|
+
protected ignored: unknown[];
|
|
11
|
+
protected default: unknown;
|
|
12
|
+
protected $hasDefault: boolean;
|
|
13
|
+
protected nullable: boolean;
|
|
14
|
+
protected validationRule?: SchemaTypes;
|
|
15
|
+
constructor(name: string, filterClass: Filter<Model>, internalName?: string);
|
|
16
|
+
rule(rule: SchemaTypes): this;
|
|
17
|
+
getRule(): SchemaTypes | undefined;
|
|
18
|
+
filter(query: ModelQueryBuilderContract<Model>, value: unknown): void;
|
|
19
|
+
getFilterClass(): Filter<Model>;
|
|
20
|
+
getName(): string;
|
|
21
|
+
isForFilter(filterName: string): boolean;
|
|
22
|
+
ignore(...values: unknown[]): this;
|
|
23
|
+
getIgnored(): unknown[];
|
|
24
|
+
getInternalName(): string;
|
|
25
|
+
setDefault(defaultValue: unknown): this;
|
|
26
|
+
getDefault(): unknown;
|
|
27
|
+
hasDefault(): boolean;
|
|
28
|
+
setNullable(isNullable?: boolean): this;
|
|
29
|
+
unsetDefault(): this;
|
|
30
|
+
protected resolveValueForFiltering(value: unknown): StrictValuesWithoutRaw | null;
|
|
31
|
+
static setFilterArrayValueDelimiter(delimiter?: string): void;
|
|
32
|
+
/**
|
|
33
|
+
* Map of filter name to its Vine rule, for every allowed filter that declares one.
|
|
34
|
+
*/
|
|
35
|
+
static extractRules(filters: readonly unknown[]): Record<string, SchemaTypes>;
|
|
36
|
+
static exact<Model extends LucidModel>(name: string, internalName?: ExtractKeys<ModelAttributes<InstanceType<Model>>>, shouldAddRelationConstraint?: boolean, arrayValueDelimiter?: string): AllowedFilter<Model>;
|
|
37
|
+
static partial<Model extends LucidModel>(name: string, internalName?: ExtractKeys<ModelAttributes<InstanceType<Model>>>, shouldAddRelationConstraint?: boolean, arrayValueDelimiter?: string): AllowedFilter<Model>;
|
|
38
|
+
static beginsWithStrict<Model extends LucidModel>(name: string, internalName?: ExtractKeys<ModelAttributes<InstanceType<Model>>>, shouldAddRelationConstraint?: boolean, arrayValueDelimiter?: string): AllowedFilter<Model>;
|
|
39
|
+
static endsWithStrict<Model extends LucidModel>(name: string, internalName?: ExtractKeys<ModelAttributes<InstanceType<Model>>>, shouldAddRelationConstraint?: boolean, arrayValueDelimiter?: string): AllowedFilter<Model>;
|
|
40
|
+
static callback<Model extends LucidModel>(name: string, callback: (query: ModelQueryBuilderContract<Model>, value: unknown, property: string) => void, internalName?: ExtractKeys<ModelAttributes<InstanceType<Model>>>, arrayValueDelimiter?: string): AllowedFilter<Model>;
|
|
41
|
+
static trashed<Model extends LucidModel>(name?: string, internalName?: ExtractKeys<ModelAttributes<InstanceType<Model>>>): AllowedFilter<Model>;
|
|
42
|
+
static scope<Model extends LucidModel = LucidModel>(name: string, internalName?: ScopeUnionKeyParams<Model>, arrayValueDelimiter?: string): AllowedFilter<Model>;
|
|
43
|
+
static custom<Model extends LucidModel>(name: string, filterClass: Filter<Model>, internalName?: ExtractKeys<ModelAttributes<InstanceType<Model>>>, arrayValueDelimiter?: string): AllowedFilter<Model>;
|
|
44
|
+
static operator<Model extends LucidModel>(name: string, filterOperator: EnumType<typeof FilterOperator>, boolean?: 'and' | 'or', internalName?: ExtractKeys<ModelAttributes<InstanceType<Model>>>, shouldAddRelationConstraint?: boolean, arrayValueDelimiter?: string): AllowedFilter<Model>;
|
|
45
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { ApiQueryBuilderRequest } from "./api_query_builder_request.js";
|
|
2
|
+
import { FiltersExact } from "./filters/filters_exact.js";
|
|
3
|
+
import { FiltersPartial } from "./filters/filters_partial.js";
|
|
4
|
+
import { FiltersBeginWithStrict } from "./filters/filters_begin_with_strict.js";
|
|
5
|
+
import { FiltersCallback } from "./filters/filters_callback.js";
|
|
6
|
+
import { FiltersEndsWithStrict } from "./filters/filters_ends_with_strict.js";
|
|
7
|
+
import { FiltersOperator } from "./filters/filters_operator.js";
|
|
8
|
+
import { FiltersScope } from "./filters/filters_scope.js";
|
|
9
|
+
import { FiltersTrashed } from "./filters/filters_trashed.js";
|
|
10
|
+
// src/allowed_filter.ts
|
|
11
|
+
var AllowedFilter = class AllowedFilter {
|
|
12
|
+
name;
|
|
13
|
+
filterClass;
|
|
14
|
+
internalName;
|
|
15
|
+
ignored = [];
|
|
16
|
+
default;
|
|
17
|
+
$hasDefault = false;
|
|
18
|
+
nullable = false;
|
|
19
|
+
validationRule;
|
|
20
|
+
constructor(name, filterClass, internalName) {
|
|
21
|
+
this.name = name;
|
|
22
|
+
this.filterClass = filterClass;
|
|
23
|
+
this.internalName = internalName ?? this.name;
|
|
24
|
+
}
|
|
25
|
+
rule(rule) {
|
|
26
|
+
this.validationRule = rule;
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
getRule() {
|
|
30
|
+
return this.validationRule;
|
|
31
|
+
}
|
|
32
|
+
filter(query, value) {
|
|
33
|
+
const valueToFilter = this.resolveValueForFiltering(value);
|
|
34
|
+
if (valueToFilter === null && !this.nullable) return;
|
|
35
|
+
this.filterClass.handle(query, valueToFilter, this.internalName);
|
|
36
|
+
}
|
|
37
|
+
getFilterClass() {
|
|
38
|
+
return this.filterClass;
|
|
39
|
+
}
|
|
40
|
+
getName() {
|
|
41
|
+
return this.name;
|
|
42
|
+
}
|
|
43
|
+
isForFilter(filterName) {
|
|
44
|
+
return this.name === filterName;
|
|
45
|
+
}
|
|
46
|
+
ignore(...values) {
|
|
47
|
+
this.ignored.push(...values.flat(Infinity));
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
getIgnored() {
|
|
51
|
+
return this.ignored;
|
|
52
|
+
}
|
|
53
|
+
getInternalName() {
|
|
54
|
+
return this.internalName;
|
|
55
|
+
}
|
|
56
|
+
setDefault(defaultValue) {
|
|
57
|
+
this.default = defaultValue;
|
|
58
|
+
this.$hasDefault = true;
|
|
59
|
+
if (defaultValue === null) this.setNullable(true);
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
getDefault() {
|
|
63
|
+
return this.default;
|
|
64
|
+
}
|
|
65
|
+
hasDefault() {
|
|
66
|
+
return this.$hasDefault;
|
|
67
|
+
}
|
|
68
|
+
setNullable(isNullable = true) {
|
|
69
|
+
this.nullable = isNullable;
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
unsetDefault() {
|
|
73
|
+
this.$hasDefault = false;
|
|
74
|
+
this.default = void 0;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
resolveValueForFiltering(value) {
|
|
78
|
+
if (Array.isArray(value)) {
|
|
79
|
+
const remainingProperties = value.map((subValue) => this.resolveValueForFiltering(subValue)).filter((subValue) => subValue !== null);
|
|
80
|
+
return remainingProperties.length === 0 ? null : remainingProperties;
|
|
81
|
+
}
|
|
82
|
+
return this.ignored.includes(value) ? null : value;
|
|
83
|
+
}
|
|
84
|
+
static setFilterArrayValueDelimiter(delimiter) {
|
|
85
|
+
if (delimiter) ApiQueryBuilderRequest.setFilterArrayValueDelimiter(delimiter);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Map of filter name to its Vine rule, for every allowed filter that declares one.
|
|
89
|
+
*/
|
|
90
|
+
static extractRules(filters) {
|
|
91
|
+
const rules = {};
|
|
92
|
+
for (const filter of filters) {
|
|
93
|
+
if (!(filter instanceof this)) continue;
|
|
94
|
+
const rule = filter.getRule();
|
|
95
|
+
if (rule) rules[filter.getName()] = rule;
|
|
96
|
+
}
|
|
97
|
+
return rules;
|
|
98
|
+
}
|
|
99
|
+
static exact(name, internalName, shouldAddRelationConstraint = true, arrayValueDelimiter) {
|
|
100
|
+
this.setFilterArrayValueDelimiter(arrayValueDelimiter);
|
|
101
|
+
return new AllowedFilter(name, new FiltersExact(shouldAddRelationConstraint), internalName);
|
|
102
|
+
}
|
|
103
|
+
static partial(name, internalName, shouldAddRelationConstraint = true, arrayValueDelimiter) {
|
|
104
|
+
this.setFilterArrayValueDelimiter(arrayValueDelimiter);
|
|
105
|
+
return new AllowedFilter(name, new FiltersPartial(shouldAddRelationConstraint), internalName);
|
|
106
|
+
}
|
|
107
|
+
static beginsWithStrict(name, internalName, shouldAddRelationConstraint = true, arrayValueDelimiter) {
|
|
108
|
+
this.setFilterArrayValueDelimiter(arrayValueDelimiter);
|
|
109
|
+
return new AllowedFilter(name, new FiltersBeginWithStrict(shouldAddRelationConstraint), internalName);
|
|
110
|
+
}
|
|
111
|
+
static endsWithStrict(name, internalName, shouldAddRelationConstraint = true, arrayValueDelimiter) {
|
|
112
|
+
this.setFilterArrayValueDelimiter(arrayValueDelimiter);
|
|
113
|
+
return new AllowedFilter(name, new FiltersEndsWithStrict(shouldAddRelationConstraint), internalName);
|
|
114
|
+
}
|
|
115
|
+
static callback(name, callback, internalName, arrayValueDelimiter) {
|
|
116
|
+
this.setFilterArrayValueDelimiter(arrayValueDelimiter);
|
|
117
|
+
return new AllowedFilter(name, new FiltersCallback(callback), internalName);
|
|
118
|
+
}
|
|
119
|
+
static trashed(name = "trashed", internalName) {
|
|
120
|
+
return new AllowedFilter(name, new FiltersTrashed(), internalName);
|
|
121
|
+
}
|
|
122
|
+
static scope(name, internalName, arrayValueDelimiter) {
|
|
123
|
+
this.setFilterArrayValueDelimiter(arrayValueDelimiter);
|
|
124
|
+
return new AllowedFilter(name, new FiltersScope(), internalName);
|
|
125
|
+
}
|
|
126
|
+
static custom(name, filterClass, internalName, arrayValueDelimiter) {
|
|
127
|
+
this.setFilterArrayValueDelimiter(arrayValueDelimiter);
|
|
128
|
+
return new AllowedFilter(name, filterClass, internalName);
|
|
129
|
+
}
|
|
130
|
+
static operator(name, filterOperator, boolean = "and", internalName, shouldAddRelationConstraint = true, arrayValueDelimiter) {
|
|
131
|
+
this.setFilterArrayValueDelimiter(arrayValueDelimiter);
|
|
132
|
+
return new AllowedFilter(name, new FiltersOperator(shouldAddRelationConstraint, filterOperator, boolean), internalName);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
export { AllowedFilter };
|
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { type Include } from './types.js';
|
|
1
|
+
import { Include } from "../types.js";
|
|
2
|
+
import { LucidModel, ModelQueryBuilderContract } from "@adonisjs/lucid/types/model";
|
|
3
|
+
import { ModelRelations } from "@adonisjs/lucid/types/relations";
|
|
5
4
|
export declare class AllowedInclude<Model extends LucidModel> {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
5
|
+
protected internalName: string;
|
|
6
|
+
protected name: string;
|
|
7
|
+
protected includeClass: Include<Model>;
|
|
8
|
+
constructor(name: string, includeClass: Include<Model>, internalName?: string);
|
|
9
|
+
include(query: ModelQueryBuilderContract<Model>): void;
|
|
10
|
+
getName(): string;
|
|
11
|
+
isForInclude(includeName: string): boolean;
|
|
12
|
+
static relationship<ParentModel extends LucidModel>(name: string, internalName?: string): AllowedInclude<ParentModel>[];
|
|
13
|
+
static count<ParentModel extends LucidModel>(name: string, internalName?: string): AllowedInclude<ParentModel>[];
|
|
14
|
+
static callback<ParentModel extends LucidModel, RelatedModel extends LucidModel>(name: string, callback: (query: ModelRelations<RelatedModel, ParentModel>['builder'] | ModelRelations<RelatedModel, ParentModel>['subQuery']) => void, internalName?: string): AllowedInclude<ParentModel>[];
|
|
15
|
+
static custom<ParentModel extends LucidModel>(name: string, includeClass: Include<ParentModel>, internalName?: string): AllowedInclude<ParentModel>[];
|
|
16
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { IncludedCallback } from "./includes/included_callback.js";
|
|
2
|
+
import { IncludedCount } from "./includes/included_count.js";
|
|
3
|
+
import { IncludedRelationship } from "./includes/included_relationship.js";
|
|
4
|
+
import app from "@adonisjs/core/services/app";
|
|
5
|
+
// src/allowed_include.ts
|
|
6
|
+
var AllowedInclude = class AllowedInclude {
|
|
7
|
+
internalName;
|
|
8
|
+
name;
|
|
9
|
+
includeClass;
|
|
10
|
+
constructor(name, includeClass, internalName) {
|
|
11
|
+
this.name = name;
|
|
12
|
+
this.includeClass = includeClass;
|
|
13
|
+
this.internalName = internalName ?? this.name;
|
|
14
|
+
}
|
|
15
|
+
include(query) {
|
|
16
|
+
this.includeClass.handle(query, this.internalName);
|
|
17
|
+
}
|
|
18
|
+
getName() {
|
|
19
|
+
return this.name;
|
|
20
|
+
}
|
|
21
|
+
isForInclude(includeName) {
|
|
22
|
+
return this.name === includeName;
|
|
23
|
+
}
|
|
24
|
+
static relationship(name, internalName) {
|
|
25
|
+
internalName ??= name;
|
|
26
|
+
const relationships = IncludedRelationship.getIndividualRelationshipPathsFromInclude(internalName);
|
|
27
|
+
const aliases = IncludedRelationship.getIndividualRelationshipPathsFromInclude(name);
|
|
28
|
+
const countSuffix = app.config.get("apiquery.countSuffix", "Count");
|
|
29
|
+
const includes = [];
|
|
30
|
+
for (const [index, relationship] of relationships.entries()) {
|
|
31
|
+
const alias = aliases[index];
|
|
32
|
+
includes.push(new AllowedInclude(alias, new IncludedRelationship(), relationship));
|
|
33
|
+
if (!relationship.includes(".")) includes.push(...this.count(`${alias}${countSuffix}`, `${relationship}${countSuffix}`));
|
|
34
|
+
}
|
|
35
|
+
return includes;
|
|
36
|
+
}
|
|
37
|
+
static count(name, internalName) {
|
|
38
|
+
return [new AllowedInclude(name, new IncludedCount(), internalName)];
|
|
39
|
+
}
|
|
40
|
+
static callback(name, callback, internalName) {
|
|
41
|
+
return [new AllowedInclude(name, new IncludedCallback(callback), internalName)];
|
|
42
|
+
}
|
|
43
|
+
static custom(name, includeClass, internalName) {
|
|
44
|
+
return [new AllowedInclude(name, includeClass, internalName)];
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
export { AllowedInclude };
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ExtractKeys, Sort } from "../types.js";
|
|
2
|
+
import { LucidModel, ModelAttributes, ModelQueryBuilderContract } from "@adonisjs/lucid/types/model";
|
|
3
3
|
export declare class AllowedSort<Model extends LucidModel> {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
4
|
+
protected defaultDirection: string;
|
|
5
|
+
protected internalName: string;
|
|
6
|
+
protected name: string;
|
|
7
|
+
protected sortClass: Sort<Model>;
|
|
8
|
+
constructor(name: string, sortClass: Sort<Model>, internalName?: ExtractKeys<ModelAttributes<InstanceType<Model>>>);
|
|
9
|
+
sort(query: ModelQueryBuilderContract<Model>, isDescending?: boolean): void;
|
|
10
|
+
getName(): string;
|
|
11
|
+
isSort(sortName: string): boolean;
|
|
12
|
+
getInternalName(): string;
|
|
13
|
+
setDefaultDirection(defaultDirection: string): this;
|
|
14
|
+
static parseSortDirection(name: string): string;
|
|
15
|
+
static field<Model extends LucidModel>(name: string, internalName?: ExtractKeys<ModelAttributes<InstanceType<Model>>>): AllowedSort<Model>;
|
|
16
|
+
static custom<Model extends LucidModel>(name: string, sortClass: Sort<Model>, internalName?: ExtractKeys<ModelAttributes<InstanceType<Model>>>): AllowedSort<Model>;
|
|
17
|
+
static callback<Model extends LucidModel>(name: string, callback: (query: ModelQueryBuilderContract<Model>, isDescending: boolean, property: string) => void, internalName?: ExtractKeys<ModelAttributes<InstanceType<Model>>>): AllowedSort<Model>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { SortDirection } from "./enums/sort_direction.js";
|
|
2
|
+
import { InvalidDirection } from "./exceptions/invalid_direction.js";
|
|
3
|
+
import { SortsCallback } from "./sorts/sorts_callback.js";
|
|
4
|
+
import { SortsField } from "./sorts/sorts_field.js";
|
|
5
|
+
// src/allowed_sort.ts
|
|
6
|
+
const REMOVE_HYPHEN_STARTING_IN_ANY_POSITION = /^-+/;
|
|
7
|
+
var AllowedSort = class AllowedSort {
|
|
8
|
+
defaultDirection;
|
|
9
|
+
internalName;
|
|
10
|
+
name;
|
|
11
|
+
sortClass;
|
|
12
|
+
constructor(name, sortClass, internalName) {
|
|
13
|
+
this.name = name.replace(REMOVE_HYPHEN_STARTING_IN_ANY_POSITION, "");
|
|
14
|
+
this.defaultDirection = AllowedSort.parseSortDirection(name);
|
|
15
|
+
this.sortClass = sortClass;
|
|
16
|
+
this.internalName = internalName ?? this.name;
|
|
17
|
+
}
|
|
18
|
+
sort(query, isDescending) {
|
|
19
|
+
isDescending ??= this.defaultDirection === SortDirection.Descending;
|
|
20
|
+
this.sortClass.handle(query, isDescending, this.internalName);
|
|
21
|
+
}
|
|
22
|
+
getName() {
|
|
23
|
+
return this.name;
|
|
24
|
+
}
|
|
25
|
+
isSort(sortName) {
|
|
26
|
+
return this.name === sortName;
|
|
27
|
+
}
|
|
28
|
+
getInternalName() {
|
|
29
|
+
return this.internalName;
|
|
30
|
+
}
|
|
31
|
+
setDefaultDirection(defaultDirection) {
|
|
32
|
+
if (![SortDirection.Ascending, SortDirection.Descending].includes(defaultDirection)) throw InvalidDirection.make(defaultDirection);
|
|
33
|
+
this.defaultDirection = defaultDirection;
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
static parseSortDirection(name) {
|
|
37
|
+
return name.startsWith("-") ? SortDirection.Descending : SortDirection.Ascending;
|
|
38
|
+
}
|
|
39
|
+
static field(name, internalName) {
|
|
40
|
+
return new AllowedSort(name, new SortsField(), internalName);
|
|
41
|
+
}
|
|
42
|
+
static custom(name, sortClass, internalName) {
|
|
43
|
+
return new AllowedSort(name, sortClass, internalName);
|
|
44
|
+
}
|
|
45
|
+
static callback(name, callback, internalName) {
|
|
46
|
+
return new AllowedSort(name, new SortsCallback(callback), internalName);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
export { AllowedSort };
|
|
@@ -1,31 +1,56 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { QueryValidator, ValidationRules } from "../types.js";
|
|
2
|
+
import { HttpRequest } from "@adonisjs/core/http";
|
|
3
|
+
import { SchemaTypes } from "@vinejs/vine/types";
|
|
3
4
|
export declare class ApiQueryBuilderRequest {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
5
|
+
protected static includesArrayValueDelimiter: string;
|
|
6
|
+
protected static appendsArrayValueDelimiter: string;
|
|
7
|
+
protected static fieldsArrayValueDelimiter: string;
|
|
8
|
+
protected static sortsArrayValueDelimiter: string;
|
|
9
|
+
protected static filterArrayValueDelimiter: string;
|
|
10
|
+
private readonly _request;
|
|
11
|
+
private _rules?;
|
|
12
|
+
private _filterRules;
|
|
13
|
+
private _autoValidate?;
|
|
14
|
+
private _validation?;
|
|
15
|
+
private _validatedData?;
|
|
16
|
+
constructor(request?: HttpRequest);
|
|
17
|
+
getRequest(): HttpRequest;
|
|
18
|
+
all(): Record<string, unknown>;
|
|
19
|
+
setRules(rules: ValidationRules): this;
|
|
20
|
+
getRules(): ValidationRules | undefined;
|
|
21
|
+
setFilterRules(rules: Record<string, SchemaTypes>): this;
|
|
22
|
+
getFilterRules(): Record<string, SchemaTypes>;
|
|
23
|
+
setAutoValidate(autoValidate: boolean): this;
|
|
24
|
+
shouldAutoValidate(): boolean;
|
|
25
|
+
hasValidation(): boolean;
|
|
26
|
+
toValidator(): Promise<QueryValidator>;
|
|
27
|
+
/**
|
|
28
|
+
* Validates the request once (the result is memoized) and returns the validated data.
|
|
29
|
+
* Returns the raw request data when no rules are configured.
|
|
30
|
+
*/
|
|
31
|
+
validate<T = Record<string, unknown>>(rules?: ValidationRules): Promise<T>;
|
|
32
|
+
getValidatedData(): Record<string, unknown> | undefined;
|
|
33
|
+
isValidated(): boolean;
|
|
34
|
+
includes(): string[];
|
|
35
|
+
appends(): string[];
|
|
36
|
+
sorts(): string[];
|
|
37
|
+
fields(): Record<string, string[]>;
|
|
38
|
+
filters(): Record<string, unknown>;
|
|
39
|
+
protected getArrayParameterData(configKey: string, defaultParam: string, delimiter: string): string[];
|
|
40
|
+
protected getFilterValue(value: unknown): unknown;
|
|
41
|
+
protected getRequestData<T = unknown>(key: string, defaultValue?: T): T;
|
|
42
|
+
protected runValidation(): Promise<Record<string, unknown>>;
|
|
43
|
+
static setArrayValueDelimiter(delimiter: string): void;
|
|
44
|
+
static setIncludesArrayValueDelimiter(includesArrayValueDelimiter: string): void;
|
|
45
|
+
static setAppendsArrayValueDelimiter(appendsArrayValueDelimiter: string): void;
|
|
46
|
+
static setFieldsArrayValueDelimiter(fieldsArrayValueDelimiter: string): void;
|
|
47
|
+
static setSortsArrayValueDelimiter(sortsArrayValueDelimiter: string): void;
|
|
48
|
+
static setFilterArrayValueDelimiter(filterArrayValueDelimiter: string): void;
|
|
49
|
+
static getIncludesArrayValueDelimiter(): string;
|
|
50
|
+
static getAppendsArrayValueDelimiter(): string;
|
|
51
|
+
static getFieldsArrayValueDelimiter(): string;
|
|
52
|
+
static getSortsArrayValueDelimiter(): string;
|
|
53
|
+
static getFilterArrayValueDelimiter(): string;
|
|
54
|
+
static resetDelimiters(): void;
|
|
55
|
+
static fromRequest(request: HttpRequest): ApiQueryBuilderRequest;
|
|
56
|
+
}
|