Skip to content

Commit

Permalink
Added ping pong api endpoints 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 5b81b69 commit 7543891
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/uuid.rocks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,6 +31,12 @@ const router = u.router({
/** Gets single uuid with JSON output */
"/api/uuid/emoji?json": u.output<EmojiJson>(),

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

/** Responds with ‘pong’ or ‘ping’ in JSON format with some info about your request */
"/api/ping?json": u.output<PingPongJson>(),

/** Gets single uuid with JSON output */
"/json": u.input<GlobalParams>().output<Json>(),

Expand Down
10 changes: 10 additions & 0 deletions packages/uuid.rocks/src/ping-pong/ping-pong.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { z } from "zod";

import type {
PingPongJsonSchema,
PingPongSchema,
} from "./ping-pong.validators";

export type PingPong = z.infer<typeof PingPongSchema>;

export type PingPongJson = z.infer<typeof PingPongJsonSchema>;
9 changes: 9 additions & 0 deletions packages/uuid.rocks/src/ping-pong/ping-pong.validators.ts
Original file line number Diff line number Diff line change
@@ -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,
});
10 changes: 10 additions & 0 deletions packages/uuid.rocks/src/runtimeSafe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),

Expand Down
1 change: 1 addition & 0 deletions packages/uuid.rocks/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
1 change: 1 addition & 0 deletions packages/uuid.rocks/src/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
33 changes: 33 additions & 0 deletions packages/uuid.rocks/tests/ping-pong.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 7543891

Please sign in to comment.