@@ -10,6 +10,9 @@ router.get('/', function(req, res, next) {
10
10
router . get ( '/record/:ownerId/:recordId' , ( req , res , next ) => handle ( "world" , req , res , next ) ) ;
11
11
router . get ( '/session/:sessionId' , ( req , res , next ) => handle ( "session" , req , res , next ) ) ;
12
12
13
+ router . get ( '/record/:ownerId/:recordId/json' , ( req , res , next ) => handleJson ( "world" , req , res , next ) ) ;
14
+ router . get ( '/session/:sessionId/json' , ( req , res , next ) => handleJson ( "session" , req , res , next ) ) ;
15
+
13
16
function getUrl ( type , req ) {
14
17
switch ( type ) {
15
18
case "world" :
@@ -45,15 +48,43 @@ async function handle(type, req, res, next) {
45
48
}
46
49
47
50
json = preProcess ( json ) ;
51
+ json . urlPath = req . getUrl ( ) ;
48
52
49
53
res . status ( 200 ) . render ( type , json ) ;
50
54
}
51
55
56
+ async function handleJson ( type , req , res , next ) {
57
+ var apiResponse = await fetch ( getUrl ( type , req ) ) ;
58
+ if ( ! apiResponse . ok ) {
59
+ res . status ( apiResponse . status ) ;
60
+ return next ( ) ;
61
+ }
62
+
63
+ var json = await apiResponse . json ( ) ;
64
+
65
+ if ( type === "world" && json . recordType !== "world" ) {
66
+ res . status ( 400 ) ;
67
+ return next ( ) ;
68
+ }
69
+
70
+ json = preProcess ( json ) ;
71
+
72
+ res . json ( {
73
+ title : json . name ,
74
+ author_name : json . name ,
75
+ author_url : req . getUrl ( ) . replace ( "/json" , "" ) ,
76
+ provider_name : "Resonite" ,
77
+ provider_url : "https://resonite.com"
78
+ } ) ;
79
+
80
+ }
81
+
52
82
function preProcessName ( name ) {
53
83
const start = / < c o l o r = " ? ( .+ ?) " ? > / gi;
54
84
const end = / < \/ c o l o r > / gi
55
85
return name . replace ( start , "<span style=\"color: $1 ;\">" ) . replace ( end , "</span>" ) ;
56
86
}
87
+
57
88
function preProcess ( json ) {
58
89
json . name = preProcessName ( json . name ) ;
59
90
0 commit comments