Skip to content

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

Merged
dinwwwh merged 2 commits intomainfrom
feat/contract-client/condition-client-context
Feb 10, 2025
Merged

feat(contract, client)!: enforce stricter client context#141
dinwwwh merged 2 commits intomainfrom
feat/contract-client/condition-client-context

Conversation

@dinwwwh
Copy link
Copy Markdown
Member

@dinwwwh dinwwwh 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'
  },
})

@vercel
Copy link
Copy Markdown

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

@pkg-pr-new
Copy link
Copy Markdown

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

@dinwwwh dinwwwh force-pushed the feat/contract-client/condition-client-context branch from d09ad22 to 70fb9f1 Compare February 10, 2025 06:02
@dinwwwh dinwwwh merged commit b4e6d3a into main Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant