Skip to content

Commit

Permalink
Fix pagination pages being parsed as strings (#32)
Browse files Browse the repository at this point in the history
The `limit` and `page` parameters were transformed as `ZodDefault` after
the first query. This means the routine for identifying the underlying
type would fail as it didn't fall in the `ZodOptional` or `ZodUnion` cases.

Hence, those parameters would be interpreted as strings and the `+` operator
for the calculation of pages would be interpreted as concatenation rather
than integer addition.
  • Loading branch information
0237h authored May 28, 2024
1 parent 45fa4da commit fcc8b5d
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import client from './src/clickhouse/client.js';
import openapi from "./tsp-output/@typespec/openapi3/openapi.json";

import { Hono } from "hono";
import { ZodBigInt, ZodBoolean, ZodDate, ZodNumber, ZodOptional, ZodTypeAny, ZodUndefined, ZodUnion, z } from "zod";
import { ZodBigInt, ZodBoolean, ZodDate, ZodDefault, ZodNumber, ZodOptional, ZodTypeAny, ZodUndefined, ZodUnion, z } from "zod";
import { EndpointByMethod } from './src/types/zod.gen.js';
import { APP_VERSION } from "./src/config.js";
import { logger } from './src/logger.js';
Expand Down Expand Up @@ -70,13 +70,18 @@ function AntelopeTokenAPI() {
// Add type coercion for query and path parameters since the codegen doesn't coerce types natively
const endpoint_parameters = Object.values(EndpointByMethod["get"][endpoint].parameters.shape).map(p => p.shape);
endpoint_parameters.forEach(
// `p` can query or path parameters
// `p` can be query or path parameters
(p) => Object.keys(p).forEach(
(key, _) => {
const zod_type = p[key] as ZodTypeAny;
let zod_type = p[key] as ZodTypeAny;
let underlying_zod_type: ZodTypeAny;
let isOptional = false;

// Strip default layer for value
if (zod_type instanceof ZodDefault) {
zod_type = zod_type.removeDefault();
}

// Detect the underlying type from the codegen
if (zod_type instanceof ZodUnion) {
underlying_zod_type = zod_type.options[0];
Expand Down

0 comments on commit fcc8b5d

Please sign in to comment.