Skip to content

Instantly share code, notes, and snippets.

@shmidt-i
Last active February 14, 2020 13:16
Show Gist options
  • Save shmidt-i/32f13bd987923a172acb1aff12b2eab6 to your computer and use it in GitHub Desktop.
Save shmidt-i/32f13bd987923a172acb1aff12b2eab6 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
//onEntry: sendParent("FAIL")
const failureMachine = Machine({
id: 'failure',
initial: 'INIT',
// context: new Error('Hi there'),
states: {
INIT: {
on: {
"*": [
{cond: ctx => ctx.statusCode>=500, target: '5xx'},
// {cond: "is400", target: '400'},
// {cond: "is401", target: '401'},
// {cond: "isTimeout", target: 'TIMEOUT'},
{target: 'UNKNOWN'},
]
}
},
UNKNOWN: {},
"TIMEOUT": {
on: {
"*": '#callback.REQUESTING ACCESS TOKEN'
}
},
'5xx': {},
401: {},
400: {
initial: 'unknown',
states: {
unknown: {
on: {}
},
invalid_request: {},
invalid_client: {},
invalid_grant: {},
unauthorized_client: {},
unsupported_grant_type: {}
},
}
},
});
const fetchMachine = Machine(
{
id: "OAUTH CLIENT",
type: "parallel",
context: {
hasAuthorizationCode: true
},
states: {
AUTHORIZED: {
id: "authed",
initial: "FALSE",
states: {
TRUE: {
// on:
},
FALSE: {}
}
},
CALLBACK: {
initial: "INIT",
id: "callback",
states: {
INIT: {
on: {
"*": [
{
target: "REQUESTING ACCESS TOKEN",
cond: "hasAuthorizationCode"
},
{
target: "#authed.FALSE"
}
]
}
},
"REQUESTING ACCESS TOKEN": {
id: "access_token",
entry: "exchangeAuthCodeForAccessToken",
initial: "loading",
states: {
loading: {
on: {
RESOLVE: "success",
REJECT: "failure"
}
},
success: {
onEntry: send("SUCC")
},
failure: failureMachine
},
on: {
SUCC: "REQUESTING SESSION",
FAIL: "#authed.FALSE"
}
},
"REQUESTING SESSION": {
entry: "exchangeAccessTokenForSession",
on: {
SUCC: {
target: ["#authed.TRUE", "SESSION RECEIVED"]
},
FAIL: "#authed.FALSE"
}
},
"SESSION RECEIVED": {
type: "final"
}
}
}
}
},
{ guards: { hasAuthorizationCode: ctx => ctx.hasAuthorizationCode } }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment