|
| 1 | +# Puter Extensions |
| 2 | + |
| 3 | +## Quickstart |
| 4 | + |
| 5 | +Create and edit this file: `mods/mods_enabled/hello-puter.js` |
| 6 | + |
| 7 | +```javascript |
| 8 | +// You can get definitions exposed by Puter via `use` |
| 9 | +const { UserActorType, AppUnderUserActorType } = use.core; |
| 10 | + |
| 11 | +// Endpoints can be registered directly on an extension |
| 12 | +extension.get('/hello-puter', (req, res) => { |
| 13 | + const actor = req.actor; |
| 14 | + |
| 15 | + |
| 16 | + // Make a string "who" which says: |
| 17 | + // "<username>", or: |
| 18 | + // "<app> acting on behalf of <username>" |
| 19 | + let who = 'unknown'; |
| 20 | + if ( actor.type instanceof UserActorType ) { |
| 21 | + who = actor.type.user.username; |
| 22 | + } |
| 23 | + if ( actor.type instanceof AppUnderUserActorType ) { |
| 24 | + who = actor.type.app.name |
| 25 | + + ' on behalf of ' |
| 26 | + + actor.type.user.username; |
| 27 | + } |
| 28 | + |
| 29 | + res.send(`Hello, ${who}!`); |
| 30 | +}); |
| 31 | + |
| 32 | +// Extensions can listen to events and manipulate Puter's behavior |
| 33 | +extension.on('core.email.validate', event => { |
| 34 | + if ( event.email.includes('evil') ) { |
| 35 | + event.allow = false; |
| 36 | + } |
| 37 | +}); |
| 38 | +``` |
| 39 | + |
| 40 | +### Scope of `extension` and `use` |
| 41 | + |
| 42 | +It is important to know that the `extension` global is temporary and does not |
| 43 | +exist after your extension is loaded. If you wish to access the extension |
| 44 | +object within a callback you will need to first bind it to a variable in |
| 45 | +your extension's scope. |
| 46 | + |
| 47 | +```javascript |
| 48 | +const ext = extension; |
| 49 | +extension.on('some-event', () => { |
| 50 | + // This would throw an error |
| 51 | + // extension.something(); |
| 52 | + |
| 53 | + // This works |
| 54 | + ext.example(); |
| 55 | +}) |
| 56 | +``` |
| 57 | + |
| 58 | +The same is true for `use`. Calls to `use` should happen at the top of |
| 59 | +the file, just like imports in ES6. |
| 60 | + |
| 61 | +## Database Access |
| 62 | + |
| 63 | +A database access object is provided to the extension via `extension.db`. |
| 64 | +You **must** scope `extension` to another variable (`ext` in this example) |
| 65 | +in order to access `db` from callbacks. |
| 66 | + |
| 67 | +```javascript |
| 68 | +const ext = extension; |
| 69 | + |
| 70 | +extension.get('/user-count', { noauth: true }, (req, res) => { |
| 71 | + const [count] = await ext.db.read( |
| 72 | + 'SELECT COUNT(*) as c FROM `user`' |
| 73 | + ); |
| 74 | +}); |
| 75 | +``` |
| 76 | + |
| 77 | +The database access object has the following methods: |
| 78 | +- `read(query, params)` - read from the database using a prepared statement. If read-replicas are enabled, this will use a replica. |
| 79 | +- `write(query, params)` - write to the database using a prepared statement. If read-replicas are enabled, this will write to the primary. |
| 80 | +- `pread(query, params)` - read from the database using a prepared statement. If read-replicas are enabled, this will read from the primary. |
| 81 | +- `requireRead(query, params)` - read from the database using a prepared statement. If read-replicas are enabled, this will try reading from the replica first. If there are no results, a second attempt will be made on the primary. |
| 82 | + |
| 83 | +## Events |
| 84 | + |
| 85 | +See [events.md](./events.md) |
| 86 | + |
| 87 | +## Definitions |
| 88 | + |
| 89 | +See [definitions.md](./definitions.md) |
0 commit comments