Skip to content

Instantly share code, notes, and snippets.

View john-osullivan's full-sized avatar
Focusing

John O'Sullivan john-osullivan

Focusing
View GitHub Profile
@john-osullivan
john-osullivan / listDappsHandler.tsx
Created November 7, 2019 21:36
Dev Diaries #3 - ListDapps Handler
export function handler(args: ArgShape) {
render(
<App args={args} renderFunc={({ API }) => {
return (
<PrettyRequest
operation={ApiMethodLabel(ListDapps.HTTP, ListDapps.Path)}
resource={() => API.private.listDapps.resource()} />
)
}} />
)
@john-osullivan
john-osullivan / listDapps-builderFxn.tsx
Created November 7, 2019 19:15
Dev Diary #3 - Mounting requireAuthData Middleware
export function builder(yargs: Argv<UniversalArgs>) {
yargs.middleware(requireAuthData);
}
@john-osullivan
john-osullivan / addDefaultAuthPath.ts
Last active November 7, 2019 18:23
Dev Diaries #3 - Middleware for Default Auth Path
// Middleware defined in a service file
export function addDefaultAuthPath(args:ArgShape): ArgShape {
if (args.authPath) return args;
args.authPath = AUTH_FILE_PATH;
initAuthFile();
return args;
}
// Then used in cli.tsx
@john-osullivan
john-osullivan / requireAuthData.tsx
Last active November 7, 2019 21:44
Dev Diary #3 - Require Auth Data with Middleware
import User from "@eximchain/dappbot-types/spec/user";
export const requireAuthData:MiddlewareFunction<UniversalArgs> = (args) => {
// This function will show the user an error and then
// exit the process, preventing the command handler
// from ever actually being called.
function authErr() {
render(
<ErrorBox errMsg={"This command requires you to log in; please call 'dappbot signup' or 'dappbot login'."} />
@john-osullivan
john-osullivan / App-with-Refresh.tsx
Last active November 7, 2019 21:41
Dev Diaries #3 - Refresh w/ Hook
export function App<Additional extends AdditionalArgs>(props: AppProps<Additional>): ReactElement {
const { args, renderFunc } = props;
const [authData, setAuthData] = useState(JSON.parse(args.authFile as string));
const API = new DappbotAPI({
authData,
setAuthData: (auth) => {
setAuthData(auth);
saveAuthToFile(auth);
},
@john-osullivan
john-osullivan / CLI-App.tsx
Last active November 7, 2019 10:05
Dev Diaries #3 - Top-Level App component w/ renderFunc
import DappbotAPI from '@eximchain/dappbot-api-client';
export function function App<Additional extends AdditionalArgs>(props: AppProps<Additional>): ReactElement {
// We destructure App's two props; the args from
// the command, and the renderFunc.
const { args, renderFunc } = props;
// We use the authFile argument to initialize the
// authData, as it will safely default.
@john-osullivan
john-osullivan / viewDapp.tsx
Created November 7, 2019 09:37
Dev Diary #3 - renderProps make easy handlers
export function handler(args: ArgShape<DappNameArg>) {
fastRender(
<App args={args} renderFunc={({ API }) => {
const DappName = args.DappName;
return (
<PrettyRequest
operation={ApiMethodLabel(ViewDapp.HTTP, ViewDapp.Path(DappName))}
resource={() => API.public.viewDapp.resource(DappName)} />
)
}} />
@john-osullivan
john-osullivan / updateDapp.tsx
Last active November 7, 2019 09:31
Dev Diary #3 - Loading File via Arg Name
export function builder(yargs:Argv<UniversalArgs>) {
yargs
.middleware(requireAuthData)
.options({
'AbiPath' : {
type: 'string'
},
...
});
}
@john-osullivan
john-osullivan / authStorage.ts
Created November 7, 2019 06:56
Dev Diary #3 - CLI authStorage
import { AuthData, newAuthData } from '@eximchain/dappbot-types/spec/user';
export const AUTH_FILENAME = 'dappbotAuthData.json';
export const AUTH_FILE_PATH = path.resolve(__dirname, `./${AUTH_FILENAME}`);
export function initAuthFile() {
if (!fs.existsSync(AUTH_FILE_PATH)) {
fs.writeFileSync(AUTH_FILE_PATH, JSON.stringify(newAuthData(), null, 2));
}
}
@john-osullivan
john-osullivan / commandConfig.tsx
Created November 7, 2019 06:46
Dev Diary #3 - CLI File Loader
/**
* Middlware: Listen for any options whose name ends in "Path", and if found,
* read the file's contents as a string and add it as an argument
* whose name ends in "File". For instance, "authPath" will yield
* an "authFile" string, which can be JSON.parse()d to get the
* actual authData.
* @param args
*/
export function loadFileFromPath(args:ArgShape): ArgShape {
const pathKeys = Object.keys(args).filter(key => key.indexOf('Path') > -1);