@@ -14,44 +14,87 @@ export const POST = async ({ request }) => {
14
14
let dataset_id ;
15
15
let organization_id ;
16
16
17
- const requestJSON = await request . json ( ) ;
18
- let url = requestJSON . url ;
19
- if ( request && requestJSON . api_key ) {
20
- api_key = requestJSON . api_key ;
21
- } ;
22
- if ( request && requestJSON . organization_id ) {
23
- organization_id = requestJSON . organization_id ;
24
- } ;
25
- if ( request && requestJSON . dataset_id ) {
26
- dataset_id = requestJSON . dataset_id ;
27
- }
17
+ try {
18
+ const requestJSON = await request . json ( ) ;
19
+ let url = requestJSON . url ;
20
+ if ( request && requestJSON . api_key ) {
21
+ api_key = requestJSON . api_key ;
22
+ } ;
23
+ if ( request && requestJSON . organization_id ) {
24
+ organization_id = requestJSON . organization_id ;
25
+ } ;
26
+ if ( request && requestJSON . dataset_id ) {
27
+ dataset_id = requestJSON . dataset_id ;
28
+ }
29
+
30
+ const resp = await fetch ( `${ url } /chunk` , {
31
+ method : "POST" ,
32
+ headers : {
33
+ "Content-Type" : 'application/json' ,
34
+ "Authorization" : "Bearer " + api_key ,
35
+ "TR-organization" : organization_id ,
36
+ "TR-dataset" : dataset_id
37
+ } ,
38
+ body : JSON . stringify ( {
39
+ "chunk_html" : "Hi there bro"
40
+ } )
41
+ } ) ;
42
+
43
+ if ( resp . status !== 200 ) {
44
+ console . log ( resp ) ;
45
+ return new Response ( JSON . stringify ( {
46
+ error : statusCodeResponseTypes [ resp . status ] || `${ resp . status } Error`
47
+ } ) , {
48
+ status : resp . status ,
49
+ headers : {
50
+ 'Content-Type' : 'application/json'
51
+ }
52
+ } ) ;
53
+ }
54
+
55
+ const chunkData = await resp . json ( ) ;
56
+ console . log ( chunkData ) ;
28
57
29
- const resp = await fetch ( `${ url } /api/chunk` , {
30
- method : "POST" ,
31
- headers : {
32
- "Content-Type" : 'application/json' ,
33
- "Authorization" : api_key ,
34
- "TR-organization-id" : organization_id ,
35
- "TR-dataset-id" : dataset_id
36
- }
37
- } ) ;
38
-
39
- await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ;
40
-
41
- const resp2 = await fetch ( `${ url } /api/chunk/${ resp . id } ` , {
42
- method : "DELETE" ,
43
- headers : {
44
- "Content-Type" : 'application/json' ,
45
- "Authorization" : api_key ,
46
- "TR-Organization" : organization_id ,
47
- "TR-Dataset" : dataset_id
48
- } ,
49
- body : JSON . stringify ( {
50
- "traking_id" : resp . tracking_id
51
- } )
52
- } ) ;
53
-
54
- return new Response ( JSON . stringify ( {
55
- "ok" : true
56
- } ) ) ;
58
+ await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ;
59
+
60
+ const resp2 = await fetch ( `${ url } /chunk/${ chunkData . chunk_metadata . id } ` , {
61
+ method : "DELETE" ,
62
+ headers : {
63
+ "Content-Type" : 'application/json' ,
64
+ "Authorization" : api_key ,
65
+ "TR-Organization" : organization_id ,
66
+ "TR-Dataset" : dataset_id
67
+ } ,
68
+ } ) ;
69
+
70
+ if ( resp2 . status !== 204 ) {
71
+ return new Response ( JSON . stringify ( {
72
+ error : statusCodeResponseTypes [ resp2 . status ] || `${ resp2 . status } Error` ,
73
+ } ) , {
74
+ status : resp2 . status ,
75
+ headers : {
76
+ 'Content-Type' : 'application/json'
77
+ }
78
+ } ) ;
79
+ }
80
+
81
+ return new Response ( JSON . stringify ( {
82
+ "ok" : true
83
+ } ) , {
84
+ status : 200 ,
85
+ headers : {
86
+ 'Content-Type' : 'application/json'
87
+ }
88
+ } ) ;
89
+ } catch ( error ) {
90
+ return new Response ( JSON . stringify ( {
91
+ error : "500 Internal Server Error" ,
92
+ message : error . message
93
+ } ) , {
94
+ status : 500 ,
95
+ headers : {
96
+ 'Content-Type' : 'application/json'
97
+ }
98
+ } ) ;
99
+ }
57
100
}
0 commit comments