|
@@ -3,6 +3,10 @@ const EmitterFeature = require("@heyputer/putility/src/features/EmitterFeature")
|
|
|
const { Context } = require("./util/context");
|
|
|
const { ExtensionServiceState } = require("./ExtensionService");
|
|
|
|
|
|
+/**
|
|
|
+ * This class creates the `extension` global that is seem by Puter backend
|
|
|
+ * extensions.
|
|
|
+ */
|
|
|
class Extension extends AdvancedBase {
|
|
|
static FEATURES = [
|
|
|
EmitterFeature({
|
|
@@ -24,6 +28,9 @@ class Extension extends AdvancedBase {
|
|
|
console.log('Example method called by an extension.');
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * This will get a database instance from the default service.
|
|
|
+ */
|
|
|
get db () {
|
|
|
const db = this.service.values.get('db');
|
|
|
if ( ! db ) {
|
|
@@ -35,6 +42,12 @@ class Extension extends AdvancedBase {
|
|
|
return db;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * This will create a GET endpoint on the default service.
|
|
|
+ * @param {*} path - route for the endpoint
|
|
|
+ * @param {*} handler - function to handle the endpoint
|
|
|
+ * @param {*} options - options like noauth (bool) and mw (array)
|
|
|
+ */
|
|
|
get (path, handler, options) {
|
|
|
// this extension will have a default service
|
|
|
this.ensure_service_();
|
|
@@ -51,6 +64,12 @@ class Extension extends AdvancedBase {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * This will create a POST endpoint on the default service.
|
|
|
+ * @param {*} path - route for the endpoint
|
|
|
+ * @param {*} handler - function to handle the endpoint
|
|
|
+ * @param {*} options - options like noauth (bool) and mw (array)
|
|
|
+ */
|
|
|
post (path, handler, options) {
|
|
|
// this extension will have a default service
|
|
|
this.ensure_service_();
|
|
@@ -67,6 +86,13 @@ class Extension extends AdvancedBase {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * This method will create the "default service" for an extension.
|
|
|
+ * This is specifically for Puter extensions that do not define their
|
|
|
+ * own service classes.
|
|
|
+ *
|
|
|
+ * @returns {void}
|
|
|
+ */
|
|
|
ensure_service_ () {
|
|
|
if ( this.service ) {
|
|
|
return;
|