Skip to content

Instantly share code, notes, and snippets.

#!/bin/bash
blueutil -p 0
sleep 1
blueutil -p 1
public interface IUserRepoSearchActor : IActor
{
Task<Result<SearchOutput>> SearchAsync(SearchInput input);
Task SetUserInfoAsync(UserInfo userInfo);
Task SetRepositoryAsync(Repository repository);
Task<AutoCompleteOutput> AutoCompleteAsync(SearchInput input);
Task<SearchOutput> GetSuggestionsAsync(SearchInput input);
}
function sendRequest<TInput, TOutput>(
endpoint: ApiEndpoint<TInput, TOutput>,
data?: TInput) {
return request<TOutput>({
url: endpoint.pathStr(),
data: data,
method: 'POST'
});
}
// Initialize the express server
const server = express();
server.use(bodyParser.json());
// Mount the recipes API handlers
const mounter = new ExpressApiMounter(server);
mounter.mountHandler(api.recipe.create, create);
mounter.mountHandler(api.recipe.read, read);
mounter.mountHandler(api.recipe.update, update);
mounter.mountHandler(api.recipe.destroy, destroy);
export function setPaths<T>(
space: T, path: string[] = [],
clone: boolean = true) {
if (!space) return space;
let result = clone ? cloneDeep(space) : space;
for (const key in result) {
if (result.hasOwnProperty(key)) {
export default class ExpressApiMounter implements ApiMounter {
constructor(public readonly router: Router) { }
mountHandler<TInput, TOutput>(
endpoint: ApiEndpoint<TInput, TOutput>,
handler: ApiHandler<TInput, TOutput>): void {
this.router.post(endpoint.pathStr(), async (req, res) => {
const output = await handler(req.body as TInput);
res.json(output);
export interface ApiMounter {
mountHandler<TInput, TOutput>(
endpoint: ApiEndpoint<TInput, TOutput>,
handler: ApiHandler<TInput, TOutput>): void
}
export interface RequestHandler {
(req: Request, res: Response, next: NextFunction): any;
}
export type ApiHandler<TInput, TOutput> =
(data: TInput) => Promise<TOutput>
const api = {
create: new ApiEndpoint<Recipe, Recipe>(),
read: new ApiEndpoint<FindParams, Recipe>(),
update: new ApiEndpoint<Recipe, Recipe>(),
destroy: new ApiEndpoint<FindParams, undefined>(),
list: new ApiEndpoint<undefined, Recipe[]>()
};