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

feat(client)!: client context mechanism and rewrite #62

Merged
merged 18 commits into from
Dec 29, 2024

Conversation

unnoq
Copy link
Owner

@unnoq unnoq commented Dec 29, 2024

🚨 Breaking Changes Alert 🚨

The @orpc/client package has been rewritten to include powerful new features: Client Context and Dynamic Link mechanisms! 🎉

🧩 Client Context

The Client Context feature allows you to pass additional contextual information (like caching policies) to your client calls.

type ClientContext = { cache?: RequestCache } | undefined;
// If the context is not `undefined`, you must provide it in every call.

const orpcLink = new ORPCLink<ClientContext>({
  url: 'http://localhost:3000/api',
  // headers: Add custom headers here.
  fetch: (input, init, context) => globalThis.fetch(input, {
    ...init,
    cache: context?.cache, // Leverages the client context.
  }),
});

const client = createORPCClient<typeof router, ClientContext>(orpcLink);

client.getting({ name: 'unnoq' }, { context: { cache: 'force-cache' } });

Note: This works seamlessly with Vue Query and React Query.


🔄 Dynamic Link

With the Dynamic Link mechanism, you can define custom logic to dynamically choose between different links based on the request's context, path, or input.

const orpcLink1 = new ORPCLink({
  url: 'http://localhost:3000/api',
  // headers: Add custom headers here.
});

const orpcLink2 = new ORPCLink({
  url: 'http://localhost:8000/api',
  // headers: Add custom headers here.
});

const dynamicLink = new DynamicLink((path, input, options) => { 
  // Custom logic to select the appropriate link.
  if (path.includes('post')) {
    return orpcLink1;
  }

  return orpcLink2;
});

Copy link

cloudflare-workers-and-pages bot commented Dec 29, 2024

Deploying orpc with  Cloudflare Pages  Cloudflare Pages

Latest commit: e7f1e1a
Status:⚡️  Build in progress...

View logs

Copy link

pkg-pr-new bot commented Dec 29, 2024

Open in Stackblitz

More templates

@orpc/client

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

@orpc/next

npm i https://pkg.pr.new/@orpc/next@62

@orpc/contract

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

@orpc/openapi

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

@orpc/react-query

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

@orpc/react

npm i https://pkg.pr.new/@orpc/react@62

@orpc/server

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

@orpc/shared

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

@orpc/vue-query

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

@orpc/zod

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

commit: e7f1e1a

@unnoq unnoq merged commit d7b5662 into main Dec 29, 2024
6 of 7 checks passed
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