-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmappings.ts
More file actions
126 lines (112 loc) · 3.77 KB
/
mappings.ts
File metadata and controls
126 lines (112 loc) · 3.77 KB
1
2
3
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Key and payload mapping helpers between Clockodo snake_case and SDK
* camelCase.
*
* @module
*/
/* eslint-disable @typescript-eslint/no-deprecated */
// On first sight, you might think that the typings in this module could be improved.
// However, we don't have typings for the snake_case models which is why we don't
// gain much more confidence by removing 'any' here
import mapObject, { mapObjectSkip } from "map-obj";
import { warnDeprecated } from "./deprecations.js";
/**
* @deprecated Do not rely on using these query param mappings. You can use the
* "filter" object instead.
*/
export const queryParamMapping: Record<string, string> = {
filterId: "filter[id]",
filterUsersId: "filter[users_id]",
filterYear: "filter[year]",
filterCustomersId: "filter[customers_id]",
filterProjectsId: "filter[projects_id]",
filterServicesId: "filter[services_id]",
filterLumpsumServicesId: "filter[lumpsum_services_id]",
filterLumpsumsServicesId: "filter[lumpsum_services_id]",
filterBillable: "filter[billable]",
filterText: "filter[text]",
filterTextsId: "filter[texts_id]",
filterBudgetType: "filter[budget_type]",
filterLumpsumsId: "filter[lumpsums_id]",
filterTimeSince: "filter[time_since]",
filterTimeUntil: "filter[time_until]",
filterTeamsId: "filter[teams_id]",
filterActive: "filter[active]",
filterFulltext: "filter[fulltext]",
};
// The purpose of the type parameter is to provide an easy way to specify the type of the result.
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
export const mapQueryParams = <Result = Record<string, unknown>>(
queryParams: Record<string, any>,
): Result => {
return mapObject(
queryParams,
(key, value) => {
if (value === undefined || value === null) {
return mapObjectSkip;
}
if (value === "") {
return mapObjectSkip;
}
const mappedKey =
key in queryParamMapping
? queryParamMapping[key]!
: camelCaseToSnakeCase(key);
if (key.startsWith("filter") && key in queryParamMapping) {
warnDeprecated(
"CLOCKODO_DEPRECATED_FILTER_QUERY_PARAM",
`Query parameter "${key}" is deprecated. Use the "filter" object instead.`,
);
}
if (mappedKey === "include" && Array.isArray(value)) {
return [mappedKey, value.join(",")];
}
return [mappedKey, value];
},
{
deep: true,
},
) as unknown as Result;
};
// The purpose of the type parameter is to provide an easy way to specify the type of the result.
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
export const mapRequestBody = <Result = Record<string, unknown>>(
requestBody: Record<string, any>,
): Result => {
return mapObject(
requestBody,
(key, value) => {
const mappedKey = camelCaseToSnakeCase(key);
return [mappedKey, value];
},
{
deep: true,
},
) as unknown as Result;
};
// The purpose of the type parameter is to provide an easy way to specify the type of the result.
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
export const mapResponseBody = <Result = Record<string, unknown>>(
responseBody: Record<string, any>,
): Result => {
return mapObject(
responseBody,
(key, value) => {
const mappedKey = snakeCaseToCamelCase(key);
return [mappedKey, value];
},
{
deep: true,
},
) as unknown as Result;
};
export const snakeCaseToCamelCase = (key: string): string => {
return key.replaceAll(/_+(\d*)([a-z])/gi, (_, $1, $2) => {
return ($1 + $2.toUpperCase()) as string;
});
};
export const camelCaseToSnakeCase = (key: string): string => {
return key.replaceAll(/(\d*)([A-Z])/g, (_, $1, $2) => {
return "_" + $1 + $2.toLowerCase();
});
};