From 75438915f433973782cb08c975797bb2fff61104 Mon Sep 17 00:00:00 2001 From: nurodev Date: Sun, 28 Apr 2024 13:10:06 +0100 Subject: [PATCH] Added ping pong api endpoints to uuid.rocks package --- packages/uuid.rocks/src/index.ts | 7 ++++ .../src/ping-pong/ping-pong.types.ts | 10 ++++++ .../src/ping-pong/ping-pong.validators.ts | 9 +++++ packages/uuid.rocks/src/runtimeSafe.ts | 10 ++++++ packages/uuid.rocks/src/types.ts | 1 + packages/uuid.rocks/src/zod.ts | 1 + packages/uuid.rocks/tests/ping-pong.test.ts | 33 +++++++++++++++++++ 7 files changed, 71 insertions(+) create mode 100644 packages/uuid.rocks/src/ping-pong/ping-pong.types.ts create mode 100644 packages/uuid.rocks/src/ping-pong/ping-pong.validators.ts create mode 100644 packages/uuid.rocks/tests/ping-pong.test.ts diff --git a/packages/uuid.rocks/src/index.ts b/packages/uuid.rocks/src/index.ts index 657d5fc..568d0c9 100644 --- a/packages/uuid.rocks/src/index.ts +++ b/packages/uuid.rocks/src/index.ts @@ -10,6 +10,7 @@ import type { JsonMapParams, } from "./json/json.types"; import type { NanoID, NanoIDParams } from "./nanoid/nanoid.types"; +import type { PingPong, PingPongJson } from "./ping-pong/ping-pong.types"; import type { Plain, PlainBulk, @@ -30,6 +31,12 @@ const router = u.router({ /** Gets single uuid with JSON output */ "/api/uuid/emoji?json": u.output(), + /** Responds with ‘pong’ or ‘ping’ */ + "/api/ping": u.output(), + + /** Responds with ‘pong’ or ‘ping’ in JSON format with some info about your request */ + "/api/ping?json": u.output(), + /** Gets single uuid with JSON output */ "/json": u.input().output(), diff --git a/packages/uuid.rocks/src/ping-pong/ping-pong.types.ts b/packages/uuid.rocks/src/ping-pong/ping-pong.types.ts new file mode 100644 index 0000000..c009d90 --- /dev/null +++ b/packages/uuid.rocks/src/ping-pong/ping-pong.types.ts @@ -0,0 +1,10 @@ +import type { z } from "zod"; + +import type { + PingPongJsonSchema, + PingPongSchema, +} from "./ping-pong.validators"; + +export type PingPong = z.infer; + +export type PingPongJson = z.infer; diff --git a/packages/uuid.rocks/src/ping-pong/ping-pong.validators.ts b/packages/uuid.rocks/src/ping-pong/ping-pong.validators.ts new file mode 100644 index 0000000..b2360d6 --- /dev/null +++ b/packages/uuid.rocks/src/ping-pong/ping-pong.validators.ts @@ -0,0 +1,9 @@ +import { z } from "zod"; + +export const PingPongSchema = z.union([z.literal("ping"), z.literal("pong")]); + +export const PingPongJsonSchema = z.object({ + country: z.string(), + ip: z.string().ip({ version: "v4" }), + ping: PingPongSchema, +}); diff --git a/packages/uuid.rocks/src/runtimeSafe.ts b/packages/uuid.rocks/src/runtimeSafe.ts index a3b52b6..5fd3adc 100644 --- a/packages/uuid.rocks/src/runtimeSafe.ts +++ b/packages/uuid.rocks/src/runtimeSafe.ts @@ -10,6 +10,10 @@ import { JsonSchema, } from "./json/json.validators"; import { NanoIDSchema, NanoIdParamsSchema } from "./nanoid/nanoid.validators"; +import { + PingPongJsonSchema, + PingPongSchema, +} from "./ping-pong/ping-pong.validators"; import { PlainSchema, PlainBulkParamsSchema, @@ -30,6 +34,12 @@ export const uuidRocksSafeRouter = u.router({ /** Gets single uuid with JSON output */ "/api/uuid/emoji?json": u.output(EmojiJsonSchema), + /** Responds with ‘pong’ or ‘ping’ */ + "/api/ping": u.output(PingPongSchema), + + /** Responds with ‘pong’ or ‘ping’ in JSON format with some info about your request */ + "/api/ping?json": u.output(PingPongJsonSchema), + /** Gets single uuid with JSON output */ "/json": u.input(GlobalParamsSchema).output(JsonSchema), diff --git a/packages/uuid.rocks/src/types.ts b/packages/uuid.rocks/src/types.ts index 92da445..c6e4a99 100644 --- a/packages/uuid.rocks/src/types.ts +++ b/packages/uuid.rocks/src/types.ts @@ -2,5 +2,6 @@ export * from "./_shared/_shared.types"; export * from "./emoji/emoji.types"; export * from "./json/json.types"; export * from "./nanoid/nanoid.types"; +export * from "./ping-pong/ping-pong.types"; export * from "./plain/plain.types"; export * from "./stats/stats.types"; diff --git a/packages/uuid.rocks/src/zod.ts b/packages/uuid.rocks/src/zod.ts index e92155c..01a7eac 100644 --- a/packages/uuid.rocks/src/zod.ts +++ b/packages/uuid.rocks/src/zod.ts @@ -2,5 +2,6 @@ export * from "./_shared/_shared.validators"; export * from "./emoji/emoji.validators"; export * from "./json/json.validators"; export * from "./nanoid/nanoid.validators"; +export * from "./ping-pong/ping-pong.validators"; export * from "./plain/plain.validators"; export * from "./stats/stats.validators"; diff --git a/packages/uuid.rocks/tests/ping-pong.test.ts b/packages/uuid.rocks/tests/ping-pong.test.ts new file mode 100644 index 0000000..bf999bb --- /dev/null +++ b/packages/uuid.rocks/tests/ping-pong.test.ts @@ -0,0 +1,33 @@ +import { describe, it, expect } from "vitest"; + +import { PingPongJsonSchema, PingPongSchema } from "../src/zod"; +import { useTestClient } from "./_client"; + +describe.concurrent("uuid.rocks - ping pong", () => { + it("GET - /api/ping", async () => { + const client = useTestClient({ json: false }); + + const pingPong = await client("/api/ping"); + + expect(pingPong).toBeDefined(); + expect(pingPong).toBeTypeOf("string"); + + expect(PingPongSchema.safeParse(pingPong).success).toBe(true); + }); + + it("GET - /api/ping?json", async () => { + const client = useTestClient({ json: true }); + + const pingPongJson = await client("/api/ping?json"); + + expect(pingPongJson).toBeDefined(); + expect(pingPongJson.country).toBeDefined(); + expect(pingPongJson.country).toBeTypeOf("string"); + expect(pingPongJson.ip).toBeDefined(); + expect(pingPongJson.ip).toBeTypeOf("string"); + expect(pingPongJson.ping).toBeDefined(); + expect(pingPongJson.ping).toBeTypeOf("string"); + + expect(PingPongJsonSchema.safeParse(pingPongJson).success).toBe(true); + }); +});