forked from Lumieducation/H5P-Nodejs-library
-
Notifications
You must be signed in to change notification settings - Fork 1
/
H5PAjaxExpressRouter.ts
134 lines (121 loc) · 4.65 KB
/
H5PAjaxExpressRouter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Router, static as ExpressStatic } from 'express';
import { H5PEditor } from '../..';
import {
errorHandler,
undefinedOrTrue,
catchAndPassOnErrors
} from '../expressErrorHandler';
import H5PAjaxExpressController from './H5PAjaxExpressController';
import H5PAjaxExpressRouterOptions from './H5PAjaxExpressRouterOptions';
/**
* This router implements all Ajax calls necessary for the H5P (editor) client to work.
* Use it like this: server.use('/h5p', H5P.adapters.express(h5pEditor, path.resolve('h5p/core'), path.resolve('h5p/editor')));
* If you only want certain routes, you can specify this in the options parameter.
* @param h5pEditor the editor object
* @param h5pCorePath the path on the local disk at which the core files (of the player) can be found
* @param h5pEditorLibraryPath the path on the local disk at which the core files of the editor can be found
* @param routeOptions sets which routes you want and how to handle errors
* @param languageOverride the language to use when returning errors.
* Only has an effect if you use the i18next http middleware, as it relies on
* req.i18n.changeLanguage to be present. Defaults to auto, which means the
* a language detector must have detected language and req.t translated to the
* detected language.
*/
export default function (
h5pEditor: H5PEditor,
h5pCorePath: string,
h5pEditorLibraryPath: string,
routeOptions: H5PAjaxExpressRouterOptions = new H5PAjaxExpressRouterOptions(),
languageOverride: string | 'auto' = 'auto'
): Router {
const router = Router();
const h5pController = new H5PAjaxExpressController(h5pEditor);
// get library file
if (undefinedOrTrue(routeOptions.routeGetLibraryFile)) {
router.get(
`${h5pEditor.config.librariesUrl}/:uberName/:file(*)`,
catchAndPassOnErrors(
h5pController.getLibraryFile,
routeOptions.handleErrors
)
);
}
// get content file
if (undefinedOrTrue(routeOptions.routeGetContentFile)) {
router.get(
`${h5pEditor.config.contentFilesUrl}/:id/:file(*)`,
catchAndPassOnErrors(
h5pController.getContentFile,
routeOptions.handleErrors
)
);
}
// get temporary content file
if (undefinedOrTrue(routeOptions.routeGetTemporaryContentFile)) {
router.get(
`${h5pEditor.config.temporaryFilesUrl}/:file(*)`,
catchAndPassOnErrors(
h5pController.getTemporaryContentFile,
routeOptions.handleErrors
)
);
}
// get parameters (= content.json) of content
if (undefinedOrTrue(routeOptions.routeGetParameters)) {
router.get(
`${h5pEditor.config.paramsUrl}/:contentId`,
catchAndPassOnErrors(
h5pController.getContentParameters,
routeOptions.handleErrors
)
);
}
// get various things through the Ajax endpoint
if (undefinedOrTrue(routeOptions.routeGetAjax)) {
router.get(
h5pEditor.config.ajaxUrl,
catchAndPassOnErrors(
h5pController.getAjax,
routeOptions.handleErrors
)
);
}
// post various things through the Ajax endpoint
// Don't be confused by the fact that many of the requests dealt with here are not
// really POST requests, but look more like GET requests. This is simply how the H5P
// client works and we can't change it.
if (undefinedOrTrue(routeOptions.routePostAjax)) {
router.post(
h5pEditor.config.ajaxUrl,
catchAndPassOnErrors(
h5pController.postAjax,
routeOptions.handleErrors
)
);
}
// serve core files (= JavaScript + CSS from h5p-php-library)
if (undefinedOrTrue(routeOptions.routeCoreFiles)) {
router.use(h5pEditor.config.coreUrl, ExpressStatic(h5pCorePath));
}
// serve editor core files (= JavaScript + CSS from h5p-editor-php-library)
if (undefinedOrTrue(routeOptions.routeEditorCoreFiles)) {
router.use(
h5pEditor.config.editorLibraryUrl,
ExpressStatic(h5pEditorLibraryPath)
);
}
// serve download links
if (undefinedOrTrue(routeOptions.routeGetDownload)) {
router.get(
`${h5pEditor.config.downloadUrl}/:contentId`,
catchAndPassOnErrors(
h5pController.getDownload,
routeOptions.handleErrors
)
);
}
if (undefinedOrTrue(routeOptions.handleErrors)) {
router.use(errorHandler(languageOverride));
}
return router;
}