Skip to content

Commit

Permalink
refactor: move resolveAndMerge to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
tatomyr committed Jun 8, 2024
1 parent b636618 commit a4e1acd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 43 deletions.
3 changes: 2 additions & 1 deletion applications/__tests__/adapter.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {describe, expect, test} from "vitest"
import {resolveAndMerge, translateXTypeToSchema} from "../x-types-adapter"
const {translateXTypeToSchema} = require("../x-types-adapter")
const {resolveAndMerge} = require("../x-types-resolver")

describe("adapter", () => {
test("translates primitive strings", () => {
Expand Down
42 changes: 1 addition & 41 deletions applications/x-types-adapter.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,4 @@
const {isObject, mergeAll} = require("./x-types-utils")

const resolveAndMerge = (xType, ctx) => {
if (typeof xType.$ref !== "undefined") {
const resolved = ctx.resolve(xType).node
if (resolved === undefined) {
console.error()
console.error("ERROR! Cannot resolve $ref:")
console.error(xType.$ref)
console.error()
return "any"
}
return resolveAndMerge(resolved, ctx)
}

if (typeof xType.$and !== "undefined") {
if (!Array.isArray(xType.$and)) {
console.error()
console.error("ERROR! Expected array but got:")
console.error(xType.$and)
console.error()
return "any"
}
return mergeAll(...resolveAndMerge(xType.$and, ctx))
}

if (Array.isArray(xType)) {
return xType.map(type => resolveAndMerge(type, ctx)).flat()
}

if (isObject(xType)) {
let obj = {}
for (const key in xType) {
obj[key] = resolveAndMerge(xType[key], ctx)
}
return obj
}

return xType
}
const {isObject} = require("./x-types-utils")

const translateXTypeToSchema = xType => {
if (typeof xType === "undefined") {
Expand Down Expand Up @@ -178,5 +139,4 @@ const translateXTypeToSchema = xType => {

module.exports = {
translateXTypeToSchema,
resolveAndMerge,
}
3 changes: 2 additions & 1 deletion applications/x-types-decorators.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const {isObject} = require("./x-types-utils")
const {translateXTypeToSchema, resolveAndMerge} = require("./x-types-adapter")
const {translateXTypeToSchema} = require("./x-types-adapter")
const {resolveAndMerge} = require("./x-types-resolver")

const generateSchema = () => {
return {
Expand Down
44 changes: 44 additions & 0 deletions applications/x-types-resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const {isObject, mergeAll} = require("./x-types-utils")

const resolveAndMerge = (xType, ctx) => {
if (typeof xType.$ref !== "undefined") {
const resolved = ctx.resolve(xType).node
if (resolved === undefined) {
console.error()
console.error("ERROR! Cannot resolve $ref:")
console.error(xType.$ref)
console.error()
return "any"
}
return resolveAndMerge(resolved, ctx)
}

if (typeof xType.$and !== "undefined") {
if (!Array.isArray(xType.$and)) {
console.error()
console.error("ERROR! Expected array but got:")
console.error(xType.$and)
console.error()
return "any"
}
return mergeAll(...resolveAndMerge(xType.$and, ctx))
}

if (Array.isArray(xType)) {
return xType.map(type => resolveAndMerge(type, ctx)).flat()
}

if (isObject(xType)) {
let obj = {}
for (const key in xType) {
obj[key] = resolveAndMerge(xType[key], ctx)
}
return obj
}

return xType
}

module.exports = {
resolveAndMerge,
}

0 comments on commit a4e1acd

Please sign in to comment.