Skip to content

Commit

Permalink
feat(rpc-client): adds generate
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Oct 25, 2019
1 parent 0aff68f commit f6cca63
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/rpc-client/src/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { client, CollectionTree, ClientGenerateOptions } from '@karmic/core';

/**
* Code generator for a collection's RPC client.
*/
export async function generate(
collection: CollectionTree | Promise<CollectionTree>,
options?: ClientGenerateOptions
): Promise<string> {
const opts = Object.assign(
{ typescript: true, write: null, headComments: true },
options
);

return client(await collection, opts, {
headInclude: opts.typescript
? `import RPCClient from '@karmic/rpc-client';`
: undefined,
mapDefault(str) {
let fn = `function createClient(rpc`;
fn += opts.typescript ? `: RPCClient) {\n` : `) {\n`;
fn += ` return ` + str.replace(/\n/g, '\n ');
fn += `}`;
return fn;
},
mapService(element, info) {
return `return rpc.${element.kind}('${info.route.join(':')}', data);`;
}
});
}
1 change: 1 addition & 0 deletions packages/rpc-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default } from './client';
export * from './client';
export * from './obtain';
export * from './generate';

0 comments on commit f6cca63

Please sign in to comment.