-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.js
140 lines (124 loc) · 3.06 KB
/
common.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
export function isPlainObject(wat) {
return Object.prototype.toString.call(wat) === "[object Object]";
}
export const AVAILABLE_OPERATIONS = [
"select",
"insert",
"upsert",
"update",
"delete",
];
export const DEFAULT_OPTIONS = {
tracing: true,
breadcrumbs: true,
errors: false,
operations: [...AVAILABLE_OPERATIONS],
shouldCreateSpan: undefined,
shouldCreateBreadcrumb: undefined,
sanitizeBody: undefined,
};
export function validateOption(availableOptions, key, value) {
if (!availableOptions.includes(key)) {
throw new Error(`Unknown option: ${key}`);
}
if (key === "operations") {
if (!Array.isArray(value)) {
throw new TypeError(`operations should be an array`);
}
for (const operation of value) {
if (!AVAILABLE_OPERATIONS.includes(operation)) {
throw new Error(`Unknown operation: ${operation}`);
}
}
}
if (key === "shouldCreateSpan" && typeof value !== "function") {
throw new TypeError(
"shouldCreateSpan should be a function that returns a boolean"
);
}
if (key === "shouldCreateBreadcrumb" && typeof value !== "function") {
throw new TypeError(
"shouldCreateBreadcrumb should be a function that returns a boolean"
);
}
if (key === "sanitizeBody" && typeof value !== "function") {
throw new TypeError(
"sanitizeBody should be a function that returns a valid data object"
);
}
}
export function extractOperation(method, headers = {}) {
switch (method) {
case "GET": {
return "select";
}
case "POST": {
if (headers["Prefer"]?.includes("resolution=")) {
return "upsert";
} else {
return "insert";
}
}
case "PATCH": {
return "update";
}
case "DELETE": {
return "delete";
}
}
}
export const FILTER_MAPPINGS = {
eq: "eq",
neq: "neq",
gt: "gt",
gte: "gte",
lt: "lt",
lte: "lte",
like: "like",
"like(all)": "likeAllOf",
"like(any)": "likeAnyOf",
ilike: "ilike",
"ilike(all)": "ilikeAllOf",
"ilike(any)": "ilikeAnyOf",
is: "is",
in: "in",
cs: "contains",
cd: "containedBy",
sr: "rangeGt",
nxl: "rangeGte",
sl: "rangeLt",
nxr: "rangeLte",
adj: "rangeAdjacent",
ov: "overlaps",
fts: "",
plfts: "plain",
phfts: "phrase",
wfts: "websearch",
not: "not",
};
export function translateFiltersIntoMethods(key, query) {
if (query === "" || query === "*") {
return `select(*)`;
}
if (key === "select") {
return `select(${query})`;
}
if (key === "or" || key.endsWith(".or")) {
return `${key}${query}`;
}
const [filter, ...value] = query.split(".");
let method;
// Handle optional `configPart` of the filter
if (filter.startsWith("fts")) {
method = "textSearch";
} else if (filter.startsWith("plfts")) {
method = "textSearch[plain]";
} else if (filter.startsWith("phfts")) {
method = "textSearch[phrase]";
} else if (filter.startsWith("wfts")) {
method = "textSearch[websearch]";
} else {
method = FILTER_MAPPINGS[filter] || "filter";
}
return `${method}(${key}, ${value.join(".")})`;
}