elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
10.58k stars 225 forks source link

[Feature Request] Standardized way of renaming third party plugin-scoped stuff #83

Closed bogeychan closed 1 year ago

bogeychan commented 1 year ago

@LoganArnett mentioned in a PR the idea of renaming derived or decorated properties of plugins when they are included in an actual application. This could prevent naming collisions in the future and give users the freedom to name things as they wish.

It would be nice to do this standardized via Elysia itself, such as:

import { Elysia } from 'elysia';

// this would be a plugin provided by a third party
const myPlugin = (app: Elysia) => app.decorate('myProperty', 42);

new Elysia()
  .use(
    myPlugin
      .mapContext({
        myProperty: 'prop' // rename derived or decorated properties one by one
      })
      .prefixAll('my-') // and/or apply a prefix to all
  )
  .get('/', (ctx) => ctx['my-prop'])
  .listen(8080);

In the logger plugin it is currently implemented as follows:

const app = new Elysia()
  .use(
    logger({
      contextKeyName: 'myLogger'
    })
  )
  .get('/', (ctx) => {
    // property "myLogger" is available instead of "log"
    ctx.myLogger.info(ctx.request, 'Request');

    return 'Hello World';
  })
  .listen(8080);
SaltyAom commented 1 year ago

Fixed in #103 with Definitions Remap

https://elysiajs.com/patterns/remapping.html#remapping