Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to a monorepo setup #2

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changesets

Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets)

We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
11 changes: 11 additions & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [],
"access": "restricted",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": ["packages/tsconfig", "apps"]
}
5 changes: 5 additions & 0 deletions .changeset/quick-candles-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@webrpc/react-query-adapter": major
---

First version
41 changes: 41 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CI
on:
pull_request:
types: [opened, synchronize]

jobs:
setup:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3
with:
fetch-depth: 2

- name: Read .nvmrc
run: echo ::set-output name=NODE_VERSION::$(cat .nvmrc)
id: nvm

- name: Use Node.js ${{ steps.nvm.outputs.NODE_VERSION }}
uses: actions/setup-node@v3
with:
node-version: ${{ steps.nvm.outputs.NODE_VERSION }}

- name: Set up pnpm
uses: pnpm/action-setup@v2
with:
version: 7

- name: Set up pnpm cache
uses: actions/setup-node@v3
with:
cache: "pnpm"

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Lint and build adapter
run: pnpm run lint --filter @webrpc/react-query-adapter && pnpm run build --filter @webrpc/react-query-adapter

- name: Lint and run react example app
run: pnpm install --filter react-example --frozen-lockfile && pnpm run lint --filter react-example && pnpm run build --filter react-example
29 changes: 29 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Publish
on:
push:
branches:
- "main"

concurrency: ${{ github.workflow }}-${{ github.ref }}

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
with:
version: 7
- uses: actions/setup-node@v3
with:
node-version: 16.x
cache: "pnpm"

- run: pnpm install --frozen-lockfile
- name: Create Release Pull Request or Publish
id: changesets
uses: changesets/action@v1
with:
publish: pnpm run build
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
33 changes: 32 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
node_modules
dist
.pnp
.pnp.js

# testing
coverage

# next.js
.next/
out/
build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local

# turbo
.turbo
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16
5 changes: 0 additions & 5 deletions .prettierrc

This file was deleted.

28 changes: 10 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
@webrpc/react-query -- a react-query adapter for webrpc
=======================================================
# @webrpc/react-query -- a react-query adapter for webrpc

Enjoy the RPC code-gen ergonomics + type-safety benefits of webrpc, with the comfort
of react-query from your React apps :)

of react-query from your React apps :)

## Install

Expand All @@ -12,7 +10,6 @@ From your webapps: `npm install @webrpc/react-query`
As well, make sure you install `@tanstack/react-query` and `react` in your app, which are
peer-dependencies of this library.


## Example

Please see a full example project with both server and client [here](./example).
Expand All @@ -26,7 +23,6 @@ To run the example:
5. Start the webapp -- in another terminal: `make run-webapp`
6. Open the webapp at http://localhost:4444 and open your browser console


## How to use

First, you'll need a webrpc schema definition either in JSON or RIDL.
Expand All @@ -35,45 +31,41 @@ Then create an instance of your RPC client and pass it as an argument to the Web
It should look something like this:

```ts
import { Example } from './client.gen'
import { WebRpcQueryClient } from '@webrpc/react-query'
import { Example } from "./client.gen";
import { WebRpcQueryClient } from "@webrpc/react-query";

const rpc = new Example('http://localhost:4242', fetch)
const client = new WebRpcQueryClient(rpc)
const rpc = new Example("http://localhost:4242", fetch);
const client = new WebRpcQueryClient(rpc);
```

Import `client` where you need to make your API calls and let type inference guide your way!


## Differentiating queries and mutations

If you want to make the distinction between a query and a mutation even clearer, you can define custom _query prefixes_.
You do so by adding a generic type to your `WebRpcClient` instance.

```ts
import { Example } from './client.gen'
import { WebRpcQueryClient } from '@webrpc/react-query'
import { Example } from "./client.gen";
import { WebRpcQueryClient } from "@webrpc/react-query";

const rpc = new Example('http://localhost:4242', fetch)
const client = new WebRpcQueryClient<typeof rpc, ['get', 'list']>(rpc)
const rpc = new Example("http://localhost:4242", fetch);
const client = new WebRpcQueryClient<typeof rpc, ["get", "list"]>(rpc);
```

With this configuration, you can only use `client.useQuery` hook with paths that start with either `'get'` or `'list'`.

Any other method from your contract will be considered a _mutation_. If you choose not to provide _query prefixes_, you will be able to call both `client.useQuery` and `client.useMutation` with any path from your contract.


## Local dev

You can update `example/webapp/package.json` package to `"@webrpc/react-query": "workspace:../../"`
which will use the build from the local repo.


## Credits

Thank you to @vojoup from the github.com/golang-cz team for the idea and original implementation of this library :)


## LICENSE

Licensed under [MIT License](./LICENSE)
24 changes: 24 additions & 0 deletions apps/example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
4 changes: 4 additions & 0 deletions apps/example/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tabWidth": 2,
"useTabs": false
}
13 changes: 13 additions & 0 deletions apps/example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>webrpc react-query adapter example</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
27 changes: 27 additions & 0 deletions apps/example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "react-example",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"lint": "tsc",
"ts": "tsc --noEmit"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"@tanstack/react-query-devtools": "^4.24.4",
"@webrpc/react-query-adapter": "workspace:*"
},
"devDependencies": {
"@types/react": "^18.0.27",
"@types/react-dom": "^18.0.10",
"@vitejs/plugin-react": "^3.1.0",
"typescript": "^4.9.3",
"vite": "^4.1.0",
"tsconfig": "workspace:*"
}
}
1 change: 1 addition & 0 deletions apps/example/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions apps/example/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}

@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}
Loading