eproxe
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

eproxe

use typesafe server actions in the client while keeping the same structure without having to learn new meta-syntax

superpower your requests with extensions

Install

npm i eproxe
pnpm add eproxe

Usage

server/api.ts

const api = {
	users: {
		async getUserById(id: number) {
			// do some server work
			const user = await db.getUser(id);

			return user;
		},
	},
};

export type ServerAPI = typeof api;

export default api;

client/api.ts

import eproxe from 'eproxe';
import type ServerAPI from '../../server/api';

const api = eproxe<ServerAPI>();

api.users.getUserById(1).then(console.log);

Note: calling from the eproxe object does little when not paired with an extension, if you wish to send http requests when calling methods on the eproxe proxy, see the example project

Keeping it clean

in a real life scenario we would not want to have all of our apis in a single file, so instead separate each locigcal compartment to a separate file, and aggregate it at the api file

server/api/users.ts

export default {
	async getUserById(id: number) {
		// do some server work
		const user = await db.getUser(id);

		return user;
	},
};

server/api/index.ts

import users from 'api/users';

const api = {
	users,
};

export type ServerAPI = typeof api;

export default api;

Readme

Keywords

none

Package Sidebar

Install

npm i eproxe

Weekly Downloads

2

Version

3.0.0

License

ISC

Unpacked Size

57.2 kB

Total Files

39

Last publish

Collaborators

  • leddit