@tritio/client 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # client
2
+
3
+ To install dependencies:
4
+
5
+ ```bash
6
+ bun install
7
+ ```
8
+
9
+ To run:
10
+
11
+ ```bash
12
+ bun run index.ts
13
+ ```
14
+
15
+ This project was created using `bun init` in bun v1.3.4. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
@@ -0,0 +1,2 @@
1
+ import type { Client } from './types';
2
+ export declare const createClient: <T extends Record<string, any>>(baseUrl: string) => Client<T>;
package/dist/index.js ADDED
@@ -0,0 +1,39 @@
1
+ // src/index.ts
2
+ var createClient = (baseUrl) => {
3
+ const request = async (path, method, payload) => {
4
+ const url = `${baseUrl}/${path.join("/")}`;
5
+ const headers = {
6
+ "Content-Type": "application/json",
7
+ Accept: "application/json"
8
+ };
9
+ const options = {
10
+ method: method.toUpperCase(),
11
+ headers
12
+ };
13
+ if (method === "get" && payload) {
14
+ const params = new URLSearchParams(payload).toString();
15
+ return fetch(`${url}?${params}`, options).then((r) => r.json());
16
+ }
17
+ if (payload) {
18
+ options.body = JSON.stringify(payload);
19
+ }
20
+ return fetch(url, options).then((r) => r.json());
21
+ };
22
+ const buildProxy = (path) => {
23
+ return new Proxy(() => {}, {
24
+ get(_target, prop) {
25
+ return buildProxy([...path, prop]);
26
+ },
27
+ apply(_target, _this, args) {
28
+ const method = path.pop();
29
+ if (!method)
30
+ throw new Error("Invalid RPC call");
31
+ return request(path, method, args[0]);
32
+ }
33
+ });
34
+ };
35
+ return buildProxy([]);
36
+ };
37
+ export {
38
+ createClient
39
+ };
@@ -0,0 +1,25 @@
1
+ export type Prettify<T> = {
2
+ [K in keyof T]: T[K];
3
+ } & {};
4
+ export type Method = 'get' | 'post' | 'put' | 'delete' | 'patch';
5
+ export interface RouteDefinition {
6
+ input?: any;
7
+ output?: any;
8
+ }
9
+ type RequestClient<Input, Output> = Input extends undefined | null ? (options?: RequestInit) => Promise<Output> : (input: Input, options?: RequestInit) => Promise<Output>;
10
+ type MethodClient<Routes> = {
11
+ [M in keyof Routes]: Routes[M] extends RouteDefinition ? RequestClient<Routes[M]['input'], Routes[M]['output']> : never;
12
+ };
13
+ type StripSlash<T> = T extends `/${infer R}` ? R : T;
14
+ type Split<S extends string> = S extends `${infer Head}/${infer Tail}` ? [Head, ...Split<Tail>] : [S];
15
+ type PathToObject<Segments extends string[], Value> = Segments extends [
16
+ infer Head extends string,
17
+ ...infer Tail extends string[]
18
+ ] ? {
19
+ [K in Head]: Tail['length'] extends 0 ? Value : PathToObject<Tail, Value>;
20
+ } : Value;
21
+ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
22
+ export type Client<Schema> = Prettify<UnionToIntersection<{
23
+ [K in keyof Schema]: PathToObject<Split<StripSlash<K & string>>, MethodClient<Schema[K]>>;
24
+ }[keyof Schema]>>;
25
+ export {};
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@tritio/client",
3
+ "version": "0.1.0",
4
+ "description": "Type-safe client for Tritio",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.js",
12
+ "types": "./dist/index.d.ts"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "scripts": {
22
+ "build": "bun build ./src/index.ts --outdir ./dist --target browser && tsc --emitDeclarationOnly --outDir dist"
23
+ }
24
+ }