server-bridge
Code generation for a statically typed bridge between the client and server.
What does this library do?
- Helps you write code on the server that listens for requests.
- Generates client-side code from the server-side code to send requests to the server.
Advantages
- You don't have to write the client-side code to send requests.
- After code generation, your client-side code will throw a compile error if a breaking change happened so you won't ever forget to update your client-side code.
- Auto-completion will show you what is expected to be sent to the server.
- The interfaces that describe what data is being sent/received from the server are automatically included in your client application.
Current Support
Only express (server) and super-agent (client) are supported right now, but more support can be trivially added. If there's something you would like open up an issue and I'll take a look at it.
Example
A simple client-server example can be found here.
Server Side
-
Install
server-bridge
:npm install server-bridge --save
-
Declare a route class that inherits from
Routes
. Add a@Use
decorator with the path if necessary and then define@Get
and@Post
decorators on the methods similar to as shown:// NoteRoutes.ts;; -
Install
server-bridge-express
to initialize routes for express:npm install server-bridge-express --save
-
Initialize the routes with
initializeRoutes
fromserver-bridge-express
// index.ts;;;;;;;initializeRoutesrouter, ;app.usebodyParser.json;app.use"/", router;;
Client Side
-
Generate client side code from the server side code:
;;// get the generated code;// write it to a file in the client applicationfs.writeFile"../client/src/server.ts", clientSideCode; -
Install
server-bridge-superagent-client
in the client application by running:npm install server-bridge-superagent-client --save
After generating the code, server.ts
would contain the following code for use in a client-side application:
;
Which could then be used in the client like so:
; ;notes.getAll.then;
Dependency: Promises
ES6 promises are used in the client application. If you are using an environment that doesn't support ES6 promises, then install the es6-promise
package:
npm install es6-promise --save
typings install dt~es6-promise --save --global
And run the polyfill by running the following code when your application starts:
import "es6-promise";