You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
982B

  1. import { RandomString as string } from "./generators";
  2. import Armpits from "@sniff/armpits";
  3. import config from "../config";
  4. const nano = require("nano")(config.nano);
  5. nano.db
  6. .create("click")
  7. .then((e) => Armpits.plus("Database created"))
  8. .catch((e) => Armpits.minus("Database already exists"));
  9. export const click = nano.db.use("click");
  10. var exists = async function (id) {
  11. return await click
  12. .head(id)
  13. .then((r) => {
  14. return true;
  15. })
  16. .catch((e) => {
  17. return false;
  18. });
  19. };
  20. export async function CreateLink({
  21. target,
  22. code,
  23. }: {
  24. code?: string;
  25. target: string;
  26. }): Promise<{ code: string; target: string }> {
  27. if (!code) code = string(5, { chars: false, lower: true });
  28. let checkExists = await exists(code);
  29. if (checkExists) throw new Error("CODE_EXISTS");
  30. else
  31. await click
  32. .insert({ target, code }, code)
  33. .then((e) => Armpits.plus(`Link/${code} created for ${target}`));
  34. return {
  35. code,
  36. target,
  37. };
  38. }