@alien-rpc/client v0.2.10
@alien-rpc/client
This package wraps a Ky client with a Proxy that allows you to call your API routes as TypeScript methods.
!NOTE Typically, you should install
alien-rpcinstead of this package directly, then import from this package viaalien-rpc/client.
// client/index.ts
import { defineClient } from '@alien-rpc/client'
import * as API from './generated/api.js'
export default defineClient(API)Where you import the generated API namespace depends on what you passed to the alien-rpc CLI (or the @alien-rpc/generator library). The default path is ./client/generated/api.ts relative to your root directory. The --clientOutFile option can be used to change this.
Options
The defineClient function also accepts an options object, which supports all Ky options in addition to the following:
resultCache: A cache for storing the results of yourGETroutes. This cache is checked before sending aGETrequest. It remains empty until you manually call theClient#setCachedResponsemethod.- Probably the only time you might set this is if you want a “least-recently-used” cache (e.g. you might use quick-lru), whereas the default cache is a simple
Mapwhose size is unbounded.
- Probably the only time you might set this is if you want a “least-recently-used” cache (e.g. you might use quick-lru), whereas the default cache is a simple
In TypeScript, you can use the ClientOptions type whenever you have an object variable/parameter whose properties should match the options supported by defineClient.
Methods
Every RPC route found by @alien-rpc/generator will be available as a method on the Client object.
Each client also has the following methods:
extend(options: ClientOptions): Create a client with the given options, using the current client as a source of default options.getCachedResponse(path: string): Get a cached response for a given path.setCachedResponse(path: string, response: unknown): Manually set a cached response for a given path.unsetCachedResponse(path: string): Manually unset a cached response for a given path.request(input, init?): Send an unsafe request using the underlying Ky client. This is also a reference to the underlyingkyclient, so you can use all of its methods.
Example
An end-to-end example is in the works.
For now, you can take a look at the test snapshots to get an idea of what the generated code looks like.
8 months ago
8 months ago
10 months ago
10 months ago
10 months ago
1 year ago
10 months ago
9 months ago
9 months ago
10 months ago
9 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
1 year ago
1 year ago