README
xiv
A full typed micro-service rpc system for Node.js.
Features
- Full typed with TypeScript
- High configurable, just like method, url, and headers forward
- Framework free, works with any framework, such as express, koa, etc.
- Support any encoding protocol, such as JSON(default), Protobuf, Msgpack, urlencoded, etc.
- Support different client with
fetch
,http2
,websocket
, etc. - Support browser and Node.js.
Install
yarn add xiv
# or via npm
npm i xiv
Usage
Service definition
The following code declared a service EchoService
which with name echo
, and
has one method echo
:
import { XivModel } from './src/model';
import { XivService } from './src/types';
import { str } from './src/validate/string';
import { XivClient } from './src/client';
export class EchoInput extends XivModel {
@str() message: string;
}
export class EchoOutput extends XivModel {
@str() message: string;
}
export const EchoService = XivService('echo', {
echo: [EchoInput, EchoOutput],
});
export type EchoService = XivService<typeof EchoService>;
export const EchoClient = XivClient(EchoService);
Service Implementation
The following class EchoServiceImpl
implemented EchoService
:
import { XivServiceImpl, XivServiceApi } from './src/types';
export class EchoServiceImpl implements XivServiceImpl<EchoService> {
service = EchoService;
echo: XivServiceApi<EchoService, 'echo'> = async (input, req) => {
const output = EchoOutput.c();
output.message = input.message + '';
return output;
};
}
Mount service to express application
The following code mounted the EchoServiceImpl
to express application app
:
import e from 'express';
import { applyExpressService } from './src/apply/express';
const app = e();
// you should use json parser for xiv use json as default encoding
app.use(e.json());
applyExpressService(app, [new EchoServiceImpl()], { prefix: '/api/' });
app.listen(3000);
Client usage
Before call the service, you should configure its address:
import { XivClient } from './src/client';
// config for EchoClient only
EchoClient.config({
address: 'http://localhost:3000/api/{service}.{action}',
});
// or config for all the clients created by XivClient,
// this will not overwrite the client instance custom config
XivClient.config({
address: 'http://localhost:3000/api/{service}.{action}',
});
And then, you can call the service very easy:
EchoClient.echo({ message: 'hello world!' }).then((r) => {
console.log(r); // { message: 'hello world!' }
});
Advanced usage
Model validation
TODO
Complex model definition
TODO
Submodule definition
TODO
Invoke service implementation locally
TODO
Call options
TODO
Configure api schema
TODO
With koa
TODO
With WebSocket(stmp
)
TODO
Change encoding
TODO
LICENSE
The MIT License (MIT)
Copyright (c) 2016 acrazing
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.