cookies.ts 686 B

123456789101112131415161718192021222324252627
  1. import parser from "yargs-parser"
  2. import * as cookie from "cookie"
  3. import * as O from "fp-ts/Option"
  4. import * as S from "fp-ts/string"
  5. import { pipe, flow } from "fp-ts/function"
  6. import { objHasProperty } from "~/helpers/functional/object"
  7. export function getCookies(parsedArguments: parser.Arguments) {
  8. return pipe(
  9. parsedArguments,
  10. O.fromPredicate(objHasProperty("cookie", "string")),
  11. O.map((args) => args.cookie),
  12. O.alt(() =>
  13. pipe(
  14. parsedArguments,
  15. O.fromPredicate(objHasProperty("b", "string")),
  16. O.map((args) => args.b)
  17. )
  18. ),
  19. O.map(flow(S.replace(/^cookie: /i, ""), cookie.parse)),
  20. O.getOrElse(() => ({}))
  21. )
  22. }