Skip to content

Commit 0021fba

Browse files
authored
docs: Publish Client to NPM (#883)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Documentation** * Added a “Publish Client to NPM” guide covering prerequisites, package setup, build/publish commands, install/usage examples, and compatibility notes. * Reorganized the Advanced docs sidebar under “/docs/”: after “Extend Body Parser” the sequence is now Mocking, Publish Client to NPM, RPC JSON Serializer, RPC Protocol, SuperJson; “Validation Errors” moved to the end. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent e9d23ba commit 0021fba

File tree

2 files changed

+116
-4
lines changed

2 files changed

+116
-4
lines changed

apps/content/.vitepress/config.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,14 @@ export default withMermaid(defineConfig({
209209
collapsed: true,
210210
items: [
211211
{ text: 'Building Custom Plugins', link: '/docs/advanced/building-custom-plugins' },
212-
{ text: 'Validation Errors', link: '/docs/advanced/validation-errors' },
213-
{ text: 'RPC Protocol', link: '/docs/advanced/rpc-protocol' },
214-
{ text: 'RPC JSON Serializer', link: '/docs/advanced/rpc-json-serializer' },
215-
{ text: 'Mocking', link: '/docs/advanced/mocking' },
216212
{ text: 'Exceeds the maximum length ...', link: '/docs/advanced/exceeds-the-maximum-length-problem' },
217213
{ text: 'Extend Body Parser', link: '/docs/advanced/extend-body-parser' },
214+
{ text: 'Mocking', link: '/docs/advanced/mocking' },
215+
{ text: 'Publish Client to NPM', link: '/docs/advanced/publish-client-to-npm' },
216+
{ text: 'RPC JSON Serializer', link: '/docs/advanced/rpc-json-serializer' },
217+
{ text: 'RPC Protocol', link: '/docs/advanced/rpc-protocol' },
218218
{ text: 'SuperJson', link: '/docs/advanced/superjson' },
219+
{ text: 'Validation Errors', link: '/docs/advanced/validation-errors' },
219220
],
220221
},
221222
{
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
title: Publish Client to NPM
3+
description: How to publish your oRPC client to NPM for users to consume your APIs as an SDK.
4+
---
5+
6+
# Publish Client to NPM
7+
8+
Publishing your oRPC client to NPM allows users to easily consume your APIs as a software development kit (SDK).
9+
10+
::: info
11+
Before you start, we recommend watching some [publish typescript library to npm tutorials](https://www.youtube.com/results?search_query=publish+typescript+library+to+npm) to get familiar with the process.
12+
:::
13+
14+
## Prerequisites
15+
16+
You must have a project already set up with oRPC. [Contract First](/docs/contract-first/define-contract) is the preferred approach. If you haven't set one up yet, you can clone an [oRPC playground](/docs/playgrounds) and start from there.
17+
18+
::: info
19+
In this guide, we'll use [pnpm](https://pnpm.io/) as the package manager and [tsdown](https://tsdown.dev/) for bundling the package. You can use other package managers and bundlers, but the commands may differ.
20+
:::
21+
22+
## Export & Scripts
23+
24+
First, create a `src/index.ts` file to set up and export your client.
25+
26+
```ts [src/index.ts]
27+
import { createORPCClient } from '@orpc/client'
28+
import { RPCLink } from '@orpc/client/fetch'
29+
import type { ContractRouterClient } from '@orpc/contract'
30+
31+
export function createMyApi(apiKey: string): ContractRouterClient<typeof contract> {
32+
const link = new RPCLink({
33+
url: 'https://example.com/rpc',
34+
headers: {
35+
'x-api-key': apiKey,
36+
}
37+
})
38+
39+
return createORPCClient(link)
40+
}
41+
```
42+
43+
::: info
44+
This example uses [RPCLink](/docs/client/rpc-link) combined with [Contract First](/docs/contract-first/define-contract) to create a client. This is just an example, you can use any other link or client setup that you prefer.
45+
:::
46+
47+
Next, configure your `package.json` with the necessary fields for publishing to NPM.
48+
49+
```json [package.json]
50+
{
51+
"name": "<package-name>", // [!code highlight]
52+
"type": "module",
53+
"version": "0.0.0", // [!code highlight]
54+
"publishConfig": {
55+
"access": "public" // [!code highlight]
56+
},
57+
"exports": {
58+
".": {
59+
"types": "./dist/index.d.ts", // [!code highlight]
60+
"import": "./dist/index.js", // [!code highlight]
61+
"default": "./dist/index.js" // [!code highlight]
62+
}
63+
},
64+
"files": [
65+
"dist" // [!code highlight]
66+
],
67+
"scripts": {
68+
"build": "tsdown --dts src/index.ts", // [!code highlight]
69+
"release": "pnpm publish" // [!code highlight]
70+
},
71+
"dependencies": {
72+
"@orpc/client": "...", // [!code highlight]
73+
"@orpc/contract": "..." // [!code highlight]
74+
// ... other dependencies that `src/index.ts` depends on
75+
},
76+
"devDependencies": {
77+
"tsdown": "latest",
78+
"typescript": "latest"
79+
}
80+
}
81+
```
82+
83+
## Build & Publish
84+
85+
After completing the necessary setup, commit your changes and run the following commands to build and publish your client to NPM:
86+
87+
```bash
88+
pnpm login # if you haven't logged in yet
89+
pnpm run build
90+
pnpm run release
91+
```
92+
93+
## Install & Use
94+
95+
Once your client is published to NPM, you can install it in your project and use it like this:
96+
97+
```bash
98+
pnpm add "<package-name>"
99+
```
100+
101+
```ts [example.ts]
102+
import { createMyApi } from '<package-name>'
103+
104+
const myApi = createMyApi('your-api-key')
105+
106+
const output = await myApi.someMethod('input')
107+
```
108+
109+
::: info
110+
This client includes all oRPC client features, so you can use it with any supported integrations like [Tanstack Query](/docs/integrations/tanstack-query).
111+
:::

0 commit comments

Comments
 (0)