|
| 1 | +# twoslash-remote |
| 2 | + |
| 3 | +[![npm version][npm-version-src]][npm-version-href] |
| 4 | +[![npm downloads][npm-downloads-src]][npm-downloads-href] |
| 5 | + |
| 6 | +Run [Twoslash](https://github.com/twoslashes/twoslash) by delegating resolution to a remote HTTP API. Keep the heavy dependencies (TypeScript, type acquisition, virtual file system, Vue language server, ...) on a server and ship only a tiny client to the browser, build pipeline, or edge function. |
| 7 | + |
| 8 | +The package exposes two entry points: |
| 9 | + |
| 10 | +- `twoslash-remote` — a client that sends a code snippet + metadata to your endpoint and resolves the [`TwoslashGenericResult`](https://github.com/twoslashes/twoslash/blob/main/packages/twoslash-protocol/src/types/returns.ts) you get back. |
| 11 | +- `twoslash-remote/server` — web-standard `Request → Response` helpers to implement the contract on the server side, with any backend that returns a `TwoslashGenericResult` (`twoslash`, `twoslash-eslint`, `twoslash-vue`, or your own). |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +### Client |
| 16 | + |
| 17 | +```ts |
| 18 | +import { createTwoslashFromRemote } from 'twoslash-remote' |
| 19 | + |
| 20 | +const twoslash = createTwoslashFromRemote({ |
| 21 | + endpoint: 'https://my-api.example.com/twoslash', |
| 22 | +}) |
| 23 | + |
| 24 | +const result = await twoslash.run( |
| 25 | + ` |
| 26 | + const count = 1 |
| 27 | + // ^? |
| 28 | + `, |
| 29 | + 'ts', |
| 30 | + // Optional: serializable twoslasher options forwarded to the backend |
| 31 | + { compilerOptions: { strict: true } }, |
| 32 | + // Optional: transport-level options (meta, headers, signal) |
| 33 | + { meta: { tenant: 'docs-site' } }, |
| 34 | +) |
| 35 | + |
| 36 | +console.log(result) // { code: '...', nodes: [...] } |
| 37 | +``` |
| 38 | + |
| 39 | +The third argument is forwarded to the server backend as `options` in the request body. The fourth argument is transport-only (`meta`, `headers`, `signal`). |
| 40 | + |
| 41 | +### Server |
| 42 | + |
| 43 | +`createTwoslashRequestHandler` returns a web-standard `(req: Request) => Promise<Response>`. It runs in Node ≥ 18, Bun, Deno, Cloudflare Workers, Vercel/Netlify edge functions, and any framework that speaks the fetch API. |
| 44 | + |
| 45 | +```ts |
| 46 | +import { createTwoslasher } from 'twoslash' |
| 47 | +import { createTwoslashRequestHandler } from 'twoslash-remote/server' |
| 48 | + |
| 49 | +const twoslasher = createTwoslasher() |
| 50 | + |
| 51 | +export default createTwoslashRequestHandler({ |
| 52 | + twoslasher, |
| 53 | + cors: true, |
| 54 | +}) |
| 55 | +``` |
| 56 | + |
| 57 | +Plug in any backend that returns a `TwoslashGenericResult` — `twoslash-eslint`, `twoslash-vue`, or a custom wrapper: |
| 58 | + |
| 59 | +```ts |
| 60 | +import { createTwoslasher as createESLintTwoslasher } from 'twoslash-eslint' |
| 61 | +import { createTwoslashRequestHandler } from 'twoslash-remote/server' |
| 62 | + |
| 63 | +const twoslasher = createESLintTwoslasher({ eslintConfig: [/* ... */] }) |
| 64 | + |
| 65 | +export default createTwoslashRequestHandler({ twoslasher }) |
| 66 | +``` |
| 67 | + |
| 68 | +Lower-level building blocks (`parseTwoslashRequest`, `serializeTwoslashError`) are also exported for users who want to integrate into a framework manually. |
| 69 | + |
| 70 | +## License |
| 71 | + |
| 72 | +[MIT](./LICENSE) License © 2024-PRESENT [Anthony Fu](https://github.com/antfu) |
| 73 | + |
| 74 | +<!-- Badges --> |
| 75 | + |
| 76 | +[npm-version-src]: https://img.shields.io/npm/v/twoslash-remote?style=flat&colorA=161514&colorB=EAB836 |
| 77 | +[npm-version-href]: https://npmjs.com/package/twoslash-remote |
| 78 | +[npm-downloads-src]: https://img.shields.io/npm/dm/twoslash-remote?style=flat&colorA=161514&colorB=E66041 |
| 79 | +[npm-downloads-href]: https://npmjs.com/package/twoslash-remote |
0 commit comments