Class: Apiwork::Export::ZodMapper
- Inherits:
-
Object
- Object
- Apiwork::Export::ZodMapper
- Defined in:
- lib/apiwork/export/zod_mapper.rb
Constant Summary collapse
- TYPE_MAP =
{ binary: 'z.string()', boolean: 'z.boolean()', date: 'z.iso.date()', datetime: 'z.iso.datetime()', decimal: 'z.number()', integer: 'z.number().int()', number: 'z.number()', string: 'z.string()', time: 'z.iso.time()', unknown: 'z.unknown()', uuid: 'z.uuid()', }.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #action_type_name(resource_name, action_name, suffix, parent_identifiers: []) ⇒ Object
- #build_action_request_body_schema(resource_name, action_name, body_params, parent_identifiers: []) ⇒ Object
- #build_action_request_query_schema(resource_name, action_name, query_params, parent_identifiers: []) ⇒ Object
- #build_action_request_schema(resource_name, action_name, request, parent_identifiers: []) ⇒ Object
- #build_action_response_body_schema(resource_name, action_name, response_body, parent_identifiers: []) ⇒ Object
- #build_action_response_schema(resource_name, action_name, response, parent_identifiers: [], raises:) ⇒ Object
- #build_action_response_schemas ⇒ Object
- #build_action_schemas ⇒ Object
- #build_enum_schemas(enums) ⇒ Object
- #build_object_schema(type_name, type, recursive: false) ⇒ Object
- #build_object_schema_code(schema_name, properties, extends, type_annotation: '') ⇒ Object
- #build_type_schemas(types) ⇒ Object
- #build_union_schema(type_name, type, recursive: false) ⇒ Object
-
#initialize(export) ⇒ ZodMapper
constructor
A new instance of ZodMapper.
- #map(surface) ⇒ Object
- #map_array_type(param) ⇒ Object
- #map_discriminated_union(param) ⇒ Object
- #map_field(param, force_optional: nil) ⇒ Object
- #map_format_to_zod(format) ⇒ Object
- #map_literal_type(param) ⇒ Object
- #map_object_type(param) ⇒ Object
- #map_param(param) ⇒ Object
- #map_primitive(param) ⇒ Object
- #map_record_type(param) ⇒ Object
- #map_union_type(param) ⇒ Object
- #pascal_case(name) ⇒ Object
- #schema_reference(symbol) ⇒ Object
- #traverse_resources(resources: @export.api.resources, &block) ⇒ Object
Constructor Details
#initialize(export) ⇒ ZodMapper
Returns a new instance of ZodMapper.
26 27 28 |
# File 'lib/apiwork/export/zod_mapper.rb', line 26 def initialize(export) @export = export end |
Class Method Details
.map(export, surface) ⇒ Object
21 22 23 |
# File 'lib/apiwork/export/zod_mapper.rb', line 21 def map(export, surface) new(export).map(surface) end |
Instance Method Details
#action_type_name(resource_name, action_name, suffix, parent_identifiers: []) ⇒ Object
181 182 183 |
# File 'lib/apiwork/export/zod_mapper.rb', line 181 def action_type_name(resource_name, action_name, suffix, parent_identifiers: []) "#{pascal_case((parent_identifiers + [resource_name.to_s, action_name.to_s]).join('_'))}#{suffix.split(/(?=[A-Z])/).map(&:capitalize).join}" end |
#build_action_request_body_schema(resource_name, action_name, body_params, parent_identifiers: []) ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'lib/apiwork/export/zod_mapper.rb', line 125 def build_action_request_body_schema(resource_name, action_name, body_params, parent_identifiers: []) properties = body_params.sort_by { |name, _param| name.to_s }.map do |param_name, param| key = @export.transform_key(param_name) zod_type = map_field(param) " #{key}: #{zod_type}" end.join(",\n") "export const #{action_type_name(resource_name, action_name, 'RequestBody', parent_identifiers:)}Schema = z.object({\n#{properties}\n});" end |
#build_action_request_query_schema(resource_name, action_name, query_params, parent_identifiers: []) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/apiwork/export/zod_mapper.rb', line 115 def build_action_request_query_schema(resource_name, action_name, query_params, parent_identifiers: []) properties = query_params.sort_by { |name, _param| name.to_s }.map do |param_name, param| key = @export.transform_key(param_name) zod_type = map_field(param) " #{key}: #{zod_type}" end.join(",\n") "export const #{action_type_name(resource_name, action_name, 'RequestQuery', parent_identifiers:)}Schema = z.object({\n#{properties}\n});" end |
#build_action_request_schema(resource_name, action_name, request, parent_identifiers: []) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/apiwork/export/zod_mapper.rb', line 135 def build_action_request_schema(resource_name, action_name, request, parent_identifiers: []) nested_properties = [] if request[:query].any? nested_properties << " query: #{action_type_name(resource_name, action_name, 'RequestQuery', parent_identifiers:)}Schema" end if request[:body].any? nested_properties << " body: #{action_type_name(resource_name, action_name, 'RequestBody', parent_identifiers:)}Schema" end "export const #{action_type_name( resource_name, action_name, 'Request', parent_identifiers:, )}Schema = z.object({\n#{nested_properties.join(",\n")}\n});" end |
#build_action_response_body_schema(resource_name, action_name, response_body, parent_identifiers: []) ⇒ Object
154 155 156 |
# File 'lib/apiwork/export/zod_mapper.rb', line 154 def build_action_response_body_schema(resource_name, action_name, response_body, parent_identifiers: []) "export const #{action_type_name(resource_name, action_name, 'ResponseBody', parent_identifiers:)}Schema = #{map_param(response_body)};" end |
#build_action_response_schema(resource_name, action_name, response, parent_identifiers: [], raises:) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/apiwork/export/zod_mapper.rb', line 158 def build_action_response_schema(resource_name, action_name, response, parent_identifiers: [], raises:) schema_name = action_type_name(resource_name, action_name, 'Response', parent_identifiers:) success_variant = if response.no_content? 'z.object({ status: z.literal(204) })' else body_ref = "#{action_type_name(resource_name, action_name, 'ResponseBody', parent_identifiers:)}Schema" "z.object({ status: z.literal(200), body: #{body_ref} })" end error_statuses = resolve_error_statuses(raises) if error_statuses.empty? "export const #{schema_name}Schema = #{success_variant};" else error_variants = error_statuses.map do |status| "z.object({ status: z.literal(#{status}), body: #{pascal_case(:error)}Schema })" end all_variants = ([success_variant] + error_variants).map { |variant| " #{variant}" }.join(",\n") "export const #{schema_name}Schema = z.discriminatedUnion('status', [\n#{all_variants}\n]);" end end |
#build_action_response_schemas ⇒ Object
407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/apiwork/export/zod_mapper.rb', line 407 def build_action_response_schemas schemas = [] traverse_resources do |resource| resource_name = resource.identifier.to_sym parent_identifiers = resource.parent_identifiers resource.actions.each do |action_name, action| schemas << build_action_response_schema(resource_name, action_name, action.response, parent_identifiers:, raises: action.raises) end end schemas.join("\n\n") end |
#build_action_schemas ⇒ Object
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/apiwork/export/zod_mapper.rb', line 365 def build_action_schemas schemas = [] traverse_resources do |resource| resource_name = resource.identifier.to_sym parent_identifiers = resource.parent_identifiers resource.actions.each do |action_name, action| request = action.request if request && (request.query? || request.body?) if request.query? schemas << build_action_request_query_schema( resource_name, action_name, request.query, parent_identifiers:, ) end if request.body? schemas << build_action_request_body_schema( resource_name, action_name, request.body, parent_identifiers:, ) end schemas << build_action_request_schema( resource_name, action_name, { body: request.body, query: request.query }, parent_identifiers:, ) end response = action.response schemas << build_action_response_body_schema(resource_name, action_name, response.body, parent_identifiers:) if response.body? end end schemas.join("\n\n") end |
#build_enum_schemas(enums) ⇒ Object
341 342 343 344 345 346 347 |
# File 'lib/apiwork/export/zod_mapper.rb', line 341 def build_enum_schemas(enums) return '' if enums.empty? enums.map do |name, enum| "export const #{pascal_case(name)}Schema = z.enum([#{enum.values.sort.map { |value| "'#{value}'" }.join(', ')}]);" end.join("\n\n") end |
#build_object_schema(type_name, type, recursive: false) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/apiwork/export/zod_mapper.rb', line 48 def build_object_schema(type_name, type, recursive: false) schema_name = pascal_case(type_name) properties = type.shape.sort_by { |name, _param| name.to_s }.map do |name, param| key = @export.transform_key(name) zod_type = map_field(param) " #{key}: #{zod_type}" end.join(",\n") type_annotation = recursive ? ": z.ZodType<#{schema_name}>" : '' if recursive "export const #{schema_name}Schema#{type_annotation} = z.lazy(() => z.object({\n#{properties}\n}));" elsif type.extends? build_object_schema_code(schema_name, properties, type.extends, type_annotation:) else "export const #{schema_name}Schema#{type_annotation} = z.object({\n#{properties}\n});" end end |
#build_object_schema_code(schema_name, properties, extends, type_annotation: '') ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/apiwork/export/zod_mapper.rb', line 68 def build_object_schema_code(schema_name, properties, extends, type_annotation: '') base_schemas = extends.map { |type| "#{pascal_case(type)}Schema" } base_chain = if base_schemas.size == 1 base_schemas.first else first, *rest = base_schemas rest.reduce(first) { |acc, schema| "#{acc}.merge(#{schema})" } end if properties.empty? "export const #{schema_name}Schema#{type_annotation} = #{base_chain};" else "export const #{schema_name}Schema#{type_annotation} = #{base_chain}.extend({\n#{properties}\n});" end end |
#build_type_schemas(types) ⇒ Object
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/apiwork/export/zod_mapper.rb', line 349 def build_type_schemas(types) types_hash = types.transform_values(&:to_h) lazy_types = TypeAnalysis.cycle_breaking_types(types_hash) TypeAnalysis.topological_sort_types(types_hash).map(&:first).map do |type_name| type = types[type_name] recursive = lazy_types.include?(type_name) if type.union? build_union_schema(type_name, type, recursive:) else build_object_schema(type_name, type, recursive:) end end.join("\n\n") end |
#build_union_schema(type_name, type, recursive: false) ⇒ Object
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 |
# File 'lib/apiwork/export/zod_mapper.rb', line 85 def build_union_schema(type_name, type, recursive: false) schema_name = pascal_case(type_name) variant_schemas = type.variants.map do |variant| base_schema = map_param(variant) if type.discriminator && variant.tag && !reference_contains_discriminator?(variant, type.discriminator) "#{base_schema}.extend({ #{@export.transform_key(type.discriminator)}: z.literal('#{variant.tag}') })" else base_schema end end union_body = variant_schemas.map { |schema| " #{schema}" }.join(",\n") type_annotation = recursive ? ": z.ZodType<#{schema_name}>" : '' union_code = if type.discriminator "z.discriminatedUnion('#{@export.transform_key(type.discriminator)}', [\n#{union_body}\n])" else "z.union([\n#{union_body}\n])" end if recursive "export const #{schema_name}Schema#{type_annotation} = z.lazy(() => #{union_code});" else "export const #{schema_name}Schema#{type_annotation} = #{union_code};" end end |
#map(surface) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/apiwork/export/zod_mapper.rb', line 30 def map(surface) parts = [] enum_schemas = build_enum_schemas(surface.enums) parts << enum_schemas if enum_schemas.present? type_schemas = build_type_schemas(surface.types) parts << type_schemas if type_schemas.present? action_schemas = build_action_schemas parts << action_schemas if action_schemas.present? action_response_schemas = build_action_response_schemas parts << action_response_schemas if action_response_schemas.present? parts.join("\n\n") end |
#map_array_type(param) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/apiwork/export/zod_mapper.rb', line 235 def map_array_type(param) items_type = param.of if items_type.nil? && param.shape.any? items_schema = map_object_type(param) base = "z.array(#{items_schema})" elsif items_type items_schema = map_param(items_type) base = "z.array(#{items_schema})" else base = 'z.array(z.unknown())' end base += ".min(#{param.min})" unless param.min.nil? base += ".max(#{param.max})" unless param.max.nil? base end |
#map_discriminated_union(param) ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/apiwork/export/zod_mapper.rb', line 267 def map_discriminated_union(param) discriminator_field = @export.transform_key(param.discriminator) variant_schemas = param.variants.map do |variant| if variant.tag && variant.object? discriminator_prop = "#{discriminator_field}: z.literal('#{variant.tag}')" properties = variant.shape.sort_by { |name, _| name.to_s }.map do |name, field| "#{@export.transform_key(name)}: #{map_field(field)}" end all_properties = [discriminator_prop, *properties].join(', ') "z.object({ #{all_properties} })" elsif variant.tag base_schema = map_param(variant) "#{base_schema}.extend({ #{discriminator_field}: z.literal('#{variant.tag}') })" else map_param(variant) end end "z.discriminatedUnion('#{discriminator_field}', [#{variant_schemas.join(', ')}])" end |
#map_field(param, force_optional: nil) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/apiwork/export/zod_mapper.rb', line 185 def map_field(param, force_optional: nil) if param.reference? && type_or_enum_reference?(param.reference) schema_name = pascal_case(param.reference) type = "#{schema_name}Schema" return apply_modifiers(type, param, force_optional:) end type = map_param(param) type = resolve_enum_schema(param) || type apply_modifiers(type, param, force_optional:) end |
#map_format_to_zod(format) ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/apiwork/export/zod_mapper.rb', line 317 def map_format_to_zod(format) case format when :email then 'z.email()' when :uuid then 'z.uuid()' when :url then 'z.url()' when :ipv4 then 'z.ipv4()' when :ipv6 then 'z.ipv6()' when :date then 'z.iso.date()' when :datetime then 'z.iso.datetime()' when :password, :hostname then 'z.string()' when :int32, :int64 then 'z.number().int()' when :float, :double then 'z.number()' else 'z.string()' end end |
#map_literal_type(param) ⇒ Object
289 290 291 292 293 294 295 296 |
# File 'lib/apiwork/export/zod_mapper.rb', line 289 def map_literal_type(param) case param.value when nil then 'z.null()' when String then "z.literal('#{param.value}')" when Numeric, TrueClass, FalseClass then "z.literal(#{param.value})" else "z.literal('#{param.value}')" end end |
#map_object_type(param) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/apiwork/export/zod_mapper.rb', line 216 def map_object_type(param) return 'z.record(z.string(), z.unknown())' if param.shape.empty? partial = param.partial? properties = param.shape.sort_by { |name, _field| name.to_s }.map do |name, field| key = @export.transform_key(name) zod_type = if partial map_field(field, force_optional: false) else map_field(field) end "#{key}: #{zod_type}" end.join(', ') base_object = "z.object({ #{properties} })" partial ? "#{base_object}.partial()" : base_object end |
#map_param(param) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/apiwork/export/zod_mapper.rb', line 198 def map_param(param) if param.object? map_object_type(param) elsif param.array? map_array_type(param) elsif param.record? map_record_type(param) elsif param.union? map_union_type(param) elsif param.literal? map_literal_type(param) elsif param.reference? && type_or_enum_reference?(param.reference) resolve_enum_schema(param) || schema_reference(param.reference) else resolve_enum_schema(param) || map_primitive(param) end end |
#map_primitive(param) ⇒ Object
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/apiwork/export/zod_mapper.rb', line 298 def map_primitive(param) return 'z.unknown()' if param.unknown? format = param.format&.to_sym if param.formattable? base_type = if format map_format_to_zod(format) else TYPE_MAP[param.type.to_sym] || 'z.unknown()' end if param.boundable? base_type += ".min(#{param.min})" unless param.min.nil? base_type += ".max(#{param.max})" unless param.max.nil? end base_type end |
#map_record_type(param) ⇒ Object
253 254 255 256 |
# File 'lib/apiwork/export/zod_mapper.rb', line 253 def map_record_type(param) value_schema = param.of ? map_param(param.of) : 'z.unknown()' "z.record(z.string(), #{value_schema})" end |
#map_union_type(param) ⇒ Object
258 259 260 261 262 263 264 265 |
# File 'lib/apiwork/export/zod_mapper.rb', line 258 def map_union_type(param) if param.discriminator map_discriminated_union(param) else variants = param.variants.map { |variant| map_param(variant) } "z.union([#{variants.join(', ')}])" end end |
#pascal_case(name) ⇒ Object
337 338 339 |
# File 'lib/apiwork/export/zod_mapper.rb', line 337 def pascal_case(name) name.to_s.camelize(:upper) end |
#schema_reference(symbol) ⇒ Object
333 334 335 |
# File 'lib/apiwork/export/zod_mapper.rb', line 333 def schema_reference(symbol) "#{pascal_case(symbol)}Schema" end |
#traverse_resources(resources: @export.api.resources, &block) ⇒ Object
422 423 424 425 426 427 |
# File 'lib/apiwork/export/zod_mapper.rb', line 422 def traverse_resources(resources: @export.api.resources, &block) resources.each_value do |resource| yield(resource) traverse_resources(resources: resource.resources, &block) if resource.resources.any? end end |