Merge context type declarations for wrappers
- Dominant language
- JavaScript
- Stars
- 5
- Forks
- 7
- Avg merge
- 10h 16m
- Merged PRs (30d)
- 4
Description
Right now using universal in Typescript means you need to manually set `main()`'s parameter types. This is expected, not much we can do about it, but the pain point is when you're using wrappers that extend the context.
This works great:
```ts
import { UniversalContext, Request } from '@adobe/helix-universal';
export function main (req: Request, ctx: UniversalContext) {
...
}
```
This doesn't:
```ts
import { UniversalContext, Request } from '@adobe/helix-universal';
import wrap from '@adobe/helix-shared-wrap';
import { logger } from '@adobe/helix-universal-logger';
function _main(req: Request, ctx: UniversalContext) {
ctx.log.info("woo"); // Type error, ctx.log doesn't exist
}
export const main = wrap(_main).with(logger);
```
Ignoring the last line which has its own type errors, the context doesn't "know" about the logger wrapper. Workaround could be to import each context and make a custom interface merging them, but that would be repeated every time it's used.
The `context` APIs seem pretty useful to expose, so I'm proposing a namespace that would be exposed, allowing wrapper functions to extend the context as they need.
Assuming `@adobe/helix-universal-logger` adds an extension to the namespace like this:
```ts
// index.d.ts
declare module '@adobe/helix-universal' {
namespace HelixUniversal {
export interface UniversalContext {
info: (...msgs: any[]) => void;
// ...
}
}
}
```
TS clients could then use:
```ts
import { HelixUniversal, Request } from '@adobe/helix-universal';
import wrap from '@adobe/helix-shared-wrap';
import { logger } from '@adobe/helix-universal-logger';
async function _main(req: Request, ctx: HelixUniversal.UniversalContext) {
ctx.log.info("woo"); // 👍
}
export const main = wrap(_main).with(logger);
```
wdyt @tripodsan @trieloff ?
Contributor guide
Research direction
Review the existing TypeScript declarations for UniversalContext and the wrapper type definitions in the mentioned packages. Determine how a HelixUniversal namespace could support context augmentation, then verify that a logger extension exposes its added context members to TypeScript clients without requiring repeated custom interfaces.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100