Skip to content

Instantly share code, notes, and snippets.

@kazagkazag
kazagkazag / machine.js
Last active January 26, 2021 19:59
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
interface User {
fullName(): string;
adult(): boolean;
}
class Citizen implements User {
constructor(private firstName: string, private lastName: string, private age: number) {
}
fullName() {
# fetch articles for user with ID "myUserId"
curl --verbose -X GET "http://localhost:4000/users/myUserId/articles"
# response:
# [{"title":"qui vero quo enim error","content":"...","createdAt":"2018-04-26T05:50:26.130Z"}, ...]
# try to register user with valid user name
curl --verbose -X POST http://localhost:4000/register -H 'content-type: application/json' -d '{"userName":"valid"}'
# response:
# ...
# < HTTP/1.1 200 OK
server.get("/users/:id/articles", (request, response) => {
const delay = faker.random.number({min: 400, max: 1000});
// delay response with fake articles
setTimeout(() => {
response.send(200, getArticlesForUser());
}, delay);
});
const faker = require("faker");
server.get("/users/:id/articles", (request, response) => {
response.send(200, getArticlesForUser());
});
// you can create articles on the fly
// or prepare few articles upfront
// and just select them now
function getArticlesForUser() {
server.post("/register", (request, response) => {
const userName = request.body.userName;
// we can use request data to trigger positive
// or negative response, whatever we need
if (userName === "invalid") {
response.send(400, {
message: "Invalid user name."
});
} else {
const restify = require("restify");
// create server instance
const server = restify.createServer();
// use plugins to parse body and query string of incoming requests
server.use(restify.plugins.queryParser());
server.use(restify.plugins.bodyParser());
// start listening
/*
import {connect} from "react-redux";
import * as actions from "./actions";
import LoginForm from "./components/LoginForm";
*/
export class Login extends Component {
render() {
return (
class Button extends React.Component {
render() {
return <button style={{background: this.context.color}}> {this.props.children}</button>;
}
}
Button.contextTypes = {
color: React.PropTypes.string
};
class MyComponent extends Component {
render() {
return <input ref={(element) => this._input = element} />;
}
componentDidMount() {
this._input.focus();
}