Files
puter/extensions/hellodriver/hellodriver.js
T
KernelDeimos ec8111cc2f dev(extensions): improved configuration support
Extensions now read configuration objects under the "extensions" block
in the backend configuration. Additionally, a `config.json` for default
values directly in the extension's directory is now supported.
2025-10-01 15:29:13 -04:00

70 lines
2.5 KiB
JavaScript

/**
* Here we create an interface called 'hello-world'. This interface
* specifies that any implementation of 'hello-world' should implement
* a method called `greet`. The greet method has a couple of optional
* parameters including `subject` and `locale`. The `locale` parameter
* is not implemented by the driver implementation in the proceeding
* definition, showing how driver implementations don't always need
* to support optional features.
*
* subject: the person to greet
* locale: a standard locale string (ex: en_US.UTF-8)
*/
extension.on('create.interfaces', event => {
event.createInterface('hello-world', {
description: 'Provides methods for generating greetings',
methods: {
greet: {
description: 'Returns a greeting',
parameters: {
subject: {
type: 'string',
optional: true,
},
locale: {
type: 'string',
optional: true,
},
},
},
},
});
});
/**
* Here we register an implementation of the `hello-world` driver
* interface. This implementation is called "no-frills" which is
* the most basic reasonable implementation of the interface. The
* default return value is "Hello, World!", but if subject is
* provided it will be "Hello, <subject>!".
*
* This implementation can be called from puter.js like this:
*
* await puter.call('hello-world', 'no-frills', 'greet', { subject: 'Dave' });
*
* If you get an authorization error it's because the user you're
* logged in as does not have permission to invoke the `no-frills`
* implementation of `hello-world`. Users must be granted the following
* permission to access this driver:
*
* service:no-frills:ii:hello-world
*
* The value of `<subject>` can be one of many "special" values
* to demonstrate capabilities of drivers or extensions, including:
* - `%fail%`: simulate an error response from a driver
* - `%config%`: return the effective configuration object
*/
extension.on('create.drivers', event => {
event.createDriver('hello-world', 'no-frills', {
greet ({ subject }) {
if ( subject === '%fail%' ) {
throw new Error('failing on purpose');
}
if ( subject === '%config%' ) {
return JSON.stringify(config ?? null);
}
return `Hello, ${subject ?? 'World'}!`;
},
});
});