Skip to content

Commit 1432ae8

Browse files
authored
Merge pull request #59 from ctrl-hub/fix-sort-direction-ts
Fix sort direction ts
2 parents 15ae5b0 + 893df4e commit 1432ae8

File tree

7 files changed

+40
-20
lines changed

7 files changed

+40
-20
lines changed

dist/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,10 +1331,15 @@ class RequestBuilder {
13311331
return this;
13321332
}
13331333
withSort(sort) {
1334-
this.requestOptions.sort = sort.map((sortOption) => ({
1335-
key: sortOption.key,
1336-
direction: sortOption.direction || "asc"
1337-
}));
1334+
this.requestOptions.sort = sort.map((sortOption) => {
1335+
if (sortOption.direction && sortOption.direction !== "asc" && sortOption.direction !== "desc") {
1336+
throw new Error(`Sort direction must be either 'asc' or 'desc', got: ${sortOption.direction}`);
1337+
}
1338+
return {
1339+
key: sortOption.key,
1340+
direction: sortOption.direction
1341+
};
1342+
});
13381343
return this;
13391344
}
13401345
withFilters(filters) {

dist/utils/RequestBuilder.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { RequestOptions } from "../utils/RequestOptions";
2-
import type { RequestOptionsType } from "../utils/RequestOptions";
2+
import type { RequestOptionsType, SortDirection } from "../utils/RequestOptions";
33
export declare class RequestBuilder {
44
protected requestOptions: RequestOptionsType;
55
withIncludes(includes: string[]): this;
66
withSort(sort: Array<{
77
key: string;
8-
direction?: 'asc' | 'desc';
8+
direction?: SortDirection;
99
}>): this;
1010
withFilters(filters: Array<{
1111
key: string;

dist/utils/RequestBuilder.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ export class RequestBuilder {
66
return this;
77
}
88
withSort(sort) {
9-
this.requestOptions.sort = sort.map(sortOption => ({
10-
key: sortOption.key,
11-
direction: sortOption.direction || 'asc'
12-
}));
9+
this.requestOptions.sort = sort.map(sortOption => {
10+
if (sortOption.direction && sortOption.direction !== 'asc' && sortOption.direction !== 'desc') {
11+
throw new Error(`Sort direction must be either 'asc' or 'desc', got: ${sortOption.direction}`);
12+
}
13+
return {
14+
key: sortOption.key,
15+
direction: sortOption.direction
16+
};
17+
});
1318
return this;
1419
}
1520
withFilters(filters) {

dist/utils/RequestOptions.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
export type SortDirection = 'asc' | 'desc' | string;
12
type Sort = {
23
key: string;
3-
direction?: 'asc' | 'desc';
4+
direction?: SortDirection;
45
};
56
type Filter = {
67
key: string;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "git",
55
"url": "https://github.com/ctrl-hub/sdk.ts"
66
},
7-
"version": "0.1.134",
7+
"version": "0.1.135",
88
"main": "dist/index.js",
99
"types": "dist/index.d.ts",
1010
"type": "module",

src/utils/RequestBuilder.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {RequestOptions} from "../utils/RequestOptions";
2-
import type {RequestOptionsType} from "../utils/RequestOptions";
2+
import type {RequestOptionsType, SortDirection} from "../utils/RequestOptions";
33

44
export class RequestBuilder {
55
protected requestOptions: RequestOptionsType = {};
@@ -9,11 +9,18 @@ export class RequestBuilder {
99
return this;
1010
}
1111

12-
withSort(sort: Array<{ key: string, direction?: 'asc' | 'desc' }>): this {
13-
this.requestOptions.sort = sort.map(sortOption => ({
14-
key: sortOption.key,
15-
direction: sortOption.direction || 'asc'
16-
}));
12+
withSort(sort: Array<{ key: string, direction?: SortDirection }>): this {
13+
this.requestOptions.sort = sort.map(sortOption => {
14+
15+
if (sortOption.direction && sortOption.direction !== 'asc' && sortOption.direction !== 'desc') {
16+
throw new Error(`Sort direction must be either 'asc' or 'desc', got: ${sortOption.direction}`);
17+
}
18+
19+
return {
20+
key: sortOption.key,
21+
direction: sortOption.direction
22+
};
23+
});
1724
return this;
1825
}
1926

src/utils/RequestOptions.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
export type SortDirection = 'asc' | 'desc' | string;
2+
13
type Sort = {
24
key: string;
3-
direction?: 'asc' | 'desc';
5+
direction?: SortDirection;
46
};
57

68
type Filter = {
@@ -115,4 +117,4 @@ export class RequestOptions {
115117

116118
return requestOptions;
117119
}
118-
}
120+
}

0 commit comments

Comments
 (0)