Skip to content

Commit

Permalink
Added hash endpoint to uuid.rocks package
Browse files Browse the repository at this point in the history
  • Loading branch information
NuroDev committed Apr 28, 2024
1 parent 7543891 commit 7cfcbc3
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/uuid.rocks/src/hash/hash.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { z } from "zod";

import type { HashParamsSchema, HashSchema } from "./hash.validators";

export type HashParams = z.infer<typeof HashParamsSchema>;

export type Hash = z.infer<typeof HashSchema>;
8 changes: 8 additions & 0 deletions packages/uuid.rocks/src/hash/hash.validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { z } from "zod";

export const HashParamsSchema = z.object({
algo: z.union([z.literal("md5"), z.literal("sha1"), z.literal("sha256")]),
data: z.string().min(1),
});

export const HashSchema = z.string();
4 changes: 4 additions & 0 deletions packages/uuid.rocks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { initUntypeable } from "untypeable";

import type { GlobalParams } from "./types";
import type { Emoji, EmojiJson } from "./emoji/emoji.types";
import type { Hash, HashParams } from "./hash/hash.types";
import type {
Json,
JsonBulk,
Expand Down Expand Up @@ -31,6 +32,9 @@ const router = u.router({
/** Gets single uuid with JSON output */
"/api/uuid/emoji?json": u.output<EmojiJson>(),

/** This api will hash data in the url or the body. */
"/api/hash/:algo/:data": u.input<HashParams>().output<Hash>(),

/** Responds with ‘pong’ or ‘ping’ */
"/api/ping": u.output<PingPong>(),

Expand Down
4 changes: 4 additions & 0 deletions packages/uuid.rocks/src/runtimeSafe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { initUntypeable } from "untypeable";

import { GlobalParamsSchema } from "./_shared/_shared.validators";
import { EmojiSchema, EmojiJsonSchema } from "./emoji/emoji.validators";
import { HashParamsSchema, HashSchema } from "./hash/hash.validators";
import {
JsonBulkParamsSchema,
JsonBulkSchema,
Expand Down Expand Up @@ -34,6 +35,9 @@ export const uuidRocksSafeRouter = u.router({
/** Gets single uuid with JSON output */
"/api/uuid/emoji?json": u.output(EmojiJsonSchema),

/** This api will hash data in the url or the body. */
"/api/hash/:algo/:data": u.input(HashParamsSchema).output(HashSchema),

/** Responds with ‘pong’ or ‘ping’ */
"/api/ping": u.output(PingPongSchema),

Expand Down
1 change: 1 addition & 0 deletions packages/uuid.rocks/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./_shared/_shared.types";
export * from "./emoji/emoji.types";
export * from "./hash/hash.types";
export * from "./json/json.types";
export * from "./nanoid/nanoid.types";
export * from "./ping-pong/ping-pong.types";
Expand Down
1 change: 1 addition & 0 deletions packages/uuid.rocks/src/zod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./_shared/_shared.validators";
export * from "./emoji/emoji.validators";
export * from "./hash/hash.validators";
export * from "./json/json.validators";
export * from "./nanoid/nanoid.validators";
export * from "./ping-pong/ping-pong.validators";
Expand Down
48 changes: 48 additions & 0 deletions packages/uuid.rocks/tests/hash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { describe, it, expect } from "vitest";

import { HashSchema } from "../src/zod";
import { useTestClient } from "./_client";

describe.concurrent("uuid.rocks - hash", () => {
it("GET - /api/hash/md5/Hello World", async () => {
const client = useTestClient({ json: false });

const hash = await client("/api/hash/:algo/:data", {
algo: "md5",
data: "Hello World",
});

expect(hash).toBeDefined();
expect(hash).toBeTypeOf("string");

expect(HashSchema.safeParse(hash).success).toBe(true);
});

it("GET - /api/hash/sha1/Hello World", async () => {
const client = useTestClient({ json: false });

const hash = await client("/api/hash/:algo/:data", {
algo: "sha1",
data: "Hello World",
});

expect(hash).toBeDefined();
expect(hash).toBeTypeOf("string");

expect(HashSchema.safeParse(hash).success).toBe(true);
});

it("GET - /api/hash/sha256/Hello World", async () => {
const client = useTestClient({ json: false });

const hash = await client("/api/hash/:algo/:data", {
algo: "sha256",
data: "Hello World",
});

expect(hash).toBeDefined();
expect(hash).toBeTypeOf("string");

expect(HashSchema.safeParse(hash).success).toBe(true);
});
});

0 comments on commit 7cfcbc3

Please sign in to comment.