@cuple/client
TypeScript icon, indicating that this package has built-in type declarations

1.0.8 • Public • Published

@cuple/client

Client side for @cuple RPC.

Installation

npm i @cuple/client

NOTE: @cuple/client and @cuple/server versions have to match

Example

import { createClient } from "@cuple/client";
import type { routes } from "../backend/src/app";

const client = createClient<typeof routes>({
  path: "http://localhost:8080/rpc",
});

async function changePassword() {
  const response = await authedClient.setUserPassword.post({
    body: {
      oldPassword: "something",
      password1: "newPass",
      password2: "newPass",
    },
  });

  console.log(response)
}

changePassword();

API

Feature 1: Calling procedures

// Server:
const routes = {
    foo: builder.get(/**/),
    bar: builder.post(/**/),
    baz: builder.put(/**/),
    patch: builder.patch(/**/),
    delete: builder.delete(/**/)
}

// Client:
await client.foo.get({});
await client.bar.post({});
await client.baz.put({});
await client.patch.patch({});
await client.delete.delete({});

Parameters:

body: The request body
headers: HTTP headers
params: URL parameters (e.g.: in case of /post/:id, it can be { id: "1" })
query: query parameters (e.g.: in case of ?id=2, it can be { id: "2" })

Every parameter's type is defined based on the built request handler.

Feature 2: Client chaining

const client = createClient<typeof routes>({
  path: "http://localhost:8080/rpc",
});

const authedClient = client.with(() => ({
  headers: {
    authorization: localStorage.getItem("token") || "nothing",
  },
}));

await authedClient.getProfile.get({});

// IT'S ALSO AN OPTION:
await client.getProfile.get({
  headers: {
    authorization: localStorage.getItem("token") || "nothing",
  }
});

// BAD EXAMPLE:
// Calling the handler which requires auth headers without auth headers cause type error:
await client.getProfile.get({});

Package Sidebar

Install

npm i @cuple/client

Weekly Downloads

29

Version

1.0.8

License

MIT

Unpacked Size

41.1 kB

Total Files

10

Last publish

Collaborators

  • fxdave