Skip to content
New issue

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

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

Already on GitHub? # to your account

feat(contract, client)!: enforce stricter client context #141

Merged
merged 2 commits into from
Feb 10, 2025

Conversation

unnoq
Copy link
Owner

@unnoq unnoq commented Feb 10, 2025

Previous: Client Context can be anything, and use undefined to indicate optional client context
Now: Client Context must satisfy Record<string, any> where optional client context can automatically detect if all field is optional.

type ClientContext = { cache?: RequestCache }
 
const rpcLink = new RPCLink<ClientContext>({
  url: 'http://localhost:3000/rpc',
  // headers: provide additional headers
  fetch: (input, init, context) => globalThis.fetch(input, {
    ...init,
    cache: context?.cache,
  }),
  method: (path, input, context) => {
    // if input contain file, and you return GET, oRPC will change it to POST automatically
 
    if (context?.cache) {
      return 'GET'
    }
 
    // or base on the path
    if (['get', 'find', 'list', 'search'].includes(path.at(-1)!)) {
      return 'GET'
    }
 
    return 'POST'
  },
})

Copy link

vercel bot commented Feb 10, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
orpc ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 10, 2025 6:03am

Copy link

pkg-pr-new bot commented Feb 10, 2025

Open in Stackblitz

More templates

@orpc/client

npm i https://pkg.pr.new/@orpc/client@141

@orpc/openapi

npm i https://pkg.pr.new/@orpc/openapi@141

@orpc/react-query

npm i https://pkg.pr.new/@orpc/react-query@141

@orpc/contract

npm i https://pkg.pr.new/@orpc/contract@141

@orpc/server

npm i https://pkg.pr.new/@orpc/server@141

@orpc/shared

npm i https://pkg.pr.new/@orpc/shared@141

@orpc/vue-colada

npm i https://pkg.pr.new/@orpc/vue-colada@141

@orpc/vue-query

npm i https://pkg.pr.new/@orpc/vue-query@141

@orpc/zod

npm i https://pkg.pr.new/@orpc/zod@141

commit: 70fb9f1

@unnoq unnoq force-pushed the feat/contract-client/condition-client-context branch from d09ad22 to 70fb9f1 Compare February 10, 2025 06:02
@unnoq unnoq merged commit b4e6d3a into main Feb 10, 2025
6 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant