# dts-for-context-bridge

> dts-cb generate .d.ts file for contextBridge.exposeInMainWorld

Latest version **0.7.1** (published 2021-12-21) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install dts-for-context-bridge
pnpm add dts-for-context-bridge
yarn add dts-for-context-bridge
bun add dts-for-context-bridge
```

Provides the command `dts-cb`.

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.7.1 |
| Published | 2021-12-21 |
| First published | 2021-11-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 11.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Alex Kozack |
| Maintainers | kozack |
| Keywords | electron, typescript |

## Links

- npm: https://www.npmjs.com/package/dts-for-context-bridge
- Repository: https://github.com/cawa-93/dts-for-context-bridge
- Homepage: https://github.com/cawa-93/dts-for-context-bridge#readme
- Issues: https://github.com/cawa-93/dts-for-context-bridge/issues
- npm.io page: https://npm.io/package/dts-for-context-bridge

## Dependencies (2)

- [coa](https://npm.io/package/coa.md) 2.0.2
- [ts-morph](https://npm.io/package/ts-morph.md) 13.0.2

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.7.1 (latest) — 2021-12-21
- 0.7.0 — 2021-12-15
- 0.6.3 — 2021-12-15
- 0.6.2 — 2021-11-29
- 0.6.1 — 2021-11-29
- 0.6.0 — 2021-11-28
- 0.5.0 — 2021-11-28
- 0.4.0 — 2021-11-27
- 0.3.0 — 2021-11-27
- 0.2.0 — 2021-11-26
- 0.1.0 — 2021-11-23
- 0.0.1 — 2021-11-23

## README

# dts-cb

This utility is designed to
analyze [context bridges in Electron application](https://www.electronjs.org/ru/docs/latest/api/context-bridge) and
create `.d.ts` files.

## Problem

In one part of your application, you describe some API and expose it into the global context of the renderer.

```js
contextBridge.exposeInMainWorld(
  'electron',
  {
    doThing: () => ipcRenderer.send('do-a-thing')
  }
)
```

However, for the TypeScript in the renderer knows nothing about the new global api.

```js
// Property 'electron' does not exist on type 'Window & typeof globalThis'.
window.electron.doThing()
```

Therefore, you must somehow register these apps.

## Decision

This utility scans the source code and finds all `exposeInMainWorld` calls. Then it generates a `.d.ts` file with an
interface that can later be connected in the renderer.

```js
contextBridge.exposeInMainWorld(
  'electron',
  {
    doThing: () => ipcRenderer.send('do-a-thing')
  }
)
```

```ts
// generated.d.ts
interface Window {
    readonly electron: {doThing: () => void}
}
```

**See more [examples](/examples).**

## Installation

```bash
# global
npm i -g dts-for-context-bridge

# for project 
npm i dts-for-context-bridge
```

## Usage

```bash
# You can use glob pattern as input
dts-cb --input="preload/src/**/*.ts" --output="preload/exposed.d.ts"

# You can use tsconfig.json as input
dts-cb --input="preload/tsconfig.json" --output="preload/exposed.d.ts"
```

See all options by

```bash
dts-cb --help
```

### Programmatic usage

```js
import {generate} from 'dts-for-context-bridge'

generate({
    input: 'preload/src/**/*.ts', // Or path to tsconfig.json
    output: 'preload/exposed.d.ts',
})
```

## Is it stable?

The utility relies on AST analysis and currently does not cover all cases. **Feel free to create PR.**

---
_Source: https://npm.io/package/dts-for-context-bridge · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
