Adding /map endpoint to Node API
This commit is contained in:
parent
9a04836215
commit
ac86914d82
@ -2,6 +2,8 @@ import {OK} from "http-status-codes";
|
|||||||
import {URL_ROOM_STARTED} from "../Enum/EnvironmentVariable";
|
import {URL_ROOM_STARTED} from "../Enum/EnvironmentVariable";
|
||||||
import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js";
|
import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js";
|
||||||
import {BaseController} from "./BaseController";
|
import {BaseController} from "./BaseController";
|
||||||
|
import {parse} from "query-string";
|
||||||
|
import {adminApi} from "../Services/AdminApi";
|
||||||
|
|
||||||
//todo: delete this
|
//todo: delete this
|
||||||
export class MapController extends BaseController{
|
export class MapController extends BaseController{
|
||||||
@ -9,26 +11,42 @@ export class MapController extends BaseController{
|
|||||||
constructor(private App : TemplatedApp) {
|
constructor(private App : TemplatedApp) {
|
||||||
super();
|
super();
|
||||||
this.App = App;
|
this.App = App;
|
||||||
this.getStartMap();
|
this.getMapUrl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Returns a map mapping map name to file name of the map
|
// Returns a map mapping map name to file name of the map
|
||||||
getStartMap() {
|
getMapUrl() {
|
||||||
this.App.options("/start-map", (res: HttpResponse, req: HttpRequest) => {
|
this.App.options("/map", (res: HttpResponse, req: HttpRequest) => {
|
||||||
this.addCorsHeaders(res);
|
this.addCorsHeaders(res);
|
||||||
|
|
||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
this.App.get("/start-map", (res: HttpResponse, req: HttpRequest) => {
|
this.App.get("/map", (res: HttpResponse, req: HttpRequest) => {
|
||||||
this.addCorsHeaders(res);
|
this.addCorsHeaders(res);
|
||||||
|
|
||||||
const url = req.getHeader('host').replace('api.', 'maps.') + URL_ROOM_STARTED;
|
const query = parse(req.getQuery());
|
||||||
res.writeStatus("200 OK").end(JSON.stringify({
|
|
||||||
mapUrlStart: url,
|
if (typeof query.organizationSlug !== 'string') {
|
||||||
startInstance: "global"
|
console.error('Expected organizationSlug parameter');
|
||||||
}));
|
res.writeStatus("400 Bad request").end("Expected organizationSlug parameter");
|
||||||
|
}
|
||||||
|
if (typeof query.worldSlug !== 'string') {
|
||||||
|
console.error('Expected worldSlug parameter');
|
||||||
|
res.writeStatus("400 Bad request").end("Expected worldSlug parameter");
|
||||||
|
}
|
||||||
|
if (typeof query.roomSlug !== 'string' && query.roomSlug !== undefined) {
|
||||||
|
console.error('Expected only one roomSlug parameter');
|
||||||
|
res.writeStatus("400 Bad request").end("Expected only one roomSlug parameter");
|
||||||
|
}
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
const mapDetails = await adminApi.fetchMapDetails(query.organizationSlug as string, query.worldSlug as string, query.roomSlug as string|undefined);
|
||||||
|
|
||||||
|
res.writeStatus("200 OK").end(JSON.stringify(mapDetails));
|
||||||
|
})();
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,30 @@ export interface AdminApiData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class AdminApi {
|
class AdminApi {
|
||||||
|
|
||||||
|
async fetchMapDetails(organizationSlug: string, worldSlug: string, roomSlug: string|undefined): Promise<AdminApiData> {
|
||||||
|
if (!ADMIN_API_URL) {
|
||||||
|
return Promise.reject('No admin backoffice set!');
|
||||||
|
}
|
||||||
|
|
||||||
|
const params: { organizationSlug: string, worldSlug: string, mapSlug?: string } = {
|
||||||
|
organizationSlug,
|
||||||
|
worldSlug
|
||||||
|
};
|
||||||
|
|
||||||
|
if (roomSlug) {
|
||||||
|
params.mapSlug = roomSlug;
|
||||||
|
}
|
||||||
|
|
||||||
|
const res = await Axios.get(ADMIN_API_URL+'/api/map',
|
||||||
|
{
|
||||||
|
headers: {"Authorization" : `${ADMIN_API_TOKEN}`},
|
||||||
|
params
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return res.data;
|
||||||
|
}
|
||||||
|
|
||||||
async fetchMemberDataByToken(organizationMemberToken: string): Promise<AdminApiData> {
|
async fetchMemberDataByToken(organizationMemberToken: string): Promise<AdminApiData> {
|
||||||
if (!ADMIN_API_URL) {
|
if (!ADMIN_API_URL) {
|
||||||
return Promise.reject('No admin backoffice set!');
|
return Promise.reject('No admin backoffice set!');
|
||||||
@ -40,4 +63,4 @@ class AdminApi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const adminApi = new AdminApi();
|
export const adminApi = new AdminApi();
|
||||||
|
Loading…
Reference in New Issue
Block a user