reactivated 0.30.2 → 0.31.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/dist/components/Widget.d.ts +0 -1
- package/dist/components/Widget.js +0 -13
- package/dist/components/Widget.js.map +1 -1
- package/dist/eslintrc.js +3 -0
- package/dist/eslintrc.js.map +1 -1
- package/dist/forms/index.d.ts +31 -13
- package/dist/forms/index.js +89 -29
- package/dist/forms/index.js.map +1 -1
- package/dist/forms/widgets.d.ts +2 -0
- package/dist/forms/widgets.js +1 -1
- package/dist/forms/widgets.js.map +1 -1
- package/dist/generated.d.ts +10 -0
- package/dist/generator.mjs +143 -5
- package/dist/generator.mjs.map +1 -1
- package/dist/rpc.d.ts +58 -0
- package/dist/rpc.js +126 -0
- package/dist/rpc.js.map +1 -0
- package/package.json +2 -1
- package/scripts/setup_environment.sh +6 -2
- package/src/components/Widget.tsx +0 -13
- package/src/eslintrc.tsx +4 -0
- package/src/forms/index.tsx +154 -43
- package/src/forms/widgets.tsx +4 -1
- package/src/generated.tsx +10 -0
- package/src/generator.mts +171 -4
- package/src/rpc.tsx +207 -0
package/src/rpc.tsx
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// lets us specify the cookie string rather than always looking at
|
|
2
|
+
// document.cookie.
|
|
3
|
+
export function getCookieFromCookieString(name: string, cookieString: string) {
|
|
4
|
+
let cookieValue = null;
|
|
5
|
+
if (cookieString && cookieString != "") {
|
|
6
|
+
const cookies = cookieString.split(";");
|
|
7
|
+
for (let i = 0; i < cookies.length; i++) {
|
|
8
|
+
const cookie = cookies[i].trim();
|
|
9
|
+
|
|
10
|
+
// Does this cookie string begin with the name we want?
|
|
11
|
+
if (cookie.substring(0, name.length + 1) == name + "=") {
|
|
12
|
+
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
|
13
|
+
break;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return cookieValue;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type ParamsAndIterator = {iterator: string[]; params: Record<string, string | number>};
|
|
22
|
+
|
|
23
|
+
function buildUrl(baseUrl: string, paramsAndIterator: ParamsAndIterator | null) {
|
|
24
|
+
if (paramsAndIterator == null) {
|
|
25
|
+
return baseUrl;
|
|
26
|
+
} else {
|
|
27
|
+
const params = [];
|
|
28
|
+
|
|
29
|
+
for (const key of paramsAndIterator.iterator) {
|
|
30
|
+
params.push(paramsAndIterator.params[key]);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return `${baseUrl}${params.join("/")}/`;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type RequesterResult =
|
|
38
|
+
| {type: "invalid"; errors: any}
|
|
39
|
+
| {type: "success"; data: any}
|
|
40
|
+
| {type: "denied"; reason: any}
|
|
41
|
+
| {type: "unauthorized"}
|
|
42
|
+
| {type: "exception"; exception: unknown};
|
|
43
|
+
|
|
44
|
+
export type Requester = (
|
|
45
|
+
url: string,
|
|
46
|
+
payload: FormData | null,
|
|
47
|
+
) => Promise<RequesterResult>;
|
|
48
|
+
|
|
49
|
+
export const defaultRequester: Requester = async (url, payload) => {
|
|
50
|
+
try {
|
|
51
|
+
const response = await fetch(url, {
|
|
52
|
+
method: payload == null ? "GET" : "POST",
|
|
53
|
+
body: payload,
|
|
54
|
+
headers: {
|
|
55
|
+
Accept: "application/json",
|
|
56
|
+
"X-CSRFToken":
|
|
57
|
+
getCookieFromCookieString("csrftoken", document.cookie) ?? "",
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
if (response.status === 200) {
|
|
62
|
+
return {
|
|
63
|
+
type: "success",
|
|
64
|
+
data: await response.json(),
|
|
65
|
+
};
|
|
66
|
+
} else if (response.status === 400) {
|
|
67
|
+
return {
|
|
68
|
+
type: "invalid",
|
|
69
|
+
errors: await response.json(),
|
|
70
|
+
};
|
|
71
|
+
} else if (response.status === 401) {
|
|
72
|
+
return {
|
|
73
|
+
type: "unauthorized",
|
|
74
|
+
};
|
|
75
|
+
} else if (response.status === 403) {
|
|
76
|
+
return {
|
|
77
|
+
type: "denied",
|
|
78
|
+
reason: response.json(),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
} catch (error) {
|
|
82
|
+
return {
|
|
83
|
+
type: "exception",
|
|
84
|
+
exception: error,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
type: "exception",
|
|
90
|
+
exception: null,
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const buildValues = (
|
|
95
|
+
type: "form" | "form_set",
|
|
96
|
+
formData: FormData,
|
|
97
|
+
prefix: string | null,
|
|
98
|
+
values: any,
|
|
99
|
+
) => {
|
|
100
|
+
if (values == null) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (type === "form_set") {
|
|
104
|
+
// TODO: can initial always be 0?
|
|
105
|
+
formData.append(`${prefix}-INITIAL_FORMS`, values.length);
|
|
106
|
+
formData.append(`${prefix}-TOTAL_FORMS`, values.length);
|
|
107
|
+
for (const index in values as Array<any>) {
|
|
108
|
+
const formSetForm = values[index];
|
|
109
|
+
Object.keys(formSetForm).forEach((key) =>
|
|
110
|
+
formData.append(`${prefix}-${index}-${key}`, formSetForm[key] ?? ""),
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
Object.keys(values).forEach((key) =>
|
|
115
|
+
formData.append(
|
|
116
|
+
prefix == null ? key : `${prefix}-${key}`,
|
|
117
|
+
values[key as keyof typeof values] ?? "",
|
|
118
|
+
),
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export async function rpcCall(
|
|
124
|
+
requester: Requester,
|
|
125
|
+
options: {
|
|
126
|
+
url: string;
|
|
127
|
+
input: {
|
|
128
|
+
values: Record<string, any>;
|
|
129
|
+
type: "form" | "form_set" | "form_group";
|
|
130
|
+
} | null;
|
|
131
|
+
paramsAndIterator: ParamsAndIterator | null;
|
|
132
|
+
name: string;
|
|
133
|
+
},
|
|
134
|
+
): Promise<Result<any, any, any>> {
|
|
135
|
+
const {url, input, paramsAndIterator, name} = options;
|
|
136
|
+
const {type, values} = input != null ? input : {type: "", values: {}};
|
|
137
|
+
|
|
138
|
+
const formData = new FormData();
|
|
139
|
+
|
|
140
|
+
if (type === "form_group") {
|
|
141
|
+
Object.keys(values).map((prefix) => {
|
|
142
|
+
const formOrFormSet = (values as any)[prefix];
|
|
143
|
+
if (Array.isArray(formOrFormSet)) {
|
|
144
|
+
buildValues("form_set", formData, prefix, formOrFormSet);
|
|
145
|
+
} else {
|
|
146
|
+
buildValues("form", formData, prefix, formOrFormSet);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
} else if (type === "form_set") {
|
|
150
|
+
buildValues("form_set", formData, "form", values);
|
|
151
|
+
} else {
|
|
152
|
+
buildValues("form", formData, null, values);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
let urlWithPossibleInstance = buildUrl(url, paramsAndIterator);
|
|
156
|
+
|
|
157
|
+
if (name != null) {
|
|
158
|
+
urlWithPossibleInstance = urlWithPossibleInstance.concat(`${name}/`);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const result = await requester(
|
|
162
|
+
urlWithPossibleInstance,
|
|
163
|
+
input == null ? null : formData,
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
const request: Request = {
|
|
167
|
+
url: urlWithPossibleInstance,
|
|
168
|
+
params: paramsAndIterator?.params,
|
|
169
|
+
name: name,
|
|
170
|
+
data: formData,
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
return {...result, request};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
type Request = {
|
|
177
|
+
url: string;
|
|
178
|
+
params: unknown;
|
|
179
|
+
name: string;
|
|
180
|
+
data: FormData;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
export type Result<TSuccess, TInvalid, TDenied> =
|
|
184
|
+
| {
|
|
185
|
+
type: "success";
|
|
186
|
+
data: TSuccess;
|
|
187
|
+
request: Request;
|
|
188
|
+
}
|
|
189
|
+
| {
|
|
190
|
+
type: "invalid";
|
|
191
|
+
errors: TInvalid;
|
|
192
|
+
request: Request;
|
|
193
|
+
}
|
|
194
|
+
| {
|
|
195
|
+
type: "denied";
|
|
196
|
+
reason: TDenied;
|
|
197
|
+
request: Request;
|
|
198
|
+
}
|
|
199
|
+
| {
|
|
200
|
+
type: "unauthorized";
|
|
201
|
+
request: Request;
|
|
202
|
+
}
|
|
203
|
+
| {
|
|
204
|
+
type: "exception";
|
|
205
|
+
exception: unknown;
|
|
206
|
+
request: Request;
|
|
207
|
+
};
|