All files / src/lib schema.ts

100% Statements 27/27
100% Branches 4/4
100% Functions 2/2
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321                    1x                                                                                                   1x 3x 3x                                                                                                                                                                                                                                                                                                                                                                                                                                                                             1x 2x 2x 2x 2x 1x   1x 2x   2x 2x 2x 2x   1x 1x 1x 1x 1x 1x 1x 1x   1x 2x 1x  
import type { RouteConfig } from '@hono/zod-openapi';
import type { CreateSchemaInterface, NeverWrapper, ResponsesConfig, UniqueTuple, UserDefinedStatusCode } from './types';
 
/**
 * The ZodOpenAPISchema class is a utility for creating OpenAPI schema definitions
 *
 * @author Yuki Osada <r.rstudio.c@gmail.com>
 * @template M - The user-defined status codes type (M extends ResponsesConfig).
 * @extends {ResponsesConfig}
 */
export class ZodOpenAPISchema<M extends ResponsesConfig> implements CreateSchemaInterface {
  /**
   * The private property that holds the user-defined status codes type object (M extends ResponsesConfig).
   *
   * @readonly
   * @private
   * @type {Readonly<M>}
   */
  private responses: Readonly<M>;
 
  /**
   * The constructor to initialize the ZodOpenAPISchema class with a ResponsesConfig type object.
   *
   * @param {M} responses - The user-defined status codes type object (M extends ResponsesConfig).
   *
   * @example
   * ```ts
   * import { z } from '@hono/zod-openapi';
   * import { ZodOpenAPISchema } from 'zod-openapi-share';
   *
   * const ContentlyStatusCodeArray = [
   *   100, 102, 103, 200, 201, 202, 203, 206, 207, 208, 226, 300, 301, 302, 303, 305, 306, 307, 308, 400, 401, 402, 403,
   *   404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 429,
   *   431, 451, 500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511, -1,
   * ] as const;
   *
   * export const errorResponseSchema = z.object({
   *   status: z.union(ContentlyStatusCodeArray.map((code) => z.literal(code))).meta({
   *     example: 400,
   *     description: 'HTTP Status Code',
   *   }),
   *   message: z.string().min(1).meta({
   *     example: 'Bad Request',
   *     description: 'Error Message',
   *   }),
   * });
   *
   * const route = new ZodOpenAPISchema({
   *   400: {
   *     description: 'Bad Request',
   *     content: { 'application/json': { schema: errorResponseSchema } },
   *   },
   *   500: {
   *     description: 'Internal Server Error',
   *     content: { 'application/json': { schema: errorResponseSchema } },
   *   },
   * } as const);
   *
   * ```
   */
  constructor(responses: M) {
    this.responses = responses;
  }
 
  /**
   * Overload signatures for createSchema method.
   *
   * @template R - The route config type (R extends RouteConfig). This type is provided by @hono/zod-openapi.
   * @extends {RouteConfig}
   * @template T - The readonly array of unique user-defined status codes type (M extends ResponsesConfig).
   * @extends {Readonly<UserDefinedStatusCode<M>[]>}
   */
  /**
   * `statusCodes` argument omitted
   *
   * @template R - The route config type (R extends RouteConfig). This type is provided by @hono/zod-openapi.
   * @extends {RouteConfig}
   *
   * @param {R} route - Route config (R extends RouteConfig). This type is provided by @hono/zod-openapi.
   * @returns {R} - The route config type (R extends RouteConfig) with the specified status codes added to the responses. This type is provided by @hono/zod-openapi.
   *
   * @example
   * ```ts
   * import { z } from '@hono/zod-openapi';
   * import { ZodOpenAPISchema } from 'zod-openapi-share';
   *
   * const ContentlyStatusCodeArray = [
   *   100, 102, 103, 200, 201, 202, 203, 206, 207, 208, 226, 300, 301, 302, 303, 305, 306, 307, 308, 400, 401, 402, 403,
   *   404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 429,
   *   431, 451, 500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511, -1,
   * ] as const;
   *
   * const errorResponseSchema = z.object({
   *   status: z.union(ContentlyStatusCodeArray.map((code) => z.literal(code))).meta({
   *     example: 400,
   *     description: 'HTTP Status Code',
   *   }),
   *   message: z.string().min(1).meta({
   *     example: 'Bad Request',
   *     description: 'Error Message',
   *   }),
   * });
   *
   *
   * const route = new ZodOpenAPISchema({
   *   400: {
   *     description: 'Bad Request',
   *     content: { 'application/json': { schema: errorResponseSchema } },
   *   },
   *   500: {
   *     description: 'Internal Server Error',
   *     content: { 'application/json': { schema: errorResponseSchema } },
   *   },
   * } as const);
   *
   * const responseBodySchema = z.object({
   *   result: z.string().meta({
   *     example: 'Hello World!',
   *     description: 'Sample Endpoint Response',
   *   }),
   * });
   *
   * const rootRoute = route.createSchema(
   *   {
   *     path: '/',
   *     method: 'get',
   *     description: 'Sample Endpoint',
   *     responses: {
   *       200: {
   *         description: 'OK',
   *         content: {
   *           'application/json': {
   *             schema: responseBodySchema,
   *           },
   *         },
   *       },
   *     },
   *   }
   *   \/\/ You can omit the `statusCodes` argument here
   * );
   * ```
   */
  createSchema<R extends RouteConfig>(route: R): R;
  /**
   * `statusCodes` argument included
   *
   * @template R - The route config type (R extends RouteConfig). This type is provided by @hono/zod-openapi.
   * @extends {RouteConfig}
   * @template T - The readonly array of unique user-defined status codes type (M extends ResponsesConfig).
   * @extends {Readonly<UserDefinedStatusCode<M>[]>}
   *
   * @param {R} route - Route config (R extends RouteConfig). This type is provided by @hono/zod-openapi.
   * @param {Readonly<UserDefinedStatusCode<M>[]>} statusCodes - Optional array of unique status codes (only user-defined ones) to be added to the route.
   * @returns {R} - The route config type (R extends RouteConfig) with the specified status codes added to the responses. This type is provided by @hono/zod-openapi.
   *
   * @example
   * ```ts
   * import { z } from '@hono/zod-openapi';
   * import { ZodOpenAPISchema } from 'zod-openapi-share';
   *
   * const ContentlyStatusCodeArray = [
   *   100, 102, 103, 200, 201, 202, 203, 206, 207, 208, 226, 300, 301, 302, 303, 305, 306, 307, 308, 400, 401, 402, 403,
   *   404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 429,
   *   431, 451, 500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511, -1,
   * ] as const;
   *
   * const errorResponseSchema = z.object({
   *   status: z.union(ContentlyStatusCodeArray.map((code) => z.literal(code))).meta({
   *     example: 400,
   *     description: 'HTTP Status Code',
   *   }),
   *   message: z.string().min(1).meta({
   *     example: 'Bad Request',
   *     description: 'Error Message',
   *   }),
   * });
   *
   *
   * const route = new ZodOpenAPISchema({
   *   400: {
   *     description: 'Bad Request',
   *     content: { 'application/json': { schema: errorResponseSchema } },
   *   },
   *   500: {
   *     description: 'Internal Server Error',
   *     content: { 'application/json': { schema: errorResponseSchema } },
   *   },
   * } as const);
   *
   * const responseBodySchema = z.object({
   *   result: z.string().meta({
   *     example: 'Hello World!',
   *     description: 'Sample Endpoint Response',
   *   }),
   * });
   *
   * const rootRoute = route.createSchema(
   *   {
   *     path: '/',
   *     method: 'get',
   *     description: 'Sample Endpoint',
   *     responses: {
   *       200: {
   *         description: 'OK',
   *         content: {
   *           'application/json': {
   *             schema: responseBodySchema,
   *           },
   *         },
   *       },
   *     },
   *   },
   *   [400, 500] \/\/ This argument is the `statusCodes`
   * );
   * ```
   */
  createSchema<R extends RouteConfig, T extends Readonly<UserDefinedStatusCode<M>[]>>(
    route: R,
    statusCodes: NeverWrapper<UserDefinedStatusCode<M>, T>
  ): R;
 
  /**
   * Create a new RouteConfig type object by adding specified status codes to the responses of the given route.
   *
   * @template R - The route config type (R extends RouteConfig). This type is provided by @hono/zod-openapi.
   * @extends {RouteConfig}
   * @template T - The readonly array of unique user-defined status codes type (M extends ResponsesConfig).
   * @extends {Readonly<UserDefinedStatusCode<M>[]>}
   *
   * @param {R} route - Route config (R extends RouteConfig). This type is provided by @hono/zod-openapi.
   * @param {Readonly<UserDefinedStatusCode<M>[]>} statusCodes - Optional array of unique status codes (only user-defined ones) to be added to the route.
   * @returns {R} - The route config type (R extends RouteConfig) with the specified status codes added to the responses. This type is provided by @hono/zod-openapi.
   *
   * @example
   * ```ts
   * import { z } from '@hono/zod-openapi';
   * import { ZodOpenAPISchema } from 'zod-openapi-share';
   *
   * const ContentlyStatusCodeArray = [
   *   100, 102, 103, 200, 201, 202, 203, 206, 207, 208, 226, 300, 301, 302, 303, 305, 306, 307, 308, 400, 401, 402, 403,
   *   404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 429,
   *   431, 451, 500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511, -1,
   * ] as const;
   *
   * const errorResponseSchema = z.object({
   *   status: z.union(ContentlyStatusCodeArray.map((code) => z.literal(code))).meta({
   *     example: 400,
   *     description: 'HTTP Status Code',
   *   }),
   *   message: z.string().min(1).meta({
   *     example: 'Bad Request',
   *     description: 'Error Message',
   *   }),
   * });
   *
   * const route = new ZodOpenAPISchema({
   *   400: {
   *     description: 'Bad Request',
   *     content: { 'application/json': { schema: errorResponseSchema } },
   *   },
   *   500: {
   *     description: 'Internal Server Error',
   *     content: { 'application/json': { schema: errorResponseSchema } },
   *   },
   * } as const);
   *
   * const responseBodySchema = z.object({
   *   result: z.string().meta({
   *     example: 'Hello World!',
   *     description: 'Sample Endpoint Response',
   *   }),
   * });
   *
   * const rootRoute = route.createSchema(
   *   {
   *     path: '/',
   *     method: 'get',
   *     description: 'Sample Endpoint',
   *     responses: {
   *       200: {
   *         description: 'OK',
   *         content: {
   *           'application/json': {
   *             schema: responseBodySchema,
   *           },
   *         },
   *       },
   *     },
   *   },
   *   [400, 500]
   * );
   * ```
   */
  createSchema<R extends RouteConfig, T extends Readonly<UserDefinedStatusCode<M>[]>>(
    route: R,
    statusCodes?: UniqueTuple<UserDefinedStatusCode<M>, T>
  ): R {
    if (statusCodes) {
      const extraResponses: ResponsesConfig = {};
      // Collect responses for the specified status codes
      for (const statusCode of statusCodes) {
        const response = this.responses[statusCode];
        // Add only status codes that are defined by the user.
        if (response) {
          extraResponses[statusCode] = response;
        }
      }
 
      return {
        ...route,
        responses: {
          ...extraResponses,
          ...route.responses,
        },
      } as R;
    }
    // When no status codes are provided, return the route as is.
    return route;
  }
}