chore: split app to commons and web (squash commit)

This commit is contained in:
Andrew Bastin
2022-12-02 02:57:46 -05:00
parent fb827e3586
commit 3d004f2322
535 changed files with 1487 additions and 501 deletions

View File

@@ -0,0 +1,68 @@
import parser from "yargs-parser"
import { pipe } from "fp-ts/function"
import * as O from "fp-ts/Option"
import * as R from "fp-ts/Refinement"
import { getDefaultRESTRequest } from "~/newstore/RESTSession"
import {
objHasProperty,
objHasArrayProperty,
} from "~/helpers/functional/object"
const defaultRESTReq = getDefaultRESTRequest()
const getMethodFromXArg = (parsedArguments: parser.Arguments) =>
pipe(
parsedArguments,
O.fromPredicate(objHasProperty("X", "string")),
O.map((args) => args.X.trim()),
O.chain((xarg) =>
pipe(
O.fromNullable(
xarg.match(/GET|POST|PUT|PATCH|DELETE|HEAD|CONNECT|OPTIONS|TRACE/i)
),
O.alt(() => O.fromNullable(xarg.match(/[a-zA-Z]+/)))
)
),
O.map((method) => method[0])
)
const getMethodByDeduction = (parsedArguments: parser.Arguments) => {
if (
pipe(
objHasProperty("T", "string"),
R.or(objHasProperty("upload-file", "string"))
)(parsedArguments)
)
return O.some("put")
else if (
pipe(
objHasProperty("I", "boolean"),
R.or(objHasProperty("head", "boolean"))
)(parsedArguments)
)
return O.some("head")
else if (objHasProperty("G", "boolean")(parsedArguments)) return O.some("get")
else if (
pipe(
objHasProperty("d", "string"),
R.or(objHasArrayProperty("d", "string")),
R.or(objHasProperty("F", "string")),
R.or(objHasArrayProperty("F", "string"))
)(parsedArguments)
)
return O.some("POST")
else return O.none
}
/**
* Get method type from X argument in curl string or
* find it out through other arguments
* @param parsedArguments Parsed Arguments object
* @returns Method string
*/
export const getMethod = (parsedArguments: parser.Arguments): string =>
pipe(
getMethodFromXArg(parsedArguments),
O.alt(() => getMethodByDeduction(parsedArguments)),
O.getOrElse(() => defaultRESTReq.method)
)