@miso.ai/doggoganger 0.9.1-beta.3 → 0.9.1-beta.4
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/package.json +1 -1
- package/src/fetch.js +28 -26
package/package.json
CHANGED
package/src/fetch.js
CHANGED
|
@@ -1,32 +1,34 @@
|
|
|
1
1
|
import { wrapResponse } from './route/utils.js';
|
|
2
2
|
|
|
3
|
-
export default function
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
let type = 'query';
|
|
3
|
+
export default async function fetch(api, url, { method = 'GET', body } = {}) {
|
|
4
|
+
method = method.toUpperCase();
|
|
5
|
+
if (typeof body === 'string') {
|
|
6
|
+
body = JSON.parse(body);
|
|
7
|
+
}
|
|
8
|
+
url = new URL(url);
|
|
9
|
+
const segments = url.pathname.substring(1).split('/');
|
|
10
|
+
if (segments[0] === 'v1') {
|
|
11
|
+
segments.splice(0, 1);
|
|
12
|
+
}
|
|
13
|
+
const group = segments[0];
|
|
14
|
+
let name = segments[1];
|
|
15
|
+
let type = 'query';
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
if (group === 'interactions' && segments.length === 1) {
|
|
18
|
+
name = 'upload';
|
|
19
|
+
type = 'data';
|
|
20
|
+
} else if (group === 'ask' && segments.length === 4 && segments[2] === 'questions' && segments[3] === 'answer') {
|
|
21
|
+
name = 'answer';
|
|
22
|
+
body = segments[3];
|
|
23
|
+
}
|
|
25
24
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
if (!api[group][name]) {
|
|
26
|
+
throw new Error(`Unknown path: ${url.pathname}`);
|
|
27
|
+
}
|
|
29
28
|
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
const resBody = await wrapResponse(api[group][name], type)(body);
|
|
30
|
+
|
|
31
|
+
return new Response(JSON.stringify(resBody), {
|
|
32
|
+
status: 200,
|
|
33
|
+
});
|
|
32
34
|
}
|